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

49
LINES

< > BotCompany Repo | #1024494 // tok_delegateTo

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

Libraryless. Click here for Pure Java version (10067L/56K).

1  
svoid tok_delegateTo(LS tok) {
2  
  ITokCondition cond = (_tok, nIdx) -> !(empty(_get(_tok, nIdx)) && eqGet(_tok, nIdx-1, "."));
3  
  
4  
  int i;
5  
  while ping ((i = jfind_any(tok, 
6  
    "delegate <id> to", "delegate <id>,")) >= 0) {
7  
    Set<S> tokens = litset(tok.get(i+2));
8  
    int i2 = i+4;
9  
    while (eqGet(tok, i2, ",")) {
10  
      tokens.add(assertIdentifier(get(tok, i2+2)));
11  
      i2 += 4;
12  
    }
13  
    assertEquals(get(tok, i2), "to");
14  
    
15  
    int repStart = i2+2;
16  
    int repEnd = repStart;
17  
    
18  
    // Find . with space or line break or EOF afterwards
19  
    while ping (repEnd < l(tok) && !(
20  
      eqGet(tok, repEnd, ".") && // end when there is a dot
21  
      (nempty(get(tok, repEnd+1)) || repEnd == l(tok)-2))) // ...and it's the end of the text OR there is a space or newline after the dot
22  
      repEnd += 2;
23  
    print("tok_replaceWith: Found " + joinSubList(tok, repStart, repEnd));
24  
    //int repEnd = smartIndexOf(tok, repStart, ".");
25  
26  
    S replacement = joinSubList(tok, repStart, repEnd-1) + ".";
27  
    clearTokens(tok, i, repEnd+1);
28  
    print("Replacing " + tokens + " with " + replacement + "<token>");
29  
    int end = findEndOfBlock(tok, repEnd)-1;
30  
    
31  
    for ping (int j = repEnd+2; j < end; j += 2) {
32  
      if (!tokens.contains(tok.get(j)))
33  
        continue;
34  
      
35  
      // skip if it's a field or method access
36  
      if (empty(get(tok, j-1)) && eqGet(tok, j-2, "."))
37  
        continue;
38  
        
39  
      // skip creation of an non-static inner class instance
40  
      // (a.new B)
41  
      if (eqGet(tok, j-2, "new") && eqGet(tok, j-4, "."))
42  
        continue;
43  
        
44  
      tokPrepend(tok, j, replacement);
45  
    }
46  
    
47  
    reTok(tok, i, end);
48  
  }
49  
}

Author comment

Began life as a copy of #1013759

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1024494
Snippet name: tok_delegateTo
Eternal ID of this version: #1024494/9
Text MD5: 2a224551f0760ba65f965eba202b2e0c
Transpilation MD5: 3bce894e03324bdfbfb2cf4e2a034633
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-24 00:00:49
Source code size: 1693 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 336 / 494
Version history: 8 change(s)
Referenced in: [show references]