sbool tok_defaultArguments_debug = true; // only one default argument per declaration for now // example: // static LS recursiveProbabilisticParse(S productions, S sentenceClass default "sentence", S input) { ... } svoid tok_defaultArguments(LS tok) { for (int i : jfindAll_reversed(tok, " default", tokCondition { ret !eqGetOneOf(tok, i-1, "ifclass", "ifdef", "ifndef") && !isJavaModifier(_get(tok, i+1)); })) { int iOpening = lastIndexOf(tok, i, "("); int iClosing = findEndOfBracketPart2(tok, iOpening)-1; //S args = joinSubList(tok, iOpening+2, iClosing-1); // function name int iName = iOpening-2; S name = get(tok, iName); if (!isIdentifier(name)) continue; int iType = tok_leftScanType(tok, iName); int iStart = tok_leftScanTypeArgsOpt(tok, iType); iStart = leftScanModifiers(tok, iStart); //print("Found method head: " + joinSubList(tok, iStart, iClosing+1)); int iEndOfExpr = tok_findEndOfExpression(tok, i+4)+1; // should point at comma or closing bracket S expr = joinSubList(tok, i+4, iEndOfExpr); int iParamTypeStart = tok_leftScanType(tok, i); LS _args1 = subList(tok, iOpening+1, iParamTypeStart); LS _args2 = subList(tok, iEndOfExpr+1, iClosing+2); LS args1 = tok_parseArgsDeclList2(_args1); LS args2 = tok_parseArgsDeclList2(_args2); LS type = subList(tok, iType-1, iName); bool isVoid = containsOneOf(codeTokens(type), javaxVoidAliases()); if (eq(expr, "new")) expr = "new " + joinSubList(tok, iParamTypeStart, i-1) + "()"; S modifiers = join(listMinus(subList(tok, iStart, iType), "abstract")); S rewrittenHead = modifiers + joinSubList(tok, iType, iOpening+1) + joinWithComma(concatLists(args1, args2)) + ")"; if (tok_defaultArguments_debug) printVars tok_defaultArguments(+expr, +_args1, +_args2, +args1, +args2, +type, +isVoid, +rewrittenHead); clearTokens_reTok(tok, i+1, iEndOfExpr-1); tokPrepend_reTok(tok, iStart, rewrittenHead + " { " + (isVoid ? "" : "return ") + name + "(" + joinWithComma(concatLists( map tok_lastIdentifier(args1), ll(expr), map tok_lastIdentifier(args2))) + "); }\n"); } }