@Override public void finishRule(RuleAST rule, GrammarAST ID, GrammarAST block) { if ( rule.isLexerRule() ) return; BlockAST blk = (BlockAST)rule.getFirstChildWithType(BLOCK); int nalts = blk.getChildCount(); GrammarAST idAST = (GrammarAST)rule.getChild(0); for (int i=0; i< nalts; i++) { AltAST altAST = (AltAST)blk.getChild(i); if ( prevRuleForLabel!=null && !prevRuleForLabel.equals(rule.getRuleName()) ) { g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_REDEF, g.fileName, altAST.altLabel.token, altLabel, rule.getRuleName(), prevRuleForLabel); List<GrammarAST> altLabels = ruleCollector.ruleToAltLabels.get(rule.getRuleName()); int numAltLabels = 0; if ( altLabels!=null ) numAltLabels = altLabels.size(); if ( numAltLabels>0 && nalts != numAltLabels ) { g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS, g.fileName, idAST.token, rule.getRuleName());
@Override public RuleAST dupNode() { return new RuleAST(this); }
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; }
((GrammarAST)t.getChild(0)).token = ((GrammarAST)prevRuleAST.getChild(0)).getToken(); r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAndOtherAlts); if (r.recPrimaryAlts.isEmpty()) { tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name); ActionAST arg = (ActionAST)r.ast.getFirstChildWithType(ANTLRParser.ARG_ACTION); if ( arg!=null ) { r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g); tool.log("grammar", "added: "+t.toStringTree()); return true;
String ruleName = ((RuleAST) ruleNode).getRuleName(); Rule r = ast.g.getRule(ruleName); if ( r instanceof LeftRecursiveRule ) { RuleAST originalAST = ((LeftRecursiveRule) r).getOriginalAST(); tokenRegion = Interval.of(originalAST.getTokenStartIndex(), originalAST.getTokenStopIndex());
public String getRuleName() { GrammarAST nameNode = (GrammarAST)getChild(0); if ( nameNode!=null ) return nameNode.getText(); return null; }
public ActionAST getLexerAction() { Tree blk = getFirstChildWithType(ANTLRParser.BLOCK); if ( blk.getChildCount()==1 ) { Tree onlyAlt = blk.getChild(0); Tree lastChild = onlyAlt.getChild(onlyAlt.getChildCount()-1); if ( lastChild.getType()==ANTLRParser.ACTION ) { return (ActionAST)lastChild; } } return null; }
MultiMap<String, Rule> baseContexts = new MultiMap<String, Rule>(); for (Rule r : ruleCollector.rules.values()) { GrammarAST optionAST = r.ast.getOptionAST("baseContext"); if (r.ast.isLexerRule()) { if (optionAST != null) { Token errorToken = optionAST.getToken(); targetRule != null && targetRule.ast != null && (targetRule.ast.getOptionAST("baseContext") != null || !targetRule.name.equals(targetRule.getBaseContext())); errorToken = ((CommonTree)r.ast.getChild(0)).getToken(); Rule errorRule = entry.getValue().get(0); g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS_GROUP, g.fileName, ((CommonTree)errorRule.ast.getChild(0)).getToken(), errorRule.name);
public boolean isLexerRule() { String name = getRuleName(); return name!=null && Grammar.isTokenName(name); }
protected static Token getStartTokenOfFirstRule(Collection<? extends Collection<Rule>> cycles) { if (cycles == null) { return null; } for (Collection<Rule> collection : cycles) { if (collection == null) { return null; } for (Rule rule : collection) { if (rule.ast != null) { return rule.ast.getToken(); } } } return null; } }
((GrammarAST)t.getChild(0)).token = ((GrammarAST)prevRuleAST.getChild(0)).getToken(); r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.otherAlts); if (r.recPrimaryAlts.isEmpty()) { tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name); ActionAST arg = (ActionAST)r.ast.getFirstChildWithType(ANTLRParser.ARG_ACTION); if ( arg!=null ) { r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g); tool.log("grammar", "added: "+t.toStringTree()); return true;
String ruleName = ((RuleAST) ruleNode).getRuleName(); Rule r = ast.g.getRule(ruleName); if ( r instanceof LeftRecursiveRule ) { RuleAST originalAST = ((LeftRecursiveRule) r).getOriginalAST(); tokenRegion = Interval.of(originalAST.getTokenStartIndex(), originalAST.getTokenStopIndex());
public String getRuleName() { GrammarAST nameNode = (GrammarAST)getChild(0); if ( nameNode!=null ) return nameNode.getText(); return null; }
public ActionAST getLexerAction() { Tree blk = getFirstChildWithType(ANTLRParser.BLOCK); if ( blk.getChildCount()==1 ) { Tree onlyAlt = blk.getChild(0); Tree lastChild = onlyAlt.getChild(onlyAlt.getChildCount()-1); if ( lastChild.getType()==ANTLRParser.ACTION ) { return (ActionAST)lastChild; } } return null; }
public boolean isLexerRule() { String name = getRuleName(); return name!=null && Grammar.isTokenName(name); }
protected static Token getStartTokenOfFirstRule(Collection<? extends Collection<Rule>> cycles) { if (cycles == null) { return null; } for (Collection<Rule> collection : cycles) { if (collection == null) { return null; } for (Rule rule : collection) { if (rule.ast != null) { return rule.ast.getToken(); } } } return null; } }
@Override public void finishRule(RuleAST rule, GrammarAST ID, GrammarAST block) { if ( rule.isLexerRule() ) return; BlockAST blk = (BlockAST)rule.getFirstChildWithType(BLOCK); int nalts = blk.getChildCount(); GrammarAST idAST = (GrammarAST)rule.getChild(0); for (int i=0; i< nalts; i++) { AltAST altAST = (AltAST)blk.getChild(i); if ( prevRuleForLabel!=null && !prevRuleForLabel.equals(rule.getRuleName()) ) { g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_REDEF, g.fileName, altAST.altLabel.token, altLabel, rule.getRuleName(), prevRuleForLabel); List<GrammarAST> altLabels = ruleCollector.ruleToAltLabels.get(rule.getRuleName()); int numAltLabels = 0; if ( altLabels!=null ) numAltLabels = altLabels.size(); if ( numAltLabels>0 && nalts != numAltLabels ) { g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS, g.fileName, idAST.token, rule.getRuleName());
((GrammarAST)t.getChild(0)).token = ((GrammarAST)prevRuleAST.getChild(0)).getToken(); r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAndOtherAlts); if (r.recPrimaryAlts.isEmpty()) { tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.getChild(0)).getToken(), r.name); ActionAST arg = (ActionAST)r.ast.getFirstChildWithType(ANTLRParser.ARG_ACTION); if ( arg!=null ) { r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g); tool.log("grammar", "added: "+t.toStringTree()); return true;
String ruleName = ((RuleAST) ruleNode).getRuleName(); Rule r = ast.g.getRule(ruleName); if ( r instanceof LeftRecursiveRule ) { RuleAST originalAST = ((LeftRecursiveRule) r).getOriginalAST(); tokenRegion = Interval.of(originalAST.getTokenStartIndex(), originalAST.getTokenStopIndex());
public String getRuleName() { GrammarAST nameNode = (GrammarAST)getChild(0); if ( nameNode!=null ) return nameNode.getText(); return null; }