Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

66
LINES

< > BotCompany Repo | #1030037 // CodeSafetyChecker

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11005L/78K).

1  
sclass CodeSafetyChecker {
2  
  // general data
3  
  SS identifierSafetyMap;
4  
  bool identifiersStartingWithDollarAreSafe;
5  
  
6  
  // data for code currently tested
7  
  S code;
8  
  Set<S> identifiers;
9  
  LS unknownIdentifiers;
10  
  LS tags;
11  
  
12  
  void init {
13  
    if (identifierSafetyMap == null)
14  
      identifierSafetyMap = codeAnalysis_identifierSafetyMap();
15  
  }
16  
  
17  
  LS tokenize(S code) { ret javaTok(code); }
18  
  
19  
  run {
20  
    init();
21  
    LS tok = tokenize(code);
22  
    tok_inStringEvals(tok); 
23  
    identifiers = tok_allIdentifiersWithoutColonEquals(tok);
24  
    unknownIdentifiers = new L;
25  
    Set<S> tags = treeSet();
26  
    for (S id : identifiers) {
27  
      S tag;
28  
      if (identifiersStartingWithDollarAreSafe && startsWith(id, "$"))
29  
        tag = "safe";
30  
      else
31  
        tag = or2(mapGet(identifierSafetyMap, id), "?");
32  
      if (eq(tag, "?"))
33  
        unknownIdentifiers.add(id);
34  
      tags.addAll(tokSplitAtComma(tag));
35  
    }
36  
    this.tags = simplifySafetyTags(tags);
37  
    if (empty(this.tags)) this.tags.add("safe");
38  
  }
39  
  
40  
  S checkCode(S code) {
41  
    this.code = code;
42  
    run();
43  
    ret joinWithComma(tags);
44  
  }
45  
  
46  
  S verbalCheckResult(S code) {
47  
    checkCode(code);
48  
    ret verbalCheckResult();
49  
  }
50  
  
51  
  S verbalCheckResult() {
52  
    S safety = joinWithComma(tags);
53  
    if (neq(safety, "safe") && nempty(unknownIdentifiers))
54  
      safety += ". Unknown identifiers: " + joinWithComma(unknownIdentifiers);
55  
    ret safety;
56  
  }
57  
  
58  
  void markSafe(S... ids) {
59  
    init();
60  
    putMultipleKeys(identifierSafetyMap, asList(ids), "safe");
61  
  }
62  
  
63  
  bool isSafe() {
64  
    ret eq(tags, ll("safe"));
65  
  }
66  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030037
Snippet name: CodeSafetyChecker
Eternal ID of this version: #1030037/15
Text MD5: 79d186576d1be2194c4274f613aacc0f
Transpilation MD5: e60ae60d1cae71d07eaafa39d0a67f76
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-30 15:18:01
Source code size: 1639 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 178 / 430
Version history: 14 change(s)
Referenced in: [show references]