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

58
LINES

< > BotCompany Repo | #1015648 - tok_scopes

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

Libraryless. Click here for Pure Java version (2656L/17K).

// process scope x. ... end scope blocks
static void tok_scopes(L<S> tok, O... _) {
  if (!tok.contains("scope")) ret;
  optPar bool autoCloseScopes;
  
  int i;
  while ((i = rjfind(tok, "scope <id> .")) >= 0) {
    S scopeName = tok.get(i+2);
    int j = jfind(tok, i+6, "end scope");
    if (j < 0)
      if (autoCloseScopes)
        j = l(tok);
      else
        fail("Scope " + scopeName + " opened but not closed");
    else
      clearTokens(tok, j, j+3);

    new HashSet<S> names;
    new HashSet<S> functions;
    
    clearTokens(tok, i, i+5);
    L<S> subTok = subList(tok, i+5, j);
    
    // auto-make #lock variable
    if (jfind(subTok, "lock #lock") >= 0
      && jfind(subTok, "Lock #lock") < 0)
        tok.set(i, "static Lock " + scopeName + "_lock = lock();\n");
    
    // first pass (find # declarations)
    for (int k = i+6; k < j-2; k += 2) {
      S t = get(tok, k+2);
      if (eqGet(tok, k, "#") && isIdentifier(t)) {
        names.add(t);
        if (eqGetOneOf(tok, k+4, "(", "{", "<", "extends", "implements", ">")) // cover class declarations too
          functions.add(t); 
        replaceTokens(tok, k, k+3, scopeName + "_" + t);
      }
    }
    
    // second pass (references to scoped variables)
    for (int k = i+6; k < j; k += 2) {
      S t = get(tok, k);
      if (isIdentifier(t)) {
        if (names.contains(t)) {
          if (eqGet(tok, k-2, ".")) {}
          else if (eqGet(tok, k+2, "(") && !functions.contains(t)) {}
          // avoid lock ... and map ...
          else if (eqOneOf(t, "lock", "map") && isIdentifier(get(tok, k+2))) {}
          else
            tok.set(k, scopeName + "_" + t);
        } else if (eq(t, "__scope"))
          tok.set(k, quote(scopeName));
      }
    }
    
    reTok(tok, i, j+1);
  }
}

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1015648
Snippet name: tok_scopes
Eternal ID of this version: #1015648/16
Text MD5: 4681e8f24f7e41eed2a6e8e2be458004
Transpilation MD5: f3b8106de3f673cc64c554fdfb1734d7
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-26 18:43:45
Source code size: 1841 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 143 / 172
Version history: 15 change(s)
Referenced in: [show references]

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