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

66
LINES

< > BotCompany Repo | #1027956 // tok_quicknew2

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

Libraryless. Click here for Pure Java version (4951L/29K).

1  
// pattern, replacement, index of following token
2  
static L<T3<S, S, Int>> tok_quicknew2_patterns = ll(
3  
  t3("new <id>", "new $2()", 5),
4  
  t3("new <id>.<id>", "new $2.$4()", 9),
5  
  t3("new <id> <>", "new $2<>()", 9),
6  
  t3("new <id> < <id> >", "new $2<$4>()", 11),
7  
);
8  
9  
svoid tok_quicknew2(L<S> tok) {
10  
  tok_quicknew(tok);
11  
  
12  
  bool needFix;
13  
14  
  // [abandoned, confusing, looks like a function definition] with arguments - new A a(...); => A a = new A(...);
15  
  //jreplace(tok, "new <id> <id>(", "$2 $3 = new $2(");
16  
17  
  jreplace(tok, "for args " + "{", "for (int i = 0; i < args.length; i++) { final String arg = args[i];");
18  
  
19  
  // Constructor calls without parentheses
20  
  // So you can say something like: predictors.add(new P1);
21  
  
22  
  for (T3<S, S, Int> t : tok_quicknew2_patterns)
23  
    jreplace1(tok, t.a, t.b, tokcondition {
24  
      ret eqOneOf(_get(tok, i+t.c), "{", ",", ")", ";", ":", "!");
25  
    });
26  
27  
  jreplace(tok, "new Collection(", "new List(");
28  
  jreplace(tok, "new List(", "new ArrayList(");
29  
  jreplace(tok, "new Map(", "new HashMap(");
30  
  jreplace(tok, "new Set(", "new HashSet(");
31  
  jreplace(tok, "new (Hash)Set", "new HashSet"); // rough
32  
  jreplace(tok, "new (Tree)Set", "new TreeSet");
33  
  jreplace(tok, "new (Hash)Map", "new HashMap");
34  
  jreplace(tok, "new (Tree)Map", "new TreeMap");
35  
  jreplace(tok, "\\*<id>[<id>] <id>;", "$2[] $6 = new $2[$4];");
36  
  
37  
  // X x = new(...)  =>  X x = new X(...)
38  
  // X x = new {...} =>  X x = new X {...}
39  
  // X x = new       =>  X x = new
40  
  jreplace(tok, "<id>.<id> <id> = new", "$1.$3 $4 = new $1.$3", tokcondition {
41  
    ret tokCondition_shortNew(tok, i+13);
42  
  });
43  
  jreplace(tok, "<id> <id> = new", "$1 $2 = new $1", tokcondition {
44  
    ret tokCondition_shortNew(tok, i+9);
45  
  });
46  
  jreplace(tok, "<id> <id> = new \\*", "$1 $2 = new $1");
47  
  jreplace(tok, "\\* <id> = new <id>", "$5 $2 = new $5");
48  
  
49  
  // TODO: use backward type scanning
50  
  
51  
  needFix |= jreplace(tok, "<id><<id>> <id> = new", "$1 $2 $3 $4 $5 = new $1<>", tokcondition {
52  
    ret tokCondition_shortNew(tok, i+9+3*2);
53  
  });
54  
  needFix |= jreplace(tok, "<id><<id>,<id>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 = new $1<>", tokcondition {
55  
    ret tokCondition_shortNew(tok, i+9+5*2);
56  
  });
57  
  needFix |= jreplace(tok, "<id><<id><<id>>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 $8 = new $1<>", tokcondition {
58  
    ret tokCondition_shortNew(tok, i+9+6*2);
59  
  });
60  
  needFix |= jreplace(tok, "<id><<id><<id>>,<id>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 = new $1<>", tokcondition {
61  
    ret tokCondition_shortNew(tok, i+9+8*2);
62  
  });
63  
  
64  
  // fix
65  
  if (needFix) jreplace(tok, "<><>", "<>");
66  
}

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: #1027956
Snippet name: tok_quicknew2
Eternal ID of this version: #1027956/13
Text MD5: b07ec6d9f0109f29b479713d42c0c5c4
Transpilation MD5: 3fdcc38b88c9e220e54de12a798fea3f
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-18 06:50:37
Source code size: 2640 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 225 / 341
Version history: 12 change(s)
Referenced in: [show references]