sbool tok_doubleFor_v3_debug; static void tok_doubleFor_v3(L 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 (, :", "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 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"; 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); } }