/// <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 <= 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); }
/// <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(ref 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(); } }
/// <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); }
public virtual void resetState() { traceDepth = 0; returnAST = null; retTree_ = null; inputState.reset(); }
/*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); } }
/*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> /// 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); }
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); } }
/*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 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(""); } }
/// <summary> /// Make an AST the root of current AST. /// </summary> /// <param name="currentAST"></param> /// <param name="root"></param> public virtual void makeASTRoot(ref 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> /// 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); }
/// <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); }
/// <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 = createFromAST(aNode); newNode.initialize(aNode); } return(newNode); }
// 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_; }
// 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_; }
/*Walk the tree looking for all exact subtree matches. Return * an IEnumerator that lets the caller walk the list * of subtree roots found herein. */ public virtual IEnumerator findAll(AST target) { ArrayList roots = new ArrayList(10); //AST sibling; // the empty tree cannot result in an enumeration if (target == null) { return(null); } doWorkForFindAll(roots, target, false); // find all matches recursively return(roots.GetEnumerator()); }
/*Walk the tree looking for all subtrees. Return * an IEnumerator that lets the caller walk the list * of subtree roots found herein. */ public virtual IEnumerator findAllPartial(AST sub) { ArrayList roots = new ArrayList(10); //AST sibling; // the empty tree cannot result in an enumeration if (sub == null) { return(null); } doWorkForFindAll(roots, sub, true); // find all matches recursively return(roots.GetEnumerator()); }
private AST createFromNodeTypeObject(Type nodeTypeObject) { AST newNode = null; try { newNode = (AST)Activator.CreateInstance(nodeTypeObject); if (newNode == null) { throw new ArgumentException("Unable to create AST Node Type: '" + nodeTypeObject.FullName + "'"); } } catch (Exception ex) { throw new ArgumentException("Unable to create AST Node Type: '" + nodeTypeObject.FullName + "'", ex); } return(newNode); }
/// <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(params 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); }
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); } } }
public virtual string ToStringTree() { AST t = this; string ts = ""; if (t.getFirstChild() != null) { ts += " ("; } ts += " " + this.ToString(); if (t.getFirstChild() != null) { ts += ((BaseAST)t.getFirstChild()).ToStringList(); } if (t.getFirstChild() != null) { ts += " )"; } return(ts); }
private AST createFromNodeName(string nodeTypeName) { AST newNode = null; ASTNodeCreator creator = (ASTNodeCreator)typename2creator_[nodeTypeName]; if (creator != null) { newNode = creator.Create(); if (newNode == null) { throw new ArgumentException("Unable to create AST Node Type: '" + nodeTypeName + "'"); } } else { newNode = createFromNodeTypeObject(loadNodeTypeObject(nodeTypeName)); } return(newNode); }
private AST createFromAST(AST node) { AST newNode = null; Type nodeAsTypeObj = node.GetType(); ASTNodeCreator creator = (ASTNodeCreator)typename2creator_[nodeAsTypeObj.FullName]; if (creator != null) { newNode = creator.Create(); if (newNode == null) { throw new ArgumentException("Unable to create AST Node Type: '" + nodeAsTypeObj.FullName + "'"); } } else { newNode = createFromNodeTypeObject(nodeAsTypeObj); } return(newNode); }
/*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; } }
/*Is tree rooted at 'this' equal to 't'? The siblings * of 'this' are ignored. */ public virtual bool EqualsTree(AST t) { // check roots first. if (!this.Equals(t)) { return(false); } // if roots match, do full list match test on children. if (this.getFirstChild() != null) { if (!this.getFirstChild().EqualsList(t.getFirstChild())) { return(false); } } else if (t.getFirstChild() != null) { return(false); } return(true); }
public virtual void xmlSerialize(TextWriter outWriter) { // print out this node and all siblings for (AST node = this; node != null; node = node.getNextSibling()) { if (node.getFirstChild() == null) { // print guts (class name, attributes) ((BaseAST)node).xmlSerializeNode(outWriter); } else { ((BaseAST)node).xmlSerializeRootOpen(outWriter); // print children ((BaseAST)node.getFirstChild()).xmlSerialize(outWriter); // print end tag ((BaseAST)node).xmlSerializeRootClose(outWriter); } } }
/*Is t an exact structural and equals() match of this tree. The * 'this' reference is considered the start of a sibling list. */ public virtual bool EqualsList(AST t) { AST sibling; // the empty tree is not a match of any non-null tree. if (t == null) { return(false); } // Otherwise, start walking sibling lists. First mismatch, return false. for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling()) { // as a quick optimization, check roots first. if (!sibling.Equals(t)) { return(false); } // if roots match, do full list match test on children. if (sibling.getFirstChild() != null) { if (!sibling.getFirstChild().EqualsList(t.getFirstChild())) { return(false); } } else if (t.getFirstChild() != null) { return(false); } } if (sibling == null && t == null) { return(true); } // one sibling list has more than the other return(false); }