Search in sources :

Example 11 with GrammarAST

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

the class SemanticPipeline method identifyStartRules.

void identifyStartRules(SymbolCollector collector) {
    for (GrammarAST ref : collector.rulerefs) {
        String ruleName = ref.getText();
        Rule r = g.getRule(ruleName);
        if (r != null)
            r.isStartRule = false;
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule)

Example 12 with GrammarAST

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

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

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

use of org.antlr.v4.tool.ast.GrammarAST 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)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5