1 package fri.patterns.interpreter.parsergenerator.builder; 2 3 import java.util.List ; 4 import fri.util.TimeStopper; 5 import fri.patterns.interpreter.parsergenerator.Lexer; 6 import fri.patterns.interpreter.parsergenerator.syntax.*; 7 import fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxSeparation; 8 import fri.patterns.interpreter.parsergenerator.lexer.*; 9 10 36 37 public class SerializedLexer extends SerializedObject 38 { 39 private SyntaxSeparation separation; 40 protected boolean PRODUCTION; 42 43 public SerializedLexer() { 44 this(true); 45 } 46 47 48 public SerializedLexer(boolean production) { 49 this.PRODUCTION = production; 50 } 51 52 57 public Lexer get(Object syntaxInput) 58 throws Exception 59 { 60 return get(syntaxInput, null); 61 } 62 63 69 public Lexer get(Object syntaxInput, String baseName) 70 throws Exception 71 { 72 return get(syntaxInput, baseName, null, null); 73 } 74 75 83 public Lexer get(Object syntaxInput, String baseName, List tokenSymbols, List ignoredSymbols) 84 throws Exception 85 { 86 Lexer lexer = readLexer(syntaxInput, baseName); 87 if (lexer == null) 88 lexer = buildAndStoreLexer(syntaxInput, baseName, tokenSymbols, ignoredSymbols); 89 return lexer; 90 } 91 92 private String ensureFileName(Object syntaxInput, String baseName) { 93 if (baseName == null) 94 baseName = baseNameFromSyntax(syntaxInput); 95 return baseName+"Lexer.ser"; 96 } 97 98 103 public Lexer readLexer(Object syntaxInput, String baseName) { 104 if (PRODUCTION) 105 return (Lexer) read(ensureFileName(syntaxInput, baseName)); 106 return null; 107 } 108 109 114 public Lexer buildAndStoreLexer(Object syntaxInput, String baseName, List tokenSymbols, List ignoredSymbols) 115 throws Exception 116 { 117 Syntax syntax = toSyntax(syntaxInput); 118 119 if (tokenSymbols == null || ignoredSymbols == null) { 120 this.separation = newSyntaxSeparation(syntax); 121 syntax = separation.getLexerSyntax(); 122 123 if (tokenSymbols == null) 124 tokenSymbols = separation.getTokenSymbols(); 125 if (ignoredSymbols == null) 126 ignoredSymbols = separation.getIgnoredSymbols(); 127 } 128 130 TimeStopper ts = new TimeStopper(); 131 LexerBuilder builder = newLexerBuilder(syntax, ignoredSymbols); 132 Lexer lexer = builder.getLexer(); 133 lexer.setTerminals(tokenSymbols); 134 System.err.println("Lexer scratch construction took "+ts.getTimeMillis()+" millis"); 135 136 if (PRODUCTION) 137 write(ensureFileName(syntaxInput, baseName), lexer); 138 139 return lexer; 140 } 141 142 143 protected SyntaxSeparation newSyntaxSeparation(Syntax syntax) 144 throws SyntaxException 145 { 146 return new SyntaxSeparation(syntax); 147 } 148 149 150 protected LexerBuilder newLexerBuilder(Syntax syntax, List ignoredSymbols) 151 throws LexerException, SyntaxException 152 { 153 return new LexerBuilder(syntax, ignoredSymbols); 154 } 155 156 161 public SyntaxSeparation getSyntaxSeparation() { 162 return separation; 163 } 164 165 166 167 168 public static void main(String [] args) { 169 try { 170 TimeStopper ts = new TimeStopper(); 171 Lexer lexer = new SerializedLexer().get(StandardLexerRules.lexerSyntax, "SyntaxBuilder"); 172 System.err.println("Lexer was built in "+ts.getTimeMillis()+" millis"); 173 } 174 catch (Exception e) { 175 e.printStackTrace(); 176 } 177 } 178 179 } 180 | Popular Tags |