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, "<id> 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"); } }
Began life as a copy of #1027996
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1032682 |
Snippet name: | tok_defaultArguments [unused] |
Eternal ID of this version: | #1032682/4 |
Text MD5: | 338b7b670ca24af75c016ebd22cb2679 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-22 17:35:39 |
Source code size: | 2299 bytes / 53 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 157 / 171 |
Version history: | 3 change(s) |
Referenced in: | -