import java.net.*; import java.io.*; import javax.swing.*; import java.util.regex.*; import java.util.*; // Syntax: stringFunc { s.trim() } public class main { public static void main(String[] args) throws Exception { List tok = javaTok(loadMainJava()); for (int i = 0; i < 100; i++) { int idx = findCodeTokens(tok, "stringfunc", "{"); if (idx < 0) break; int j = findEndOfBracketPart(tok, idx+2); tok.set(idx, "new StringFunc() {\n" + "public String get(String s) {\n" + "try { return "); tok.set(idx+2, ""); tok.set(j-1, "; } catch (Exception _e) {\n" + " throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }"); } // add interface to main class List c = findMainClass(tok); if (join(c).indexOf("interface StringFunc") < 0) c.set(c.size()-2, "static interface StringFunc {\n" + " String get(String s);\n" + "}\n" + "}"); saveMainJava(join(tok)); } static List findMainClass(List tok) { for (List c : allClasses(tok)) if (getClassDeclarationName(c).equals("main")) return c; return null; } static int findCodeTokens(List tok, String... tokens) { outer: for (int i = 1; i+tokens.length*2-2 < tok.size(); i += 2) { for (int j = 0; j < tokens.length; j++) if (!tok.get(i+j*2).equals(tokens[j])) continue outer; return i; } return -1; } // i must point at the opening bracket (any of the 3 types) // index returned is index of closing bracket + 1 static int findEndOfBracketPart(List cnc, int i) { int j = i+2, level = 1; while (j < cnc.size()) { if (litlist("{", "(", "<").contains(cnc.get(j))) ++level; else if (litlist("}", ")", ">").contains(cnc.get(j))) --level; if (level == 0) return j+1; ++j; } return cnc.size(); } // replacement for class JavaTok // maybe incomplete, might want to add floating point numbers // todo also: extended multi-line strings static List javaTok(String s) { List tok = new ArrayList(); int l = s.length(); int i = 0; while (i < l) { int j = i; char c; String cc; // scan for whitespace while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j+2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/")); j = Math.min(j+2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); // cc is not needed in rest of loop body cc = s.substring(i, Math.min(i+2, l)); // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else if (cc.equals("[[")) { do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]")); j = Math.min(j+2, l); } else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } static String loadMainJava() throws IOException { return loadTextFile("input/main.java", ""); } static void saveMainJava(String s) throws IOException { saveTextFile("output/main.java", s); } public static String join(String glue, Iterable strings) { StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static String join(String glue, String[] strings) { return join(glue, Arrays.asList(strings)); } public static String join(Iterable strings) { return join("", strings); } public static String join(String[] strings) { return join("", strings); } static String getClassDeclarationName(List c) { for (int i = 1; i+2 < c.size(); i += 2) if (c.get(i).equals("class")) return c.get(i+2); return null; } // lists returned are actual CNC (N/C/N/.../C/N) static List> allClasses(List tok) { List> l = new ArrayList>(); for (int i = 1; i < tok.size(); i += 2) { if (tok.get(i).equals("class") && (i == 1 || !tok.get(i-2).equals("."))) { int j = i; while (j < tok.size() && !tok.get(j).equals("{")) j += 2; j = findEndOfBlock(tok, j)+1; i = leftScanModifiers(tok, i); l.add(tok.subList(i-1, Math.min(tok.size(), j))); i = j-2; } } return l; } static ArrayList litlist(A... a) { return new ArrayList(Arrays.asList(a)); } /** writes safely (to temp file, then rename) */ public static void saveTextFile(String fileName, String contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; if (contents != null) { FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (contents != null) if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } public static void saveTextFile(File fileName, String contents) { try { saveTextFile(fileName.getPath(), contents); } catch (IOException e) { throw new RuntimeException(e); } } public static String loadTextFile(String fileName) { try { return loadTextFile(fileName, null); } catch (IOException e) { throw new RuntimeException(e); } } public static String loadTextFile(String fileName, String defaultContents) throws IOException { if (!new File(fileName).exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(fileName); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } public static String loadTextFile(File fileName) { try { return loadTextFile(fileName, null); } catch (IOException e) { throw new RuntimeException(e); } } public static String loadTextFile(File fileName, String defaultContents) throws IOException { try { return loadTextFile(fileName.getPath(), defaultContents); } catch (IOException e) { throw new RuntimeException(e); } } public static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1024]; int n; while (-1 != (n = reader.read(buffer))) builder.append(buffer, 0, n); } finally { reader.close(); } return builder.toString(); } static List leftScanModifiers_list = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient"); static int leftScanModifiers(List tok, int i) { while (i > 1 && leftScanModifiers_list.contains(tok.get(i-2))) i -= 2; return i; } // i must point at the opening bracket ("{") // index returned is index of closing bracket + 1 static int findEndOfBlock(List cnc, int i) { int j = i+2, level = 1; while (j < cnc.size()) { if (cnc.get(j).equals("{")) ++level; else if (cnc.get(j).equals("}")) --level; if (level == 0) return j+1; ++j; } return cnc.size(); } }