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);
}
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);
}
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;
}
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);
}
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;
}
Aggregations