Libraryless. Click here for Pure Java version (142L/2K/5K).
| 1 | // Syntax: void method() no exceptions { doSomething(); }
 | 
| 2 | // (rethrows all inner exceptions as runtime exceptions) | 
| 3 | // | 
| 4 | // Alternative syntax: void method() ctex { doSomething(); }
 | 
| 5 | // (ctex for catches thrown exceptions or something like that) | 
| 6 | |
| 7 | !592 1000265 // auto-importer, standard function adder | 
| 8 | |
| 9 | public class main {
 | 
| 10 |   public static void main(String[] args) throws Exception {
 | 
| 11 | String s = loadMainJava(); | 
| 12 | |
| 13 |     Pattern regex = Pattern.compile("\\s+(no\\s+exceptions|ctex|null on exception)\\s*\\{");
 | 
| 14 | |
| 15 |     for (int i = 0; i < 100; i++) {
 | 
| 16 | Matcher matcher = regex.matcher(s); | 
| 17 | if (!matcher.find()) | 
| 18 | break; | 
| 19 | String kind = matcher.group(1); | 
| 20 | |
| 21 |       System.out.println("Iteration " + (i+1));
 | 
| 22 | int start = matcher.start(), end = matcher.end(); | 
| 23 | int endOfBlock = findEndOfBlock(s, end); | 
| 24 | |
| 25 | String catchBlock, catchWhat; | 
| 26 |       if (kind.startsWith("null")) {
 | 
| 27 | catchBlock = "return null;"; | 
| 28 | catchWhat = "Throwable"; | 
| 29 |       } else {
 | 
| 30 | catchBlock = "throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e);"; | 
| 31 | catchWhat = "Throwable"; | 
| 32 | } | 
| 33 | |
| 34 |       String tryBlock = " { try {\n " +
 | 
| 35 |         s.substring(end, endOfBlock) + "\n} catch (" + catchWhat + " __e) { " + catchBlock + " }";
 | 
| 36 | s = s.substring(0, start) + tryBlock + s.substring(endOfBlock); | 
| 37 | } | 
| 38 | |
| 39 | saveMainJava(s); | 
| 40 | } | 
| 41 | |
| 42 | // start is the index AFTER the opening bracket | 
| 43 | // returns index OF closing bracket | 
| 44 |   static int findEndOfBlock(String s, int start) {
 | 
| 45 | int level = 1; | 
| 46 |     for (int i = start; i < s.length(); i++) {
 | 
| 47 |       if (s.charAt(i) == '{') ++level;
 | 
| 48 | else if (s.charAt(i) == '}') --level; | 
| 49 | if (level == 0) | 
| 50 | return i; | 
| 51 | } | 
| 52 | return s.length(); | 
| 53 | } | 
| 54 | } | 
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: | 1159 / 21732 | 
| Referenced in: | [show references] |