Search in sources :

Example 56 with Rule

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

the class ActionTranslator method nonLocalAttr.

@Override
public void nonLocalAttr(String expr, Token x, Token y) {
    gen.g.tool.log("action-translator", "nonLocalAttr " + x + "::" + y);
    Rule r = factory.getGrammar().getRule(x.getText());
    chunks.add(new NonLocalAttrRef(nodeContext, x.getText(), y.getText(), r.index));
}
Also used : Rule(org.antlr.v4.tool.Rule) NonLocalAttrRef(org.antlr.v4.codegen.model.chunk.NonLocalAttrRef)

Example 57 with Rule

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

the class ActionTranslator method qualifiedAttr.

@Override
public void qualifiedAttr(String expr, Token x, Token y) {
    gen.g.tool.log("action-translator", "qattr " + x + "." + y);
    if (node.resolver.resolveToAttribute(x.getText(), node) != null) {
        // must be a member access to a predefined attribute like $ctx.foo
        attr(expr, x);
        chunks.add(new ActionText(nodeContext, "." + y.getText()));
        return;
    }
    Attribute a = node.resolver.resolveToAttribute(x.getText(), y.getText(), node);
    if (a == null) {
        // Added in response to https://github.com/antlr/antlr4/issues/1211
        gen.g.tool.errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE, gen.g.fileName, x, x.getText(), "rule");
        return;
    }
    switch(a.dict.type) {
        // has to be current rule
        case ARG:
            chunks.add(new ArgRef(nodeContext, y.getText()));
            break;
        case RET:
            chunks.add(new QRetValueRef(nodeContext, getRuleLabel(x.getText()), y.getText()));
            break;
        case PREDEFINED_RULE:
            chunks.add(getRulePropertyRef(x, y));
            break;
        case TOKEN:
            chunks.add(getTokenPropertyRef(x, y));
            break;
    }
}
Also used : ActionText(org.antlr.v4.codegen.model.chunk.ActionText) Attribute(org.antlr.v4.tool.Attribute) ArgRef(org.antlr.v4.codegen.model.chunk.ArgRef) QRetValueRef(org.antlr.v4.codegen.model.chunk.QRetValueRef)

Example 58 with Rule

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

the class OutputModelController method buildNormalRuleFunction.

public void buildNormalRuleFunction(Rule r, RuleFunction function) {
    CodeGenerator gen = delegate.getGenerator();
    // TRIGGER factory functions for rule alts, elements
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
    GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
    walker = new SourceGenTriggers(nodes, this);
    try {
        // walk AST of rule alts/elements
        function.code = DefaultOutputModelFactory.list(walker.block(null, null));
        function.hasLookaheadBlock = walker.hasLookaheadBlock;
    } catch (org.antlr.runtime.RecognitionException e) {
        e.printStackTrace(System.err);
    }
    function.ctxType = delegate.getTarget().getRuleFunctionContextStructName(function);
    function.postamble = rulePostamble(function, r);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 59 with Rule

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

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

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

the class OutputModelController method buildLexerRuleActions.

public void buildLexerRuleActions(Lexer lexer, final Rule r) {
    if (r.actions.isEmpty()) {
        return;
    }
    CodeGenerator gen = delegate.getGenerator();
    Grammar g = delegate.getGrammar();
    String ctxType = delegate.getTarget().getRuleFunctionContextStructName(r);
    RuleActionFunction raf = lexer.actionFuncs.get(r);
    if (raf == null) {
        raf = new RuleActionFunction(delegate, r, ctxType);
    }
    for (ActionAST a : r.actions) {
        if (a instanceof PredAST) {
            PredAST p = (PredAST) a;
            RuleSempredFunction rsf = lexer.sempredFuncs.get(r);
            if (rsf == null) {
                rsf = new RuleSempredFunction(delegate, r, ctxType);
                lexer.sempredFuncs.put(r, rsf);
            }
            rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
        } else if (a.getType() == ANTLRParser.ACTION) {
            raf.actions.put(g.lexerActions.get(a), new Action(delegate, a));
        }
    }
    if (!raf.actions.isEmpty() && !lexer.actionFuncs.containsKey(r)) {
        // only add to lexer if the function actually contains actions
        lexer.actionFuncs.put(r, raf);
    }
}
Also used : RuleActionFunction(org.antlr.v4.codegen.model.RuleActionFunction) Action(org.antlr.v4.codegen.model.Action) PredAST(org.antlr.v4.tool.ast.PredAST) RuleSempredFunction(org.antlr.v4.codegen.model.RuleSempredFunction) Grammar(org.antlr.v4.tool.Grammar) ActionAST(org.antlr.v4.tool.ast.ActionAST)

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