List of usage examples for org.antlr.v4.runtime.tree ParseTreeWalker ParseTreeWalker
ParseTreeWalker
From source file:HSMgen.java
License:Open Source License
public static void main(String[] args) throws Exception { String inputFile = null;//from w w w .j a va2s . c om // Push global environment. pushEnv(new SymEnv()); if (args.length > 0) inputFile = args[0]; InputStream is = System.in; if (inputFile != null) is = new FileInputStream(inputFile); HSMgenLexer lexer = new HSMgenLexer(new ANTLRInputStream(is)); HSMgenParser parser = new HSMgenParser(new CommonTokenStream(lexer)); parser.setBuildParseTree(true); // tell ANTLR to build a parse tree ParseTree tree = parser.init(); // Needed for 'NULL' definition. System.out.println("#include <cstddef>"); new ParseTreeWalker().walk(new HSMgen(), tree); }
From source file:ComplexityListenerAggregator.java
License:Open Source License
public void analyzeFile(String filePath) throws Exception { ParseTree tree = GetFileParseTree(filePath); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(this, tree); }
From source file:tns2toad.java
License:Open Source License
public static void main(String[] args) throws Exception { // A bit of "sign on and blow my own trumpet stuff" ;-) String thisVersion = "0.1"; // Version of this utility. // Assume tnsnames.ora will be piped via stdin, unless we get a parameter passed. String tnsnamesFilename = null; InputStream iStream = System.in; // How many positional options are we expecting? int expectedPositionalArgs = 1; // These are collected from the command line. File inputFile = null;// ww w . j av a 2s . c o m String oracleHome = ""; String userName = ""; // These are used to process the command line. int i = 0; String thisArg; //--------------------------------------------------------- // Let's scan the command line and see what needs doing ... //--------------------------------------------------------- // Scan along the args array, looking at all the options. These // are all prefixed by "--" and must all be before any of the // positional arguments. // When we find one, we zap it! // Each option takes a parameter - they get zapped also. // ThisArg holds the argument, i points at the parameter for it. while (i < args.length && args[i].startsWith("--")) { thisArg = args[i].toLowerCase(); args[i++] = ""; // Oracle Home... if (thisArg.equals("--oracle_home")) { if (i < args.length) { oracleHome = args[i]; args[i++] = ""; } else { usage("ERROR: --oracle_home requires a path name"); } } // User name... else if (thisArg.equals("--user")) { if (i < args.length) { userName = args[i]; args[i++] = ""; } else { usage("ERROR: --user requires a username"); } } // Something else? Not permitted. else { usage("Invalid option '" + thisArg + "'"); } } // At this point we should be sitting with i pointing at the first // positional argument. Scan those next. All the options have been // extracted now, and zapped. // However, just exactly how many positional args do we want? This will // also catch any --options mingling within the positional args. if (i != args.length - expectedPositionalArgs) { usage("Unexpected or insufficient positional parameter(s) supplied."); } // We should only have a single parameter here, the tnsnames.ora file. tnsnamesFilename = args[i]; //--------------------------------------------------------- // Well, we got here, args on the command line must be ok // Check if we can open and/or read the tnsnames.ora file. //--------------------------------------------------------- inputFile = new File(tnsnamesFilename); if (inputFile.isFile()) { iStream = new FileInputStream(tnsnamesFilename); //tnsnamesFilename = inputFile.getCanonicalPath(); } else { System.out.println("\nERROR 1: '" + tnsnamesFilename + "' is not a valid filename.\n"); System.exit(1); // Error exit. } //--------------------------------------------------------- // Everything is fine, let's JFDI! :-) //--------------------------------------------------------- // Feed the tnsnames.ora file into the lexer and get a // token stream from the lexer... ANTLRInputStream input = new ANTLRInputStream(iStream); tnsnamesLexer lexer = new tnsnamesLexer(input); // Feed the lexer's token stream to the parser and get // a parse tree out in return... CommonTokenStream tokens = new CommonTokenStream(lexer); tnsnamesParser parser = new tnsnamesParser(tokens); ParseTree tree = parser.tnsnames(); // Feed the parse tree to the tree walker & the listener // and get a load of text on stdout as a final result. // That is your import file, redirect it to a file and // let Toad import it for you. ParseTreeWalker tnsWalker = new ParseTreeWalker(); tns2toadListener tnsListener = new tns2toadListener(parser, userName, oracleHome); tnsWalker.walk(tnsListener, tree); }
From source file:Cymbol.java
/** * @param args the command line arguments *//* ww w .j a va2 s.c om*/ public static void main(String[] args) { if (args.length == 1) { try { // parsing // create file input stream FileInputStream source = new FileInputStream(args[0]); // create a CharStream that reads from standard input ANTLRInputStream input = new ANTLRInputStream(source); // create a lexer that feeds off of input CharStream CymbolLexer lexer = new CymbolLexer(input); // create a buffer of tokens pulled from the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); // create a parser that feeds off the tokens buffer CymbolParser parser = new CymbolParser(tokens); ParseTree tree = parser.file(); // begin parsing at init rule // dump ast System.out.println(tree.toStringTree(parser)); // print LISP-style tree // build call graph ParseTreeWalker walker = new ParseTreeWalker(); FunctionListener collector = new FunctionListener(); walker.walk(collector, tree); System.out.println(collector.graph.toString()); System.out.println(collector.graph.toDOT()); } catch (IOException e) { System.out.print("error: " + e.getMessage()); } } else { System.out.print("error: syntax is Cymbol <file path> !"); } }
From source file:Utah01.java
/** * @param args the command line arguments *///from ww w . j ava 2 s . c om public static void main(String[] args) { // TODO code application logic here try { // make Lexer ANTLRFileStream inputStream = new ANTLRFileStream(args[0]); utah01Lexer lexer = new utah01Lexer(inputStream); // make Parser CommonTokenStream tokens = new CommonTokenStream(lexer); utah01Parser parser = new utah01Parser(tokens); // make Walker ParseTreeWalker parseTreeWalker = new ParseTreeWalker(); utah01ListenerForJSON listener = new utah01ListenerForJSON(); ParserRuleContext parserRuleContext = parser.start(); // do walk parseTreeWalker.walk(listener, parserRuleContext); } catch (Exception e) { System.out.println(e); } //System.out.print("number of elements = "); //System.out.println(SymbolTable.count); }
From source file:Ecmascript.java
/** * @param args the command line arguments *//*from w w w . j a v a2 s . c om*/ public static void main(String[] args) { if (args.length == 1) { try { // parsing // create file input stream FileInputStream source = new FileInputStream(args[0]); // create a CharStream that reads from standard input ANTLRInputStream input = new ANTLRInputStream(source); // create a lexer that feeds off of input CharStream ECMAScriptLexer lexer = new ECMAScriptLexer(input); // create a buffer of tokens pulled from the lexer CommonTokenStream tokens = new CommonTokenStream(lexer); // create a parser that feeds off the tokens buffer ECMAScriptParser parser = new ECMAScriptParser(tokens); ParseTree tree = parser.program(); // begin parsing at init rule // dump ast System.out.println("AST is : " + tree.toStringTree(parser)); // print LISP-style tree // build call graph ParseTreeWalker walker = new ParseTreeWalker(); FunctionListener collector = new FunctionListener(); walker.walk(collector, tree); System.out.println(collector.graph.toString()); System.out.println(collector.graph.toDOT()); } catch (IOException e) { System.out.print("error: " + e.getMessage()); } } else { System.out.print("error: syntax is Cymbol <file path> !"); } }
From source file:amulet.translator.attributemapper.AttributeMapper.java
public AttributeMapper(String code, Vector<String> attributes) { ANTLRInputStream input = new ANTLRInputStream(code); CLexer lexer = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); ParseTree tree;//w ww.j a v a2 s .co m if (code.trim().length() > 0 && code.indexOf(";") == -1 && code.indexOf("//") == -1 && code.indexOf("/*") == -1) { //Guard Code tree = parser.expression(); } else { tree = parser.blockItemList(); } ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker m_extractor = new CExtractor(tokens, parser, attributes); walker.walk(m_extractor, tree); // initiate walk of tree with listener }
From source file:amulet.translator.authorizationmodule.AuthorizationModule.java
public boolean checkApiAuthorization(String code) { ANTLRInputStream input = new ANTLRInputStream(code); CLexer lexer = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); ParseTree tree;/*from w w w .j a v a2 s.c o m*/ if (code.trim().length() > 0 && code.indexOf(";") == -1 && code.indexOf("//") == -1 && code.indexOf("/*") == -1) { //Guard Code tree = parser.expression(); } else { tree = parser.blockItemList(); } ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker CExtractor extractor = new CExtractor(tokens, parser, authorizedApiList, firstArgumentMap); walker.walk(extractor, tree); // initiate walk of tree with listener printMsgs("WARNINGS", extractor.getWarnings()); printMsgs("ERRORS", extractor.getErrors()); if (extractor.getErrors().size() > 0) { System.err.println("Incompatible code found."); setSuccess(false); return false; } else { setSuccess(true); return true; } }
From source file:amulet.translator.compatibilitychecker.CompatChecker.java
public CompatChecker(String code, String className) { ANTLRInputStream input = new ANTLRInputStream(code); CLexer lexer = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); ParseTree tree;/*w w w . jav a 2s . c o m*/ if (code.trim().length() > 0 && code.indexOf(";") == -1 && code.indexOf("//") == -1 && code.indexOf("/*") == -1) { //Guard Code tree = parser.expression(); } else { tree = parser.blockItemList(); } ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker CExtractor extractor = new CExtractor(tokens, parser, className); walker.walk(extractor, tree); // initiate walk of tree with listener extractor.doRecursionCheck(); printMsgs("WARNINGS", extractor.getWarnings()); printMsgs("ERRORS", extractor.getErrors()); if (extractor.getErrors().size() > 0) { System.err.println("Incompatible code found."); setSuccess(false); } else { setSuccess(true); } }
From source file:amulet.translator.functionwhitelist.FunctionWhitelist.java
public String addFuncDefFuncCalls(String className, String code, Vector<String> operationNames) { ANTLRInputStream input = new ANTLRInputStream(code); CLexer lexer = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); ParseTree tree;// ww w.j a v a 2 s.co m if (code.trim().length() > 0 && code.indexOf(";") == -1 && code.indexOf("//") == -1 && code.indexOf("/*") == -1) { //Guard Code tree = parser.expression(); } else { tree = parser.blockItemList(); } ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker CExtractor extractor = new CExtractor(tokens, parser, className, operationNames); walker.walk(extractor, tree); // initiate walk of tree with listener functionDefinitions.addAll(extractor.getFunctionDefinitions()); functionCalls.addAll(extractor.getFunctionCalls()); String translatedCode = extractor.rewriter.getText(); return translatedCode; }