📄 basetest.java
字号:
treeParserName, lexerName, parserStartRuleName, treeParserStartRuleName, debug); } else if ( parserBuildsTemplate ) { writeTemplateTestFile(parserName, lexerName, parserStartRuleName, debug); } else { writeTestFile(parserName, lexerName, parserStartRuleName, debug); } compile("Test.java"); try { String[] args = new String[] { "java", "-classpath", CLASSPATH+pathSep+tmpdir, "Test", new File(tmpdir, "input").getAbsolutePath() }; String cmdLine = "java -classpath "+CLASSPATH+pathSep+tmpdir+" Test " + new File(tmpdir, "input").getAbsolutePath(); //System.out.println("execParser: "+cmdLine); this.stderr = null; Process process = Runtime.getRuntime().exec(args, null, new File(tmpdir)); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stdoutVacuum.start(); stderrVacuum.start(); process.waitFor(); stdoutVacuum.join(); stderrVacuum.join(); String output = null; output = stdoutVacuum.toString(); if ( stderrVacuum.toString().length()>0 ) { this.stderr = stderrVacuum.toString(); System.err.println("exec parser stderrVacuum: "+ stderrVacuum); } return output; } catch (Exception e) { System.err.println("can't exec parser"); e.printStackTrace(System.err); } return null; } public static class StreamVacuum implements Runnable { StringBuffer buf = new StringBuffer(); BufferedReader in; Thread sucker; public StreamVacuum(InputStream in) { this.in = new BufferedReader( new InputStreamReader(in) ); } public void start() { sucker = new Thread(this); sucker.start(); } public void run() { try { String line = in.readLine(); while (line!=null) { buf.append(line); buf.append('\n'); line = in.readLine(); } } catch (IOException ioe) { System.err.println("can't read output from process"); } } /** wait for the thread to finish */ public void join() throws InterruptedException { sucker.join(); } public String toString() { return buf.toString(); } } protected void writeFile(String dir, String fileName, String content) { try { File f = new File(dir, fileName); FileWriter w = new FileWriter(f); BufferedWriter bw = new BufferedWriter(w); bw.write(content); bw.close(); w.close(); } catch (IOException ioe) { System.err.println("can't write file"); ioe.printStackTrace(System.err); } } protected void mkdir(String dir) { File f = new File(dir); f.mkdirs(); } protected void writeTestFile(String parserName, String lexerName, String parserStartRuleName, boolean debug) { StringTemplate outputFileST = new StringTemplate( "import org.antlr.runtime.*;\n" + "import org.antlr.runtime.tree.*;\n" + "import org.antlr.runtime.debug.*;\n" + "\n" + "class Profiler2 extends Profiler {\n" + " public void terminate() { ; }\n" + "}\n"+ "public class Test {\n" + " public static void main(String[] args) throws Exception {\n" + " CharStream input = new ANTLRFileStream(args[0]);\n" + " $lexerName$ lex = new $lexerName$(input);\n" + " CommonTokenStream tokens = new CommonTokenStream(lex);\n" + " $createParser$\n"+ " parser.$parserStartRuleName$();\n" + " }\n" + "}" ); StringTemplate createParserST = new StringTemplate( " Profiler2 profiler = new Profiler2();\n"+ " $parserName$ parser = new $parserName$(tokens,profiler);\n" + " profiler.setParser(parser);\n"); if ( !debug ) { createParserST = new StringTemplate( " $parserName$ parser = new $parserName$(tokens);\n"); } outputFileST.setAttribute("createParser", createParserST); outputFileST.setAttribute("parserName", parserName); outputFileST.setAttribute("lexerName", lexerName); outputFileST.setAttribute("parserStartRuleName", parserStartRuleName); writeFile(tmpdir, "Test.java", outputFileST.toString()); } protected void writeTreeTestFile(String parserName, String treeParserName, String lexerName, String parserStartRuleName, String treeParserStartRuleName, boolean debug) { StringTemplate outputFileST = new StringTemplate( "import org.antlr.runtime.*;\n" + "import org.antlr.runtime.tree.*;\n" + "import org.antlr.runtime.debug.*;\n" + "\n" + "class Profiler2 extends Profiler {\n" + " public void terminate() { ; }\n" + "}\n"+ "public class Test {\n" + " public static void main(String[] args) throws Exception {\n" + " CharStream input = new ANTLRFileStream(args[0]);\n" + " $lexerName$ lex = new $lexerName$(input);\n" + " TokenRewriteStream tokens = new TokenRewriteStream(lex);\n" + " $createParser$\n"+ " $parserName$.$parserStartRuleName$_return r = parser.$parserStartRuleName$();\n" + " $if(!treeParserStartRuleName)$\n" + " if ( r.tree!=null )\n" + " System.out.println(((Tree)r.tree).toStringTree());\n" + " $else$\n" + " CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);\n" + " nodes.setTokenStream(tokens);\n" + " $treeParserName$ walker = new $treeParserName$(nodes);\n" + " walker.$treeParserStartRuleName$();\n" + " $endif$\n" + " }\n" + "}" ); StringTemplate createParserST = new StringTemplate( " Profiler2 profiler = new Profiler2();\n"+ " $parserName$ parser = new $parserName$(tokens,profiler);\n" + " profiler.setParser(parser);\n"); if ( !debug ) { createParserST = new StringTemplate( " $parserName$ parser = new $parserName$(tokens);\n"); } outputFileST.setAttribute("createParser", createParserST); outputFileST.setAttribute("parserName", parserName); outputFileST.setAttribute("treeParserName", treeParserName); outputFileST.setAttribute("lexerName", lexerName); outputFileST.setAttribute("parserStartRuleName", parserStartRuleName); outputFileST.setAttribute("treeParserStartRuleName", treeParserStartRuleName); writeFile(tmpdir, "Test.java", outputFileST.toString()); } protected void writeTemplateTestFile(String parserName, String lexerName, String parserStartRuleName, boolean debug) { StringTemplate outputFileST = new StringTemplate( "import org.antlr.runtime.*;\n" + "import org.antlr.stringtemplate.*;\n" + "import org.antlr.stringtemplate.language.*;\n" + "import org.antlr.runtime.debug.*;\n" + "import java.io.*;\n" + "\n" + "class Profiler2 extends Profiler {\n" + " public void terminate() { ; }\n" + "}\n"+ "public class Test {\n" + " static String templates =\n" + " \"group test;\"+" + " \"foo(x,y) ::= \\\"<x> <y>\\\"\";\n"+ " static StringTemplateGroup group ="+ " new StringTemplateGroup(new StringReader(templates)," + " AngleBracketTemplateLexer.class);"+ " public static void main(String[] args) throws Exception {\n" + " CharStream input = new ANTLRFileStream(args[0]);\n" + " $lexerName$ lex = new $lexerName$(input);\n" + " CommonTokenStream tokens = new CommonTokenStream(lex);\n" + " $createParser$\n"+ " parser.setTemplateLib(group);\n"+ " $parserName$.$parserStartRuleName$_return r = parser.$parserStartRuleName$();\n" + " if ( r.st!=null )\n" + " System.out.print(r.st.toString());\n" + " else\n" + " System.out.print(\"\");\n" + " }\n" + "}" ); StringTemplate createParserST = new StringTemplate( " Profiler2 profiler = new Profiler2();\n"+ " $parserName$ parser = new $parserName$(tokens,profiler);\n" + " profiler.setParser(parser);\n"); if ( !debug ) { createParserST = new StringTemplate( " $parserName$ parser = new $parserName$(tokens);\n"); } outputFileST.setAttribute("createParser", createParserST); outputFileST.setAttribute("parserName", parserName); outputFileST.setAttribute("lexerName", lexerName); outputFileST.setAttribute("parserStartRuleName", parserStartRuleName); writeFile(tmpdir, "Test.java", outputFileST.toString()); } protected void eraseFiles(final String filesEndingWith) { File tmpdirF = new File(tmpdir); String[] files = tmpdirF.list(); for(int i = 0; files!=null && i < files.length; i++) { if ( files[i].endsWith(filesEndingWith) ) { new File(tmpdir+"/"+files[i]).delete(); } } } public String getFirstLineOfException() { if ( this.stderr==null ) { return null; } String[] lines = this.stderr.split("\n"); String prefix="Exception in thread \"main\" "; return lines[0].substring(prefix.length(),lines[0].length()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -