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

sbool tok_doubleFor_v3_debug;

static void tok_doubleFor_v3(L<S> tok) {
  // The problem with going all "var" is that
  // if the map is of a raw type (e.g. Map), _entrySet(map) is of type
  // Set which means we can't call getKey()/getValue() on the entries.
  // We can, however, all it for typed maps.
  
  jreplace(tok, "for (<id>, <id> :", "for (var $3, var $5 :");
  
  for (int i : reversed(indexesOf(tok, "for"))) {
    if (neq(get(tok, i+2), "(")) continue;
    int argsFrom = i+4;
    // move loop label to proper place
    if (eqGet(tok, i-2, ":") && isIdentifier(get(tok, i-4))) i -= 4;
    int iColon = indexOfAny(tok, argsFrom, ":", ")");
    if (neq(get(tok, iColon), ":")) continue;
    if (tok_doubleFor_v3_debug) print("have colon");
    tok_typesAndNamesOfParams_keepModifiers.set(true);
    L<PairS> args;
    try {
      args = tok_typesAndNamesOfParams(subList(tok, argsFrom-1, iColon-1));
    } catch {
      print("tok_doubleFor_v3: Skipping parsing complicated for statement (probably not a double for)");
      continue;
    }
    
    if (l(args) != 2) continue; // simple for or a three-argument for (out of this world!)
    
    // S a, b => S a, S b
    if (eq(second(args).a, "?")) second(args).a = first(args).a;
    
    if (tok_doubleFor_v3_debug) print("have 2 args: " + sfu(args));
    int iClosing = tok_findEndOfForExpression(tok, iColon+2);
    if (iClosing < 0) continue;
    if (tok_doubleFor_v3_debug) print("have closing");
    S exp = trimJoinSubList(tok, iColon+2, iClosing-1);
    if (tok_doubleFor_v3_debug) print("Expression: " + exp);
    int iCurly = iClosing+2;
    tok_statementToBlock(tok, iCurly);
    int iCurlyEnd = tok_findEndOfStatement(tok, iCurly)-1;
    if (iCurlyEnd < 0) continue;
    if (tok_doubleFor_v3_debug) print("have curly end");
    
    tokAppend(tok, iColon, " _entrySet(");
    tokPrepend(tok, iClosing, ")");
    
    S keyType = first(first(args));
    S valueType = first(second(args));
    
    S entryVar = makeVar();
    S entryType = eq(keyType, "var") || eq(valueType, "var")
      ? "var" 
      : "Map.Entry<? extends " + tok_toNonPrimitiveTypes(keyType) + ", ? extends "
        + tok_toNonPrimitiveTypes(first(second(args))) + ">";
    
    replaceTokens(tok, argsFrom, iColon-1, entryType + " " + entryVar);
      /*"Map.Entry<" + first(first(args)) + ", "
        + first(second(args)) + "> " + entryVar);*/
    
    tokAppend(tok, iCurly, " " + joinPairWithSpace(first(args)) + " = " + entryVar + ".getKey(); "
      + joinPairWithSpace(second(args)) + " = " + entryVar + ".getValue(); ");
    reTok(tok, i, iCurlyEnd+1);
  }
}

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