Search in sources :

Example 16 with RuleContext

use of org.antlr.v4.runtime.RuleContext in project antlr4 by tunnelvisionlabs.

the class ATN method getExpectedTokens.

/**
 * Computes the set of input symbols which could follow ATN state number
 * {@code stateNumber} in the specified full {@code context}. This method
 * considers the complete parser context, but does not evaluate semantic
 * predicates (i.e. all predicates encountered during the calculation are
 * assumed true). If a path in the ATN exists from the starting state to the
 * {@link RuleStopState} of the outermost context without matching any
 * symbols, {@link Token#EOF} is added to the returned set.
 *
 * <p>If {@code context} is {@code null}, it is treated as
 * {@link ParserRuleContext#EMPTY}.</p>
 *
 * <p>Note that this does NOT give you the set of all tokens that could
 * appear at a given token position in the input phrase.  In other words, it
 * does not answer:</p>
 *
 * <quote>"Given a specific partial input phrase, return the set of all
 * tokens that can follow the last token in the input phrase."</quote>
 *
 * <p>The big difference is that with just the input, the parser could land
 * right in the middle of a lookahead decision. Getting all
 * <em>possible</em> tokens given a partial input stream is a separate
 * computation. See https://github.com/antlr/antlr4/issues/1428</p>
 *
 * <p>For this function, we are specifying an ATN state and call stack to
 * compute what token(s) can come next and specifically: outside of a
 * lookahead decision. That is what you want for error reporting and
 * recovery upon parse error.</p>
 *
 * @param stateNumber the ATN state number
 * @param context the full parse context
 * @return The set of potentially valid input symbols which could follow the
 * specified state in the specified context.
 * @throws IllegalArgumentException if the ATN does not contain a state with
 * number {@code stateNumber}
 */
@NotNull
public IntervalSet getExpectedTokens(int stateNumber, @Nullable RuleContext context) {
    if (stateNumber < 0 || stateNumber >= states.size()) {
        throw new IllegalArgumentException("Invalid state number.");
    }
    RuleContext ctx = context;
    ATNState s = states.get(stateNumber);
    IntervalSet following = nextTokens(s);
    if (!following.contains(Token.EPSILON)) {
        return following;
    }
    IntervalSet expected = new IntervalSet();
    expected.addAll(following);
    expected.remove(Token.EPSILON);
    while (ctx != null && ctx.invokingState >= 0 && following.contains(Token.EPSILON)) {
        ATNState invokingState = states.get(ctx.invokingState);
        RuleTransition rt = (RuleTransition) invokingState.transition(0);
        following = nextTokens(rt.followState);
        expected.addAll(following);
        expected.remove(Token.EPSILON);
        ctx = ctx.parent;
    }
    if (following.contains(Token.EPSILON)) {
        expected.add(Token.EOF);
    }
    return expected;
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 17 with RuleContext

use of org.antlr.v4.runtime.RuleContext in project antlr4 by tunnelvisionlabs.

the class TestXPath method getNodeStrings.

public List<String> getNodeStrings(String input, String xpath, String startRuleName, String parserName, String lexerName) throws Exception {
    Tuple2<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.getItem1();
    ParseTree tree = execStartRule(startRuleName, parser);
    List<String> nodes = new ArrayList<String>();
    for (ParseTree t : XPath.findAll(tree, xpath, parser)) {
        if (t instanceof RuleContext) {
            RuleContext r = (RuleContext) t;
            nodes.add(parser.getRuleNames()[r.getRuleIndex()]);
        } else {
            TerminalNode token = (TerminalNode) t;
            nodes.add(token.getText());
        }
    }
    return nodes;
}
Also used : Lexer(org.antlr.v4.runtime.Lexer) RuleContext(org.antlr.v4.runtime.RuleContext) ArrayList(java.util.ArrayList) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Parser(org.antlr.v4.runtime.Parser)

Example 18 with RuleContext

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

the class TestXPath method getNodeStrings.

public List<String> getNodeStrings(String input, String xpath, String startRuleName, String parserName, String lexerName) throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    ParseTree tree = execStartRule(startRuleName, parser);
    List<String> nodes = new ArrayList<String>();
    for (ParseTree t : XPath.findAll(tree, xpath, parser)) {
        if (t instanceof RuleContext) {
            RuleContext r = (RuleContext) t;
            nodes.add(parser.getRuleNames()[r.getRuleIndex()]);
        } else {
            TerminalNode token = (TerminalNode) t;
            nodes.add(token.getText());
        }
    }
    return nodes;
}
Also used : Lexer(org.antlr.v4.runtime.Lexer) RuleContext(org.antlr.v4.runtime.RuleContext) ArrayList(java.util.ArrayList) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Parser(org.antlr.v4.runtime.Parser)

