Search in sources :

Example 11 with RuleAST

use of org.antlr.v4.tool.ast.RuleAST in project antlr4 by antlr.

the class RuleCollector method discoverRule.

@Override
public void discoverRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, List<GrammarAST> actions, GrammarAST block) {
    int numAlts = block.getChildCount();
    Rule r;
    if (LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(rule, ID.getText())) {
        r = new LeftRecursiveRule(g, ID.getText(), rule);
    } else {
        r = new Rule(g, ID.getText(), rule, numAlts);
    }
    rules.put(r.name, r);
    if (arg != null) {
        r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g);
        r.args.type = AttributeDict.DictType.ARG;
        r.args.ast = arg;
        arg.resolver = r.alt[currentOuterAltNumber];
    }
    if (returns != null) {
        r.retvals = ScopeParser.parseTypedArgList(returns, returns.getText(), g);
        r.retvals.type = AttributeDict.DictType.RET;
        r.retvals.ast = returns;
    }
    if (locals != null) {
        r.locals = ScopeParser.parseTypedArgList(locals, locals.getText(), g);
        r.locals.type = AttributeDict.DictType.LOCAL;
        r.locals.ast = locals;
    }
    for (GrammarAST a : actions) {
        // a = ^(AT ID ACTION)
        ActionAST action = (ActionAST) a.getChild(1);
        r.namedActions.put(a.getChild(0).getText(), action);
        action.resolver = r;
    }
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 12 with RuleAST

use of org.antlr.v4.tool.ast.RuleAST in project antlr4 by antlr.

the class RuleCollector method discoverLexerRule.

@Override
public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, GrammarAST block) {
    int numAlts = block.getChildCount();
    Rule r = new Rule(g, ID.getText(), rule, numAlts);
    r.mode = currentModeName;
    if (!modifiers.isEmpty())
        r.modifiers = modifiers;
    rules.put(r.name, r);
}
Also used : Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)8 RuleAST (org.antlr.v4.tool.ast.RuleAST)7 Rule (org.antlr.v4.tool.Rule)4 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)3 ActionAST (org.antlr.v4.tool.ast.ActionAST)3 AltAST (org.antlr.v4.tool.ast.AltAST)3 BlockAST (org.antlr.v4.tool.ast.BlockAST)3 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 CommonToken (org.antlr.runtime.CommonToken)2 RecognitionException (org.antlr.runtime.RecognitionException)2 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)1 Token (org.antlr.runtime.Token)1 OrderedHashMap (org.antlr.v4.misc.OrderedHashMap)1