Search in sources :

Example 16 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project antlr4 by antlr.

the class GrammarParserInterpreter method getLookaheadParseTrees.

/** Return a list of parse trees, one for each alternative in a decision
	 *  given the same input.
	 *
	 *  Very similar to {@link #getAllPossibleParseTrees} except
	 *  that it re-parses the input for every alternative in a decision,
	 *  not just the ambiguous ones (there is no alts parameter here).
	 *  This method also tries to reduce the size of the parse trees
	 *  by stripping away children of the tree that are completely out of range
	 *  of startIndex..stopIndex. Also, because errors are expected, we
	 *  use a specialized error handler that more or less bails out
	 *  but that also consumes the first erroneous token at least. This
	 *  ensures that an error node will be in the parse tree for display.
	 *
	 *  NOTES:
    // we must parse the entire input now with decision overrides
	// we cannot parse a subset because it could be that a decision
	// above our decision of interest needs to read way past
	// lookaheadInfo.stopIndex. It seems like there is no escaping
	// the use of a full and complete token stream if we are
	// resetting to token index 0 and re-parsing from the start symbol.
	// It's not easy to restart parsing somewhere in the middle like a
	// continuation because our call stack does not match the
	// tree stack because of left recursive rule rewriting. grrrr!
	 *
	 * @since 4.5.1
	 */
public static List<ParserRuleContext> getLookaheadParseTrees(Grammar g, ParserInterpreter originalParser, TokenStream tokens, int startRuleIndex, int decision, int startIndex, int stopIndex) {
    List<ParserRuleContext> trees = new ArrayList<ParserRuleContext>();
    // Create a new parser interpreter to parse the ambiguous subphrase
    ParserInterpreter parser = deriveTempParserInterpreter(g, originalParser, tokens);
    DecisionState decisionState = originalParser.getATN().decisionToState.get(decision);
    for (int alt = 1; alt <= decisionState.getTransitions().length; alt++) {
        // re-parse entire input for all ambiguous alternatives
        // (don't have to do first as it's been parsed, but do again for simplicity
        //  using this temp parser.)
        GrammarParserInterpreter.BailButConsumeErrorStrategy errorHandler = new GrammarParserInterpreter.BailButConsumeErrorStrategy();
        parser.setErrorHandler(errorHandler);
        parser.reset();
        parser.addDecisionOverride(decision, startIndex, alt);
        ParserRuleContext tt = parser.parse(startRuleIndex);
        int stopTreeAt = stopIndex;
        if (errorHandler.firstErrorTokenIndex >= 0) {
            // cut off rest at first error
            stopTreeAt = errorHandler.firstErrorTokenIndex;
        }
        Interval overallRange = tt.getSourceInterval();
        if (stopTreeAt > overallRange.b) {
            // If we try to look beyond range of tree, stopTreeAt must be EOF
            // for which there is no EOF ref in grammar. That means tree
            // will not have node for stopTreeAt; limit to overallRange.b
            stopTreeAt = overallRange.b;
        }
        ParserRuleContext subtree = Trees.getRootOfSubtreeEnclosingRegion(tt, startIndex, stopTreeAt);
        // Use higher of overridden decision tree or tree enclosing all tokens
        if (Trees.isAncestorOf(parser.getOverrideDecisionRoot(), subtree)) {
            subtree = parser.getOverrideDecisionRoot();
        }
        Trees.stripChildrenOutOfRange(subtree, parser.getOverrideDecisionRoot(), startIndex, stopTreeAt);
        trees.add(subtree);
    }
    return trees;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) ArrayList(java.util.ArrayList) DecisionState(org.antlr.v4.runtime.atn.DecisionState) Interval(org.antlr.v4.runtime.misc.Interval)

Example 17 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project antlr4 by antlr.

the class TestExpectedTokens method testFollowIncluded.

