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 | int i; |
8 | while ((i = jfind(tok, "<id> default", tokCondition { |
9 | ret !eqGetOneOf(tok, i-1, "ifclass", "ifdef", "ifndef") |
10 | && !isJavaModifier(_get(tok, i+1)); |
11 | })) >= 0) { |
12 | int iOpening = lastIndexOf(tok, i, "("); |
13 | int iClosing = findEndOfBracketPart2(tok, iOpening)-1; |
14 | //S args = joinSubList(tok, iOpening+2, iClosing-1); |
15 | |
16 | // function name |
17 | int iName = iOpening-2; |
18 | S name = assertIdentifier(get(tok, iName)); |
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 | S rewrittenHead = joinSubList(tok, iStart, iOpening+1) |
35 | + joinWithComma(concatLists(args1, args2)) + ")"; |
36 | |
37 | if (tok_defaultArguments_debug) |
38 | printVars tok_defaultArguments(+expr, +_args1, +_args2, +args1, +args2, +type, +isVoid, +rewrittenHead); |
39 | |
40 | clearTokens_reTok(tok, i+1, iEndOfExpr-1); |
41 | tokPrepend_reTok(tok, iStart, rewrittenHead + " { " |
42 | + (isVoid ? "" : "return ") + name + "(" + |
43 | joinWithComma(concatLists( |
44 | map tok_lastIdentifier(args1), |
45 | ll(expr), |
46 | map tok_lastIdentifier(args2))) + "); }\n"); |
47 | } |
48 | } |
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: | #1032681 |
Snippet name: | tok_defaultArguments [backup] |
Eternal ID of this version: | #1032681/1 |
Text MD5: | b80216731f36d1bd7987d89bab70a937 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-01 22:15:18 |
Source code size: | 2165 bytes / 48 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 128 / 153 |
Referenced in: | [show references] |