1 package fri.patterns.interpreter.parsergenerator.builder; 2 3 import java.io.File ; 4 import fri.patterns.interpreter.parsergenerator.*; 5 import fri.patterns.interpreter.parsergenerator.syntax.Syntax; 6 7 25 26 public class SerializedParser extends SerializedObject 27 { 28 29 public static final String PARSERFILE_SUFFIX = "Parser.ser"; 30 protected boolean PRODUCTION; 32 33 public SerializedParser() { 34 this(true); 35 } 36 37 38 public SerializedParser(boolean production) { 39 this.PRODUCTION = production; 40 } 41 42 47 public Parser get(Object syntaxInput) 48 throws Exception 49 { 50 return get(null, syntaxInput); 51 } 52 53 59 public Parser get(Object syntaxInput, String baseName) 60 throws Exception 61 { 62 return get(null, syntaxInput, baseName); 63 } 64 65 71 public Parser get(Class parserType, Object syntaxInput) 72 throws Exception 73 { 74 return get(parserType, syntaxInput, null); 75 } 76 77 84 public Parser get(Class parserType, Object syntaxInput, String baseName) 85 throws Exception 86 { 87 fri.util.TimeStopper ts = new fri.util.TimeStopper(); 88 89 Parser parser = PRODUCTION ? (Parser) read(ensureFileName(syntaxInput, baseName)) : null; 90 if (parser == null) { 91 SerializedLexer lexerFactory = newSerializedLexer(); 92 lexerFactory.PRODUCTION = false; Lexer lexer = lexerFactory.buildAndStoreLexer(syntaxInput, baseName, null, null); Syntax parserSyntax = lexerFactory.getSyntaxSeparation().getParserSyntax(); 95 SerializedTables tablesFactory = new SerializedTables(false); ParserTables tables = tablesFactory.buildAndStoreParserTables(parserType, parserSyntax, syntaxInput, baseName); 97 parser = new Parser(tables); 98 parser.setLexer(lexer); 99 100 if (PRODUCTION) 101 write(ensureFileName(syntaxInput, baseName), parser); 102 } 103 104 System.err.println("Parser construction took "+ts.getTimeMillis()+" millis"); 105 return parser; 106 } 107 108 109 protected SerializedLexer newSerializedLexer() 110 throws Exception 111 { 112 return new SerializedLexer(false); 113 } 114 115 private String ensureFileName(Object syntaxInput, String baseName) { 116 if (baseName == null) 117 baseName = baseNameFromSyntax(syntaxInput); 118 return baseName+PARSERFILE_SUFFIX; 119 } 120 121 122 123 124 public static void main(String [] args) { 125 try { 126 File ebnfFile = new File ("fri/patterns/interpreter/parsergenerator/syntax/builder/examples/SyntaxBuilder.syntax"); 127 File fileToParse = new File ("fri/patterns/interpreter/parsergenerator/syntax/builder/examples/SimpleSyntax.syntax"); 128 129 fri.util.TimeStopper ts = new fri.util.TimeStopper(); 130 SerializedParser parserBuilder = new SerializedParser(); 131 Parser parser = parserBuilder.get(null, ebnfFile, "SyntaxBuilder"); 132 Lexer lexer = parser.getLexer(); 133 System.err.println("Parser was built in "+ts.getTimeMillis()+" millis"); 134 lexer.setDebug(true); 135 136 lexer.setInput(fileToParse); 137 boolean ok = parser.parse(lexer); 138 System.err.println("Parsing result is "+ok); 139 } 140 catch (Exception e) { 141 e.printStackTrace(); 142 } 143 } 144 145 } 146 | Popular Tags |