@Test
public void testFollowIncluded() throws Exception {
    String gtext = "parser grammar T;\n" + "a : b A ;\n" + "b : B | ;";
    Grammar g = new Grammar(gtext);
    String atnText = "RuleStart_a_0->s4\n" + "s4-b->RuleStart_b_2\n" + "s5-A->s6\n" + "s6->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s11\n";
    checkRuleATN(g, "a", atnText);
    atnText = "RuleStart_b_2->BlockStart_9\n" + "BlockStart_9->s7\n" + "BlockStart_9->s8\n" + "s7-B->BlockEnd_10\n" + "s8->BlockEnd_10\n" + "BlockEnd_10->RuleStop_b_3\n" + "RuleStop_b_3->s5\n";
    checkRuleATN(g, "b", atnText);
    ATN atn = g.getATN();
    // From the start of 'b' with empty stack, can only see B and EOF
    int blkStartStateNumber = 9;
    IntervalSet tokens = atn.getExpectedTokens(blkStartStateNumber, RuleContext.EMPTY);
    assertEquals("{<EOF>, B}", tokens.toString(g.getTokenNames()));
    // Now call from 'a'
    tokens = atn.getExpectedTokens(blkStartStateNumber, new ParserRuleContext(ParserRuleContext.EMPTY, 4));
    assertEquals("{A, B}", tokens.toString(g.getTokenNames()));
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Grammar(org.antlr.v4.tool.Grammar) ATN(org.antlr.v4.runtime.atn.ATN) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Example 18 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project antlr4 by antlr.

the class TestParserProfiler method interpAndGetDecisionInfo.

public DecisionInfo[] interpAndGetDecisionInfo(LexerGrammar lg, Grammar g, String startRule, String... input) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(null);
    ParserInterpreter parser = g.createParserInterpreter(null);
    parser.setProfile(true);
    for (String s : input) {
        lexEngine.reset();
        parser.reset();
        lexEngine.setInputStream(new ANTLRInputStream(s));
        CommonTokenStream tokens = new CommonTokenStream(lexEngine);
        parser.setInputStream(tokens);
        Rule r = g.rules.get(startRule);
        if (r == null) {
            return parser.getParseInfo().getDecisionInfo();
        }
        ParserRuleContext t = parser.parse(r.index);
    //			try {
    //				Utils.waitForClose(t.inspect(parser).get());
    //			}
    //			catch (Exception e) {
    //				e.printStackTrace();
    //			}
    //
    //			System.out.println(t.toStringTree(parser));
    }
    return parser.getParseInfo().getDecisionInfo();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) Rule(org.antlr.v4.tool.Rule) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 19 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project crate by crate.

the class SqlParser method invokeParser.

private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
    try {
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);
        parser.addParseListener(new PostProcessor());
        lexer.removeErrorListeners();
        lexer.addErrorListener(ERROR_LISTENER);
        parser.removeErrorListeners();
        parser.addErrorListener(ERROR_LISTENER);
        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            // rewind input stream
            tokenStream.reset();
            parser.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }
        return new AstBuilder().visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}
Also used : SqlBaseParser(io.crate.sql.parser.antlr.v4.SqlBaseParser) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) SqlBaseLexer(io.crate.sql.parser.antlr.v4.SqlBaseLexer)

Example 20 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project incubator-systemml by apache.

the class CommonSyntacticValidator method unaryExpressionHelper.

protected void unaryExpressionHelper(ParserRuleContext ctx, ExpressionInfo left, ExpressionInfo me, String op) {
    if (left.expr != null) {
        Token start = ctx.start;
        String fileName = currentFile;
        int line = start.getLine();
        int col = start.getCharPositionInLine();
        if (left.expr instanceof IntIdentifier) {
            if (op.equals("-")) {
                ((IntIdentifier) left.expr).multiplyByMinusOne();
            }
            me.expr = left.expr;
        } else if (left.expr instanceof DoubleIdentifier) {
            if (op.equals("-")) {
                ((DoubleIdentifier) left.expr).multiplyByMinusOne();
            }
            me.expr = left.expr;
        } else {
            Expression right = new IntIdentifier(1, fileName, line, col, line, col);
            if (op.equals("-")) {
                right = new IntIdentifier(-1, fileName, line, col, line, col);
            }
            Expression.BinaryOp bop = Expression.getBinaryOp("*");
            BinaryExpression be = new BinaryExpression(bop);
            be.setLeft(left.expr);
            be.setRight(right);
            me.expr = be;
        }
        setFileLineColumn(me.expr, ctx);
    }
}
Also used : DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) Token(org.antlr.v4.runtime.Token)

Aggregations

ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)24 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 SmaliParser (com.googlecode.d2j.smali.antlr4.SmaliParser)3 BitSet (java.util.BitSet)3 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)3 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 Token (org.antlr.v4.runtime.Token)3 ATN (org.antlr.v4.runtime.atn.ATN)3 DFAState (org.antlr.v4.runtime.dfa.DFAState)3 Interval (org.antlr.v4.runtime.misc.Interval)3 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)3 ArrayList (java.util.ArrayList)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2