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).

sclass CodeSafetyChecker {
  // general data
  SS identifierSafetyMap;
  bool identifiersStartingWithDollarAreSafe;
  
  // data for code currently tested
  S code;
  Set<S> identifiers;
  LS unknownIdentifiers;
  LS tags;
  
  void init {
    if (identifierSafetyMap == null)
      identifierSafetyMap = codeAnalysis_identifierSafetyMap();
  }
  
  LS tokenize(S code) { ret javaTok(code); }
  
  run {
    init();
    LS tok = tokenize(code);
    tok_inStringEvals(tok); 
    identifiers = tok_allIdentifiersWithoutColonEquals(tok);
    unknownIdentifiers = new L;
    Set<S> tags = treeSet();
    for (S id : identifiers) {
      S tag;
      if (identifiersStartingWithDollarAreSafe && startsWith(id, "$"))
        tag = "safe";
      else
        tag = or2(mapGet(identifierSafetyMap, id), "?");
      if (eq(tag, "?"))
        unknownIdentifiers.add(id);
      tags.addAll(tokSplitAtComma(tag));
    }
    this.tags = simplifySafetyTags(tags);
    if (empty(this.tags)) this.tags.add("safe");
  }
  
  S checkCode(S code) {
    this.code = code;
    run();
    ret joinWithComma(tags);
  }
  
  S verbalCheckResult(S code) {
    checkCode(code);
    ret verbalCheckResult();
  }
  
  S verbalCheckResult() {
    S safety = joinWithComma(tags);
    if (neq(safety, "safe") && nempty(unknownIdentifiers))
      safety += ". Unknown identifiers: " + joinWithComma(unknownIdentifiers);
    ret safety;
  }
  
  void markSafe(S... ids) {
    init();
    putMultipleKeys(identifierSafetyMap, asList(ids), "safe");
  }
  
  bool isSafe() {
    ret eq(tags, ll("safe"));
  }
}

download  show line numbers  debug dex   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

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: 50 / 119
Version history: 14 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1030037 & http://1030037.tinybrain.de