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.

1  
// e.g.
2  
//   void tailCall aka replace(Computable x) {...}
3  
// to:
4  
//   void tailCall(Computable x) {...}
5  
//   final void replace(Computable x) { tailCall(x); }
6  
// (or the other way around)
7  
svoid tok_akaFunctionNames(LS tok) {
8  
  int i;
9  
  bool useFinal = true;
10  
  while ((i = jfind(tok, "<id> aka <id>")) >= 0) {
11  
    int iName1 = i, _iLastName = i+4;
12  
    while (eqGet(tok, _iLastName+2, "aka")
13  
      && isIdentifier(get(tok, _iLastName+4)))
14  
      _iLastName += 4;
15  
    int iLastName = _iLastName;
16  
17  
    // find arguments
18  
    int iOpening = indexOf(tok, iLastName, "(");
19  
    int iClosing = findEndOfBracketPart2(tok, iOpening)-1;
20  
    
21  
    // find all names
22  
    new LS names;
23  
    for (int j = iName1; j <= iLastName; j += 4)
24  
      names.add(tok.get(j));
25  
26  
    // find javax modifiers, return type & modifiers
27  
    int iType = tok_leftScanJavaxModifiers(tok, iName1);
28  
    iType = tok_leftScanType(tok, iType);
29  
    int iStart = leftScanModifiers(tok, tok_leftScanTypeArgsOpt(tok, iType));
30  
31  
    // parse argument list
32  
    LS _args = subList(tok, iOpening+1, iClosing);
33  
    LS args = tok_parseArgsDeclList2(_args);
34  
    
35  
    // analyze return type
36  
    LS type = subList(tok, iType-1, iName1); // return type
37  
    bool isVoid = containsOneOf(codeTokens(type), javaxVoidAliases());
38  
    
39  
    // drop all but first name
40  
    clearTokens(tok, iName1+1, iLastName+1);
41  
    
42  
    int iBodyOpen = indexOfAny(tok, iClosing, "{", ";");
43  
    bool hasBody = eqGet(tok, iBodyOpen, "{");
44  
    
45  
    // The following logic is nasty, but the tests in test_tok_akaFunctionNames
46  
    // make it alright
47  
    
48  
    // modifiers include return type actually
49  
    LS tokModifiers = cloneSubList(tok, iStart, iName1-1);
50  
    bool isAbstract = tokModifiers.remove("abstract");
51  
    bool isDefault = tokModifiers.contains("default");
52  
    tokModifiers.remove("synchronized");
53  
    
54  
    if (!isDefault && !isAbstract && !hasBody) {
55  
      set isDefault;
56  
      tokModifiers.add(0, "default ");
57  
    }
58  
59  
    S modifiers = join(tokModifiers);
60  
    
61  
    if (isDefault && !hasBody)
62  
      replaceElementInSubList(tok, iStart, iType, "default", "");
63  
64  
    // make synonyms final unless we're in an interface
65  
    if (!isDefault && !contains(tokModifiers, "final"))
66  
      modifiers = "final " + modifiers;
67  
    
68  
    S _modifiers = modifiers;
69  
    
70  
    //S exceptionClauses = joinSubList(tok, iClosing+1, iBodyOpen-1);
71  
    
72  
    // add synonyms
73  
    tokPrepend(tok, iStart, joinMap(dropFirst(names), name ->
74  
      _modifiers + " "
75  
      + name
76  
      
77  
      // arguments and declared exceptions
78  
      + joinSubList(tok, iLastName+1, iBodyOpen-1)
79  
      
80  
      + "{ " + stringIf(!isVoid, "return ")
81  
      + first(names) + "(" + joinWithComma(lmap tok_lastIdentifier(args)) + "); }\n"));
82  
      
83  
    reTok(tok, iStart, iLastName+1);
84  
  }
85  
}

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: 219 / 370
Version history: 37 change(s)
Referenced in: [show references]