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

85
LINES

< > BotCompany Repo | #1030979 // tok_akaFunctionNames

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

Transpiled version (6367L) is out of date.

// e.g.
//   void tailCall aka replace(Computable x) {...}
// to:
//   void tailCall(Computable x) {...}
//   final void replace(Computable x) { tailCall(x); }
// (or the other way around)
svoid tok_akaFunctionNames(LS tok) {
  int i;
  bool useFinal = true;
  while ((i = jfind(tok, "<id> aka <id>")) >= 0) {
    int iName1 = i, _iLastName = i+4;
    while (eqGet(tok, _iLastName+2, "aka")
      && isIdentifier(get(tok, _iLastName+4)))
      _iLastName += 4;
    int iLastName = _iLastName;

    // find arguments
    int iOpening = indexOf(tok, iLastName, "(");
    int iClosing = findEndOfBracketPart2(tok, iOpening)-1;
    
    // find all names
    new LS names;
    for (int j = iName1; j <= iLastName; j += 4)
      names.add(tok.get(j));

    // find javax modifiers, return type & modifiers
    int iType = tok_leftScanJavaxModifiers(tok, iName1);
    iType = tok_leftScanType(tok, iType);
    int iStart = leftScanModifiers(tok, tok_leftScanTypeArgsOpt(tok, iType));

    // parse argument list
    LS _args = subList(tok, iOpening+1, iClosing);
    LS args = tok_parseArgsDeclList2(_args);
    
    // analyze return type
    LS type = subList(tok, iType-1, iName1); // return type
    bool isVoid = containsOneOf(codeTokens(type), javaxVoidAliases());
    
    // drop all but first name
    clearTokens(tok, iName1+1, iLastName+1);
    
    int iBodyOpen = indexOfAny(tok, iClosing, "{", ";");
    bool hasBody = eqGet(tok, iBodyOpen, "{");
    
    // The following logic is nasty, but the tests in test_tok_akaFunctionNames
    // make it alright
    
    // modifiers include return type actually
    LS tokModifiers = cloneSubList(tok, iStart, iName1-1);
    bool isAbstract = tokModifiers.remove("abstract");
    bool isDefault = tokModifiers.contains("default");
    tokModifiers.remove("synchronized");
    
    if (!isDefault && !isAbstract && !hasBody) {
      set isDefault;
      tokModifiers.add(0, "default ");
    }

    S modifiers = join(tokModifiers);
    
    if (isDefault && !hasBody)
      replaceElementInSubList(tok, iStart, iType, "default", "");

    // make synonyms final unless we're in an interface
    if (!isDefault && !contains(tokModifiers, "final"))
      modifiers = "final " + modifiers;
    
    S _modifiers = modifiers;
    
    //S exceptionClauses = joinSubList(tok, iClosing+1, iBodyOpen-1);
    
    // add synonyms
    tokPrepend(tok, iStart, joinMap(dropFirst(names), name ->
      _modifiers + " "
      + name
      
      // arguments and declared exceptions
      + joinSubList(tok, iLastName+1, iBodyOpen-1)
      
      + "{ " + stringIf(!isVoid, "return ")
      + first(names) + "(" + joinWithComma(lmap tok_lastIdentifier(args)) + "); }\n"));
      
    reTok(tok, iStart, iLastName+1);
  }
}

Author comment

Began life as a copy of #1027996

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030979
Snippet name: tok_akaFunctionNames
Eternal ID of this version: #1030979/38
Text MD5: fdfa2b60572930b9685b138637a661bf
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-07 18:23:56
Source code size: 2844 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 278 / 448
Version history: 37 change(s)
Referenced in: [show references]