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

41
LINES

< > BotCompany Repo | #711 // "runnable {" (JavaX translator)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (129L/2K/5K).

1  
// Syntax: runnable { doSomething(); }
2  
3  
!592 1000265 // auto-importer, standard function adder 
4  
5  
public class main {
6  
  public static void main(String[] args) throws Exception {
7  
    String s = loadMainJava();
8  
    
9  
    Pattern regex = Pattern.compile("(\\W)(?:runnable|r)\\s*\\{");
10  
    
11  
    for (int i = 0; i < 100; i++) {
12  
      Matcher matcher = regex.matcher(s);
13  
      if (!matcher.find())
14  
        break;
15  
16  
      System.out.println("Iteration " + (i+1));
17  
      int start = matcher.start()+1, end = matcher.end();
18  
      int endOfBlock = findEndOfBlock(s, end);
19  
      
20  
      String listener = "new Runnable() {\n" +
21  
        "public void run() { try {\n" +
22  
        s.substring(end, endOfBlock) + "\n} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}";
23  
      s = s.substring(0, start) + listener + s.substring(endOfBlock);
24  
    }
25  
    
26  
    saveMainJava(s);
27  
  }
28  
  
29  
  // start is the index AFTER the opening bracket
30  
  // returns index OF closing bracket
31  
  static int findEndOfBlock(String s, int start) {
32  
    int level = 1;
33  
    for (int i = start; i < s.length(); i++) {
34  
      if (s.charAt(i) == '{') ++level;
35  
      else if (s.charAt(i) == '}') --level;
36  
      if (level == 0)
37  
        return i;
38  
    }
39  
    return s.length();
40  
  }
41  
}

Author comment

Began life as a copy of #614

download  show line numbers  debug dex  old transpilations   

Travelled to 21 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, dhtvkmknsjym, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, jlatgrcjtklg, jtubtzbbkimh, liwcxgsjrgqn, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #711
Snippet name: "runnable {" (JavaX translator)
Eternal ID of this version: #711/1
Text MD5: 24eec70f516a8b8c02a054e0f34b5c22
Transpilation MD5: f354894b00855c9da2b85e041aec3c7c
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-04 23:55:51
Source code size: 1329 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 681 / 14338
Referenced in: [show references]