Search in sources :

Example 71 with Rule

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

the class SemanticPipeline method hasTypeOrMoreCommand.

boolean hasTypeOrMoreCommand(@NotNull 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 72 with Rule

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

the class SymbolChecks method checkForModeConflicts.

public void checkForModeConflicts(Grammar g) {
    if (g.isLexer()) {
        LexerGrammar lexerGrammar = (LexerGrammar) g;
        for (String modeName : lexerGrammar.modes.keySet()) {
            if (!modeName.equals("DEFAULT_MODE") && reservedNames.contains(modeName)) {
                Rule rule = lexerGrammar.modes.get(modeName).iterator().next();
                g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, rule.ast.parent.getToken(), modeName);
            }
            if (g.getTokenType(modeName) != Token.INVALID_TYPE) {
                Rule rule = lexerGrammar.modes.get(modeName).iterator().next();
                g.tool.errMgr.grammarError(ErrorType.MODE_CONFLICTS_WITH_TOKEN, g.fileName, rule.ast.parent.getToken(), modeName);
            }
        }
    }
}
Also used : Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) LexerGrammar(org.antlr.v4.tool.LexerGrammar)

Example 73 with Rule

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

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 74 with Rule

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

the class UseDefAnalyzer method trackTokenRuleRefsInActions.

// side-effect: updates Alternative with refs in actions
public static void trackTokenRuleRefsInActions(Grammar g) {
    for (Rule r : g.rules.values()) {
        for (int i = 1; i <= r.numberOfAlts; i++) {
            Alternative alt = r.alt[i];
            for (ActionAST a : alt.actions) {
                ActionSniffer sniffer = new ActionSniffer(g, r, alt, a, a.token);
                sniffer.examineAction();
            }
        }
    }
}
Also used : Alternative(org.antlr.v4.tool.Alternative) Rule(org.antlr.v4.tool.Rule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 75 with Rule

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

the class RuleFunction method addContextGetters.

public void addContextGetters(OutputModelFactory factory, Collection<RuleAST> contextASTs) {
    List<AltAST> unlabeledAlternatives = new ArrayList<AltAST>();
    Map<String, List<AltAST>> labeledAlternatives = new LinkedHashMap<String, List<AltAST>>();
    for (RuleAST ast : contextASTs) {
        try {
            unlabeledAlternatives.addAll(rule.g.getUnlabeledAlternatives(ast));
            for (Map.Entry<String, List<Tuple2<Integer, AltAST>>> entry : rule.g.getLabeledAlternatives(ast).entrySet()) {
                List<AltAST> list = labeledAlternatives.get(entry.getKey());
                if (list == null) {
                    list = new ArrayList<AltAST>();
                    labeledAlternatives.put(entry.getKey(), list);
                }
                for (Tuple2<Integer, AltAST> tuple : entry.getValue()) {
                    list.add(tuple.getItem2());
                }
            }
        } catch (RecognitionException ex) {
        }
    }
    // Add ctx labels for elements in alts with no '#' label
    if (!unlabeledAlternatives.isEmpty()) {
        Set<Decl> decls = getDeclsForAllElements(unlabeledAlternatives);
        // put directly in base context
        for (Decl decl : decls) {
            ruleCtx.addDecl(decl);
        }
    }
    // make structs for '#' labeled alts, define ctx labels for elements
    altLabelCtxs = new LinkedHashMap<String, AltLabelStructDecl>();
    if (!labeledAlternatives.isEmpty()) {
        for (Map.Entry<String, List<AltAST>> entry : labeledAlternatives.entrySet()) {
            AltLabelStructDecl labelDecl = new AltLabelStructDecl(factory, rule, entry.getKey());
            altLabelCtxs.put(entry.getKey(), labelDecl);
            Set<Decl> decls = getDeclsForAllElements(entry.getValue());
            for (Decl decl : decls) {
                labelDecl.addDecl(decl);
            }
        }
    }
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) ArrayList(java.util.ArrayList) ContextRuleListGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) StructDecl(org.antlr.v4.codegen.model.decl.StructDecl) AttributeDecl(org.antlr.v4.codegen.model.decl.AttributeDecl) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) ContextTokenListGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl) ContextRuleListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl) ContextRuleGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl) ContextTokenListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl) ContextTokenGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)100 Rule (org.antlr.v4.tool.Rule)95 Test (org.junit.Test)94 ATN (org.antlr.v4.runtime.atn.ATN)87 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)69 ArrayList (java.util.ArrayList)59 Grammar (org.antlr.v4.tool.Grammar)58 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)51 ATNState (org.antlr.v4.runtime.atn.ATNState)42 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)37 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)36 Token (org.antlr.v4.runtime.Token)21 ActionAST (org.antlr.v4.tool.ast.ActionAST)21 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)20 ParseTree (org.antlr.v4.runtime.tree.ParseTree)19 RuleAST (org.antlr.v4.tool.ast.RuleAST)19 HashMap (java.util.HashMap)18 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)16 AltAST (org.antlr.v4.tool.ast.AltAST)16 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)14