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

54
LINES

< > BotCompany Repo | #700 // "no exceptions"/"ctex"/"null on exception" short-hand (JavaX translator)

JavaX translator [tags: use-pretranspiled]

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

// Syntax: void method() no exceptions { doSomething(); }
// (rethrows all inner exceptions as runtime exceptions)
//
// Alternative syntax: void method() ctex { doSomething(); }
// (ctex for catches thrown exceptions or something like that)

!592 1000265 // auto-importer, standard function adder 

public class main {
  public static void main(String[] args) throws Exception {
    String s = loadMainJava();
    
    Pattern regex = Pattern.compile("\\s+(no\\s+exceptions|ctex|null on exception)\\s*\\{");
    
    for (int i = 0; i < 100; i++) {
      Matcher matcher = regex.matcher(s);
      if (!matcher.find())
        break;
      String kind = matcher.group(1);

      System.out.println("Iteration " + (i+1));
      int start = matcher.start(), end = matcher.end();
      int endOfBlock = findEndOfBlock(s, end);
      
      String catchBlock, catchWhat;
      if (kind.startsWith("null")) {
        catchBlock = "return null;";
        catchWhat = "Throwable";
      } else {
        catchBlock = "throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e);";
        catchWhat = "Throwable";
      }
        
      String tryBlock = " { try {\n " +
        s.substring(end, endOfBlock) + "\n} catch (" + catchWhat + " __e) { " + catchBlock + " }";
      s = s.substring(0, start) + tryBlock + s.substring(endOfBlock);
    }
    
    saveMainJava(s);
  }
  
  // start is the index AFTER the opening bracket
  // returns index OF closing bracket
  static int findEndOfBlock(String s, int start) {
    int level = 1;
    for (int i = start; i < s.length(); i++) {
      if (s.charAt(i) == '{') ++level;
      else if (s.charAt(i) == '}') --level;
      if (level == 0)
        return i;
    }
    return s.length();
  }
}

Author comment

Began life as a copy of #614

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #700
Snippet name: "no exceptions"/"ctex"/"null on exception" short-hand (JavaX translator)
Eternal ID of this version: #700/1
Text MD5: c08c6f2361a5b473fd03ab85e71a0b80
Transpilation MD5: 1678bfc4e20059e570fbba1139cd0e07
Author: stefan
Category:
Type: JavaX translator
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-22 00:00:45
Source code size: 1818 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 664 / 21148
Referenced in: [show references]