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

41
LINES

< > BotCompany Repo | #696 // "*" constructor syntax (translator)

JavaX translator

!636
!quicktranslator
!class JavaTok

// expand shortened constructor syntax:
//   *(...) { ... }
// to
//   MyClass(...) { ... }

List<String> tok = JavaTok.split(s);
mainLoop: for (int i = 3; i+6 < tok.size(); i += 2) {
  String t = tok.get(i), l = tok.get(i-2);
  if (!t.equals("*"))
    continue;
  if (!tok.get(i+2).equals("("))
    continue;
  if (!l.equals("}") && !l.equals(";")) // is this correct...??
    continue;
    
  // ok, it seems like a constructor declaration.
  // Now find class name by going backwards.
  
  int j = i, level = 1;
  while (j > 0 && level > 0) {
    t = tok.get(j);
    if (t.equals("}")) ++level;
    if (t.equals("{")) --level;
    j -= 2;
  }
  
  while (j > 0) {
    t = tok.get(j);
    if (t.equals("class")) {
      String className = tok.get(j+2);
      tok.set(i, className); // done!
      continue mainLoop;
    }
    j -= 2;
  }
}
s = JavaTok.join(tok);

Author comment

Began life as a copy of #1000381

download  show line numbers  debug dex  old transpilations   

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

Comments [hide]

ID Author/Program Comment Date
618 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
!636
!quicktranslator
!class JavaTok

// expand shortened constructor syntax:
// *(...) { ... }
// to
// MyClass(...) { ... }

List<String> tok = JavaTok.split(s);
mainLoop: for (int i = 3; i+6 < tok.size(); i += 2) {
String t = tok.get(i), l = tok.get(i-2);
if (!t.equals("*"))
continue;
if (!tok.get(i+2).equals("("))
continue;
if (!l.equals("}") && !l.equals(";")) // is this correct...??
continue;

// ok, it seems like a constructor declaration.
// Now find class name by going backwards.

int j = i, level = 1;
while (j > 0 && level > 0) {
t = tok.get(j);
if (t.equals("}")) ++level;
if (t.equals("{")) --level;
j -= 2;
}

while (j > 0) {
t = tok.get(j);
if (t.equals("class")) {
String className = tok.get(j+2);
tok.set(i, className); // done!
continue mainLoop;
}
j -= 2;
}
}
s = JavaTok.join(tok);
}}
2015-08-19 00:55:38  delete 
616 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #696
Snippet name: "*" constructor syntax (translator)
Eternal ID of this version: #696/1
Text MD5: 63e0ba9f5897f4cd80065d3d78621c86
Author: stefan
Category: javax
Type: JavaX translator
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-31 15:15:46
Source code size: 942 bytes / 41 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1437 / 1185
Referenced in: [show references]