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

48
LINES

< > BotCompany Repo | #1032681 // tok_defaultArguments [backup]

JavaX fragment (include)

sbool tok_defaultArguments_debug = true;

// only one default argument per declaration for now
// example:
// static LS recursiveProbabilisticParse(S productions, S sentenceClass default "sentence", S input) { ... }
svoid tok_defaultArguments(LS tok) {
  int i;
  while ((i = jfind(tok, "<id> default", tokCondition {
    ret !eqGetOneOf(tok, i-1, "ifclass", "ifdef", "ifndef")
      && !isJavaModifier(_get(tok, i+1));
  })) >= 0) {
    int iOpening = lastIndexOf(tok, i, "(");
    int iClosing = findEndOfBracketPart2(tok, iOpening)-1;
    //S args = joinSubList(tok, iOpening+2, iClosing-1);
    
    // function name
    int iName = iOpening-2;
    S name = assertIdentifier(get(tok, iName));
    
    int iType = tok_leftScanType(tok, iName);
    int iStart = tok_leftScanTypeArgsOpt(tok, iType);
    iStart = leftScanModifiers(tok, iStart);
    //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 = joinSubList(tok, iStart, iOpening+1)
      + joinWithComma(concatLists(args1, args2)) + ")";
      
    if (tok_defaultArguments_debug)
      printVars tok_defaultArguments(+expr, +_args1, +_args2, +args1, +args2, +type, +isVoid, +rewrittenHead);
    
    clearTokens_reTok(tok, i+1, iEndOfExpr-1);
    tokPrepend_reTok(tok, iStart, rewrittenHead + " { "
      + (isVoid ? "" : "return ") + name + "(" + 
        joinWithComma(concatLists(
          map tok_lastIdentifier(args1),
          ll(expr),
          map tok_lastIdentifier(args2))) + "); }\n");
 }
}

Author comment

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: 129 / 154
Referenced in: