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

67
LINES

< > BotCompany Repo | #1015648 // tok_scopes

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

Libraryless. Click here for Pure Java version (8989L/51K).

1  
// process scope x. ... end scope blocks
2  
svoid tok_scopes(L<S> tok, O... _) {
3  
  if (!tok.contains("scope")) ret;
4  
  optPar bool autoCloseScopes;
5  
  
6  
  // New syntax: scope bla { ... }
7  
  replaceKeywordBlock(tok, "scope <id>",
8  
    "scope $2. ",
9  
    " end scope ");
10  
  
11  
  int i;
12  
  // Old syntax: scope bla ... end scope
13  
  while ((i = rjfind(tok, "scope <id>", (_tok, nIdx) ->
14  
      !subListContains(_tok, nIdx-1, nIdx+6, "aka"))) >= 0) {
15  
    S scopeName = tok.get(i+2);
16  
    int start = i+4;
17  
    if (eqGet(tok, start, ".")) start += 2;
18  
    int j = jfind(tok, start, "end scope");
19  
    if (j < 0)
20  
      if (autoCloseScopes)
21  
        j = l(tok);
22  
      else
23  
        fail("Scope " + scopeName + " opened but not closed");
24  
    else
25  
      clearTokens(tok, j, j+3);
26  
27  
    new HashSet<S> names;
28  
    new HashSet<S> functions;
29  
    
30  
    clearTokens(tok, i, start-1);
31  
    L<S> subTok = subList(tok, start-1, j);
32  
    
33  
    // auto-make #lock variable
34  
    if (jfind(subTok, "lock #lock") >= 0
35  
      && jfind(subTok, "Lock #lock") < 0)
36  
        tok.set(i, "static Lock " + scopeName + "_lock = lock();\n");
37  
    
38  
    // first pass (find # declarations)
39  
    for (int k = start; k < j-2; k += 2) {
40  
      S t = get(tok, k+2);
41  
      if (eqGet(tok, k, "#") && isIdentifier(t)) {
42  
        names.add(t);
43  
        if (eqGetOneOf(tok, k+4, "(", "{", "<", "extends", "implements", ">")) // cover class declarations too
44  
          functions.add(t); 
45  
        replaceTokens(tok, k, k+3, scopeName + "_" + t);
46  
      }
47  
    }
48  
    
49  
    // second pass (references to scoped variables)
50  
    for (int k = start; k < j; k += 2) {
51  
      S t = get(tok, k);
52  
      if (isIdentifier(t)) {
53  
        if (names.contains(t)) {
54  
          if (eqGet(tok, k-2, ".")) {}
55  
          else if (eqGet(tok, k+2, "(") && !functions.contains(t)) {}
56  
          // avoid lock ... and map ...
57  
          else if (eqOneOf(t, "lock", "map") && isIdentifier(get(tok, k+2))) {}
58  
          else
59  
            tok.set(k, scopeName + "_" + t);
60  
        } else if (eq(t, "__scope"))
61  
          tok.set(k, quote(scopeName));
62  
      }
63  
    }
64  
    
65  
    reTok(tok, i, j+3);
66  
  }
67  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015648
Snippet name: tok_scopes
Eternal ID of this version: #1015648/24
Text MD5: d8ce9f750a3be0f2515c734c50280275
Transpilation MD5: 711b9e821072dd5617e4df138de876d0
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-28 23:40:23
Source code size: 2144 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 360 / 509
Version history: 23 change(s)
Referenced in: [show references]