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

110
LINES

< > BotCompany Repo | #1012742 // tok_recordDecls

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

Transpiled version (8606L) is out of date.

1  
// TODO: sometimes equals/hashCode is not generated. See an old version of #1026301
2  
3  
sbool tok_recordDecls_autoWithToList = true;
4  
5  
static void tok_recordDecls(L<S> tok) {
6  
  int i;
7  
  bool re = false;
8  
  
9  
  jreplace(tok, "record <id> > <id> {", "record $2 extends $4 {");
10  
  jreplace(tok, "record <id> > <id><<id>> {", "record $2 extends $4  $5 $6 $7 {");
11  
  
12  
  jreplace(tok, "record <id> {", "record $2() {");
13  
  jreplace(tok, "record <id> implements", "record $2() implements");
14  
  jreplace(tok, "record <id> extends", "record $2() extends");
15  
  
16  
  while ((i = jfind_any(tok, "record <id>(", "record <id><")) >= 0) {
17  
    int argsFrom = smartIndexOf(tok, i, "(")+2;
18  
    int argsTo = findCodeTokens(tok, argsFrom, false, ")");
19  
    int idx = findCodeTokens(tok, argsTo, false, "{");
20  
    ifdef tok_recordDecls_debug
21  
      assertEqualsVerbose("{", get(tok, idx));
22  
    endifdef
23  
    int j = findEndOfBracketPart(tok, idx);
24  
    S name = tok.get(i+2);
25  
    
26  
    int iMod = tok_leftScanCustomModifiers(tok, i, addAllAndReturnCollection(litset("noeq", "flexeq", "noToString", "withToList", "transformable"), getJavaModifiers());
27  
    Set<S> mods = codeTokensAsSet(subList(tok, iMod-1, i));
28  
    clearTokensExceptFromSet(tok, iMod, i-1, getJavaModifiers());
29  
30  
    bool withToList = mods.contains("withToList");
31  
    withToList = withToList || tok_recordDecls_autoWithToList;
32  
    
33  
    new StringBuilder buf;
34  
    
35  
    L<S> tArgs = subList(tok, argsFrom-1, argsTo);
36  
    tok_typesAndNamesOfParams_keepModifiers.set(true);
37  
    LPairS argsWithModifiers = tok_typesAndNamesOfParams(tArgs, typelessMeansObject := true);
38  
    
39  
    LPairS args = tok_typesAndNamesOfParams(tArgs, typelessMeansObject := true);
40  
    L<S> contents = subList(tok, idx+1, j);
41  
    bool hasDefaultConstructor = tok_hasDefaultConstructor(contents, name);
42  
    
43  
    // Make fields
44  
    
45  
    for (Pair<S> p : argsWithModifiers)
46  
      buf.append("\n  " + jreplace(p.a, "...", "[]") + " " + p.b + ";");
47  
      
48  
    if (nempty(args) && !hasDefaultConstructor) buf.append("\n  *() {}");
49  
    
50  
    // add full constructor if not there
51  
    S ctorHead = "\n  *(" + joinWithComma(map(args,
52  
      p -> dropPrefix("new ", p.a) + " *" + p.b)) + ")";
53  
      
54  
    var comments = trimAll(getJavaLineCommentsWithoutNestedBlocks(contents));
55  
    printVars(+ctorHead, +comments);
56  
    if (!contains(comments, "!customConstructor")
57  
      && jfind(contents, tok_escapeAsterisk(ctorHead)) < 0)
58  
      buf.append(ctorHead + " {}");
59  
      
60  
    // Make toString
61  
    
62  
    if (!mods.contains("noToString") && !(jfind(contents, "toString {") >= 0 || jfind(contents, "toString()") >= 0))
63  
      buf.append("\n  toString { ret " 
64  
        + "shortClassName_dropNumberPrefix(this)"
65  
        + (empty(args) ? ""
66  
          : [[ + "(" + ]] + join([[ + ", " + ]], secondOfPairs(args))
67  
            + [[ + ")"]])
68  
        + "; }");
69  
        
70  
    // Make equals and hashCode
71  
      
72  
    if (!mods.contains("noeq")) {
73  
      //buf.append("\n  [stdEq]");
74  
      bool flexEq = mods.contains("flexeq"); // fix for records inside of parameterized classes
75  
      buf.append(tok_genEqualsAndHashCode(name, args, +flexEq));
76  
    }
77  
    
78  
    if (withToList)
79  
      buf.append(tok_genRecordFieldsToList(args));
80  
      
81  
    bool transformable = mods.contains("transformable");
82  
    if (transformable)
83  
      buf.append(tok_genRecordTransformable(name, args));
84  
      
85  
    S interfaces = joinNemptiesWithComma(
86  
      withToList ? "IFieldsToList" : "",
87  
      transformable ? "Transformable, Visitable" : "");
88  
89  
    tok.set(idx, (empty(interfaces) ? ""
90  
      : (contains(subList(tok, argsTo, idx), "implements") ? "," : "implements") + " " + interfaces)
91  
      + "{" + buf);
92  
    ifdef tok_recordDecls_debug
93  
      print("tok_recordDecls replaced " + tok.get(idx));
94  
      print("tok_recordDecls whole: " + join(tok));
95  
    endifdef
96  
    tok.set(i, "class");
97  
    clearTokens(tok, argsFrom-2, argsTo+1);
98  
    reTok(tok, i, idx+1);
99  
    
100  
    // no static fields allowed in non-static classes (for whatever reason)
101  
    if (contains(tok_modifiersLeftOf(tok, i), "static"))
102  
      tok_addFieldOrder(tok, i);
103  
104  
    ifdef tok_recordDecls_debug
105  
      print("tok_recordDecls whole 2: " + join(tok));
106  
    endifdef
107  
    set re;
108  
  }
109  
  if (re) reTok(tok);
110  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1012742
Snippet name: tok_recordDecls
Eternal ID of this version: #1012742/73
Text MD5: 889b6a3a7e61e01b8f155330189cdbfa
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-21 16:18:40
Source code size: 4305 bytes / 110 lines
Pitched / IR pitched: No / No
Views / Downloads: 781 / 1135
Version history: 72 change(s)
Referenced in: [show references]