Search in sources :

Example 6 with RuleContext

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

the class ParseTreeWalker method enterRule.

/**
	 * The discovery of a rule node, involves sending two events: the generic
	 * {@link ParseTreeListener#enterEveryRule} and a
	 * {@link RuleContext}-specific event. First we trigger the generic and then
	 * the rule specific. We to them in reverse order upon finishing the node.
	 */
protected void enterRule(ParseTreeListener listener, RuleNode r) {
    ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
    listener.enterEveryRule(ctx);
    ctx.enterRule(listener);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext)

Example 7 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 8 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)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 RuleContext (org.antlr.v4.runtime.RuleContext)3 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 DispatchMethod (org.antlr.v4.codegen.model.DispatchMethod)1 ListenerDispatchMethod (org.antlr.v4.codegen.model.ListenerDispatchMethod)1 VisitorDispatchMethod (org.antlr.v4.codegen.model.VisitorDispatchMethod)1 CommonToken (org.antlr.v4.runtime.CommonToken)1 Lexer (org.antlr.v4.runtime.Lexer)1 Parser (org.antlr.v4.runtime.Parser)1 Token (org.antlr.v4.runtime.Token)1 ATN (org.antlr.v4.runtime.atn.ATN)1 ATNState (org.antlr.v4.runtime.atn.ATNState)1 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1