public static void loadFrom(InputStream in, Proto target) throws Exception { // Create an input character stream from standard in ANTLRInputStream input = new ANTLRInputStream(in); // Create an ExprLexer that feeds from that stream ProtoLexer lexer = new ProtoLexer(input); // Create a stream of tokens fed by the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); // Create a parser that feeds off the token stream ProtoParser parser = new ProtoParser(tokens); // Begin parsing at rule parse parser.parse(target); }
public static void main(String[] args) throws Exception { // Display help screen. if (args.length == 0 || args[0].compareToIgnoreCase("-h") == 0) { System.out.println("Protoc Embedded C Compiler for Protocol Buffers"); System.out.println("Version " + VERSION); System.out.println( "Copyright (c) 2009-2011, Technische Universitaet Muenchen, http://www4.in.tum.de/"); System.out.println( "Use: Protoc [-I=<source-directory>][--c_out=<output-directory>] <proto-file>"); System.out.println(); System.exit(0); } // Parse command-line arguments. File sourceDirectory = null; File outputDirectory = null; for (String arg : args) { if (arg.startsWith("-I=")) { sourceDirectory = new File(arg.substring(3)); if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) { System.out.println( "Source directory '" + sourceDirectory + "' does not exist or is not a directory"); System.exit(1); } } if (arg.startsWith("--c_out=")) { outputDirectory = new File(arg.substring(8)); if (!outputDirectory.exists() || !outputDirectory.isDirectory()) { System.out.println( "Output directory '" + outputDirectory + "' does not exist or is not a directory"); System.exit(1); } } if (arg.equals("--debug")) { debug = true; } } if (sourceDirectory == null) { sourceDirectory = new File(System.getProperty("user.dir")); } if (outputDirectory == null) { outputDirectory = new File(System.getProperty("user.dir")); } File protoFile = new File(sourceDirectory, args[args.length - 1]); if (!protoFile.exists() || !protoFile.isFile()) { System.out.println("Proto file '" + protoFile + "' does not exist or is not a file"); System.exit(1); } // Parse input and build AST. final ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(protoFile)); final ProtoLexer lexer = new ProtoLexer(input); final CommonTokenStream tokens = new CommonTokenStream(lexer); final ProtoParser parser = new ProtoParser(tokens); final ProtoParser.proto_return proto = parser.proto(); final CommonTree tree = proto.tree; if (debug) { TreePrintUtils.printTree(tree); } // Walk AST to check constraints. final CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); nodes.setTokenStream(tokens); final ConstraintChecker checker = new ConstraintChecker(nodes); checker.proto(); if (checker.constraintErrors > 0) { System.err.println( "ERROR(s): " + checker.constraintErrors + " constraint violation(s), no files generated"); System.exit(-1); } String name = protoFile.getName(); if (name.contains(".")) { name = name.substring(0, name.lastIndexOf(".")); } generate( "embedded-h-file.stg", new File(outputDirectory, name + ".h"), tokens, tree, name, checker.topologicalOrder); generate( "embedded-c-file.stg", new File(outputDirectory, name + ".c"), tokens, tree, name, checker.topologicalOrder); }