package cad; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Java2JS { List comments = new ArrayList<>(); List symbols = new ArrayList<>(); // List methods = new ArrayList<>(); int initCount = 0; public void convert() throws FileNotFoundException { // Scanner scanner = new Scanner(Java2JS.class.getResourceAsStream("lm.in")); Scanner scanner = new Scanner(new FileInputStream("/home/xibyte/Dropbox/project/cadit/src/cad/gcs/constr/Perpendicular.java")); String text = scanner.useDelimiter("\\A").next(); recordTo(symbols); text = rplc(text, "(? recorder; private void recordTo(List fields) { recorder = fields; } private void buildComment(String text) { comments.clear(); Pattern p = Pattern.compile("(?m)//.+$"); Matcher matcher = p.matcher(text); while (matcher.find()) { comments.add(matcher.toMatchResult()); } p = Pattern.compile("(?s)/\\*.+?\\*/"); matcher = p.matcher(text); while (matcher.find()) { comments.add(matcher.toMatchResult()); } } private String rplc(String text, String pattern, String replacement, int... groups) { buildComment(text); Pattern p = Pattern.compile(pattern); Matcher matcher = p.matcher(text); StringBuffer out = new StringBuffer(); while (matcher.find()) { if (recorder != null) recorder.add(matcher.toMatchResult()); if (isComment(matcher)) { continue; } String[] params = new String[groups.length]; for (int i = 0; i < groups.length; i++) { params[i] = matcher.group(groups[i]); } matcher.appendReplacement(out, String.format(replacement, params)); } matcher.appendTail(out); return out.toString(); } private boolean isComment(MatchResult matcher) { for (MatchResult comment : comments) { if (matcher.start() >= comment.start() && matcher.start() < comment.end()) { return true; } } return false; } private String rplcParams(String text) { buildComment(text); Pattern p = Pattern.compile("this\\.[^\\(]+=\\s*function\\s*\\((.+)\\)"); Matcher matcher = p.matcher(text); List replacements = new ArrayList<>(); while (matcher.find()) { replacements.add(matcher.toMatchResult()); } Collections.reverse(replacements); int off = text.length(); StringBuilder out = new StringBuilder(); for (MatchResult m : replacements) { if (isComment(m)) continue; out.insert(0, text.substring(m.end(1), off)); String sst = m.group(); out.insert(0, sst.replaceAll("(?