1 8 9 package beaver.comp.run; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import beaver.Parser; 15 import beaver.comp.ParserGenerator; 16 import beaver.comp.io.SrcReader; 17 import beaver.comp.util.Log; 18 import beaver.spec.Grammar; 19 20 22 public class Make 23 { 24 static void printVersion() 25 { 26 System.err.print("Beaver parser generator v"); 27 System.err.println(ParserGenerator.VERSION); 28 System.err.println("Copyright (C) 2003,2004 Alexander Demenchuk <alder@softanvil.com>."); 29 System.err.println("All rights reserved."); 30 } 31 32 static void printUsage() 33 { 34 System.err.println("Usage: java -jar beaver.jar [options] <grammar file>"); 35 System.err.println("where possible options are:"); 36 System.err.println(" -a Generate parsing states report (.stat)"); 37 System.err.println(" -c Do not compress parsing tables"); 38 System.err.println(" -d <dir> Specify where to place generated files"); 39 System.err.println(" -D Do not generate anything - dry-run"); 40 System.err.println(" -e Export parsing tables into a file (.spec)"); 41 System.err.println(" -n Generate non-anonymous delegates for action routines"); 42 System.err.println(" -s Sort terminals (by name)"); 43 System.err.println(" -t Generate terminal names"); 44 System.err.println(" -T Export Terminals \"enum\" class into a file"); 45 System.err.println(" -s Sort terminals (by name)"); 46 System.err.println(" -w Use \"switch\" to invoke action routines"); 47 System.err.println(" -v Print version information and exit"); 48 System.err.println(" -h Print this help text and exit"); 49 } 50 51 static Options parseOptions(String [] args) 52 { 53 Options opt = new Options(); 54 int file_name_arg_index = args.length - 1; 55 for (int i = 0; i < file_name_arg_index; i++) 56 { 57 int len = args[i].length(); 58 if (len < 2 || args[i].charAt(0) != '-') 59 throw new IllegalArgumentException ("Error: \"" + args[i] + "\" is an invalid option."); 60 multioptions: 61 for (int j = 1; j < len; j++) 62 { 63 switch (args[i].charAt(j)) 64 { 65 case 'a': 66 opt.report_actions = true; 67 break; 68 case 'c': 69 opt.no_compression = true; 70 break; 71 case 'd': 72 { 73 if (++i == file_name_arg_index) 74 throw new IllegalArgumentException ("-d option specified without a destination directory."); 75 76 opt.dest_dir = new File (args[i]); 77 break multioptions; 78 } 79 case 'D': 80 opt.no_output = true; 81 break; 82 case 'e': 83 opt.exp_parsing_tables = true; 84 break; 85 case 'n': 86 opt.name_action_classes = true; 87 break; 88 case 's': 89 opt.sort_terminals = true; 90 break; 91 case 't': 92 opt.terminal_names = true; 93 break; 94 case 'T': 95 opt.export_terminals = true; 96 break; 97 case 'w': 98 opt.use_switch = true; 99 break; 100 case 'v': 101 printVersion(); 102 System.exit(0); 103 break; 104 case 'h': 105 printUsage(); 106 System.exit(0); 107 break; 108 default: 109 throw new IllegalArgumentException ("Error: \"-" + args[i].charAt(j) + "\" is an invalid option."); 110 } 111 } 112 } 113 return opt; 114 } 115 116 static File getSrcFile(String name) 117 { 118 File file = new File (name); 119 if (!file.canRead()) 120 throw new IllegalArgumentException ("Error: cannot read \"" + name + "\""); 121 return file; 122 } 123 124 static void compile(SrcReader src, Options opt, Log log) 125 { 126 try 127 { 128 ParserGenerator.compile(src, opt, log); 129 } 130 catch (Parser.Exception e) 131 { 132 System.err.print("Error: "); 133 System.err.println(e.getMessage()); 134 } 135 catch (Grammar.Exception e) 136 { 137 System.err.print("Error: "); 138 System.err.println(e.getMessage()); 139 } 140 catch (IOException e) 141 { 142 System.err.print("System Error: "); 143 System.err.println(e.getMessage()); 144 } 145 catch (Exception e) 146 { 147 e.printStackTrace(); 148 } 149 } 150 151 public static void main(String [] args) throws IOException 152 { 153 if (args.length == 0) 154 { 155 printUsage(); 156 } 157 else 158 { 159 try 160 { 161 Log log = new Log(); 162 Options opts = parseOptions(args); 163 File src_file = getSrcFile(args[args.length - 1]); 164 SrcReader src_reader = new SrcReader(src_file); 165 compile(src_reader, opts, log); 166 log.report(src_file.getName(), src_reader); 167 } 168 catch (IllegalArgumentException e) 169 { 170 System.err.println(e.getMessage()); 171 printUsage(); 172 } 173 } 174 } 175 } 176 | Popular Tags |