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).

svoid tok_swappableFunctions(LS tok) {
  jreplace(tok, "swappable static", "static swappable");
  jreplace(tok, "swappable protected", "protected swappable");
  jreplace(tok, "pswappable <id>", "persistently swappable $2");
  jreplace(tok, "persistent swappable <id>", "persistently swappable $3");
  jreplace(tok, "transient swappable <id>", "swappable $3");
  
  int i;
  while ((i = jfind(tok, "swappable <id> <id>(")) >= 0) {
    int iModEnd = i;
    bool persistently = eqGet(tok, i-2, "persistently");
    if (persistently) iModEnd -= 2;
    bool atOverride = subListEquals(tok, iModEnd-4, "@", "", "Override");
    if (atOverride) iModEnd -= 4;
    int iMod = leftScanModifiers(tok, iModEnd);
    S modifiers = joinSubList(tok, iMod, iModEnd);
    S returnType = tok.get(i+2), name = tok.get(i+4);
    int iCurly = indexOf(tok, i, "{");
    LS tokArgs = subList(tok, i+7, iCurly-2);
    LPairS args = tok_typesAndNamesOfParams(tokArgs);
    S args1 = join(tokArgs);
    S args2 = joinWithComma(pairsB(args));
    bool isVoidMethod = eq(returnType, "void");
    LS types = pairsA(args);
    if (!isVoidMethod) types.add(returnType);
    S typeParams = joinWithComma(map tok_toNonPrimitiveTypes(types)); // TODO: modifiers
    S base = name + "_base", fallback = name + "_fallback";
    S mods = modifiers + (persistently || containsJavaToken(modifiers, "static") ? "" : "transient ");
    if (atOverride) modifiers = "@Override " + modifiers;
    S src;
    if (isVoidMethod)
      if (empty(args))
        // no args, returning void
        src = mods + "Runnable \*name*/;\n" + 
          "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.run(); else \*base*/(\*args2*/); }\n" +
          "final \*modifiers*/\*returnType*/ \*fallback*/(Runnable _f, \*args1*/) { if (_f != null) _f.run(); else \*base*/(); }\n" +
          "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
      else {
        // args, returning void
        S varType = "IVF\*l(args)*/<\*typeParams*/>"; 
        src = "\*mods*/ \*varType*/ \*name*/;\n" + 
          "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
          "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { if (_f != null) _f.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
          "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
      }
    else {
      // zero or more args, not returning void
      S varType = "IF\*l(args)*/<\*typeParams*/>";
      src = "\*mods*/ \*varType*/ \*name*/;\n" + 
        "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { ret \*name*/ != null ? \*name*/.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
          "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { ret _f != null ? _f.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
        "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
    }
    replaceTokens_reTok(tok, iMod, iCurly+1, src);
  }
}

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: 252 / 339
Version history: 7 change(s)
Referenced in: