1 21 22 package net.percederberg.grammatica.ant; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileReader ; 27 28 import org.apache.tools.ant.BuildException; 29 30 import net.percederberg.grammatica.Grammar; 31 import net.percederberg.grammatica.GrammarException; 32 import net.percederberg.grammatica.TreePrinter; 33 import net.percederberg.grammatica.parser.Analyzer; 34 import net.percederberg.grammatica.parser.Node; 35 import net.percederberg.grammatica.parser.ParseException; 36 import net.percederberg.grammatica.parser.Parser; 37 import net.percederberg.grammatica.parser.ParserCreationException; 38 import net.percederberg.grammatica.parser.ParserLogException; 39 import net.percederberg.grammatica.parser.Token; 40 import net.percederberg.grammatica.parser.Tokenizer; 41 42 50 public class ValidationElement implements ProcessingElement { 51 52 55 private String type = null; 56 57 60 private File file = null; 61 62 65 private boolean quiet = false; 66 67 70 public ValidationElement() { 71 } 72 73 79 public void setType(String type) { 80 this.type = type; 81 } 82 83 89 public void setInputfile(File file) { 90 this.file = file; 91 } 92 93 98 public void setQuiet(boolean quiet) { 99 this.quiet = quiet; 100 } 101 102 108 public void validate() throws BuildException { 109 if (type == null) { 110 throw new BuildException( 111 "missing 'type' attribute in <validate>"); 112 } 113 if (!type.equals("debug") 114 && !type.equals("tokenize") 115 && !type.equals("parse") 116 && !type.equals("profile")) { 117 118 throw new BuildException( 119 "value of 'type' attribute in <validate> must be one " + 120 "of 'debug', 'tokenize', 'parse', or 'profile'"); 121 } 122 if (file == null && !type.equals("debug")) { 123 throw new BuildException( 124 "missing 'inputfile' attribute in <validate>"); 125 } 126 } 127 128 136 public void process(Grammar grammar) throws BuildException { 137 if (type.equals("debug")) { 138 debug(grammar); 139 } else if (type.equals("tokenize")) { 140 tokenize(grammar); 141 } else if (type.equals("parse")) { 142 parse(grammar); 143 } else if (type.equals("profile")) { 144 profile(grammar); 145 } else { 146 throw new BuildException("unknown <validation> type: " + type); 147 } 148 } 149 150 157 private void debug(Grammar grammar) throws BuildException { 158 Tokenizer tokenizer = null; 159 Parser parser = null; 160 161 try { 163 tokenizer = grammar.createTokenizer(null); 164 parser = grammar.createParser(tokenizer); 165 } catch (GrammarException e) { 166 throw new BuildException("in grammar " + grammar.getFileName() + 167 ": " + e.getMessage()); 168 } 169 170 if (!quiet) { 172 System.out.println("Contents of " + grammar.getFileName() + ":"); 173 System.out.println(); 174 System.out.println("Token Declarations:"); 175 System.out.println("-------------------"); 176 System.out.print(tokenizer); 177 System.out.println("Production Declarations:"); 178 System.out.println("------------------------"); 179 System.out.print(parser); 180 } 181 } 182 183 192 private void tokenize(Grammar grammar) throws BuildException { 193 Tokenizer tokenizer; 194 Token token; 195 196 try { 197 tokenizer = grammar.createTokenizer(new FileReader (file)); 198 if (!quiet) { 199 System.out.println("Tokens from " + file + ":"); 200 } 201 while ((token = tokenizer.next()) != null) { 202 if (!quiet) { 203 System.out.println(token); 204 } 205 } 206 } catch (FileNotFoundException e) { 207 throw new BuildException(e.getMessage()); 208 } catch (GrammarException e) { 209 throw new BuildException("in grammar " + grammar.getFileName() + 210 ": " + e.getMessage()); 211 } catch (ParseException e) { 212 throw new BuildException("in file " + file + ": " + 213 e.getMessage()); 214 } 215 } 216 217 225 private void parse(Grammar grammar) throws BuildException { 226 Tokenizer tokenizer; 227 Analyzer analyzer; 228 Parser parser; 229 230 try { 231 tokenizer = grammar.createTokenizer(new FileReader (file)); 232 if (quiet) { 233 analyzer = null; 234 } else { 235 analyzer = new TreePrinter(System.out); 236 } 237 parser = grammar.createParser(tokenizer, analyzer); 238 if (!quiet) { 239 System.out.println("Parse tree from " + file + ":"); 240 } 241 parser.parse(); 242 } catch (FileNotFoundException e) { 243 throw new BuildException(e.getMessage()); 244 } catch (GrammarException e) { 245 throw new BuildException("in grammar " + grammar.getFileName() + 246 ": " + e.getMessage()); 247 } catch (ParserCreationException e) { 248 throw new BuildException("in grammar " + grammar.getFileName() + 249 ": " + e.getMessage()); 250 } catch (ParserLogException e) { 251 throw new BuildException("in file " + file + ": " + 252 e.getMessage()); 253 } 254 } 255 256 265 private void profile(Grammar grammar) throws BuildException { 266 Tokenizer tokenizer; 267 Parser parser; 268 Node node; 269 long time; 270 int counter; 271 272 try { 274 tokenizer = grammar.createTokenizer(new FileReader (file)); 275 System.out.println("Tokenizing " + file); 276 time = System.currentTimeMillis(); 277 counter = 0; 278 while (tokenizer.next() != null) { 279 counter++; 280 } 281 time = System.currentTimeMillis() - time; 282 System.out.println(" Time elapsed: " + time + " millisec"); 283 System.out.println(" Tokens found: " + counter); 284 System.out.println(" Average speed: " + (counter / time) + 285 " tokens/millisec"); 286 } catch (FileNotFoundException e) { 287 throw new BuildException(e.getMessage()); 288 } catch (GrammarException e) { 289 throw new BuildException("in grammar " + grammar.getFileName() + 290 ": " + e.getMessage()); 291 } catch (ParseException e) { 292 throw new BuildException("in file " + file + ": " + 293 e.getMessage()); 294 } 295 296 try { 298 tokenizer = grammar.createTokenizer(new FileReader (file)); 299 parser = grammar.createParser(tokenizer); 300 System.out.println("Parsing " + file); 301 time = System.currentTimeMillis(); 302 node = parser.parse(); 303 time = System.currentTimeMillis() - time; 304 counter = 1 + node.getDescendantCount(); 305 System.out.println(" Time elapsed: " + time + " millisec"); 306 System.out.println(" Nodes found: " + counter); 307 System.out.println(" Average speed: " + (counter / time) + 308 " nodes/millisec"); 309 } catch (FileNotFoundException e) { 310 throw new BuildException(e.getMessage()); 311 } catch (GrammarException e) { 312 throw new BuildException("in grammar " + grammar.getFileName() + 313 ": " + e.getMessage()); 314 } catch (ParserCreationException e) { 315 throw new BuildException("in grammar " + grammar.getFileName() + 316 ": " + e.getMessage()); 317 } catch (ParserLogException e) { 318 throw new BuildException("in file " + file + ": " + 319 e.getMessage()); 320 } 321 } 322 } 323 | Popular Tags |