Search in sources :

Example 6 with ActionAST

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

the class LexerATNFactory method action.

@Override
public Handle action(ActionAST action) {
    int ruleIndex = currentRule.index;
    int actionIndex = g.lexerActions.get(action);
    LexerCustomAction lexerAction = new LexerCustomAction(ruleIndex, actionIndex);
    return action(action, lexerAction);
}
Also used : LexerCustomAction(org.antlr.v4.runtime.atn.LexerCustomAction)

Example 7 with ActionAST

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

the class Tool method checkForRuleIssues.

/**
	 * Important enough to avoid multiple definitions that we do very early,
	 * right after AST construction. Also check for undefined rules in
	 * parser/lexer to avoid exceptions later. Return true if we find multiple
	 * definitions of the same rule or a reference to an undefined rule or
	 * parser rule ref in lexer rule.
	 */
public boolean checkForRuleIssues(final Grammar g) {
    // check for redefined rules
    GrammarAST RULES = (GrammarAST) g.ast.getFirstChildWithType(ANTLRParser.RULES);
    List<GrammarAST> rules = new ArrayList<GrammarAST>(RULES.getAllChildrenWithType(ANTLRParser.RULE));
    for (GrammarAST mode : g.ast.getAllChildrenWithType(ANTLRParser.MODE)) {
        rules.addAll(mode.getAllChildrenWithType(ANTLRParser.RULE));
    }
    boolean redefinition = false;
    final Map<String, RuleAST> ruleToAST = new HashMap<String, RuleAST>();
    for (GrammarAST r : rules) {
        RuleAST ruleAST = (RuleAST) r;
        GrammarAST ID = (GrammarAST) ruleAST.getChild(0);
        String ruleName = ID.getText();
        RuleAST prev = ruleToAST.get(ruleName);
        if (prev != null) {
            GrammarAST prevChild = (GrammarAST) prev.getChild(0);
            g.tool.errMgr.grammarError(ErrorType.RULE_REDEFINITION, g.fileName, ID.getToken(), ruleName, prevChild.getToken().getLine());
            redefinition = true;
            continue;
        }
        ruleToAST.put(ruleName, ruleAST);
    }
    // check for undefined rules
    class UndefChecker extends GrammarTreeVisitor {

        public boolean badref = false;

        @Override
        public void tokenRef(TerminalAST ref) {
            if ("EOF".equals(ref.getText())) {
                // this is a special predefined reference
                return;
            }
            if (g.isLexer())
                ruleRef(ref, null);
        }

        @Override
        public void ruleRef(GrammarAST ref, ActionAST arg) {
            RuleAST ruleAST = ruleToAST.get(ref.getText());
            String fileName = ref.getToken().getInputStream().getSourceName();
            if (Character.isUpperCase(currentRuleName.charAt(0)) && Character.isLowerCase(ref.getText().charAt(0))) {
                badref = true;
                errMgr.grammarError(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE, fileName, ref.getToken(), ref.getText(), currentRuleName);
            } else if (ruleAST == null) {
                badref = true;
                errMgr.grammarError(ErrorType.UNDEFINED_RULE_REF, fileName, ref.token, ref.getText());
            }
        }

        @Override
        public ErrorManager getErrorManager() {
            return errMgr;
        }
    }
    UndefChecker chk = new UndefChecker();
    chk.visitGrammar(g.ast);
    return redefinition || chk.badref;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) HashMap(java.util.HashMap) GrammarTreeVisitor(org.antlr.v4.parse.GrammarTreeVisitor) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 8 with ActionAST

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

the class Grammar method getIndexToPredicateMap.

public LinkedHashMap<Integer, PredAST> getIndexToPredicateMap() {
    LinkedHashMap<Integer, PredAST> indexToPredMap = new LinkedHashMap<Integer, PredAST>();
    for (Rule r : rules.values()) {
        for (ActionAST a : r.actions) {
            if (a instanceof PredAST) {
                PredAST p = (PredAST) a;
                indexToPredMap.put(sempreds.get(p), p);
            }
        }
    }
    return indexToPredMap;
}
Also used : PredAST(org.antlr.v4.tool.ast.PredAST) LinkedHashMap(java.util.LinkedHashMap) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 9 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST 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 10 with ActionAST

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

the class SymbolCollector method ruleCatch.

@Override
public void ruleCatch(GrammarAST arg, ActionAST action) {
    GrammarAST catchme = (GrammarAST) action.getParent();
    currentRule.exceptions.add(catchme);
    action.resolver = currentRule;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Aggregations

ActionAST (org.antlr.v4.tool.ast.ActionAST)11 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)5 Rule (org.antlr.v4.tool.Rule)3 PredAST (org.antlr.v4.tool.ast.PredAST)3 HashMap (java.util.HashMap)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CommonToken (org.antlr.runtime.CommonToken)2 Token (org.antlr.runtime.Token)2 Action (org.antlr.v4.codegen.model.Action)2 RuleSempredFunction (org.antlr.v4.codegen.model.RuleSempredFunction)2 ActionSplitter (org.antlr.v4.parse.ActionSplitter)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 Alternative (org.antlr.v4.tool.Alternative)2 Attribute (org.antlr.v4.tool.Attribute)2 Grammar (org.antlr.v4.tool.Grammar)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1