Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

60
LINES

< > BotCompany Repo | #1027996 // tok_defaultArguments [default arguments for functions, run after most JavaX translations, LIVE]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6083L/34K).

sbool tok_defaultArguments_debug = true;

// only one default argument per declaration for now
// example:
// static LS parse(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;
    bool hasBody = eqGet(tok, iClosing+2, "{");
    //S args = joinSubList(tok, iOpening+2, iClosing-1);
    
    // function name
    int iName = iOpening-2;
    S name = get(tok, iName);
    if (!isIdentifier(name)) continue;
    
    int iAkas = tok_leftScanAkas(tok, iName);
    name = get(tok, iAkas);
    int iJavaxMods = tok_leftScanJavaxModifiers(tok, iAkas);
    int iType = tok_leftScanType(tok, iJavaxMods);
    int iStart = tok_leftScanTypeArgsOpt(tok, iType);
    int iModsEnd = iStart;
    iStart = leftScanModifiers(tok, iModsEnd);
    Cl<S> modifiers = tok_modifiersLeftOf(tok, iModsEnd);
    if (!hasBody && !modifiers.contains("abstract"))
      modifiers.add("default");
    modifiers.remove("abstract");
    //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 rewrittenHead = spaceCombine(modifiers, joinSubList(tok, iModsEnd, iOpening+1)
      + joinWithComma(concatLists(args1, args2)) + ")";
      
    if (tok_defaultArguments_debug)
      printVars tok_defaultArguments(+expr, +_args1, +_args2, +args1, +args2, +type, +isVoid, +rewrittenHead);
      
    S forwardCall = (isVoid ? "" : "return ") + name + "(" + 
      joinWithComma(concatLists(
        map tok_lastIdentifier(args1),
        ll(expr),
        map tok_lastIdentifier(args2))) + ");";
    
    clearTokens_reTok(tok, i+1, iEndOfExpr-1);
    tokPrepend_reTok(tok, iStart, rewrittenHead + " { "
      + forwardCall + " }\n");
 }
}

Author comment

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/57
Text MD5: d3ddb77c79e4e03698968d3b4058115f
Transpilation MD5: 59180d0da81c0d55ed372c7d1d74edfa
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 05:13:18
Source code size: 2632 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 257 / 489
Version history: 56 change(s)
Referenced in: [show references]