Transpiled version (6773L) is out of date.
1 | sbool tok_defaultArguments_debug = true; |
2 | |
3 | // only one default argument per declaration for now |
4 | // example: |
5 | // static LS parse(S productions, S sentenceClass default "sentence", S input) { ... } |
6 | |
7 | svoid tok_defaultArguments(LS tok) { |
8 | while licensed { |
9 | bool change; |
10 | |
11 | for (int i : jfindAll_reversed(tok, "<id> default", tokCondition { |
12 | ret !eqGetOneOf(tok, i-1, "ifclass", "ifdef", "ifndef") |
13 | && !isJavaModifier(_get(tok, i+1)); |
14 | })) { |
15 | int iOpening = lastIndexOf(tok, i, "("); |
16 | int iClosing = findEndOfBracketPart2(tok, iOpening)-1; |
17 | bool hasBody = tok_hasBody(tok, iClosing+2); |
18 | //S args = joinSubList(tok, iOpening+2, iClosing-1); |
19 | |
20 | // function name |
21 | int iName = iOpening-2; |
22 | S name = get(tok, iName); |
23 | bool isConstructor = eq(name, "*"); |
24 | if (!isConstructor && !isIdentifier(name)) |
25 | continue; |
26 | |
27 | int iAkas = tok_leftScanAkas(tok, iName); |
28 | name = get(tok, iAkas); |
29 | int iJavaxMods = tok_leftScanJavaxModifiers(tok, iAkas); |
30 | int iType = isConstructor ? iJavaxMods : tok_leftScanType(tok, iJavaxMods); |
31 | int iStart = tok_leftScanTypeArgsOpt(tok, iType); |
32 | int iModsEnd = iStart; |
33 | iStart = leftScanModifiers(tok, iModsEnd); |
34 | Cl<S> modifiers = tok_modifiersLeftOf(tok, iModsEnd); |
35 | if (!hasBody && !modifiers.contains("abstract")) |
36 | modifiers.add("default"); |
37 | modifiers.remove("abstract"); |
38 | //print("Found method head: " + joinSubList(tok, iStart, iClosing+1)); |
39 | int iEndOfExpr = tok_findEndOfExpression(tok, i+4)+1; // should point at comma or closing bracket |
40 | S expr = joinSubList(tok, i+4, iEndOfExpr); |
41 | int iParamTypeStart = tok_leftScanType(tok, i); |
42 | LS _args1 = subList(tok, iOpening+1, iParamTypeStart); |
43 | LS _args2 = subList(tok, iEndOfExpr+1, iClosing+2); |
44 | LS args1 = tok_parseArgsDeclList2(_args1); |
45 | LS args2 = tok_parseArgsDeclList2(_args2); |
46 | LPairS args1withTypes = map tok_typeAndNameOfParam(args1); |
47 | LPairS args2withTypes = map tok_typeAndNameOfParam(args2); |
48 | LS type = subList(tok, iType-1, iName); |
49 | bool isVoid = isConstructor || containsOneOf(codeTokens(type), javaxVoidAliases()); |
50 | if (eq(expr, "new")) expr = "new " + joinSubList(tok, iParamTypeStart, i-1) + "()"; |
51 | S rewrittenHead = spaceCombine(modifiers, joinSubList(tok, iModsEnd, iOpening+1) |
52 | + joinWithComma(concatLists(args1, args2)) + ")"; |
53 | |
54 | if (tok_defaultArguments_debug) |
55 | printVars tok_defaultArguments(+expr, +_args1, +_args2, +args1, +args2, +type, +isVoid, +rewrittenHead, +args1withTypes, +args2withTypes); |
56 | |
57 | S forwardCall = (isVoid ? "" : "return ") + (isConstructor ? "this" : name) + "(" + |
58 | joinWithComma(concatLists( |
59 | pairsB(args1withTypes), |
60 | ll(expr), |
61 | pairsB(args2withTypes))) + ");"; |
62 | |
63 | clearTokens_reTok(tok, i+1, iEndOfExpr-1); |
64 | tokPrepend_reTok(tok, iStart, rewrittenHead + " { " |
65 | + forwardCall + " }\n"); |
66 | |
67 | set change; |
68 | } |
69 | |
70 | if (!change) break; |
71 | } |
72 | } |
Began life as a copy of #1027994
download show line numbers debug dex old transpilations
Travelled to 8 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027996 |
Snippet name: | tok_defaultArguments [default arguments for functions, run after most JavaX translations, LIVE] |
Eternal ID of this version: | #1027996/65 |
Text MD5: | d05c3847b7aeed059cd037d79c3a799a |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-10 17:07:45 |
Source code size: | 3142 bytes / 72 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 437 / 756 |
Version history: | 64 change(s) |
Referenced in: | [show references] |