public GrammarAST stripLeftRecursion(GrammarAST altAST) { GrammarAST lrlabel=null; GrammarAST first = (GrammarAST)altAST.getChild(0); int leftRecurRuleIndex = 0; if ( first.getType() == ELEMENT_OPTIONS ) { first = (GrammarAST)altAST.getChild(1); leftRecurRuleIndex = 1; } Tree rref = first.getChild(1); // if label=rule if ( (first.getType()==RULE_REF && first.getText().equals(ruleName)) || (rref!=null && rref.getType()==RULE_REF && rref.getText().equals(ruleName)) ) { if ( first.getType()==ASSIGN || first.getType()==PLUS_ASSIGN ) lrlabel = (GrammarAST)first.getChild(0); // remove rule ref (first child unless options present) altAST.deleteChild(leftRecurRuleIndex); // reset index so it prints properly (sets token range of // ALT to start to right of left recur rule we deleted) GrammarAST newFirstChild = (GrammarAST)altAST.getChild(leftRecurRuleIndex); altAST.setTokenStartIndex(newFirstChild.getTokenStartIndex()); } return lrlabel; }
public GrammarAST getNodeWithTokenIndex(int index) { if ( this.getToken()!=null && this.getToken().getTokenIndex()==index ) { return this; } // walk all children of root. for (int i= 0; i < getChildCount(); i++) { GrammarAST child = (GrammarAST)getChild(i); GrammarAST result = child.getNodeWithTokenIndex(index); if ( result!=null ) { return result; } } return null; }
@Override public void finishPrequels(GrammarAST firstPrequel) { if ( firstPrequel==null ) return; GrammarAST parent = (GrammarAST)firstPrequel.parent; List<GrammarAST> options = parent.getAllChildrenWithType(OPTIONS); List<GrammarAST> imports = parent.getAllChildrenWithType(IMPORT); List<GrammarAST> tokens = parent.getAllChildrenWithType(TOKENS_SPEC); checkNumPrequels(options, imports, tokens); }
void checkNumRules(GrammarAST rulesNode) { if ( rulesNode.getChildCount()==0 ) { GrammarAST root = (GrammarAST)rulesNode.getParent(); GrammarAST IDNode = (GrammarAST)root.getChild(0); g.tool.errMgr.grammarError(ErrorType.NO_RULES, g.fileName, null, IDNode.getText(), g); } }
GrammarAST id = (GrammarAST) root.getChild(0); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(id.token.getInputStream()); GrammarAST channelsRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.CHANNELS); GrammarAST tokensRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.TOKENS_SPEC); List<GrammarAST> actionRoots = root.getNodesWithType(ANTLRParser.AT); GrammarAST RULES = (GrammarAST)root.getFirstChildWithType(ANTLRParser.RULES); Set<String> rootRuleNames = new HashSet<String>(); List<GrammarAST> rootRules = RULES.getNodesWithType(ANTLRParser.RULE); for (GrammarAST r : rootRules) rootRuleNames.add(r.getChild(0).getText()); List<GrammarAST> rootModes = root.getNodesWithType(ANTLRParser.MODE); Set<String> rootModeNames = new HashSet<String>(); for (GrammarAST m : rootModes) rootModeNames.add(m.getChild(0).getText()); List<GrammarAST> addedModes = new ArrayList<GrammarAST>(); rootGrammar.tool.log("grammar", "imported channels: "+imp_channelRoot.getChildren()); if (channelsRoot==null) { channelsRoot = imp_channelRoot.dupTree(); channelsRoot.g = rootGrammar; root.insertChild(1, channelsRoot); // ^(GRAMMAR ID TOKENS...) } else { for (int c = 0; c < imp_channelRoot.getChildCount(); ++c) { String channel = imp_channelRoot.getChild(c).getText(); boolean channelIsInRootGrammar = false; for (int rc = 0; rc < channelsRoot.getChildCount(); ++rc) {
if ( optionsRoot!=null && optionsRoot.getChildCount()!=0 ) { GrammarAST lexerOptionsRoot = (GrammarAST)adaptor.dupNode(optionsRoot); lexerAST.addChild(lexerOptionsRoot); GrammarAST[] options = optionsRoot.getChildren().toArray(new GrammarAST[0]); for (GrammarAST o : options) { String optionName = o.getChild(0).getText(); if ( Grammar.lexerOptions.contains(optionName) && !Grammar.doNotCopyOptionsToLexer.contains(optionName) ) lexerOptionsRoot.addChild(optionTree); lexerAST.setOption(optionName, (GrammarAST)optionTree.getChild(1)); if ( e.getType()==ANTLRParser.AT ) { lexerAST.addChild((Tree)adaptor.dupTree(e)); if ( e.getChild(0).getText().equals("lexer") ) { actionsWeMoved.add(e); List<GrammarAST> rulesWeMoved = new ArrayList<GrammarAST>(); GrammarASTWithOptions[] rules; if (combinedRulesRoot.getChildCount() > 0) { rules = combinedRulesRoot.getChildren().toArray(new GrammarASTWithOptions[0]); String ruleName = r.getChild(0).getText(); if (Grammar.isTokenName(ruleName)) { lexerRulesRoot.addChild((Tree)adaptor.dupTree(r)); rulesWeMoved.add(r); combinedRulesRoot.deleteChild( r );
GrammarAST id = (GrammarAST) root.getChild(0); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(id.token.getInputStream()); GrammarAST tokensRoot = (GrammarAST)root.getFirstChildWithType(ANTLRParser.TOKENS_SPEC); List<GrammarAST> actionRoots = root.getNodesWithType(ANTLRParser.AT); GrammarAST RULES = (GrammarAST)root.getFirstChildWithType(ANTLRParser.RULES); Set<String> rootRuleNames = new HashSet<String>(); List<GrammarAST> rootRules = RULES.getNodesWithType(ANTLRParser.RULE); for (GrammarAST r : rootRules) rootRuleNames.add(r.getChild(0).getText()); rootGrammar.tool.log("grammar", "imported tokens: "+imp_tokensRoot.getChildren()); if ( tokensRoot==null ) { tokensRoot = (GrammarAST)adaptor.create(ANTLRParser.TOKENS_SPEC, "TOKENS"); tokensRoot.g = rootGrammar; root.insertChild(1, tokensRoot); // ^(GRAMMAR ID TOKENS...) tokensRoot.addChildren(Arrays.asList(imp_tokensRoot.getChildren().toArray(new Tree[0]))); String scopeName = rootGrammar.getDefaultActionScope(); GrammarAST scope, name, action; if ( at.getChildCount()>2 ) { // must have a scope scope = (GrammarAST)at.getChild(0); scopeName = scope.getText(); name = (GrammarAST)at.getChild(1); action = (GrammarAST)at.getChild(2);
switch (element.getType()) { case ANTLRParser.ASSIGN: case ANTLRParser.PLUS_ASSIGN: GrammarAST translatedChildElement = translateLeftFactoredElement((GrammarAST)element.getChild(1), factoredRule, variant, mode, includeFactoredElement); if (translatedChildElement == null) { return null; RuleAST ruleAST = (RuleAST)element.getAncestor(ANTLRParser.RULE); LOGGER.log(Level.WARNING, "Could not left factor ''{0}'' out of decision in rule ''{1}'': labeled rule references are not yet supported.", new Object[] { factoredRule, ruleAST.getChild(0).getText() }); if (factoredRule.equals(element.getToken().getText())) { if (!mode.includeFactoredAlts()) { return null; root.addChild(adaptor.create(Token.EPSILON, "EPSILON")); root.deleteChild(0); return root; Rule targetRule = _rules.get(element.getToken().getText()); if (targetRule == null) { return null; element.setText(element.getText() + marker + factoredRule); GrammarAST cloned = element.dupTree(); if (!translateLeftFactoredDecision(cloned, factoredRule, variant, mode, includeFactoredElement)) { return null;
public void getNodesWithTypePreorderDFS_(List<GrammarAST> nodes, IntervalSet types) { if ( types.contains(this.getType()) ) nodes.add(this); // walk all children of root. for (int i= 0; i < getChildCount(); i++) { GrammarAST child = (GrammarAST)getChild(i); child.getNodesWithTypePreorderDFS_(nodes, types); } }
List<GrammarAST> alternatives = block.getAllChildrenWithType(ANTLRParser.ALT); GrammarAST[] factoredAlternatives = new GrammarAST[alternatives.size()]; GrammarAST[] unfactoredAlternatives = new GrammarAST[alternatives.size()]; GrammarAST alternative = alternatives.get(i); if (mode.includeUnfactoredAlts()) { GrammarAST unfactoredAlt = translateLeftFactoredAlternative(alternative.dupTree(), factoredRule, variant, DecisionFactorMode.PARTIAL_UNFACTORED, false); unfactoredAlternatives[i] = unfactoredAlt; if (unfactoredAlt != null) { factoredAlternatives[i] = factoredAlt; if (factoredAlt != null) { factoredIntervals.add(alternative.getChildIndex()); for (int i = 0; i < factoredAlternatives.length; i++) { GrammarAST translatedAlt = factoredAlternatives[i]; if (translatedAlt.getChildCount() == 0) { adaptor.addChild(translatedAlt, adaptor.create(ANTLRParser.EPSILON, "EPSILON")); for (int i = 0; i < unfactoredAlternatives.length; i++) { GrammarAST translatedAlt = unfactoredAlternatives[i]; if (translatedAlt.getChildCount() == 0) { adaptor.addChild(translatedAlt, adaptor.create(ANTLRParser.EPSILON, "EPSILON")); if (combineWithPrevious) { GrammarAST translatedAlt = factoredAlternatives[i]; if (translatedAlt.getChildCount() == 0) { adaptor.addChild(translatedAlt, adaptor.create(ANTLRParser.EPSILON, "EPSILON"));
List<GrammarAST> rules = new ArrayList<GrammarAST>(RULES.getAllChildrenWithType(ANTLRParser.RULE)); for (GrammarAST mode : g.ast.getAllChildrenWithType(ANTLRParser.MODE)) { rules.addAll(mode.getAllChildrenWithType(ANTLRParser.RULE)); RuleAST ruleAST = (RuleAST)r; GrammarAST ID = (GrammarAST)ruleAST.getChild(0); String ruleName = ID.getText(); RuleAST prev = ruleToAST.get(ruleName); if ( prev !=null ) { g.tool.errMgr.grammarError(ErrorType.RULE_REDEFINITION, g.fileName, ID.getToken(), ruleName, prevChild.getToken().getLine()); redefinition = true; continue;
protected boolean expandOptionalQuantifiersForBlock(GrammarAST block, boolean variant) { List<GrammarAST> children = new ArrayList<GrammarAST>(); for (int i = 0; i < block.getChildCount(); i++) { GrammarAST child = (GrammarAST)block.getChild(i); if (child.getType() != ANTLRParser.ALT) { children.add(child); continue; newChildren.addChildren(children); block.replaceChildren(0, block.getChildCount() - 1, newChildren); block.freshenParentAndChildIndexesDeeply(); if (!variant && block.getParent() instanceof RuleAST) { RuleAST ruleAST = (RuleAST)block.getParent(); String ruleName = ruleAST.getChild(0).getText(); Rule r = _rules.get(ruleName); List<GrammarAST> blockAlts = block.getAllChildrenWithType(ANTLRParser.ALT); r.numberOfAlts = blockAlts.size(); r.alt = new Alternative[blockAlts.size() + 1];
protected GrammarAST expandOptionalQuantifiersForAlt(GrammarAST alt) { if (alt.getChildCount() == 0) { return null; if (alt.getChild(0).getType() == ANTLRParser.OPTIONAL) { GrammarAST root = adaptor.nil(); GrammarAST alt2 = alt.dupTree(); alt2.deleteChild(0); if (alt2.getChildCount() == 0) { adaptor.addChild(alt2, adaptor.create(ANTLRParser.EPSILON, "EPSILON")); alt.setChild(0, alt.getChild(0).getChild(0)); if (alt.getChild(0).getType() == ANTLRParser.BLOCK && alt.getChild(0).getChildCount() == 1 && alt.getChild(0).getChild(0).getType() == ANTLRParser.ALT) { GrammarAST list = adaptor.nil(); for (Object tree : ((GrammarAST)alt.getChild(0).getChild(0)).getChildren()) { adaptor.addChild(list, tree); else if (alt.getChild(0).getType() == ANTLRParser.CLOSURE) { GrammarAST root = adaptor.nil(); GrammarAST alt2 = alt.dupTree(); alt2.deleteChild(0); if (alt2.getChildCount() == 0) { adaptor.addChild(alt2, adaptor.create(ANTLRParser.EPSILON, "EPSILON")); for (int i = 0; i < alt.getChild(0).getChildCount(); i++) { plusBlockAST.addChild(alt.getChild(0).getChild(i)); alt.setChild(0, plusBlockAST);
@Override public void discoverLexerRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, GrammarAST block) { int numAlts = block.getChildCount(); Rule r = new Rule(g, ID.getText(), rule, numAlts); r.mode = currentModeName; if ( !modifiers.isEmpty() ) r.modifiers = modifiers; rules.put(r.name, r); } }
@Override public void suffixAlt(AltAST originalAltTree, int alt) { AltAST altTree = (AltAST)originalAltTree.dupTree(); String altLabel = altTree.altLabel!=null ? altTree.altLabel.getText() : null; String label = null; boolean isListLabel = false; GrammarAST lrlabel = stripLeftRecursion(altTree); if ( lrlabel!=null ) { label = lrlabel.getText(); isListLabel = lrlabel.getParent().getType() == PLUS_ASSIGN; leftRecursiveRuleRefLabels.add(new Pair<GrammarAST,String>(lrlabel,altLabel)); } stripAltLabel(altTree); String altText = text(altTree); altText = altText.trim(); LeftRecursiveRuleAltInfo a = new LeftRecursiveRuleAltInfo(alt, altText, label, altLabel, isListLabel, originalAltTree); suffixAlts.put(alt, a); // System.out.println("suffixAlt " + alt + ": " + altText + ", rewrite=" + rewriteText); }
@Override public GrammarAST dupNode() { return new GrammarAST(this); }
for (Tree id: importDecl.getAllChildrenWithType(ANTLRParser.ID)) { for (int i = 0, count = options.getChildCount(); i < count; i++) { Tree option = options.getChild(i);