| 1 | sbool tok_defaultArguments_debug = true; | 
| 2 | |
| 3 | // only one default argument per declaration for now | 
| 4 | // example: | 
| 5 | // static LS recursiveProbabilisticParse(S productions, S sentenceClass default "sentence", S input) { ... }
 | 
| 6 | svoid tok_defaultArguments(LS tok) {
 | 
| 7 |   for (int i : jfindAll_reversed(tok, "<id> default", tokCondition {
 | 
| 8 | ret !eqGetOneOf(tok, i-1, "ifclass", "ifdef", "ifndef") | 
| 9 | && !isJavaModifier(_get(tok, i+1)); | 
| 10 |   })) {
 | 
| 11 |     int iOpening = lastIndexOf(tok, i, "(");
 | 
| 12 | int iClosing = findEndOfBracketPart2(tok, iOpening)-1; | 
| 13 | //S args = joinSubList(tok, iOpening+2, iClosing-1); | 
| 14 | |
| 15 | // function name | 
| 16 | int iName = iOpening-2; | 
| 17 | S name = get(tok, iName); | 
| 18 | if (!isIdentifier(name)) continue; | 
| 19 | |
| 20 | int iType = tok_leftScanType(tok, iName); | 
| 21 | int iStart = tok_leftScanTypeArgsOpt(tok, iType); | 
| 22 | iStart = leftScanModifiers(tok, iStart); | 
| 23 |     //print("Found method head: " + joinSubList(tok, iStart, iClosing+1));
 | 
| 24 | int iEndOfExpr = tok_findEndOfExpression(tok, i+4)+1; // should point at comma or closing bracket | 
| 25 | S expr = joinSubList(tok, i+4, iEndOfExpr); | 
| 26 | int iParamTypeStart = tok_leftScanType(tok, i); | 
| 27 | LS _args1 = subList(tok, iOpening+1, iParamTypeStart); | 
| 28 | LS _args2 = subList(tok, iEndOfExpr+1, iClosing+2); | 
| 29 | LS args1 = tok_parseArgsDeclList2(_args1); | 
| 30 | LS args2 = tok_parseArgsDeclList2(_args2); | 
| 31 | LS type = subList(tok, iType-1, iName); | 
| 32 | bool isVoid = containsOneOf(codeTokens(type), javaxVoidAliases()); | 
| 33 | if (eq(expr, "new")) expr = "new " + joinSubList(tok, iParamTypeStart, i-1) + "()"; | 
| 34 | |
| 35 | S modifiers = join(listMinus(subList(tok, iStart, iType), "abstract")); | 
| 36 | |
| 37 | S rewrittenHead = | 
| 38 | modifiers | 
| 39 | + joinSubList(tok, iType, iOpening+1) | 
| 40 | + joinWithComma(concatLists(args1, args2)) + ")"; | 
| 41 | |
| 42 | if (tok_defaultArguments_debug) | 
| 43 | printVars tok_defaultArguments(+expr, +_args1, +_args2, +args1, +args2, +type, +isVoid, +rewrittenHead); | 
| 44 | |
| 45 | clearTokens_reTok(tok, i+1, iEndOfExpr-1); | 
| 46 |     tokPrepend_reTok(tok, iStart, rewrittenHead + " { "
 | 
| 47 |       + (isVoid ? "" : "return ") + name + "(" + 
 | 
| 48 | joinWithComma(concatLists( | 
| 49 | map tok_lastIdentifier(args1), | 
| 50 | ll(expr), | 
| 51 | map tok_lastIdentifier(args2))) + "); }\n"); | 
| 52 | } | 
| 53 | } | 
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: | 356 / 412 | 
| Version history: | 3 change(s) | 
| Referenced in: | [show references] |