List of usage examples for org.antlr.v4.runtime CommonTokenStream CommonTokenStream
public CommonTokenStream(TokenSource tokenSource)
From source file:ai.grakn.graql.internal.parser.GremlinVisitor.java
License:Open Source License
private static String prettify(CharStream input) { GremlinLexer lexer = new GremlinLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); GremlinParser parser = new GremlinParser(tokens); GremlinVisitor visitor = new GremlinVisitor(); PrettyStringBuilder pretty = PrettyStringBuilder.create(); visitor.visit(parser.traversal()).accept(pretty); return pretty.build(); }
From source file:ai.grakn.graql.internal.parser.QueryParser.java
License:Open Source License
private <T, S extends ParseTree> T parseQueryFragment(Function<GraqlParser, S> parseRule, BiFunction<QueryVisitor, S, T> visit, TokenSource source, GraqlErrorListener errorListener) { CommonTokenStream tokens = new CommonTokenStream(source); GraqlParser parser = new GraqlParser(tokens); parser.removeErrorListeners();/*from ww w .java 2 s. c o m*/ parser.addErrorListener(errorListener); S tree = parseRule.apply(parser); if (errorListener.hasErrors()) { throw GraqlSyntaxException.parsingError(errorListener.toString()); } return visit.apply(getQueryVisitor(), tree); }
From source file:ai.grakn.graql.internal.template.TemplateParser.java
License:Open Source License
private CommonTokenStream lexGraqlTemplate(String templateString, GraqlErrorListener errorListener) { ANTLRInputStream inputStream = new ANTLRInputStream(templateString); GraqlTemplateLexer lexer = new GraqlTemplateLexer(inputStream); lexer.removeErrorListeners();//from w w w.j ava 2 s . co m lexer.addErrorListener(errorListener); return new CommonTokenStream(lexer); }
From source file:alfa.core.runtime.meta.parser.MetaParse.java
private static void _Parse(String fileName) throws Exception { InputStream is = new FileInputStream(fileName); ANTLRInputStream input = new ANTLRInputStream(is); MetaLexer lexer = new MetaLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MetaParser parser = new MetaParser(tokens); ParseTree tree = parser.program(); // parse; start at prog <label id="code.tour.main.6"/> //System.out.println( tree.toStringTree( parser ) ); // print tree as text <label id="code.tour.main.7"/> MetaProductions prod = new MetaProductions(); prod.visit(tree);//from w w w .ja v a 2s . c om }
From source file:alfa.parser.AlfaParse.java
private static Program _Parse(String fileName) throws Exception { InputStream is = new FileInputStream(fileName); ANTLRInputStream input = new ANTLRInputStream(is); AlfaLexer lexer = new AlfaLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); AlfaParser parser = new AlfaParser(tokens); ParseTree tree = parser.program(); // parse; start at prog <label id="code.tour.main.6"/> //System.out.println( tree.toStringTree( parser ) ); // print tree as text <label id="code.tour.main.7"/> AlfaProductions prod = new AlfaProductions(); prod.visit(tree);//from w w w. j av a2s .com return prod.getProgram(); }
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 w w . jav a2 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;/* w w w . j ava 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 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;// ww w . j a va2 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); 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;//from w ww.j a v 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, 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) { /*//from w w w . j a va 2 s .c om * 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()); } } } }