Example 19 with RuleContext

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

the class StructDecl method addDispatchMethods.

public void addDispatchMethods(Rule r) {
    dispatchMethods = new ArrayList<DispatchMethod>();
    if (!r.hasAltSpecificContexts()) {
        // no enter/exit for this ruleContext if rule has labels
        if (factory.getGrammar().tool.gen_listener) {
            dispatchMethods.add(new ListenerDispatchMethod(factory, true));
            dispatchMethods.add(new ListenerDispatchMethod(factory, false));
        }
        if (factory.getGrammar().tool.gen_visitor) {
            dispatchMethods.add(new VisitorDispatchMethod(factory));
        }
    }
}
Also used : VisitorDispatchMethod(org.antlr.v4.codegen.model.VisitorDispatchMethod) ListenerDispatchMethod(org.antlr.v4.codegen.model.ListenerDispatchMethod) ListenerDispatchMethod(org.antlr.v4.codegen.model.ListenerDispatchMethod) VisitorDispatchMethod(org.antlr.v4.codegen.model.VisitorDispatchMethod) DispatchMethod(org.antlr.v4.codegen.model.DispatchMethod)

Example 20 with RuleContext

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

the class Trees method getNodeText.

public static String getNodeText(Tree t, List<String> ruleNames) {
    if (ruleNames != null) {
        if (t instanceof RuleContext) {
            int ruleIndex = ((RuleContext) t).getRuleContext().getRuleIndex();
            String ruleName = ruleNames.get(ruleIndex);
            int altNumber = ((RuleContext) t).getAltNumber();
            if (altNumber != ATN.INVALID_ALT_NUMBER) {
                return ruleName + ":" + altNumber;
            }
            return ruleName;
        } else if (t instanceof ErrorNode) {
            return t.toString();
        } else if (t instanceof TerminalNode) {
            Token symbol = ((TerminalNode) t).getSymbol();
            if (symbol != null) {
                String s = symbol.getText();
                return s;
            }
        }
    }
    // no recog for rule names
    Object payload = t.getPayload();
    if (payload instanceof Token) {
        return ((Token) payload).getText();
    }
    return t.getPayload().toString();
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token)

Aggregations

RuleContext (org.antlr.v4.runtime.RuleContext)10 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)7 Parser (org.antlr.v4.runtime.Parser)6 Token (org.antlr.v4.runtime.Token)6 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)6 ParseTree (org.antlr.v4.runtime.tree.ParseTree)6 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)3 LTLBPredicateNode (de.bmoth.parser.ast.nodes.ltl.LTLBPredicateNode)2 LTLInfixOperatorNode (de.bmoth.parser.ast.nodes.ltl.LTLInfixOperatorNode)2 LTLKeywordNode (de.bmoth.parser.ast.nodes.ltl.LTLKeywordNode)2 LTLPrefixOperatorNode (de.bmoth.parser.ast.nodes.ltl.LTLPrefixOperatorNode)2 ArrayList (java.util.ArrayList)2 KalangParser (kalang.antlr.KalangParser)2 AstBuilder (kalang.compiler.AstBuilder)2 DispatchMethod (org.antlr.v4.codegen.model.DispatchMethod)2 ListenerDispatchMethod (org.antlr.v4.codegen.model.ListenerDispatchMethod)2 VisitorDispatchMethod (org.antlr.v4.codegen.model.VisitorDispatchMethod)2 Lexer (org.antlr.v4.runtime.Lexer)2 ATN (org.antlr.v4.runtime.atn.ATN)2