private void updateLineColumnToLookaheadDFAMap(DFA lookaheadDFA) { GrammarAST decisionAST = nfa.grammar.getDecisionBlockAST(lookaheadDFA.decisionNumber); int line = decisionAST.getLine(); int col = decisionAST.getColumn(); lineColumnToLookaheadDFAMap.put(new StringBuffer().append(line + ":") .append(col).toString(), lookaheadDFA); }
private static List<GrammarAST> getChildrenForDupTree(GrammarAST t) { List<GrammarAST> result = new ArrayList<GrammarAST>(); for (int i = 0; i < t.getChildCount(); i++){ GrammarAST child = (GrammarAST)t.getChild(i); int ttype = child.getType(); if (ttype == ANTLRParser.REWRITES || ttype == ANTLRParser.REWRITE || ttype==ANTLRParser.ACTION) { continue; } if (ttype == ANTLRParser.BANG || ttype == ANTLRParser.ROOT) { for (GrammarAST subchild : getChildrenForDupTree(child)) result.add(subchild); } else { result.add(child); } } if ( result.size()==1 && result.get(0).getType()==ANTLRParser.EOA && t.getType()==ANTLRParser.ALT ) { // can't have an empty alt, insert epsilon result.add(0, new GrammarAST(ANTLRParser.EPSILON, "epsilon")); } return result; }
public static GrammarAST dupTree(GrammarAST t) { if ( t==null ) { return null; } GrammarAST root = dup(t); // make copy of root // copy all children of root. for (int i= 0; i < t.getChildCount(); i++) { GrammarAST child = (GrammarAST)t.getChild(i); root.addChild(dupTree(child)); } return root; }
public void addRule(GrammarAST grammarTree, GrammarAST t) { GrammarAST p = null; for (int i = 0; i < grammarTree.getChildCount(); i++ ) { p = (GrammarAST)grammarTree.getChild(i); if (p == null || p.getType() == ANTLRParser.RULE || p.getType() == ANTLRParser.PREC_RULE) { break; } } if (p != null) { grammarTree.addChild(t); } }
public boolean hasRewrite(int i) { GrammarAST blk = tree.findFirstType(ANTLRParser.BLOCK); GrammarAST alt = blk.getBlockALT(i); GrammarAST rew = alt.getNextSibling(); if ( rew!=null && rew.getType()==ANTLRParser.REWRITES ) return true; if ( alt.findFirstType(ANTLRParser.REWRITES)!=null ) return true; return false; }
public void stripLeftRecursion(GrammarAST altAST) { GrammarAST rref = (GrammarAST)altAST.getChild(0); if ( rref.getType()== ANTLRParser.RULE_REF && rref.getText().equals(ruleName)) { // remove rule ref altAST.deleteChild(0); // reset index so it prints properly GrammarAST newFirstChild = (GrammarAST) altAST.getChild(0); altAST.setTokenStartIndex(newFirstChild.getTokenStartIndex()); } }
/** Return a reference to the first node (depth-first) that has * token type ttype. Assume 'this' is a root node; don't visit siblings * of root. Return null if no node found with ttype. */ public GrammarAST findFirstType(int ttype) { // check this node (the root) first if ( this.getType()==ttype ) { return this; } // else check children GrammarAST child = (GrammarAST)this.getFirstChild(); while ( child!=null ) { GrammarAST result = child.findFirstType(ttype); if ( result!=null ) { return result; } child = (GrammarAST)child.getNextSibling(); } return null; }
if (blk.getType() != BLOCK) { blk = (GrammarAST)blk.getChild(0); GrammarAST eob = blk.getLastChild(); if ( blk.getSetValue()!=null ) if ( blk.getSetValue()!=null ) if ( blk.getSetValue()!=null )
GrammarAST block2=(GrammarAST)((GrammarAST)retval.start).getFirstChildWithType(BLOCK); org.antlr.analysis.DFA dfa = block2.getLookaheadDFA(); Rule ruleDescr = grammar.getRule(((GrammarAST)retval.start).getChild(0).getText()); currentRuleName = ((GrammarAST)retval.start).getChild(0).getText(); match(input, Token.DOWN, null); if (state.failed) return retval; id=(GrammarAST)match(input,ID,FOLLOW_ID_in_rule363); if (state.failed) return retval; if ( state.backtracking==0 ) {assert currentRuleName == (id!=null?id.getText():null);} if ( state.backtracking==0 ) { description = grammar.grammarTreeToString((GrammarAST)((GrammarAST)retval.start).getFirstChildWithType(BLOCK), false); description = ((mod!=null?((GrammarAST)mod.start):null)!=null&&(mod!=null?((GrammarAST)mod.start):null).getText().equals(Grammar.FRAGMENT_RULE_MODIFIER)); retval.code.add("nakedBlock", naked);
/** Duplicate tree including siblings of root. */ public static GrammarAST dupListNoActions(GrammarAST t, GrammarAST parent) { GrammarAST result = dupTreeNoActions(t, parent); // if t == null, then result==null GrammarAST nt = result; while (t != null) { // for each sibling of the root t = (GrammarAST)t.getNextSibling(); if ( t!=null && t.getType()==ANTLRParser.ACTION ) { continue; } GrammarAST d = dupTreeNoActions(t, parent); if ( d!=null ) { if ( nt!=null ) { nt.setNextSibling(d); // dup each subtree, building new tree } nt = d; } } return result; }
/**Duplicate a tree, assuming this is a root node of a tree-- * duplicate that node and what's below; ignore siblings of root node. */ public static GrammarAST dupTreeNoActions(GrammarAST t, GrammarAST parent) { if ( t==null ) { return null; } int ttype = t.getType(); if ( ttype==ANTLRParser.REWRITE ) { return null; } if ( ttype==ANTLRParser.BANG || ttype==ANTLRParser.ROOT ) { // return x from ^(ROOT x) return (GrammarAST)dupListNoActions((GrammarAST)t.getFirstChild(), t); } /* DOH! Must allow labels for sem preds if ( (ttype==ANTLRParser.ASSIGN||ttype==ANTLRParser.PLUS_ASSIGN) && (parent==null||parent.getType()!=ANTLRParser.OPTIONS) ) { return dupTreeNoActions(t.getChild(1), t); // return x from ^(ASSIGN label x) } */ GrammarAST result = dup(t); // make copy of root // copy all children of root. GrammarAST kids = dupListNoActions((GrammarAST)t.getFirstChild(), t); result.setFirstChild(kids); return result; }
public void setTreeEnclosingRuleNameDeeply(String rname) { GrammarAST t = this; t.enclosingRuleName = rname; t = t.getChild(0); while (t != null) { // for each sibling of the root t.setTreeEnclosingRuleNameDeeply(rname); t = (GrammarAST)t.getNextSibling(); } }
/**Duplicate a tree, assuming this is a root node of a tree-- * duplicate that node and what's below; ignore siblings of root node. */ public static GrammarAST dupTreeNoActions(GrammarAST t, GrammarAST parent) { if ( t==null ) { return null; } GrammarAST result = (GrammarAST)t.dupNode(); for (GrammarAST subchild : getChildrenForDupTree(t)) { result.addChild(dupTreeNoActions(subchild, result)); } return result; }
d.dfa.nfa.grammar.getDecisionBlockAST(d.dfa.decisionNumber); if ( blockAST!=null ) { String greedyS = (String)blockAST.getBlockOption("greedy"); if ( greedyS!=null && greedyS.equals("true") ) explicitlyGreedy = true; for (Integer altI : unreachableAlts) { GrammarAST decAST = dfa.getDecisionASTNode(); GrammarAST altAST = (GrammarAST)decAST.getChild(altI-1); GrammarAST delegatedTokensAlt = (GrammarAST)altAST.getFirstChildWithType(ANTLRParser.DOT); if ( delegatedTokensAlt !=null ) { isInheritedTokensRule = true; null, dfa.nfa.grammar.name, delegatedTokensAlt.getChild(0).getText());
while ( p.getType()!=ANTLRParser.LEXER_GRAMMAR ) { p = (GrammarAST)p.getNextSibling(); p.addChild(parser.getAST());
public void _findAllType(int ttype, List<GrammarAST> nodes) { // check this node (the root) first if ( this.getType()==ttype ) nodes.add(this); // check children for (int i = 0; i < getChildCount(); i++){ GrammarAST child = (GrammarAST)getChild(i); child._findAllType(ttype, nodes); } }
public static GrammarAST dup(Tree t) { if ( t==null ) { return null; } GrammarAST dup_t = new GrammarAST(); dup_t.initialize(t); return dup_t; }
private void updateLineColumnToLookaheadDFAMap(DFA lookaheadDFA) { GrammarAST decisionAST = nfa.grammar.getDecisionBlockAST(lookaheadDFA.decisionNumber); int line = decisionAST.getLine(); int col = decisionAST.getCharPositionInLine(); lineColumnToLookaheadDFAMap.put(new StringBuffer().append(line).append(":") .append(col).toString(), lookaheadDFA); }
GrammarAST b = (GrammarAST)((GrammarAST)retval.start).getChild(0); GrammarAST eob = b.getLastChild(); // loops will use EOB DFA if ( state.backtracking==0 ) { dfa = ((GrammarAST)retval.start).getLookaheadDFA(); } pushFollow(FOLLOW_block_in_ebnf1261); blk=block("block", dfa, label); if ( state.backtracking==0 ) { dfa = ((GrammarAST)retval.start).getLookaheadDFA(); } match(input,OPTIONAL,FOLLOW_OPTIONAL_in_ebnf1280); if (state.failed) return retval; match(input, Token.DOWN, null); if (state.failed) return retval; if ( state.backtracking==0 ) { dfa = eob.getLookaheadDFA(); } match(input,CLOSURE,FOLLOW_CLOSURE_in_ebnf1305); if (state.failed) return retval; match(input, Token.DOWN, null); if (state.failed) return retval; if ( state.backtracking==0 ) { dfa = eob.getLookaheadDFA(); } match(input,POSITIVE_CLOSURE,FOLLOW_POSITIVE_CLOSURE_in_ebnf1330); if (state.failed) return retval; match(input, Token.DOWN, null); if (state.failed) return retval;
Rule r = grammar.getRule(currentRuleName); if ( grammar.buildAST() && !r.hasRewrite(outerAltNum) ) { rootSuffix = new GrammarAST(ROOT,"ROOT"); retval.code.addAggr("root.{el,line,pos}", (el!=null?((CodeGenTreeWalker.element_return)el).code:null), (el!=null?((GrammarAST)el.start):null).getLine(), (el!=null?((GrammarAST)el.start):null).getCharPositionInLine() + 1 ); retval.code.addAggr("actionsAfterRoot.{el,line,pos}", (act!=null?((CodeGenTreeWalker.element_action_return)act).code:null), (act!=null?((GrammarAST)act.start):null).getLine(), (act!=null?((GrammarAST)act.start):null).getCharPositionInLine() + 1 ); retval.code.addAggr("children.{el,line,pos}", (el!=null?((CodeGenTreeWalker.element_return)el).code:null), (el!=null?((GrammarAST)el.start):null).getLine(), (el!=null?((GrammarAST)el.start):null).getCharPositionInLine() + 1 );