1 8 9 package beaver.comp.run; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.Task; 16 17 import beaver.Parser; 18 import beaver.Scanner; 19 import beaver.Symbol; 20 import beaver.comp.ParserGenerator; 21 import beaver.comp.io.SrcReader; 22 import beaver.comp.util.Log; 23 import beaver.spec.Grammar; 24 import beaver.spec.parser.GrammarScanner; 25 import beaver.spec.parser.GrammarParser.Terminals; 26 27 29 public class AntTask extends Task 30 { 31 private Options options = new Options(); 32 private File grammar_file; 33 34 public void setFile(File file) 35 { 36 grammar_file = file; 37 } 38 39 public void setDestdir(File file) 40 { 41 options.dest_dir = file; 42 } 43 44 public void setExportTables(boolean opt) 45 { 46 options.exp_parsing_tables = opt; 47 } 48 49 public void setExportTerminals(boolean opt) 50 { 51 options.export_terminals = opt; 52 } 53 54 public void setSortTerminals(boolean opt) 55 { 56 options.sort_terminals = opt; 57 } 58 59 public void setReportActions(boolean opt) 60 { 61 options.report_actions = opt; 62 } 63 64 public void setCompress(boolean opt) 65 { 66 options.no_compression = !opt; 67 } 68 69 public void setTerminalNames(boolean opt) 70 { 71 options.terminal_names = opt; 72 } 73 74 public void setAnonymousActions(boolean opt) 75 { 76 options.name_action_classes = !opt; 77 } 78 79 public void setUseSwitch(boolean opt) 80 { 81 options.use_switch = opt; 82 } 83 84 public void execute() throws BuildException 85 { 86 if (!grammar_file.canRead()) 87 throw new BuildException("Cannot read grammar file " + grammar_file); 88 89 if (options.dest_dir != null && !options.dest_dir.isDirectory()) 90 throw new IllegalArgumentException (options.dest_dir.getPath() + " is not a directory."); 91 92 SrcReader src = getSrcReader(grammar_file); 93 try 94 { 95 if (existsCurrentOutput(getOutputFileName(src))) 96 return; 97 } 98 catch (Exception e) 99 { 100 } 102 src.reset(); 103 Log log = new Log(); 104 compile(src, options, log); 105 log.report(grammar_file.getName(), src); 106 } 107 108 private boolean existsCurrentOutput(String output_file_name) 109 { 110 String dir = grammar_file.getParent(); 111 File output_file = new File (dir, output_file_name + ParserGenerator.SOURCE_FILE_EXT); 112 if (!output_file.canRead() || grammar_file.lastModified() > output_file.lastModified()) 113 return false; 114 115 output_file = new File (dir, output_file_name + ParserGenerator.SERIALIZED_PARSER_TABLES_FILE_EXT); 116 return !options.exp_parsing_tables || output_file.canRead() && grammar_file.lastModified() <= output_file.lastModified(); 117 } 118 119 static private SrcReader getSrcReader(File file) throws BuildException 120 { 121 try 122 { 123 return new SrcReader(file); 124 } 125 catch (IOException e) 126 { 127 throw new BuildException("Failed to read grammar file " + file); 128 } 129 } 130 131 static private String getOutputFileName(SrcReader src) throws IOException , Scanner.Exception 132 { 133 String output_file_name = src.file.getName(); 134 int dot_index = output_file_name.lastIndexOf('.'); 135 if (dot_index > 0) 136 { 137 output_file_name = output_file_name.substring(0, dot_index); 138 } 139 140 GrammarScanner scanner = new GrammarScanner(src); 141 for (Symbol sym = scanner.nextToken(); sym.getId() != Terminals.EOF; sym = scanner.nextToken()) 142 { 143 if (sym.getId() == Terminals.CLASS) 144 { 145 if ((sym = scanner.nextToken()).getId() == Terminals.TEXT) 146 { 147 String class_name = (String ) sym.value; 148 if (class_name != null && (class_name = class_name.trim()).length() > 0) 149 { 150 return class_name; 151 } 152 } 153 } 154 } 155 return output_file_name; 156 } 157 158 static private void compile(SrcReader src, Options opt, Log log) 159 { 160 try 161 { 162 ParserGenerator.compile(src, opt, log); 163 } 164 catch (Parser.Exception e) 165 { 166 System.err.print("Error: "); 167 System.err.println(e.getMessage()); 168 } 169 catch (Grammar.Exception e) 170 { 171 System.err.print("Error: "); 172 System.err.println(e.getMessage()); 173 } 174 catch (IOException e) 175 { 176 System.err.print("System Error: "); 177 System.err.println(e.getMessage()); 178 } 179 catch (Exception e) 180 { 181 e.printStackTrace(); 182 } 183 } 184 } 185 | Popular Tags |