| 1 17 18 package org.sablecc.sablecc.launcher; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 import java.io.PushbackReader ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import org.sablecc.sablecc.GlobalInformation; 29 import org.sablecc.sablecc.SemanticVerifier; 30 import org.sablecc.sablecc.Verbosity; 31 import org.sablecc.sablecc.exception.InternalException; 32 import org.sablecc.sablecc.exception.InvalidArgumentException; 33 import org.sablecc.sablecc.exception.SemanticException; 34 import org.sablecc.sablecc.syntax3.lexer.Lexer; 35 import org.sablecc.sablecc.syntax3.lexer.LexerException; 36 import org.sablecc.sablecc.syntax3.node.Start; 37 import org.sablecc.sablecc.syntax3.parser.Parser; 38 import org.sablecc.sablecc.syntax3.parser.ParserException; 39 40 public class SableCC { 41 42 public static void main( 43 String [] args) { 44 45 try { 46 boolean check_only = false; 47 Verbosity verbosity = Verbosity.NORMAL; 48 49 File destinationDirectory = new File (System.getProperty("user.dir")) 51 .getAbsoluteFile(); 52 53 Arguments arguments = new Arguments(args); 55 56 for (OptionArgument optionArgument : arguments.getOptionArguments()) { 58 59 switch (optionArgument.getOption()) { 60 61 case CHECK_ONLY: 62 check_only = true; 63 break; 64 65 case DESTINATION: 66 destinationDirectory = new File (optionArgument.getOperand()) 67 .getAbsoluteFile(); 68 69 if (!destinationDirectory.isDirectory()) { 70 71 if (!destinationDirectory.exists()) { 72 System.err.println("ERROR: " + destinationDirectory 73 + " does not exist"); 74 System.exit(1); 75 } 76 77 System.err.println("ERROR: " + destinationDirectory 78 + " is not a directory"); 79 System.exit(1); 80 } 81 82 break; 83 84 case QUIET: 85 verbosity = Verbosity.QUIET; 86 break; 87 88 case VERBOSE: 89 verbosity = Verbosity.VERBOSE; 90 break; 91 92 case VERSION: 93 System.out.println("SableCC version " + Version.VERSION); 94 System.exit(0); 95 96 case HELP: 97 System.out.println("usage: sablecc " 98 + Option.getShortHelpMessage() 99 + " specification.sablecc ..."); 100 System.out.println("options:"); 101 System.out.println(Option.getLongHelpMessage()); 102 System.exit(0); 103 104 default: 105 throw new InternalException("unhandled option " 106 + optionArgument.getOption()); 107 } 108 } 109 110 if (arguments.getTextArguments().size() == 0) { 112 System.err.println("usage: sablecc " 113 + Option.getShortHelpMessage() 114 + " specification.sablecc ..."); 115 System.err.println("type 'sablecc -h' for more information."); 116 System.exit(1); 117 } 118 119 switch (verbosity) { 120 case NORMAL: 121 case VERBOSE: 122 System.out.println(); 123 System.out.println("SableCC version " + Version.VERSION); 124 System.out 125 .println("by Etienne M. Gagnon <egagnon@j-meg.com> and other contributors."); 126 System.out.println(); 127 } 128 129 List <File > specificationFiles = new LinkedList <File >(); 130 131 for (TextArgument textArgument : arguments.getTextArguments()) { 134 135 if (!textArgument.getText().endsWith(".sablecc")) { 136 System.err 137 .println("ERROR: specification file name does not end with .sablecc: " 138 + textArgument.getText()); 139 System.exit(1); 140 } 141 142 File specificationFile = new File (textArgument.getText()) 143 .getAbsoluteFile(); 144 145 if (!specificationFile.isFile()) { 146 147 if (!specificationFile.exists()) { 148 System.err.println("ERROR: " + specificationFile 149 + " does not exist"); 150 System.exit(1); 151 } 152 153 System.err.println("ERROR: " + specificationFile 154 + " is not a file"); 155 System.exit(1); 156 } 157 158 specificationFiles.add(specificationFile); 159 } 160 161 for (File specificationFile : specificationFiles) { 163 164 compile(specificationFile, destinationDirectory, check_only, 165 verbosity); 166 } 167 } 168 catch (InvalidArgumentException e) { 169 System.err.println("ERROR: " + e.getMessage()); 170 System.exit(1); 171 } 172 catch (SemanticException e) { 173 System.err.println("ERROR: semantic error on '" 174 + e.getToken().getText() + "' at " + e.getMessage()); 175 } 176 catch (ParserException e) { 177 System.err.println("ERROR: syntax error on '" 178 + e.getToken().getText() + "' at " + e.getMessage()); 179 System.exit(1); 180 } 181 catch (LexerException e) { 182 System.err.println("ERROR: lexical error at " + e.getMessage()); 183 System.exit(1); 184 } 185 catch (RuntimeException e) { 186 e.printStackTrace(System.err); 187 System.err.println("INTERNAL ERROR: " + e.getMessage()); 188 System.err 189 .println("Please submit a defect ticket with the full error trace above on:"); 190 System.err.println(" http://sablecc.org/"); 191 System.exit(1); 192 } 193 catch (Error e) { 194 e.printStackTrace(System.err); 195 System.err.println("INTERNAL ERROR: " + e.getMessage()); 196 System.err 197 .println("Please submit a defect ticket with the full error trace above on:"); 198 System.err.println(" http://sablecc.org/"); 199 System.exit(1); 200 } 201 202 System.exit(0); 204 } 205 206 private static void compile( 207 File specificationFile, 208 File destinationDirectory, 209 boolean check_only, 210 Verbosity verbosity) 211 throws InvalidArgumentException, ParserException, LexerException, 212 SemanticException { 213 214 switch (verbosity) { 215 case NORMAL: 216 case VERBOSE: 217 System.out.println("Compiling " + specificationFile); 218 } 219 220 Start ast; 221 222 try { 223 FileReader fr = new FileReader (specificationFile); 224 BufferedReader br = new BufferedReader (fr); 225 PushbackReader pbr = new PushbackReader (br); 226 227 switch (verbosity) { 228 case VERBOSE: 229 System.out.println(" Parsing"); 230 } 231 232 ast = new Parser(new Lexer(pbr)).parse(); 233 234 pbr.close(); 235 br.close(); 236 fr.close(); 237 } 238 catch (IOException e) { 239 throw new InvalidArgumentException("cannot read " 240 + specificationFile, e); 241 } 242 243 GlobalInformation globalInformation = new GlobalInformation(verbosity, 244 ast); 245 new SemanticVerifier(globalInformation); 246 247 System.err.println("ERROR: unimplemented"); 248 System.exit(1); 249 } 250 } 251 | Popular Tags |