Search in sources :

Example 76 with GrammarAST

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

the class SymbolCollector method label.

@Override
public void label(GrammarAST op, GrammarAST ID, GrammarAST element) {
    LabelElementPair lp = new LabelElementPair(g, ID, element, op.getType());
    currentRule.alt[currentOuterAltNumber].labelDefs.map(ID.getText(), lp);
}
Also used : LabelElementPair(org.antlr.v4.tool.LabelElementPair)

Example 77 with GrammarAST

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

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 = gen.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 78 with GrammarAST

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

the class ParserFactory method getChoiceBlock.

@Override
public Choice getChoiceBlock(BlockAST blkAST, List<CodeBlockForAlt> alts, GrammarAST labelAST) {
    int decision = ((DecisionState) blkAST.atnState).decision;
    Choice c;
    if (!g.tool.force_atn && AnalysisPipeline.disjoint(g.decisionLOOK.get(decision))) {
        c = getLL1ChoiceBlock(blkAST, alts);
    } else {
        c = getComplexChoiceBlock(blkAST, alts);
    }
    if (labelAST != null) {
        // for x=(...), define x or x_list
        String label = labelAST.getText();
        Decl d = getTokenLabelDecl(label);
        c.label = d;
        getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), d);
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            String listLabel = gen.getTarget().getListLabel(label);
            TokenListDecl l = new TokenListDecl(this, listLabel);
            getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), l);
        }
    }
    return c;
}
Also used : Choice(org.antlr.v4.codegen.model.Choice) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) DecisionState(org.antlr.v4.runtime.atn.DecisionState)

Example 79 with GrammarAST

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

the class ParserFactory method defineImplicitLabel.

// support
public void defineImplicitLabel(GrammarAST ast, LabeledOp op) {
    Decl d;
    if (ast.getType() == ANTLRParser.SET || ast.getType() == ANTLRParser.WILDCARD) {
        String implLabel = gen.getTarget().getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
        d = getTokenLabelDecl(implLabel);
        ((TokenDecl) d).isImplicit = true;
    } else if (ast.getType() == ANTLRParser.RULE_REF) {
        // a rule reference?
        Rule r = g.getRule(ast.getText());
        String implLabel = gen.getTarget().getImplicitRuleLabel(ast.getText());
        String ctxName = gen.getTarget().getRuleFunctionContextStructName(r);
        d = new RuleContextDecl(this, implLabel, ctxName);
        ((RuleContextDecl) d).isImplicit = true;
    } else {
        String implLabel = gen.getTarget().getImplicitTokenLabel(ast.getText());
        d = getTokenLabelDecl(implLabel);
        ((TokenDecl) d).isImplicit = true;
    }
    op.getLabels().add(d);
    // all labels must be in scope struct in case we exec action out of context
    getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
}
Also used : TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) InvokeRule(org.antlr.v4.codegen.model.InvokeRule) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl)

Example 80 with GrammarAST

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

the class ParserFactory method getLL1EBNFBlock.

@Override
public Choice getLL1EBNFBlock(GrammarAST ebnfRoot, List<CodeBlockForAlt> alts) {
    int ebnf = 0;
    if (ebnfRoot != null)
        ebnf = ebnfRoot.getType();
    Choice c = null;
    switch(ebnf) {
        case ANTLRParser.OPTIONAL:
            if (alts.size() == 1)
                c = new LL1OptionalBlockSingleAlt(this, ebnfRoot, alts);
            else
                c = new LL1OptionalBlock(this, ebnfRoot, alts);
            break;
        case ANTLRParser.CLOSURE:
            if (alts.size() == 1)
                c = new LL1StarBlockSingleAlt(this, ebnfRoot, alts);
            else
                c = getComplexEBNFBlock(ebnfRoot, alts);
            break;
        case ANTLRParser.POSITIVE_CLOSURE:
            if (alts.size() == 1)
                c = new LL1PlusBlockSingleAlt(this, ebnfRoot, alts);
            else
                c = getComplexEBNFBlock(ebnfRoot, alts);
            break;
    }
    return c;
}
Also used : Choice(org.antlr.v4.codegen.model.Choice) LL1OptionalBlockSingleAlt(org.antlr.v4.codegen.model.LL1OptionalBlockSingleAlt) LL1PlusBlockSingleAlt(org.antlr.v4.codegen.model.LL1PlusBlockSingleAlt) LL1OptionalBlock(org.antlr.v4.codegen.model.LL1OptionalBlock) LL1StarBlockSingleAlt(org.antlr.v4.codegen.model.LL1StarBlockSingleAlt)

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