Search in sources :

Example 31 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class SemanticPipeline method assignLexerTokenTypes.

void assignLexerTokenTypes(Grammar g, List<GrammarAST> tokensDefs) {
    // put in root, even if imported
    Grammar G = g.getOutermostGrammar();
    for (GrammarAST def : tokensDefs) {
        // tokens { id (',' id)* } so must check IDs not TOKEN_REF
        if (Grammar.isTokenName(def.getText())) {
            G.defineTokenName(def.getText());
        }
    }
    /* Define token types for nonfragment rules which do not include a 'type(...)'
		 * or 'more' lexer command.
		 */
    for (Rule r : g.rules.values()) {
        if (!r.isFragment() && !hasTypeOrMoreCommand(r)) {
            G.defineTokenName(r.name);
        }
    }
    // FOR ALL X : 'xxx'; RULES, DEFINE 'xxx' AS TYPE X
    List<Pair<GrammarAST, GrammarAST>> litAliases = Grammar.getStringLiteralAliasesFromLexerRules(g.ast);
    Set<String> conflictingLiterals = new HashSet<String>();
    if (litAliases != null) {
        for (Pair<GrammarAST, GrammarAST> pair : litAliases) {
            GrammarAST nameAST = pair.a;
            GrammarAST litAST = pair.b;
            if (!G.stringLiteralToTypeMap.containsKey(litAST.getText())) {
                G.defineTokenAlias(nameAST.getText(), litAST.getText());
            } else {
                // oops two literal defs in two rules (within or across modes).
                conflictingLiterals.add(litAST.getText());
            }
        }
        for (String lit : conflictingLiterals) {
            // Remove literal if repeated across rules so it's not
            // found by parser grammar.
            Integer value = G.stringLiteralToTypeMap.remove(lit);
            if (value != null && value > 0 && value < G.typeToStringLiteralList.size() && lit.equals(G.typeToStringLiteralList.get(value))) {
                G.typeToStringLiteralList.set(value, null);
            }
        }
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Rule(org.antlr.v4.tool.Rule) Pair(org.antlr.v4.runtime.misc.Pair) HashSet(java.util.HashSet)

Example 32 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class SemanticPipeline method hasTypeOrMoreCommand.

boolean hasTypeOrMoreCommand(Rule r) {
    GrammarAST ast = r.ast;
    if (ast == null) {
        return false;
    }
    GrammarAST altActionAst = (GrammarAST) ast.getFirstDescendantWithType(ANTLRParser.LEXER_ALT_ACTION);
    if (altActionAst == null) {
        // the rule isn't followed by any commands
        return false;
    }
    // first child is the alt itself, subsequent are the actions
    for (int i = 1; i < altActionAst.getChildCount(); i++) {
        GrammarAST node = (GrammarAST) altActionAst.getChild(i);
        if (node.getType() == ANTLRParser.LEXER_ACTION_CALL) {
            if ("type".equals(node.getChild(0).getText())) {
                return true;
            }
        } else if ("more".equals(node.getText())) {
            return true;
        }
    }
    return false;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Example 33 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class SymbolChecks method checkRuleArgs.

// CAN ONLY CALL THE TWO NEXT METHODS AFTER GRAMMAR HAS RULE DEFS (see semanticpipeline)
public void checkRuleArgs(Grammar g, List<GrammarAST> rulerefs) {
    if (rulerefs == null)
        return;
    for (GrammarAST ref : rulerefs) {
        String ruleName = ref.getText();
        Rule r = g.getRule(ruleName);
        GrammarAST arg = (GrammarAST) ref.getFirstChildWithType(ANTLRParser.ARG_ACTION);
        if (arg != null && (r == null || r.args == null)) {
            errMgr.grammarError(ErrorType.RULE_HAS_NO_ARGS, g.fileName, ref.token, ruleName);
        } else if (arg == null && (r != null && r.args != null)) {
            errMgr.grammarError(ErrorType.MISSING_RULE_ARGS, g.fileName, ref.token, ruleName);
        }
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Example 34 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class SymbolChecks method checkForQualifiedRuleIssues.

public void checkForQualifiedRuleIssues(Grammar g, List<GrammarAST> qualifiedRuleRefs) {
    for (GrammarAST dot : qualifiedRuleRefs) {
        GrammarAST grammar = (GrammarAST) dot.getChild(0);
        GrammarAST rule = (GrammarAST) dot.getChild(1);
        g.tool.log("semantics", grammar.getText() + "." + rule.getText());
        Grammar delegate = g.getImportedGrammar(grammar.getText());
        if (delegate == null) {
            errMgr.grammarError(ErrorType.NO_SUCH_GRAMMAR_SCOPE, g.fileName, grammar.token, grammar.getText(), rule.getText());
        } else {
            if (g.getRule(grammar.getText(), rule.getText()) == null) {
                errMgr.grammarError(ErrorType.NO_SUCH_RULE_IN_SCOPE, g.fileName, rule.token, grammar.getText(), rule.getText());
            }
        }
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 35 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class SymbolChecks method checkForLabelConflict.

public void checkForLabelConflict(Rule r, GrammarAST labelID) {
    String name = labelID.getText();
    if (nameToRuleMap.containsKey(name)) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RULE;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (tokenIDs.contains(name)) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_TOKEN;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.args != null && r.args.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_ARG;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.retvals != null && r.retvals.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RETVAL;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.locals != null && r.locals.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_LOCAL;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)50 ATN (org.antlr.v4.runtime.atn.ATN)47 Rule (org.antlr.v4.tool.Rule)47 Test (org.junit.Test)46 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 Grammar (org.antlr.v4.tool.Grammar)30 ATNState (org.antlr.v4.runtime.atn.ATNState)26 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)24 ArrayList (java.util.ArrayList)20 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)17 DOTGenerator (org.antlr.v4.tool.DOTGenerator)9 HashMap (java.util.HashMap)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)8 ActionAST (org.antlr.v4.tool.ast.ActionAST)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)7 DecisionState (org.antlr.v4.runtime.atn.DecisionState)7 RuleAST (org.antlr.v4.tool.ast.RuleAST)7 LinkedHashMap (java.util.LinkedHashMap)6