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

56
LINES

< > BotCompany Repo | #1027810 // tok_swappableFunctions [v3, with fallback]

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

Libraryless. Click here for Pure Java version (5548L/32K).

1  
svoid tok_swappableFunctions(LS tok) {
2  
  jreplace(tok, "swappable static", "static swappable");
3  
  jreplace(tok, "swappable protected", "protected swappable");
4  
  jreplace(tok, "pswappable <id>", "persistently swappable $2");
5  
  jreplace(tok, "persistent swappable <id>", "persistently swappable $3");
6  
  jreplace(tok, "transient swappable <id>", "swappable $3");
7  
  
8  
  int i;
9  
  while ((i = jfind(tok, "swappable <id> <id>(")) >= 0) {
10  
    int iModEnd = i;
11  
    bool persistently = eqGet(tok, i-2, "persistently");
12  
    if (persistently) iModEnd -= 2;
13  
    bool atOverride = subListEquals(tok, iModEnd-4, "@", "", "Override");
14  
    if (atOverride) iModEnd -= 4;
15  
    int iMod = leftScanModifiers(tok, iModEnd);
16  
    S modifiers = joinSubList(tok, iMod, iModEnd);
17  
    S returnType = tok.get(i+2), name = tok.get(i+4);
18  
    int iCurly = indexOf(tok, i, "{");
19  
    LS tokArgs = subList(tok, i+7, iCurly-2);
20  
    LPairS args = tok_typesAndNamesOfParams(tokArgs);
21  
    S args1 = join(tokArgs);
22  
    S args2 = joinWithComma(pairsB(args));
23  
    bool isVoidMethod = eq(returnType, "void");
24  
    LS types = pairsA(args);
25  
    if (!isVoidMethod) types.add(returnType);
26  
    S typeParams = joinWithComma(map tok_toNonPrimitiveTypes(types)); // TODO: modifiers
27  
    S base = name + "_base", fallback = name + "_fallback";
28  
    S mods = modifiers + (persistently || containsJavaToken(modifiers, "static") ? "" : "transient ");
29  
    if (atOverride) modifiers = "@Override " + modifiers;
30  
    S src;
31  
    if (isVoidMethod)
32  
      if (empty(args))
33  
        // no args, returning void
34  
        src = mods + "Runnable \*name*/;\n" + 
35  
          "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.run(); else \*base*/(\*args2*/); }\n" +
36  
          "final \*modifiers*/\*returnType*/ \*fallback*/(Runnable _f, \*args1*/) { if (_f != null) _f.run(); else \*base*/(); }\n" +
37  
          "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
38  
      else {
39  
        // args, returning void
40  
        S varType = "IVF\*l(args)*/<\*typeParams*/>"; 
41  
        src = "\*mods*/ \*varType*/ \*name*/;\n" + 
42  
          "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
43  
          "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { if (_f != null) _f.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
44  
          "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
45  
      }
46  
    else {
47  
      // zero or more args, not returning void
48  
      S varType = "IF\*l(args)*/<\*typeParams*/>";
49  
      src = "\*mods*/ \*varType*/ \*name*/;\n" + 
50  
        "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { ret \*name*/ != null ? \*name*/.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
51  
          "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { ret _f != null ? _f.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
52  
        "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
53  
    }
54  
    replaceTokens_reTok(tok, iMod, iCurly+1, src);
55  
  }
56  
}

Author comment

Began life as a copy of #1026384

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027810
Snippet name: tok_swappableFunctions [v3, with fallback]
Eternal ID of this version: #1027810/8
Text MD5: f27f63cafbb6eb3d9f6d93bacd25f7f0
Transpilation MD5: ab94b222d26561f1cd6f4a6c684fc3d2
Author: stefan
Category: javax / linux
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-11 10:51:06
Source code size: 3060 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 169 / 234
Version history: 7 change(s)
Referenced in: [show references]