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

72
LINES

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

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

Transpiled version (6773L) is out of date.

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) {
  while licensed {
    bool change;
    
    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 = tok_hasBody(tok, iClosing+2);
      //S args = joinSubList(tok, iOpening+2, iClosing-1);
      
      // function name
      int iName = iOpening-2;
      S name = get(tok, iName);
      bool isConstructor = eq(name, "*");
      if (!isConstructor && !isIdentifier(name))
        continue;
      
      int iAkas = tok_leftScanAkas(tok, iName);
      name = get(tok, iAkas);
      int iJavaxMods = tok_leftScanJavaxModifiers(tok, iAkas);
      int iType = isConstructor ? iJavaxMods : 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);
      LPairS args1withTypes = map tok_typeAndNameOfParam(args1);
      LPairS args2withTypes = map tok_typeAndNameOfParam(args2);
      LS type = subList(tok, iType-1, iName);
      bool isVoid = isConstructor || 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, +args1withTypes, +args2withTypes);
        
      S forwardCall = (isVoid ? "" : "return ") + (isConstructor ? "this" : name) + "(" + 
        joinWithComma(concatLists(
          pairsB(args1withTypes),
          ll(expr),
          pairsB(args2withTypes))) + ");";
      
      clearTokens_reTok(tok, i+1, iEndOfExpr-1);
      tokPrepend_reTok(tok, iStart, rewrittenHead + " { "
        + forwardCall + " }\n");
        
      set change;
    }
    
    if (!change) break;
  }
}

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/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: 291 / 563
Version history: 64 change(s)
Referenced in: [show references]