Search in sources :

Example 36 with Rule

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

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

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

the class UseDefAnalyzer method getRuleDependencies.

public static Map<Rule, Set<Rule>> getRuleDependencies(Grammar g, Collection<Rule> rules) {
    Map<Rule, Set<Rule>> dependencies = new HashMap<Rule, Set<Rule>>();
    for (Rule r : rules) {
        List<GrammarAST> tokenRefs = r.ast.getNodesWithType(ANTLRParser.TOKEN_REF);
        for (GrammarAST tref : tokenRefs) {
            Set<Rule> calls = dependencies.get(r);
            if (calls == null) {
                calls = new HashSet<Rule>();
                dependencies.put(r, calls);
            }
            calls.add(g.getRule(tref.getText()));
        }
    }
    return dependencies;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule)

Example 38 with Rule

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

the class OutputModelController method buildLexerOutputModel.

public OutputModelObject buildLexerOutputModel(boolean header) {
    CodeGenerator gen = delegate.getGenerator();
    LexerFile file = lexerFile(gen.getRecognizerFileName(header));
    setRoot(file);
    file.lexer = lexer(file);
    Grammar g = delegate.getGrammar();
    for (Rule r : g.rules.values()) {
        buildLexerRuleActions(file.lexer, r);
    }
    return file;
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) LexerFile(org.antlr.v4.codegen.model.LexerFile)

Example 39 with Rule

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

the class OutputModelController method buildParserOutputModel.

/** Build a file with a parser containing rule functions. Use the
	 *  controller as factory in SourceGenTriggers so it triggers codegen
	 *  extensions too, not just the factory functions in this factory.
	 */
public OutputModelObject buildParserOutputModel(boolean header) {
    CodeGenerator gen = delegate.getGenerator();
    ParserFile file = parserFile(gen.getRecognizerFileName(header));
    setRoot(file);
    file.parser = parser(file);
    Grammar g = delegate.getGrammar();
    for (Rule r : g.rules.values()) {
        buildRuleFunction(file.parser, r);
    }
    return file;
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ParserFile(org.antlr.v4.codegen.model.ParserFile)

Example 40 with Rule

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

the class ParserFactory method ruleRef.

@Override
public List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
    InvokeRule invokeOp = new InvokeRule(this, ID, label);
    // If no manual label and action refs as token/rule not label, we need to define implicit label
    if (controller.needsImplicitLabel(ID, invokeOp))
        defineImplicitLabel(ID, invokeOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(invokeOp, label);
    return list(invokeOp, listLabelOp);
}
Also used : InvokeRule(org.antlr.v4.codegen.model.InvokeRule) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

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