private static void api(String file, Option<String> out, Option<String> prepend) throws UserError, IOException { if (!isComponent(file)) { throw new UserError( "The api command needs a Fortress component file; filename " + file + " must end with .fss"); } Component c = (Component) Parser.parseFileConvertExn(new File(file)); String logFile = file + ".api.log"; Option<Node> result = c.accept(new ApiMaker(logFile)); File log = new File(logFile); if (log.length() != 0) { System.err.println("To generate an API, the following types are required:"); BufferedReader reader = Useful.filenameToBufferedReader(logFile); String line = reader.readLine(); while (line != null) { System.err.println(line); line = reader.readLine(); } try { Files.rm(logFile); } catch (IOException e) { } throw new UserError("Missing types from the component."); } try { Files.rm(logFile); } catch (IOException e) { } if (result.isNone()) throw new UserError("The api command needs a Fortress component file."); Api a = (Api) result.unwrap(); String code = a.accept(new FortressAstToConcrete()); if (out.isSome()) { try { BufferedWriter writer = Useful.filenameToBufferedWriter(out.unwrap()); if (prepend.isSome()) { BufferedReader reader = Useful.filenameToBufferedReader(prepend.unwrap()); String line = reader.readLine(); while (line != null) { writer.write(line + "\n"); line = reader.readLine(); } } writer.write(code); writer.close(); System.out.println("Dumped code to " + out.unwrap()); } catch (IOException e) { throw new IOException("IOException " + e + " while writing " + out.unwrap()); } } else { System.out.println(code); } }
private static int parse(String file, Option<String> out) throws UserError, IOException { int return_code = 0; try { CompilationUnit unit = Parser.parseFileConvertExn(new File(file)); System.out.println("Ok"); if (out.isSome()) { try { ASTIO.writeJavaAst(unit, out.unwrap()); System.out.println("Dumped parse tree to " + out.unwrap()); } catch (IOException e) { throw new IOException("IOException " + e + "while writing " + out.unwrap()); } } } catch (ParserError e) { if (Debug.stackTraceOn()) { System.err.println(e.getMessage()); e.printStackTrace(); } else { System.err.println(turnOnDebugMessage); } return_code = 1; } catch (ProgramError e) { failureBoilerplate(e); return_code = 1; } catch (CompilerBug e) { failureBoilerplate(e); return_code = 1; } catch (InterpreterBug e) { failureBoilerplate(e); return_code = 1; } catch (FileNotFoundException f) { throw new UserError(file + " not found"); } finally { try { Files.rm(ProjectProperties.preparserErrorLog(file)); } catch (IOException e) { } } return return_code; }