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

65
LINES

< > BotCompany Repo | #1030155 // tok_doubleFor_v3 [using Map.Entry, LIVE]

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

Libraryless. Click here for Pure Java version (7717L/45K).

1  
sbool tok_doubleFor_v3_debug;
2  
3  
static void tok_doubleFor_v3(L<S> tok) {
4  
  // The problem with going all "var" is that
5  
  // if the map is of a raw type (e.g. Map), _entrySet(map) is of type
6  
  // Set which means we can't call getKey()/getValue() on the entries.
7  
  // We can, however, all it for typed maps.
8  
  
9  
  jreplace(tok, "for (<id>, <id> :", "for (var $3, var $5 :");
10  
  
11  
  for (int i : reversed(indexesOf(tok, "for"))) {
12  
    if (neq(get(tok, i+2), "(")) continue;
13  
    int argsFrom = i+4;
14  
    // move loop label to proper place
15  
    if (eqGet(tok, i-2, ":") && isIdentifier(get(tok, i-4))) i -= 4;
16  
    int iColon = indexOfAny(tok, argsFrom, ":", ")");
17  
    if (neq(get(tok, iColon), ":")) continue;
18  
    if (tok_doubleFor_v3_debug) print("have colon");
19  
    tok_typesAndNamesOfParams_keepModifiers.set(true);
20  
    L<PairS> args;
21  
    try {
22  
      args = tok_typesAndNamesOfParams(subList(tok, argsFrom-1, iColon-1));
23  
    } catch {
24  
      print("tok_doubleFor_v3: Skipping parsing complicated for statement (probably not a double for)");
25  
      continue;
26  
    }
27  
    
28  
    if (l(args) != 2) continue; // simple for or a three-argument for (out of this world!)
29  
    
30  
    // S a, b => S a, S b
31  
    if (eq(second(args).a, "?")) second(args).a = first(args).a;
32  
    
33  
    if (tok_doubleFor_v3_debug) print("have 2 args: " + sfu(args));
34  
    int iClosing = tok_findEndOfForExpression(tok, iColon+2);
35  
    if (iClosing < 0) continue;
36  
    if (tok_doubleFor_v3_debug) print("have closing");
37  
    S exp = trimJoinSubList(tok, iColon+2, iClosing-1);
38  
    if (tok_doubleFor_v3_debug) print("Expression: " + exp);
39  
    int iCurly = iClosing+2;
40  
    tok_statementToBlock(tok, iCurly);
41  
    int iCurlyEnd = tok_findEndOfStatement(tok, iCurly)-1;
42  
    if (iCurlyEnd < 0) continue;
43  
    if (tok_doubleFor_v3_debug) print("have curly end");
44  
    
45  
    tokAppend(tok, iColon, " _entrySet(");
46  
    tokPrepend(tok, iClosing, ")");
47  
    
48  
    S keyType = first(first(args));
49  
    S valueType = first(second(args));
50  
    
51  
    S entryVar = makeVar();
52  
    S entryType = eq(keyType, "var") || eq(valueType, "var")
53  
      ? "var" 
54  
      : "Map.Entry<? extends " + tok_toNonPrimitiveTypes(keyType) + ", ? extends "
55  
        + tok_toNonPrimitiveTypes(first(second(args))) + ">";
56  
    
57  
    replaceTokens(tok, argsFrom, iColon-1, entryType + " " + entryVar);
58  
      /*"Map.Entry<" + first(first(args)) + ", "
59  
        + first(second(args)) + "> " + entryVar);*/
60  
    
61  
    tokAppend(tok, iCurly, " " + joinPairWithSpace(first(args)) + " = " + entryVar + ".getKey(); "
62  
      + joinPairWithSpace(second(args)) + " = " + entryVar + ".getValue(); ");
63  
    reTok(tok, i, iCurlyEnd+1);
64  
  }
65  
}

Author comment

Began life as a copy of #1013206

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030155
Snippet name: tok_doubleFor_v3 [using Map.Entry, LIVE]
Eternal ID of this version: #1030155/15
Text MD5: 7eba0589fd72b842ff4f13459cfd2213
Transpilation MD5: 40c2dbac2a37977925235b9530d146f7
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-25 21:02:51
Source code size: 2681 bytes / 65 lines
Pitched / IR pitched: No / No
Views / Downloads: 174 / 276
Version history: 14 change(s)
Referenced in: [show references]