1 package fri.patterns.interpreter.parsergenerator.builder; 2 3 import java.io.File ; 4 import java.lang.reflect.*; 5 import fri.patterns.interpreter.parsergenerator.parsertables.*; 6 import fri.patterns.interpreter.parsergenerator.syntax.Syntax; 7 8 31 32 public class CompiledTables 33 { 34 35 public static final String CLASSFILE_SUFFIX = "ParserTables"; 36 private boolean DEVELOPMENT = false; 38 39 public CompiledTables() { 40 this(false); 41 } 42 43 44 public CompiledTables(boolean development) { 45 this.DEVELOPMENT = development; 46 } 47 48 49 55 public AbstractParserTables get(Object syntaxInput) 56 throws Exception 57 { 58 return get(syntaxInput, null); 59 } 60 61 68 public AbstractParserTables get(Object syntaxInput, String className) 69 throws Exception 70 { 71 return get(LALRParserTables.class, syntaxInput, className); 72 } 73 74 82 public AbstractParserTables get(Class parserType, Object syntaxInput, String className) 83 throws Exception 84 { 85 if (className == null) 86 className = SerializedObject.baseNameFromSyntax(syntaxInput); 87 88 className = className+CLASSFILE_SUFFIX; 89 90 AbstractParserTables parserTables = null; 91 92 if (DEVELOPMENT == false) { 93 try { 94 Class cls = Class.forName(className); 95 parserTables = AbstractParserTables.construct(cls, null); 96 } 97 catch (Exception e) { System.err.println("Could not find compiled parser tables for classname "+className+" in "+System.getProperty("java.class.path")+" - trying scratch build: "+e.toString()); 99 } 100 } 101 102 if (parserTables == null) { 103 fri.util.TimeStopper ts = new fri.util.TimeStopper(); 104 105 Syntax grammar = SerializedObject.toSyntax(syntaxInput); 106 parserTables = AbstractParserTables.construct(parserType, grammar); 107 if (DEVELOPMENT == false) { 108 String javaFile = parserTables.toSourceFile(className); 109 int ret = compile(javaFile); 110 new File (javaFile).delete(); System.err.println("ParserTables scratch compilation took "+ts.getTimeMillis()+" millis, return was "+ret); 112 } 113 } 114 115 return parserTables; 116 } 117 118 119 123 protected int compile(String javaFile) { 124 try { 125 String javaCompiler = "com.sun.tools.javac.Main"; 126 System.err.println("compiling source: "+javaCompiler+" "+javaFile); 127 128 Class cls = Class.forName(javaCompiler); 129 Object compiler = cls.newInstance(); 130 Method method = cls.getMethod("compile", new Class [] { String [].class }); 131 String [] args = new String [] { 132 "-classpath", 133 "."+System.getProperty("path.separator")+System.getProperty("java.class.path"), 134 javaFile 135 }; 136 Object o = method.invoke(compiler, new Object [] { args }); 137 int ret = ((Integer )o).intValue(); 138 139 System.err.println("javac returns "+ret); 140 return ret; 141 } 142 catch (Exception e) { 143 e.printStackTrace(); 144 System.err.println("com.sun.tools.javac.Main() not present. Please put $JAVA_HOME/lib/tools.jar into CLASSPATH for quick loading next time."); 145 } 146 return -1; 147 } 148 149 150 151 152 public static void main(String [] args) { 153 try { 154 File syntaxFile = new File ("fri/patterns/interpreter/parsergenerator/syntax/builder/examples/SyntaxBuilder.syntax"); 155 Syntax syntax = new fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxBuilder(syntaxFile).getParserSyntax(); 156 fri.util.TimeStopper ts = new fri.util.TimeStopper(); 157 AbstractParserTables parserTables = new CompiledTables().get(syntax, "SyntaxBuilderParserTables"); 158 System.err.println("ParserTables were built in "+ts.getTimeMillis()+" millis"); 159 } 160 catch (Exception e) { 161 e.printStackTrace(); 162 } 163 } 164 165 } 166 | Popular Tags |