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

// pattern, replacement, index of following token
static L<T3<S, S, Int>> tok_quicknew2_patterns = ll(
  t3("new <id>", "new $2()", 5),
  t3("new <id>.<id>", "new $2.$4()", 9),
  t3("new <id> <>", "new $2<>()", 9),
  t3("new <id> < <id> >", "new $2<$4>()", 11),
);

svoid tok_quicknew2(L<S> tok) {
  tok_quicknew(tok);
  
  bool needFix;

  // [abandoned, confusing, looks like a function definition] with arguments - new A a(...); => A a = new A(...);
  //jreplace(tok, "new <id> <id>(", "$2 $3 = new $2(");

  jreplace(tok, "for args " + "{", "for (int i = 0; i < args.length; i++) { final String arg = args[i];");
  
  // Constructor calls without parentheses
  // So you can say something like: predictors.add(new P1);
  
  for (T3<S, S, Int> t : tok_quicknew2_patterns)
    jreplace1(tok, t.a, t.b, tokcondition {
      ret eqOneOf(_get(tok, i+t.c), "{", ",", ")", ";", ":", "!");
    });

  jreplace(tok, "new Collection(", "new List(");
  jreplace(tok, "new List(", "new ArrayList(");
  jreplace(tok, "new Map(", "new HashMap(");
  jreplace(tok, "new Set(", "new HashSet(");
  jreplace(tok, "new (Hash)Set", "new HashSet"); // rough
  jreplace(tok, "new (Tree)Set", "new TreeSet");
  jreplace(tok, "new (Hash)Map", "new HashMap");
  jreplace(tok, "new (Tree)Map", "new TreeMap");
  jreplace(tok, "\\*<id>[<id>] <id>;", "$2[] $6 = new $2[$4];");
  
  // X x = new(...)  =>  X x = new X(...)
  // X x = new {...} =>  X x = new X {...}
  // X x = new       =>  X x = new
  jreplace(tok, "<id>.<id> <id> = new", "$1.$3 $4 = new $1.$3", tokcondition {
    ret tokCondition_shortNew(tok, i+13);
  });
  jreplace(tok, "<id> <id> = new", "$1 $2 = new $1", tokcondition {
    ret tokCondition_shortNew(tok, i+9);
  });
  jreplace(tok, "<id> <id> = new \\*", "$1 $2 = new $1");
  jreplace(tok, "\\* <id> = new <id>", "$5 $2 = new $5");
  
  // TODO: use backward type scanning
  
  needFix |= jreplace(tok, "<id><<id>> <id> = new", "$1 $2 $3 $4 $5 = new $1<>", tokcondition {
    ret tokCondition_shortNew(tok, i+9+3*2);
  });
  needFix |= jreplace(tok, "<id><<id>,<id>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 = new $1<>", tokcondition {
    ret tokCondition_shortNew(tok, i+9+5*2);
  });
  needFix |= jreplace(tok, "<id><<id><<id>>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 $8 = new $1<>", tokcondition {
    ret tokCondition_shortNew(tok, i+9+6*2);
  });
  needFix |= jreplace(tok, "<id><<id><<id>>,<id>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 = new $1<>", tokcondition {
    ret tokCondition_shortNew(tok, i+9+8*2);
  });
  
  // fix
  if (needFix) jreplace(tok, "<><>", "<>");
}

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: 285 / 418
Version history: 12 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)