/*Is 'sub' a subtree of this list? * The siblings of the root are NOT ignored. */ public virtual bool EqualsListPartial(AST sub) { AST sibling; // the empty tree is always a subset of any tree. if (sub == null) { return(true); } // Otherwise, start walking sibling lists. First mismatch, return false. for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) { // as a quick optimization, check roots first. if (!sibling.Equals(sub)) { return(false); } // if roots match, do partial list match test on children. if (sibling.getFirstChild() != null) { if (!sibling.getFirstChild().EqualsListPartial(sub.getFirstChild())) { return(false); } } } if (sibling == null && sub != null) { // nothing left to match in this tree, but subtree has more return(false); } // either both are null or sibling has more, but subtree doesn't return(true); }
/// <summary> /// Add a child to the current AST /// </summary> /// <param name="currentAST">The AST to add a child to</param> /// <param name="child">The child AST to be added</param> public override void addASTChild(ref ASTPair currentAST, AST child) { if (child != null) { if (currentAST.root == null) { // Make new child the current root currentAST.root = child; ((ASTNode)child).setParent(null); } else { ((ASTNode)child).setParent((ASTNode)currentAST.root); if (currentAST.child == null) { // Add new child to current root currentAST.root.setFirstChild(child); ((ASTNode)child).setPreviousSibling(null); } else { currentAST.child.setNextSibling(child); ((ASTNode)child).setPreviousSibling((ASTNode)currentAST.child); } } // Make new child the current child currentAST.child = child; currentAST.advanceChildToEnd(); } }
public static void doTreeAction(string f, AST t, string[] tokenNames) { if (t == null) return ; if (showTree) { BaseAST.setVerboseStringConversion(true, tokenNames); ASTFactory factory = new ASTFactory(); AST r = factory.create(0, "AST ROOT"); r.setFirstChild(t); ASTFrame frame = new ASTFrame("Java AST", r); frame.ShowDialog(); //frame.Visible = true; // System.out.println(t.toStringList()); } JavaTreeParser tparse = new JavaTreeParser(); try { tparse.compilationUnit(t); // System.out.println("successful walk of result AST for "+f); } catch (RecognitionException e) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); } }
/// <summary> /// Do a step-first walk, building up a buffer of tokens until /// you've reached a particular step and print out any rule subroots /// insteads of descending. /// </summary> /// <param name="buf">derivation buffer</param> /// <param name="step">derivation steps</param> /// <returns></returns> protected internal override int getLeftmostDerivation(StringBuilder buf, int step) { int numReplacements = 0; if (step <= 0) { buf.Append(' '); buf.Append(ToString()); return(numReplacements); } AST child = getFirstChild(); numReplacements = 1; // walk child printing them out, descending into at most one while (child != null) { if ((numReplacements >= step) || (child is ParseTreeToken)) { buf.Append(' '); buf.Append(child.ToString()); } else { // descend for at least one more derivation; update count int remainingReplacements = step - numReplacements; int n = ((ParseTree)child).getLeftmostDerivation(buf, remainingReplacements); numReplacements += n; } child = child.getNextSibling(); } return(numReplacements); }
public virtual void resetState() { traceDepth = 0; returnAST = null; retTree_ = null; inputState.reset(); }
/// <summary> /// Creates and initializes a new AST node using the specified Token Type ID. /// The <see cref="System.Type"/> used for creating this new AST node is /// determined by the following: /// <list type="bullet"> /// <item>the current TokenTypeID-to-ASTNodeType mapping (if any) or,</item> /// <item>the <see cref="defaultASTNodeTypeObject_"/> otherwise</item> /// </list> /// </summary> /// <param name="type">Token type ID to be used to create new AST Node.</param> /// <returns>An initialized AST node object.</returns> public virtual AST create(int type) { AST newNode = createFromNodeType(type); newNode.initialize(type, ""); return(newNode); }
/// <summary> /// Add a child to the current AST /// </summary> /// <param name="currentAST">The AST to add a child to</param> /// <param name="child">The child AST to be added</param> public virtual void addASTChild(ASTPair currentAST, AST child) { if (child != null) { if (currentAST.root == null) { // Make new child the current root currentAST.root = child; } else { if (currentAST.child == null) { // Add new child to current root currentAST.root.setFirstChild(child); } else { currentAST.child.setNextSibling(child); } } // Make new child the current child currentAST.child = child; currentAST.advanceChildToEnd(); } }
public bool testDefaultCreate() { factory = new ASTFactory(); AST t = factory.create(); return(checkNode(t, typeof(CommonAST), Token.INVALID_TYPE)); }
/// <summary> /// Creates a new AST node using the specified AST Node Type name. Once created, /// the new AST node is initialized with the specified Token type ID and string. /// The <see cref="System.Type"/> used for creating this new AST node is /// determined solely by <c>ASTNodeTypeName</c>. /// The AST Node type must have a default/parameterless constructor. /// </summary> /// <param name="type">Token type ID to be used to create new AST Node.</param> /// <param name="txt">Text for initializing the new AST Node.</param> /// <param name="ASTNodeTypeName">Fully qualified name of the Type to be used for creating the new AST Node.</param> /// <returns>An initialized AST node object.</returns> public virtual AST create(int type, string txt, string ASTNodeTypeName) { AST newNode = createFromNodeName(ASTNodeTypeName); newNode.initialize(type, txt); return(newNode); }
private AST createFromNodeType(int nodeTypeIndex) { Debug.Assert((nodeTypeIndex >= 0) && (nodeTypeIndex <= nodeTypeObjectList_.Length), "Invalid AST node type!"); AST newNode = null; ASTNodeCreator creator = (ASTNodeCreator)typename2creator_[nodeTypeIndex.ToString()]; if (creator != null) { newNode = creator.Create(); } else { Type nodeTypeObject = nodeTypeObjectList_[nodeTypeIndex]; if (nodeTypeObject == null) { if (defaultCreator_ == null) { newNode = createFromNodeTypeObject(defaultASTNodeTypeObject_); } else { newNode = defaultCreator_.Create(); } } else { newNode = createFromNodeTypeObject(nodeTypeObject); } } return(newNode); }
/// <summary> /// Creates a new AST node using the specified AST Node Type name. /// </summary> /// <param name="tok">Token instance to be used to initialize the new AST Node.</param> /// <param name="ASTNodeTypeName"> /// Fully qualified name of the Type to be used for creating the new AST Node. /// </param> /// <returns>A newly created and initialized AST node object.</returns> /// <remarks> /// Once created, the new AST node is initialized with the specified Token /// instance. The <see cref="System.Type"/> used for creating this new AST /// node is determined solely by <c>ASTNodeTypeName</c>. /// <para>The AST Node type must have a default/parameterless constructor.</para> /// </remarks> public virtual AST create(IToken tok, string ASTNodeTypeName) { AST newNode = createFromNodeName(ASTNodeTypeName); newNode.initialize(tok); return(newNode); }
private AST createFromNodeType(int nodeTypeIndex) { Debug.Assert((nodeTypeIndex >= 0) && (nodeTypeIndex <= heteroList_.Length), "Invalid AST node type!"); AST newNode = null; FactoryEntry entry = heteroList_[nodeTypeIndex]; if ((entry != null) && (entry.Creator != null)) { newNode = entry.Creator.Create(); } else { if ((entry == null) || (entry.NodeTypeObject == null)) { if (defaultCreator_ == null) { newNode = createFromNodeTypeObject(defaultASTNodeTypeObject_); } else { newNode = defaultCreator_.Create(); } } else { newNode = createFromNodeTypeObject(entry.NodeTypeObject); } } return(newNode); }
public static void doTreeAction(string f, AST t, string[] tokenNames) { if (t == null) { return; } if (showTree) { BaseAST.setVerboseStringConversion(true, tokenNames); ASTFactory factory = new ASTFactory(); AST r = factory.create(0, "AST ROOT"); r.setFirstChild(t); ASTFrame frame = new ASTFrame("Java AST", r); frame.ShowDialog(); //frame.Visible = true; // System.out.println(t.toStringList()); } JavaTreeParser tparse = new JavaTreeParser(); JavaRecognizer.initializeASTFactory(tparse.getASTFactory()); try { tparse.compilationUnit(t); // System.out.println("successful walk of result AST for "+f); } catch (RecognitionException e) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); } }
/*Make sure current lookahead symbol matches the given set * Throw an exception upon mismatch, which is catch by either the * error handler or by the syntactic predicate. */ public virtual void match(AST t, BitSet b) { if (t == null || t == ASTNULL || !b.member(t.Type)) { throw new MismatchedTokenException(getTokenNames(), t, b, false); } }
public void lstmt() //throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); AST lstmt_AST = null; try { // for error handling if ((tokenSet_3_.member(LA(1))) && (tokenSet_5_.member(LA(2)))) { stmt(); astFactory.addASTChild(ref currentAST, returnAST); lstmt_AST = (AST)currentAST.root; lstmt_AST = (AST)astFactory.make(astFactory.create(STMT, "STMT"), lstmt_AST); currentAST.root = lstmt_AST; if ((null != lstmt_AST) && (null != lstmt_AST.getFirstChild())) { currentAST.child = lstmt_AST.getFirstChild(); } else { currentAST.child = lstmt_AST; } currentAST.advanceChildToEnd(); lstmt_AST = currentAST.root; } else if ((LA(1) == ID) && (LA(2) == COLON)) { AST tmp14_AST = null; tmp14_AST = astFactory.create(LT(1)); astFactory.addASTChild(ref currentAST, tmp14_AST); match(ID); match(COLON); stmt(); astFactory.addASTChild(ref currentAST, returnAST); lstmt_AST = (AST)currentAST.root; lstmt_AST = (AST)astFactory.make(astFactory.create(LSTMT, "LSTMT"), lstmt_AST); currentAST.root = lstmt_AST; if ((null != lstmt_AST) && (null != lstmt_AST.getFirstChild())) { currentAST.child = lstmt_AST.getFirstChild(); } else { currentAST.child = lstmt_AST; } currentAST.advanceChildToEnd(); lstmt_AST = currentAST.root; } else { throw new NoViableAltException(LT(1), getFilename()); } } catch (RecognitionException ex) { reportError(ex); recover(ex, tokenSet_6_); } returnAST = lstmt_AST; }
protected internal virtual void match(AST t, int ttype) { //System.out.println("match("+ttype+"); cursor is "+t); if (t == null || t == ASTNULL || t.Type != ttype) { throw new MismatchedTokenException(getTokenNames(), t, ttype, false); } }
public bool testSpecificHomoCreate() { factory = new ASTFactory(); factory.setASTNodeType("MyAST"); AST t = factory.create(); factory.setASTNodeType("antlr.CommonAST"); // put it back return(checkNode(t, typeof(MyAST), Token.INVALID_TYPE)); }
public void decider() //throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); AST decider_AST = null; try { // for error handling { switch (LA(1)) { case QMARK: { AST tmp60_AST = null; tmp60_AST = astFactory.create(LT(1)); astFactory.addASTChild(ref currentAST, tmp60_AST); match(QMARK); break; } case ID: case LPAREN: case EMARK: case CONST: { expr(); astFactory.addASTChild(ref currentAST, returnAST); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } decider_AST = (AST)currentAST.root; decider_AST = (AST)astFactory.make(astFactory.create(DECIDER, "DECIDER"), decider_AST); currentAST.root = decider_AST; if ((null != decider_AST) && (null != decider_AST.getFirstChild())) { currentAST.child = decider_AST.getFirstChild(); } else { currentAST.child = decider_AST; } currentAST.advanceChildToEnd(); decider_AST = currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex, tokenSet_10_); } returnAST = decider_AST; }
public AST child; // current child to which siblings are added /*Make sure that child is the last sibling */ public void advanceChildToEnd() { if (child != null) { while (child.getNextSibling() != null) { child = child.getNextSibling(); } } }
public ASTFrame(string title, AST rootAST) : this() { this.Text = title; JTreeASTPanel treePanel = new JTreeASTPanel(new TreeViewEventHandler(tree_AfterSelect), rootAST); this.Controls.Add(treePanel); treePanel.Location= new Point(5, 5); treePanel.Dock=DockStyle.Fill; treePanel.Anchor=AnchorStyles.Top|AnchorStyles.Left; }
/*Is node t equal to this in terms of token type and text? */ public virtual bool Equals(AST t) { if (t == null) { return(false); } return((Object.Equals(this.getText(), t.getText())) && (this.Type == t.Type)); }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node. Ignore it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupTree(AST t) { AST result = dup(t); // make copy of root // copy all children of root. if (t != null) { result.setFirstChild(dupList(t.getFirstChild())); } return(result); }
public ASTFrame(string title, AST rootAST) : this() { this.Text = title; JTreeASTPanel treePanel = new JTreeASTPanel(new TreeViewEventHandler(tree_AfterSelect), rootAST); this.Controls.Add(treePanel); treePanel.Location = new Point(5, 5); treePanel.Dock = DockStyle.Fill; treePanel.Anchor = AnchorStyles.Top | AnchorStyles.Left; }
public void visit(AST node) { // Flatten this level of the tree if it has no children bool flatten = /*true*/ false; AST node2; for (node2 = node; node2 != null; node2 = node2.getNextSibling()) { if (node2.getFirstChild() != null) { flatten = false; break; } } for (node2 = node; node2 != null; node2 = node2.getNextSibling()) { if (!flatten || node2 == node) { tabs(); } if (node2.getText() == null) { Console.Out.Write("nil"); } else { Console.Out.Write(node2.getText()); } Console.Out.Write(" [" + node2.Type + "] "); if (flatten) { Console.Out.Write(" "); } else { Console.Out.WriteLine(""); } if (node2.getFirstChild() != null) { level++; visit(node2.getFirstChild()); level--; } } if (flatten) { Console.Out.WriteLine(""); } }
public void sseq() //throws RecognitionException, TokenStreamException { returnAST = null; ASTPair currentAST = new ASTPair(); AST sseq_AST = null; try { // for error handling { // ( ... )+ int _cnt18 = 0; for (;;) { if ((tokenSet_3_.member(LA(1)))) { lstmt(); astFactory.addASTChild(ref currentAST, returnAST); } else { if (_cnt18 >= 1) { goto _loop18_breakloop; } else { throw new NoViableAltException(LT(1), getFilename());; } } _cnt18++; } _loop18_breakloop :; } // ( ... )+ sseq_AST = (AST)currentAST.root; sseq_AST = (AST)astFactory.make(astFactory.create(SSEQ, "SSEQ"), sseq_AST); currentAST.root = sseq_AST; if ((null != sseq_AST) && (null != sseq_AST.getFirstChild())) { currentAST.child = sseq_AST.getFirstChild(); } else { currentAST.child = sseq_AST; } currentAST.advanceChildToEnd(); sseq_AST = currentAST.root; } catch (RecognitionException ex) { reportError(ex); recover(ex, tokenSet_4_); } returnAST = sseq_AST; }
/// <summary> /// Make an AST the root of current AST. /// </summary> /// <param name="currentAST"></param> /// <param name="root"></param> public virtual void makeASTRoot(ASTPair currentAST, AST root) { if (root != null) { // Add the current root as a child of new root root.addChild(currentAST.root); // The new current child is the last sibling of the old root currentAST.child = currentAST.root; currentAST.advanceChildToEnd(); // Set the new root currentAST.root = root; } }
/// <summary> /// Make an AST the root of current AST. /// </summary> /// <param name="currentAST"></param> /// <param name="root"></param> public override void makeASTRoot(ref ASTPair currentAST, AST root) { if (root != null) { // Add the current root as a child of new root ((ASTNode)root).addChildEx((ASTNode)currentAST.root); // The new current child is the last sibling of the old root currentAST.child = currentAST.root; currentAST.advanceChildToEnd(); // Set the new root currentAST.root = root; } }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node. Ignore it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public override AST dupTree(AST t) { AST result = dup(t); // make copy of root // copy all children of root. if (t != null) { AST d = dupList(t.getFirstChild()); result.setFirstChild(d); ((ASTNode)d).setParent((ASTNode)result); } return(result); }
public bool testNodeDup() { factory = new ASTFactory(); factory.setMaxNodeType(49); AST t = factory.create(); bool a = t.Equals(factory.dup(t)); bool b = !t.Equals(null); AST u = factory.create(49, "", "ASTType49"); bool c = checkNode(factory.dup(u), typeof(ASTType49), 49); bool d = u.Equals(factory.dup(u)); return(a && b && c && d); }
/// <summary> /// Returns a copy of the specified AST Node instance. The copy is obtained by /// using the <see cref="ICloneable"/> method Clone(). /// </summary> /// <param name="t">AST Node to copy.</param> /// <returns>An AST Node (or null if <c>t</c> is null).</returns> public virtual AST dup(AST t) { // The Java version is implemented using code like this: if (t == null) { return(null); } AST dup_edNode = createFromAST(t); dup_edNode.initialize(t); return(dup_edNode); }
public bool testDynamicHeteroCreate() { factory = new ASTFactory(); factory.setMaxNodeType(55); factory.setTokenTypeASTNodeType(49, "ASTType49"); AST t = factory.create(49); bool a = checkNode(t, typeof(ASTType49), 49); AST u = factory.create(55); bool b = checkNode(u, typeof(CommonAST), 55); AST v = factory.create(49, "", "MyAST"); bool c = checkNode(v, typeof(MyAST), 49); return(a && b && c); }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupList(AST t) { AST result = dupTree(t); // if t == null, then result==null AST nt = result; while (t != null) { // for each sibling of the root t = t.getNextSibling(); nt.setNextSibling(dupTree(t)); // dup each subtree, building new tree nt = nt.getNextSibling(); } return(result); }
// Expected token / not token public MismatchedTokenException(string[] tokenNames_, AST node_, int expecting_, bool matchNot) : base("Mismatched Token", "<AST>", -1, -1) { tokenNames = tokenNames_; node = node_; if (node_ == null) { tokenText = "<empty tree>"; } else { tokenText = node_.ToString(); } mismatchType = matchNot ? TokenTypeEnum.NotTokenType : TokenTypeEnum.TokenType; expecting = expecting_; }
/*Add a node to the end of the child list for this node */ public virtual void addChild(AST node) { if (node == null) return ; BaseAST t = this.down; if (t != null) { while (t.right != null) { t = t.right; } t.right = (BaseAST) node; } else { this.down = (BaseAST) node; } }
private void doWorkForFindAll(ArrayList v, AST target, bool partialMatch) { AST sibling; // Start walking sibling lists, looking for matches. //siblingWalk: for (sibling = this; sibling != null; sibling = sibling.getNextSibling()) { if ((partialMatch && sibling.EqualsTreePartial(target)) || (!partialMatch && sibling.EqualsTree(target))) { v.Add(sibling); } // regardless of match or not, check any children for matches if (sibling.getFirstChild() != null) { ((BaseAST) sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch); } } }
//throws RecognitionException /** create a new list of expressions as a new multi-value attribute */ public Object list(AST _t) { Object value=null; antlr.stringtemplate.language.StringTemplateAST list_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object e = null; IList elements = new ArrayList(); value = new CatIterator(elements); try { // for error handling AST __t6 = _t; antlr.stringtemplate.language.StringTemplateAST tmp14_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,LIST); _t = _t.getFirstChild(); { // ( ... )+ int _cnt8=0; for (;;) { if (_t == null) _t = ASTNULL; if ((tokenSet_0_.member(_t.Type))) { e=expr(_t); _t = retTree_; if ( e!=null ) { e = ASTExpr.convertAnythingToIterator(e); elements.Add(e); } } else { if (_cnt8 >= 1) { goto _loop8_breakloop; } else { throw new NoViableAltException(_t);; } } _cnt8++; } _loop8_breakloop: ; } // ( ... )+ _t = __t6; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
/// <summary> /// Creates and initializes a new AST node using the specified AST Node instance. /// the new AST node is initialized with the specified Token type ID and string. /// The <see cref="System.Type"/> used for creating this new AST node is /// determined solely by <c>aNode</c>. /// The AST Node type must have a default/parameterless constructor. /// </summary> /// <param name="aNode">AST Node instance to be used for creating the new AST Node.</param> /// <returns>An initialized AST node object.</returns> public virtual AST create(AST aNode) { AST newNode; if (aNode == null) newNode = null; else { newNode = createFromNodeTypeObject(aNode.GetType()); newNode.initialize(aNode); } return newNode; }
/// <summary> /// Returns a copy of the specified AST Node instance. The copy is obtained by /// using the <see cref="ICloneable"/> method Clone(). /// </summary> /// <param name="t">AST Node to copy.</param> /// <returns>An AST Node (or null if <c>t</c> is null).</returns> public virtual AST dup(AST t) { // The Java version is implemented using code like this: if (t == null) return null; AST dup_edNode = createFromNodeTypeObject(t.GetType()); dup_edNode.initialize(t); return dup_edNode; //return (AST)((t == null) ? null : t.Clone()); }
//throws RecognitionException public Object singleFunctionArg(AST _t) { Object value=null; antlr.stringtemplate.language.StringTemplateAST singleFunctionArg_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; try { // for error handling AST __t24 = _t; antlr.stringtemplate.language.StringTemplateAST tmp17_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,SINGLEVALUEARG); _t = _t.getFirstChild(); value=expr(_t); _t = retTree_; _t = __t24; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
private void reset() { root = null; child = null; }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupList(AST t) { AST result = dupTree(t); // if t == null, then result==null AST nt = result; while (t != null) { // for each sibling of the root t = t.getNextSibling(); nt.setNextSibling(dupTree(t)); // dup each subtree, building new tree nt = nt.getNextSibling(); } return result; }
//throws RecognitionException public boolean expression(AST _t) { boolean e ; AST expression_AST_in = (AST)_t; boolean a, b; String l, r; e = false; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case AND: { AST __t43 = _t; AST tmp21_AST_in = _t; match(_t,AND); _t = _t.getFirstChild(); a=expression(_t); _t = retTree_; b=expression(_t); _t = retTree_; e = a && b; _t = __t43; _t = _t.getNextSibling(); break; } case OR: { AST __t44 = _t; AST tmp22_AST_in = _t; match(_t,OR); _t = _t.getFirstChild(); a=expression(_t); _t = retTree_; b=expression(_t); _t = retTree_; e = a || b; _t = __t44; _t = _t.getNextSibling(); break; } case EQUAL: { AST __t45 = _t; AST tmp23_AST_in = _t; match(_t,EQUAL); _t = _t.getFirstChild(); l=value(_t); _t = retTree_; r=value(_t); _t = retTree_; e = l.equals(r); _t = __t45; _t = _t.getNextSibling(); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return e ; }
//throws RecognitionException public void singleTemplateArg(AST _t, StringTemplate embedded, IDictionary argumentContext ) { antlr.stringtemplate.language.StringTemplateAST singleTemplateArg_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object e = null; try { // for error handling AST __t41 = _t; antlr.stringtemplate.language.StringTemplateAST tmp22_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,SINGLEVALUEARG); _t = _t.getFirstChild(); e=expr(_t); _t = retTree_; _t = __t41; _t = _t.getNextSibling(); if ( e!=null ) { String soleArgName = null; // find the sole defined formal argument for embedded bool error = false; IDictionary formalArgs = embedded.getFormalArguments(); if ( formalArgs!=null ) { ICollection argNames = formalArgs.Keys; if ( argNames.Count==1 ) { string[] argNamesArray = new string[argNames.Count]; argNames.CopyTo(argNamesArray,0); soleArgName = argNamesArray[0]; //System.out.println("sole formal arg of "+embedded.getName()+" is "+soleArgName); } else { error=true; } } else { error=true; } if ( error ) { self.error("template "+embedded.getName()+ " must have exactly one formal arg in template context "+ self.getEnclosingInstanceStackString()); } else { self.rawSetArgumentAttribute(embedded,argumentContext,soleArgName,e); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; }
public virtual void traceOut(string rname, AST t) { traceIndent(); Console.Out.WriteLine("< " + rname + "(" + ((t != null) ? t.ToString() : "null") + ")" + ((inputState.guessing > 0) ? " [guessing]" : "")); traceDepth--; }
//throws RecognitionException public bool ifCondition(AST _t) { bool value=false; antlr.stringtemplate.language.StringTemplateAST ifCondition_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; Object a=null, b=null; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case APPLY: case MULTI_APPLY: case INCLUDE: case VALUE: case FUNCTION: case LIST: case PLUS: case DOT: case ID: case ANONYMOUS_TEMPLATE: case STRING: case INT: { a=ifAtom(_t); _t = retTree_; value = chunk.testAttributeTrue(a); break; } case NOT: { AST __t30 = _t; antlr.stringtemplate.language.StringTemplateAST tmp18_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,NOT); _t = _t.getFirstChild(); a=ifAtom(_t); _t = retTree_; _t = __t30; _t = _t.getNextSibling(); value = !chunk.testAttributeTrue(a); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
/// <summary> /// Duplicate AST Node tree rooted at specified AST node. Ignore it's siblings. /// </summary> /// <param name="t">Root of AST Node tree.</param> /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns> public virtual AST dupTree(AST t) { AST result = dup(t); // make copy of root // copy all children of root. if (t != null) { result.setFirstChild(dupList(t.getFirstChild())); } return result; }
/// <summary> /// Make a tree from a list of nodes. The first element in the /// array is the root. If the root is null, then the tree is /// a simple list not a tree. Handles null children nodes correctly. /// For example, build(a, b, null, c) yields tree (a b c). build(null,a,b) /// yields tree (nil a b). /// </summary> /// <param name="nodes">List of Nodes.</param> /// <returns>AST Node tree.</returns> public virtual AST make(AST[] nodes) { if (nodes == null || nodes.Length == 0) return null; AST root = nodes[0]; AST tail = null; if (root != null) { root.setFirstChild(null); // don't leave any old pointers set } // link in children; for (int i = 1; i < nodes.Length; i++) { if (nodes[i] == null) continue; // ignore null nodes if (root == null) { // Set the root and set it up for a flat list root = (tail = nodes[i]); } else if (tail == null) { root.setFirstChild(nodes[i]); tail = root.getFirstChild(); } else { tail.setNextSibling(nodes[i]); tail = tail.getNextSibling(); } // Chase tail to last sibling while (tail.getNextSibling() != null) { tail = tail.getNextSibling(); } } return root; }
// Expected BitSet / not BitSet public MismatchedTokenException(string[] tokenNames_, AST node_, BitSet set_, bool matchNot) : base("Mismatched Token", "<AST>", -1, -1) { tokenNames = tokenNames_; node = node_; if (node_ == null) { tokenText = "<empty tree>"; } else { tokenText = node_.ToString(); } mismatchType = matchNot ? TokenTypeEnum.NotSetType : TokenTypeEnum.SetType; bset = set_; }
//throws RecognitionException public void template(AST _t, ArrayList templatesToApply ) { antlr.stringtemplate.language.StringTemplateAST template_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; antlr.stringtemplate.language.StringTemplateAST t = null; antlr.stringtemplate.language.StringTemplateAST args = null; antlr.stringtemplate.language.StringTemplateAST anon = null; antlr.stringtemplate.language.StringTemplateAST args2 = null; IDictionary argumentContext = null; Object n = null; try { // for error handling AST __t26 = _t; antlr.stringtemplate.language.StringTemplateAST tmp15_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,TEMPLATE); _t = _t.getFirstChild(); { if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case ID: { t = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,ID); _t = _t.getNextSibling(); args = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; if (null == _t) throw new MismatchedTokenException(); _t = _t.getNextSibling(); String templateName = t.getText(); StringTemplateGroup group = self.getGroup(); StringTemplate embedded = group.getEmbeddedInstanceOf(self, templateName); if ( embedded!=null ) { embedded.setArgumentsAST(args); templatesToApply.Add(embedded); } break; } case ANONYMOUS_TEMPLATE: { anon = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,ANONYMOUS_TEMPLATE); _t = _t.getNextSibling(); StringTemplate anonymous = anon.getStringTemplate(); templatesToApply.Add(anonymous); break; } case VALUE: { AST __t28 = _t; antlr.stringtemplate.language.StringTemplateAST tmp16_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; match((AST)_t,VALUE); _t = _t.getFirstChild(); n=expr(_t); _t = retTree_; args2 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t; if (null == _t) throw new MismatchedTokenException(); _t = _t.getNextSibling(); StringTemplate embedded = null; if ( n!=null ) { String templateName = n.ToString(); StringTemplateGroup group = self.getGroup(); embedded = group.getEmbeddedInstanceOf(self, templateName); if ( embedded!=null ) { embedded.setArgumentsAST(args2); templatesToApply.Add(embedded); } } _t = __t28; _t = _t.getNextSibling(); break; } default: { throw new NoViableAltException(_t); } } } _t = __t26; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; }
public ASTExpr(StringTemplate enclosingTemplate, AST exprTree, IDictionary options) : base(enclosingTemplate) { this.exprTree = exprTree; this.options = options; }
//throws RecognitionException public Object ifAtom(AST _t) { Object value=null; antlr.stringtemplate.language.StringTemplateAST ifAtom_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t; try { // for error handling value=expr(_t); _t = retTree_; } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return value; }
//throws RecognitionException public String literal(AST _t) { String s ; AST literal_AST_in = (AST)_t; AST l = null; AST ql = null; s = null; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case LITERAL: { l = _t; match(_t,LITERAL); _t = _t.getNextSibling(); s = l.getText(); break; } case QLITERAL: { ql = _t; match(_t,QLITERAL); _t = _t.getNextSibling(); s = ql.getText(); break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return s ; }
//throws RecognitionException public String value(AST _t) { String s ; AST value_AST_in = (AST)_t; AST l = null; AST ql = null; AST v = null; String variable = null; s = null; try { // for error handling if (null == _t) _t = ASTNULL; switch ( _t.Type ) { case LITERAL: { l = _t; match(_t,LITERAL); _t = _t.getNextSibling(); s = l.getText(); break; } case QLITERAL: { ql = _t; match(_t,QLITERAL); _t = _t.getNextSibling(); s = ql.getText(); break; } case VAR: { AST tmp18_AST_in = _t; match(_t,VAR); _t = _t.getNextSibling(); AST tmp19_AST_in = _t; match(_t,LPAREN); _t = _t.getNextSibling(); v = _t; match(_t,LITERAL); _t = _t.getNextSibling(); AST tmp20_AST_in = _t; match(_t,RPAREN); _t = _t.getNextSibling(); variable = v.getText(); s = (String) environment.get(variable); if (s == null) { throw new RecognitionException("unrecognized variable " + variable); } break; } default: { throw new NoViableAltException(_t); } } } catch (RecognitionException ex) { reportError(ex); if (null != _t) { _t = _t.getNextSibling(); } } retTree_ = _t; return s ; }