List of usage examples for org.antlr.v4.runtime.tree ParseTreeWalker walk
public void walk(ParseTreeListener listener, ParseTree t)
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;/* www . j a v a 2s .co 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 *//*from w w w . j av a 2s .co m*/ 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 w ww . java 2 s .c o m*/ 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 av a 2 s . co m 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;//from w ww .ja v a 2 s . com 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 av a 2s .c om*/ 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;//from ww w . j a va 2 s . c om 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;/*w w w . j av a 2s . com*/ 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; }
From source file:amulet.translator.runtimecheck.CExtractor.java
@Override public void enterExpressionStatement(CParser.ExpressionStatementContext exprStctx) { /*/* w w w. j ava2s .c o m*/ * Update "basic block" count in For-loop Context. * + Rule: any expression without an amulet API function call. * * NOTE: Right now we are trying to guard against double counting Amulet API function * calls so we do some primitive checking of the line of code for indicators that * this expression includes a call to such a function. * * TODO: we should make a better way of checking if there really is a call to an * amulet API function... */ // System.out.println("DEBUG>>>>>>>>>>enterExpressionStatement: " + exprStctx.getText() + " (" + resourceProfiler.loopContext.isForLoopContext() + ")"); if (resourceProfiler != null && resourceProfiler.loopContext.isForLoopContext()) { if (!exprStctx.getText().contains("Amulet") || (!exprStctx.getText().contains("(") && !exprStctx.getText().contains(")"))) { resourceProfiler.incNumberStatementsInLoop(); // System.out.println(" LOOP INCLUDE!"); } } /* * Update "basic block" count in QM Context (e.g., state, transition). * + Rule: any expression without an amulet API function call. */ else if (resourceProfiler != null && !resourceProfiler.loopContext.isForLoopContext()) { if (!exprStctx.getText().contains("Amulet") || (!exprStctx.getText().contains("(") && !exprStctx.getText().contains(")"))) { resourceProfiler.incNumLinesOfCode(); // System.out.println(" QM INCLUDE!"); } } if (exprStctx.expression().assignmentExpression().leftExpression() != null || exprStctx.expression().assignmentExpression().rightExpression() != null) { //Enter only if the expression statement is assignment expression with left and right hand sides CParser.AssignmentExpressionContext ctx = exprStctx.expression().assignmentExpression(); CParser.LeftExpressionContext ctxLeft = ctx.leftExpression(); CParser.RightExpressionContext ctxRight = ctx.rightExpression(); CParser.AssignmentOperatorContext ctxAssignOp = ctx.assignmentOperator(); CParser.CastExpressionContext ctxLeftCast = ctxLeft.conditionalExpression().logicalOrExpression() .logicalAndExpression().inclusiveOrExpression().exclusiveOrExpression().andExpression() .equalityExpression().relationalExpression().shiftExpression().additiveExpression() .multiplicativeExpression().castExpression(); // CParser.CastExpressionContext ctxRightCast = ctxRight.conditionalExpression().logicalOrExpression().logicalAndExpression().inclusiveOrExpression().exclusiveOrExpression().andExpression().equalityExpression().relationalExpression().shiftExpression().additiveExpression().multiplicativeExpression().castExpression(); ////////////////////////////////////////////////////////////////////////// // System.out.println(" + left::"+ctxLeft.getText()); // System.out.println(" + op::"+ctxAssignOp.getText()); // System.out.println(" + right::"+ctxRight.getText()); ////////////////////////////////////////////////////////////////////////// if (ctxLeft.getText().indexOf("[") != -1 && ctxLeft.getText().indexOf("]") != -1) { //Array assignment statement (i.e., assign INTO array index)... ST arrayAssignment = templateGroup.getInstanceOf("arrayAssignment"); arrayAssignment.add("arrayName", ctxLeftCast.unaryExpression().postfixExpression().postfixExpression().getText()); arrayAssignment.add("arrayIndex", ctxLeftCast.unaryExpression().postfixExpression().expression().getText()); arrayAssignment.add("assignmentOp", ctxAssignOp.getText()); if (ctxRight.getText().indexOf("[") != -1 && ctxRight.getText().indexOf("]") != -1) { //... array assignment statement with AND array access /*ST arrayAssignmentWithArrayAccess = templateGroup.getInstanceOf("arrayAssignmentWithArrayAccess"); arrayAssignmentWithArrayAccess.add("arrayName",ctxLeftCast.unaryExpression().postfixExpression().postfixExpression().getText()); arrayAssignmentWithArrayAccess.add("arrayIndex",ctxLeftCast.unaryExpression().postfixExpression().expression().getText()); arrayAssignmentWithArrayAccess.add("assignmentOp",ctxAssignOp.getText()); arrayAssignmentWithArrayAccess.add("arrayToBeRead",ctxRightCast.unaryExpression().postfixExpression().postfixExpression().getText()); arrayAssignmentWithArrayAccess.add("arrayReadIndex",ctxRightCast.unaryExpression().postfixExpression().expression().getText()); rewriter.insertAfter(exprStctx.getStop(),arrayAssignmentWithArrayAccess.render());*/ InputStream is = new ByteArrayInputStream(ctxRight.getText().getBytes()); ANTLRInputStream input; try { input = new ANTLRInputStream(is); } catch (Exception e) { return; } CLexer lexer = new CLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CParser parser = new CParser(tokens); ParseTree tree = parser.expression(); // parse ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker CExtractor extractor = new CExtractor(qmclass, tokens, parser, togglePins, global_arrays, resourceProfiler); walker.walk(extractor, tree); // initiate walk of tree with listener arrayAssignment.add("value", extractor.rewriter.getText()); } else { //... only array assignment statement (and NO array access) /*ST arrayAssignment = templateGroup.getInstanceOf("arrayAssignment"); arrayAssignment.add("arrayName",ctxLeftCast.unaryExpression().postfixExpression().postfixExpression().getText()); arrayAssignment.add("arrayIndex",ctxLeftCast.unaryExpression().postfixExpression().expression().getText());*/ arrayAssignment.add("value", ctxRight.getText()); /*arrayAssignment.add("assignmentOp",ctxAssignOp.getText()); rewriter.insertAfter(exprStctx.getStop(),arrayAssignment.render());*/ } rewriter.insertAfter(exprStctx.getStop(), arrayAssignment.render()); } else if (ctxRight.getText().indexOf("[") != -1 && ctxRight.getText().indexOf("]") != -1) { //Assignment statement from array accessed index. /*ST arrayAccess = templateGroup.getInstanceOf("arrayAccess"); arrayAccess.add("arrayName",ctxRightCast.unaryExpression().postfixExpression().postfixExpression().getText()); arrayAccess.add("arrayIndex",ctxRightCast.unaryExpression().postfixExpression().expression().getText()); rewriter.insertAfter(exprStctx.getStop(),ctxLeft.getText()+ctxAssignOp.getText()+arrayAccess.render()+";");*/ } else { // Other Assignment (i.e., assign to non-array variable AND NO array access). // Add (*ONLY*) assignment statements that assign int values to a variable to the ResourceProfiler's intVarResolver. try { Integer.parseInt(ctxRight.getText()); resourceProfiler.setResolverIntVar(ctxLeft.getText(), ctxRight.getText()); } catch (NumberFormatException nfe) { //pass // System.out.println("WARNING: right val not an int value::" + ctxRight.getText()); } } } }