diff options
Diffstat (limited to 'runtime/CSharp3/Sources/Antlr3.Runtime/Tree')
33 files changed, 0 insertions, 6633 deletions
diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/AntlrRuntime_BaseTreeDebugView.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/AntlrRuntime_BaseTreeDebugView.cs deleted file mode 100644 index 3346e36..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/AntlrRuntime_BaseTreeDebugView.cs +++ /dev/null @@ -1,60 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Diagnostics; - - internal sealed class AntlrRuntime_BaseTreeDebugView - { - private readonly BaseTree _tree; - - public AntlrRuntime_BaseTreeDebugView(BaseTree tree) - { - _tree = tree; - } - - [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public ITree[] Children - { - get - { - if (_tree == null || _tree.Children == null) - return null; - - ITree[] children = new ITree[_tree.Children.Count]; - _tree.Children.CopyTo(children, 0); - return children; - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/AstTreeRuleReturnScope`2.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/AstTreeRuleReturnScope`2.cs deleted file mode 100644 index cc8cb6f..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/AstTreeRuleReturnScope`2.cs +++ /dev/null @@ -1,60 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - public class AstTreeRuleReturnScope<TOutputTree, TInputTree> : TreeRuleReturnScope<TInputTree>, IAstRuleReturnScope<TOutputTree>, IAstRuleReturnScope - { - private TOutputTree _tree; - - public TOutputTree Tree - { - get - { - return _tree; - } - - set - { - _tree = value; - } - } - - object IAstRuleReturnScope.Tree - { - get - { - return Tree; - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs deleted file mode 100644 index 9327860..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTree.cs +++ /dev/null @@ -1,575 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Tunnel Vision Laboratories, LLC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System; - using System.Collections.Generic; - - using StringBuilder = System.Text.StringBuilder; - - /** <summary> - * A generic tree implementation with no payload. You must subclass to - * actually have any user data. ANTLR v3 uses a list of children approach - * instead of the child-sibling approach in v2. A flat tree (a list) is - * an empty node whose children represent the list. An empty, but - * non-null node is called "nil". - * </summary> - */ - [System.Serializable] - [System.Diagnostics.DebuggerTypeProxy(typeof(AntlrRuntime_BaseTreeDebugView))] - public abstract class BaseTree : ITree - { - private IList<ITree> _children; - - public BaseTree() - { - } - - /** <summary> - * Create a new node from an existing node does nothing for BaseTree - * as there are no fields other than the children list, which cannot - * be copied as the children are not considered part of this node. - * </summary> - */ - public BaseTree( ITree node ) - { - } - - /** <summary> - * Get the children internal List; note that if you directly mess with - * the list, do so at your own risk. - * </summary> - */ - public virtual IList<ITree> Children - { - get - { - return _children; - } - - private set - { - _children = value; - } - } - - #region ITree Members - - public virtual int ChildCount - { - get - { - if ( Children == null ) - return 0; - - return Children.Count; - } - } - - /** <summary>BaseTree doesn't track parent pointers.</summary> */ - public virtual ITree Parent - { - get - { - return null; - } - set - { - } - } - - /** <summary>BaseTree doesn't track child indexes.</summary> */ - public virtual int ChildIndex - { - get - { - return 0; - } - set - { - } - } - - public virtual bool IsNil - { - get - { - return false; - } - } - - public abstract int TokenStartIndex - { - get; - set; - } - - public abstract int TokenStopIndex - { - get; - set; - } - - public abstract int Type - { - get; - set; - } - - public abstract string Text - { - get; - set; - } - - public virtual int Line - { - get; - set; - } - - public virtual int CharPositionInLine - { - get; - set; - } - - #endregion - - public virtual ITree GetChild( int i ) - { - if (i < 0) - throw new ArgumentOutOfRangeException(); - - if ( Children == null || i >= Children.Count ) - return null; - - return Children[i]; - } - - public virtual ITree GetFirstChildWithType( int type ) - { - foreach ( ITree child in Children ) - { - if ( child.Type == type ) - return child; - } - - return null; - } - - /** <summary>Add t as child of this node.</summary> - * - * <remarks> - * Warning: if t has no children, but child does - * and child isNil then this routine moves children to t via - * t.children = child.children; i.e., without copying the array. - * </remarks> - */ - public virtual void AddChild( ITree t ) - { - //System.out.println("add child "+t.toStringTree()+" "+this.toStringTree()); - //System.out.println("existing children: "+children); - if ( t == null ) - { - return; // do nothing upon addChild(null) - } - if ( t.IsNil ) - { - // t is an empty node possibly with children - BaseTree childTree = t as BaseTree; - if ( childTree != null && this.Children != null && this.Children == childTree.Children ) - { - throw new Exception( "attempt to add child list to itself" ); - } - // just add all of childTree's children to this - if ( t.ChildCount > 0 ) - { - if ( this.Children != null || childTree == null ) - { - if ( this.Children == null ) - this.Children = CreateChildrenList(); - - // must copy, this has children already - int n = t.ChildCount; - for ( int i = 0; i < n; i++ ) - { - ITree c = t.GetChild( i ); - this.Children.Add( c ); - // handle double-link stuff for each child of nil root - c.Parent = this; - c.ChildIndex = Children.Count - 1; - } - } - else - { - // no children for this but t is a BaseTree with children; - // just set pointer call general freshener routine - this.Children = childTree.Children; - this.FreshenParentAndChildIndexes(); - } - } - } - else - { - // child is not nil (don't care about children) - if ( Children == null ) - { - Children = CreateChildrenList(); // create children list on demand - } - Children.Add( t ); - t.Parent = this; - t.ChildIndex = Children.Count - 1; - } - // System.out.println("now children are: "+children); - } - - /** <summary>Add all elements of kids list as children of this node</summary> */ - public virtual void AddChildren( IEnumerable<ITree> kids ) - { - if (kids == null) - throw new ArgumentNullException("kids"); - - foreach ( ITree t in kids ) - AddChild( t ); - } - - public virtual void SetChild( int i, ITree t ) - { - if (i < 0) - throw new ArgumentOutOfRangeException("i"); - - if ( t == null ) - { - return; - } - if ( t.IsNil ) - { - throw new ArgumentException( "Can't set single child to a list" ); - } - if ( Children == null ) - { - Children = CreateChildrenList(); - } - Children[i] = t; - t.Parent = this; - t.ChildIndex = i; - } - - /** Insert child t at child position i (0..n-1) by shifting children - * i+1..n-1 to the right one position. Set parent / indexes properly - * but does NOT collapse nil-rooted t's that come in here like addChild. - */ - public virtual void InsertChild(int i, ITree t) - { - if (i < 0) - throw new ArgumentOutOfRangeException("i"); - if (i > ChildCount) - throw new ArgumentException(); - - if (i == ChildCount) - { - AddChild(t); - return; - } - - Children.Insert(i, t); - - // walk others to increment their child indexes - // set index, parent of this one too - this.FreshenParentAndChildIndexes(i); - } - - public virtual object DeleteChild( int i ) - { - if (i < 0) - throw new ArgumentOutOfRangeException("i"); - if (i >= ChildCount) - throw new ArgumentException(); - - if ( Children == null ) - return null; - - ITree killed = Children[i]; - Children.RemoveAt( i ); - // walk rest and decrement their child indexes - this.FreshenParentAndChildIndexes( i ); - return killed; - } - - /** <summary> - * Delete children from start to stop and replace with t even if t is - * a list (nil-root tree). num of children can increase or decrease. - * For huge child lists, inserting children can force walking rest of - * children to set their childindex; could be slow. - * </summary> - */ - public virtual void ReplaceChildren( int startChildIndex, int stopChildIndex, object t ) - { - if (startChildIndex < 0) - throw new ArgumentOutOfRangeException(); - if (stopChildIndex < 0) - throw new ArgumentOutOfRangeException(); - if (t == null) - throw new ArgumentNullException("t"); - if (stopChildIndex < startChildIndex) - throw new ArgumentException(); - - /* - System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+ - " with "+((BaseTree)t).toStringTree()); - System.out.println("in="+toStringTree()); - */ - if ( Children == null ) - { - throw new ArgumentException( "indexes invalid; no children in list" ); - } - int replacingHowMany = stopChildIndex - startChildIndex + 1; - int replacingWithHowMany; - ITree newTree = (ITree)t; - IList<ITree> newChildren = null; - // normalize to a list of children to add: newChildren - if ( newTree.IsNil ) - { - BaseTree baseTree = newTree as BaseTree; - if ( baseTree != null && baseTree.Children != null ) - { - newChildren = baseTree.Children; - } - else - { - newChildren = CreateChildrenList(); - int n = newTree.ChildCount; - for ( int i = 0; i < n; i++ ) - newChildren.Add( newTree.GetChild( i ) ); - } - } - else - { - newChildren = new List<ITree>( 1 ); - newChildren.Add( newTree ); - } - replacingWithHowMany = newChildren.Count; - int numNewChildren = newChildren.Count; - int delta = replacingHowMany - replacingWithHowMany; - // if same number of nodes, do direct replace - if ( delta == 0 ) - { - int j = 0; // index into new children - for ( int i = startChildIndex; i <= stopChildIndex; i++ ) - { - ITree child = newChildren[j]; - Children[i] = child; - child.Parent = this; - child.ChildIndex = i; - j++; - } - } - else if ( delta > 0 ) - { - // fewer new nodes than there were - // set children and then delete extra - for ( int j = 0; j < numNewChildren; j++ ) - { - Children[startChildIndex + j] = newChildren[j]; - } - int indexToDelete = startChildIndex + numNewChildren; - for ( int c = indexToDelete; c <= stopChildIndex; c++ ) - { - // delete same index, shifting everybody down each time - Children.RemoveAt( indexToDelete ); - } - FreshenParentAndChildIndexes( startChildIndex ); - } - else - { - // more new nodes than were there before - // fill in as many children as we can (replacingHowMany) w/o moving data - for ( int j = 0; j < replacingHowMany; j++ ) - { - Children[startChildIndex + j] = newChildren[j]; - } - int numToInsert = replacingWithHowMany - replacingHowMany; - for ( int j = replacingHowMany; j < replacingWithHowMany; j++ ) - { - Children.Insert( startChildIndex + j, newChildren[j] ); - } - FreshenParentAndChildIndexes( startChildIndex ); - } - //System.out.println("out="+toStringTree()); - } - - /** <summary>Override in a subclass to change the impl of children list</summary> */ - protected virtual IList<ITree> CreateChildrenList() - { - return new List<ITree>(); - } - - /** <summary>Set the parent and child index values for all child of t</summary> */ - public virtual void FreshenParentAndChildIndexes() - { - FreshenParentAndChildIndexes( 0 ); - } - - public virtual void FreshenParentAndChildIndexes( int offset ) - { - int n = ChildCount; - for ( int c = offset; c < n; c++ ) - { - ITree child = GetChild( c ); - child.ChildIndex = c; - child.Parent = this; - } - } - - public virtual void FreshenParentAndChildIndexesDeeply() - { - FreshenParentAndChildIndexesDeeply(0); - } - - public virtual void FreshenParentAndChildIndexesDeeply(int offset) - { - int n = ChildCount; - for (int c = offset; c < n; c++) - { - ITree child = GetChild(c); - child.ChildIndex = c; - child.Parent = this; - BaseTree baseTree = child as BaseTree; - if (baseTree != null) - baseTree.FreshenParentAndChildIndexesDeeply(); - } - } - - public virtual void SanityCheckParentAndChildIndexes() - { - SanityCheckParentAndChildIndexes( null, -1 ); - } - - public virtual void SanityCheckParentAndChildIndexes( ITree parent, int i ) - { - if ( parent != this.Parent ) - { - throw new InvalidOperationException( "parents don't match; expected " + parent + " found " + this.Parent ); - } - if ( i != this.ChildIndex ) - { - throw new InvalidOperationException( "child indexes don't match; expected " + i + " found " + this.ChildIndex ); - } - int n = this.ChildCount; - for ( int c = 0; c < n; c++ ) - { - BaseTree child = (BaseTree)this.GetChild( c ); - child.SanityCheckParentAndChildIndexes( this, c ); - } - } - - /** <summary>Walk upwards looking for ancestor with this token type.</summary> */ - public virtual bool HasAncestor( int ttype ) - { - return GetAncestor( ttype ) != null; - } - - /** <summary>Walk upwards and get first ancestor with this token type.</summary> */ - public virtual ITree GetAncestor( int ttype ) - { - ITree t = this; - t = t.Parent; - while ( t != null ) - { - if ( t.Type == ttype ) - return t; - t = t.Parent; - } - return null; - } - - /** <summary> - * Return a list of all ancestors of this node. The first node of - * list is the root and the last is the parent of this node. - * </summary> - */ - public virtual IList<ITree> GetAncestors() - { - if ( Parent == null ) - return null; - - List<ITree> ancestors = new List<ITree>(); - ITree t = this; - t = t.Parent; - while ( t != null ) - { - ancestors.Insert( 0, t ); // insert at start - t = t.Parent; - } - return ancestors; - } - - /** <summary>Print out a whole tree not just a node</summary> */ - public virtual string ToStringTree() - { - if ( Children == null || Children.Count == 0 ) - { - return this.ToString(); - } - StringBuilder buf = new StringBuilder(); - if ( !IsNil ) - { - buf.Append( "(" ); - buf.Append( this.ToString() ); - buf.Append( ' ' ); - } - for ( int i = 0; Children != null && i < Children.Count; i++ ) - { - ITree t = Children[i]; - if ( i > 0 ) - { - buf.Append( ' ' ); - } - buf.Append( t.ToStringTree() ); - } - if ( !IsNil ) - { - buf.Append( ")" ); - } - return buf.ToString(); - } - - /** <summary>Override to say how a node (not a tree) should look as text</summary> */ - public override abstract string ToString(); - - #region Tree Members - public abstract ITree DupNode(); - #endregion - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTreeAdaptor.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTreeAdaptor.cs deleted file mode 100644 index d77e031..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BaseTreeAdaptor.cs +++ /dev/null @@ -1,517 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - - using ArgumentNullException = System.ArgumentNullException; - using Exception = System.Exception; - using IDictionary = System.Collections.IDictionary; - using NotSupportedException = System.NotSupportedException; - - /** <summary>A TreeAdaptor that works with any Tree implementation.</summary> */ - public abstract class BaseTreeAdaptor : ITreeAdaptor - { - /** <summary> - * System.identityHashCode() is not always unique; we have to - * track ourselves. That's ok, it's only for debugging, though it's - * expensive: we have to create a hashtable with all tree nodes in it. - * </summary> - */ - protected IDictionary<object, int> treeToUniqueIDMap; - protected int uniqueNodeID = 1; - - public virtual object Nil() - { - return Create( null ); - } - - /** <summary> - * Create tree node that holds the start and stop tokens associated - * with an error. - * </summary> - * - * <remarks> - * If you specify your own kind of tree nodes, you will likely have to - * override this method. CommonTree returns Token.INVALID_TOKEN_TYPE - * if no token payload but you might have to set token type for diff - * node type. - * - * You don't have to subclass CommonErrorNode; you will likely need to - * subclass your own tree node class to avoid class cast exception. - * </remarks> - */ - public virtual object ErrorNode( ITokenStream input, IToken start, IToken stop, - RecognitionException e ) - { - CommonErrorNode t = new CommonErrorNode( input, start, stop, e ); - //System.out.println("returning error node '"+t+"' @index="+input.index()); - return t; - } - - public virtual bool IsNil( object tree ) - { - return ( (ITree)tree ).IsNil; - } - - public virtual object DupNode(int type, object treeNode) - { - object t = DupNode(treeNode); - SetType(t, type); - return t; - } - - public virtual object DupNode(object treeNode, string text) - { - object t = DupNode(treeNode); - SetText(t, text); - return t; - } - - public virtual object DupNode(int type, object treeNode, string text) - { - object t = DupNode(treeNode); - SetType(t, type); - SetText(t, text); - return t; - } - - public virtual object DupTree( object tree ) - { - return DupTree( tree, null ); - } - - /** <summary> - * This is generic in the sense that it will work with any kind of - * tree (not just ITree interface). It invokes the adaptor routines - * not the tree node routines to do the construction. - * </summary> - */ - public virtual object DupTree( object t, object parent ) - { - if ( t == null ) - { - return null; - } - object newTree = DupNode( t ); - // ensure new subtree root has parent/child index set - SetChildIndex( newTree, GetChildIndex( t ) ); // same index in new tree - SetParent( newTree, parent ); - int n = GetChildCount( t ); - for ( int i = 0; i < n; i++ ) - { - object child = GetChild( t, i ); - object newSubTree = DupTree( child, t ); - AddChild( newTree, newSubTree ); - } - return newTree; - } - - /** <summary> - * Add a child to the tree t. If child is a flat tree (a list), make all - * in list children of t. Warning: if t has no children, but child does - * and child isNil then you can decide it is ok to move children to t via - * t.children = child.children; i.e., without copying the array. Just - * make sure that this is consistent with have the user will build - * ASTs. - * </summary> - */ - public virtual void AddChild( object t, object child ) - { - if ( t != null && child != null ) - { - ( (ITree)t ).AddChild( (ITree)child ); - } - } - - /** <summary> - * If oldRoot is a nil root, just copy or move the children to newRoot. - * If not a nil root, make oldRoot a child of newRoot. - * </summary> - * - * <remarks> - * old=^(nil a b c), new=r yields ^(r a b c) - * old=^(a b c), new=r yields ^(r ^(a b c)) - * - * If newRoot is a nil-rooted single child tree, use the single - * child as the new root node. - * - * old=^(nil a b c), new=^(nil r) yields ^(r a b c) - * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) - * - * If oldRoot was null, it's ok, just return newRoot (even if isNil). - * - * old=null, new=r yields r - * old=null, new=^(nil r) yields ^(nil r) - * - * Return newRoot. Throw an exception if newRoot is not a - * simple node or nil root with a single child node--it must be a root - * node. If newRoot is ^(nil x) return x as newRoot. - * - * Be advised that it's ok for newRoot to point at oldRoot's - * children; i.e., you don't have to copy the list. We are - * constructing these nodes so we should have this control for - * efficiency. - * </remarks> - */ - public virtual object BecomeRoot( object newRoot, object oldRoot ) - { - //System.out.println("becomeroot new "+newRoot.toString()+" old "+oldRoot); - ITree newRootTree = (ITree)newRoot; - ITree oldRootTree = (ITree)oldRoot; - if ( oldRoot == null ) - { - return newRoot; - } - // handle ^(nil real-node) - if ( newRootTree.IsNil ) - { - int nc = newRootTree.ChildCount; - if ( nc == 1 ) - newRootTree = (ITree)newRootTree.GetChild( 0 ); - else if ( nc > 1 ) - { - // TODO: make tree run time exceptions hierarchy - throw new Exception( "more than one node as root (TODO: make exception hierarchy)" ); - } - } - // add oldRoot to newRoot; addChild takes care of case where oldRoot - // is a flat list (i.e., nil-rooted tree). All children of oldRoot - // are added to newRoot. - newRootTree.AddChild( oldRootTree ); - return newRootTree; - } - - /** <summary>Transform ^(nil x) to x and nil to null</summary> */ - public virtual object RulePostProcessing( object root ) - { - //System.out.println("rulePostProcessing: "+((Tree)root).toStringTree()); - ITree r = (ITree)root; - if ( r != null && r.IsNil ) - { - if ( r.ChildCount == 0 ) - { - r = null; - } - else if ( r.ChildCount == 1 ) - { - r = (ITree)r.GetChild( 0 ); - // whoever invokes rule will set parent and child index - r.Parent = null; - r.ChildIndex = -1; - } - } - return r; - } - - public virtual object BecomeRoot( IToken newRoot, object oldRoot ) - { - return BecomeRoot( Create( newRoot ), oldRoot ); - } - - public virtual object Create( int tokenType, IToken fromToken ) - { - fromToken = CreateToken( fromToken ); - fromToken.Type = tokenType; - object t = Create( fromToken ); - return t; - } - - public virtual object Create( int tokenType, IToken fromToken, string text ) - { - if ( fromToken == null ) - return Create( tokenType, text ); - - fromToken = CreateToken( fromToken ); - fromToken.Type = tokenType; - fromToken.Text = text; - object result = Create(fromToken); - return result; - } - - public virtual object Create(IToken fromToken, string text) - { - if (fromToken == null) - throw new ArgumentNullException("fromToken"); - - fromToken = CreateToken(fromToken); - fromToken.Text = text; - object result = Create(fromToken); - return result; - } - - public virtual object Create( int tokenType, string text ) - { - IToken fromToken = CreateToken( tokenType, text ); - object t = Create( fromToken ); - return t; - } - - public virtual int GetType( object t ) - { - ITree tree = GetTree(t); - if (tree == null) - return TokenTypes.Invalid; - - return tree.Type; - } - - public virtual void SetType( object t, int type ) - { - throw new NotSupportedException( "don't know enough about Tree node" ); - } - - public virtual string GetText( object t ) - { - ITree tree = GetTree(t); - if (tree == null) - return null; - - return tree.Text; - } - - public virtual void SetText( object t, string text ) - { - throw new NotSupportedException( "don't know enough about Tree node" ); - } - - public virtual object GetChild( object t, int i ) - { - ITree tree = GetTree(t); - if (tree == null) - return null; - - return tree.GetChild(i); - } - - public virtual void SetChild( object t, int i, object child ) - { - ITree tree = GetTree(t); - if (tree == null) - return; - - ITree childTree = GetTree(child); - tree.SetChild(i, childTree); - } - - public virtual object DeleteChild( object t, int i ) - { - return ( (ITree)t ).DeleteChild( i ); - } - - public virtual int GetChildCount( object t ) - { - ITree tree = GetTree(t); - if (tree == null) - return 0; - - return tree.ChildCount; - } - - public virtual int GetUniqueID( object node ) - { - if ( treeToUniqueIDMap == null ) - { - treeToUniqueIDMap = new Dictionary<object, int>(); - } - int id; - if ( treeToUniqueIDMap.TryGetValue( node, out id ) ) - return id; - - id = uniqueNodeID; - treeToUniqueIDMap[node] = id; - uniqueNodeID++; - return id; - // GC makes these nonunique: - // return System.identityHashCode(node); - } - - /** <summary> - * Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * </summary> - * - * <remarks> - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - * </remarks> - */ - public abstract IToken CreateToken( int tokenType, string text ); - - /** <summary> - * Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * </summary> - * - * <remarks> - * This is a variant of createToken where the new token is derived from - * an actual real input token. Typically this is for converting '{' - * tokens to BLOCK etc... You'll see - * - * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; - * - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - * </remarks> - */ - public abstract IToken CreateToken( IToken fromToken ); - - public abstract object Create( IToken payload ); - - /** <summary> - * Duplicate a node. This is part of the factory; - * override if you want another kind of node to be built. - * </summary> - * - * <remarks> - * I could use reflection to prevent having to override this - * but reflection is slow. - * </remarks> - */ - public virtual object DupNode(object treeNode) - { - ITree tree = GetTree(treeNode); - if (tree == null) - return null; - - return tree.DupNode(); - } - - public abstract IToken GetToken( object t ); - - /** <summary> - * Track start/stop token for subtree root created for a rule. - * Only works with Tree nodes. For rules that match nothing, - * seems like this will yield start=i and stop=i-1 in a nil node. - * Might be useful info so I'll not force to be i..i. - * </summary> - */ - public virtual void SetTokenBoundaries(object t, IToken startToken, IToken stopToken) - { - ITree tree = GetTree(t); - if (tree == null) - return; - - int start = 0; - int stop = 0; - - if (startToken != null) - start = startToken.TokenIndex; - if (stopToken != null) - stop = stopToken.TokenIndex; - - tree.TokenStartIndex = start; - tree.TokenStopIndex = stop; - } - - public virtual int GetTokenStartIndex(object t) - { - ITree tree = GetTree(t); - if (tree == null) - return -1; - - return tree.TokenStartIndex; - } - - public virtual int GetTokenStopIndex(object t) - { - ITree tree = GetTree(t); - if (tree == null) - return -1; - - return tree.TokenStopIndex; - } - - public virtual object GetParent(object t) - { - ITree tree = GetTree(t); - if (tree == null) - return null; - - return tree.Parent; - } - - public virtual void SetParent(object t, object parent) - { - ITree tree = GetTree(t); - if (tree == null) - return; - - ITree parentTree = GetTree(parent); - tree.Parent = parentTree; - } - - public virtual int GetChildIndex(object t) - { - ITree tree = GetTree(t); - if (tree == null) - return 0; - - return tree.ChildIndex; - } - - public virtual void SetChildIndex(object t, int index) - { - ITree tree = GetTree(t); - if (tree == null) - return; - - tree.ChildIndex = index; - } - - public virtual void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t) - { - ITree tree = GetTree(parent); - if (tree == null) - return; - - tree.ReplaceChildren(startChildIndex, stopChildIndex, t); - } - - protected virtual ITree GetTree(object t) - { - if (t == null) - return null; - - ITree tree = t as ITree; - if (tree == null) - throw new NotSupportedException(); - - return tree; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs deleted file mode 100644 index 3b5a01e..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/BufferedTreeNodeStream.cs +++ /dev/null @@ -1,663 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - - using Console = System.Console; - using IList = System.Collections.IList; - using InvalidOperationException = System.InvalidOperationException; - using StringBuilder = System.Text.StringBuilder; - - /** <summary>A buffered stream of tree nodes. Nodes can be from a tree of ANY kind.</summary> - * - * This node stream sucks all nodes out of the tree specified in - * the constructor during construction and makes pointers into - * the tree using an array of Object pointers. The stream necessarily - * includes pointers to DOWN and UP and EOF nodes. - * - * This stream knows how to mark/release for backtracking. - * - * This stream is most suitable for tree interpreters that need to - * jump around a lot or for tree parsers requiring speed (at cost of memory). - * There is some duplicated functionality here with UnBufferedTreeNodeStream - * but just in bookkeeping, not tree walking etc... - * - * TARGET DEVELOPERS: - * - * This is the old CommonTreeNodeStream that buffered up entire node stream. - * No need to implement really as new CommonTreeNodeStream is much better - * and covers what we need. - * - * @see CommonTreeNodeStream - */ - public class BufferedTreeNodeStream : ITreeNodeStream, ITokenStreamInformation - { - public const int DEFAULT_INITIAL_BUFFER_SIZE = 100; - public const int INITIAL_CALL_STACK_SIZE = 10; - - protected sealed class StreamIterator : IEnumerator<object> - { - BufferedTreeNodeStream _outer; - int _index; - - public StreamIterator( BufferedTreeNodeStream outer ) - { - _outer = outer; - _index = -1; - } - - #region IEnumerator<object> Members - - public object Current - { - get - { - if ( _index < _outer.nodes.Count ) - return _outer.nodes[_index]; - - return _outer.eof; - } - } - - #endregion - - #region IDisposable Members - - public void Dispose() - { - } - - #endregion - - #region IEnumerator Members - - public bool MoveNext() - { - if ( _index < _outer.nodes.Count ) - _index++; - - return _index < _outer.nodes.Count; - } - - public void Reset() - { - _index = -1; - } - - #endregion - } - - // all these navigation nodes are shared and hence they - // cannot contain any line/column info - - protected object down; - protected object up; - protected object eof; - - /** <summary>The complete mapping from stream index to tree node. - * This buffer includes pointers to DOWN, UP, and EOF nodes. - * It is built upon ctor invocation. The elements are type - * Object as we don't what the trees look like.</summary> - * - * Load upon first need of the buffer so we can set token types - * of interest for reverseIndexing. Slows us down a wee bit to - * do all of the if p==-1 testing everywhere though. - */ - protected IList nodes; - - /** <summary>Pull nodes from which tree?</summary> */ - protected object root; - - /** <summary>IF this tree (root) was created from a token stream, track it.</summary> */ - protected ITokenStream tokens; - - /** <summary>What tree adaptor was used to build these trees</summary> */ - ITreeAdaptor adaptor; - - /** <summary>Reuse same DOWN, UP navigation nodes unless this is true</summary> */ - bool uniqueNavigationNodes = false; - - /** <summary>The index into the nodes list of the current node (next node - * to consume). If -1, nodes array not filled yet.</summary> - */ - protected int p = -1; - - /** <summary>Track the last mark() call result value for use in rewind().</summary> */ - protected int lastMarker; - - /** <summary>Stack of indexes used for push/pop calls</summary> */ - protected Stack<int> calls; - - public BufferedTreeNodeStream( object tree ) - : this( new CommonTreeAdaptor(), tree ) - { - } - - public BufferedTreeNodeStream( ITreeAdaptor adaptor, object tree ) - : this( adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE ) - { - } - - public BufferedTreeNodeStream( ITreeAdaptor adaptor, object tree, int initialBufferSize ) - { - this.root = tree; - this.adaptor = adaptor; - nodes = new List<object>( initialBufferSize ); - down = adaptor.Create( TokenTypes.Down, "DOWN" ); - up = adaptor.Create( TokenTypes.Up, "UP" ); - eof = adaptor.Create( TokenTypes.EndOfFile, "EOF" ); - } - - #region Properties - - public virtual int Count - { - get - { - if ( p == -1 ) - { - throw new InvalidOperationException( "Cannot determine the Count before the buffer is filled." ); - } - return nodes.Count; - } - } - - public virtual object TreeSource - { - get - { - return root; - } - } - - public virtual string SourceName - { - get - { - return TokenStream.SourceName; - } - } - - public virtual ITokenStream TokenStream - { - get - { - return tokens; - } - set - { - tokens = value; - } - } - - public virtual ITreeAdaptor TreeAdaptor - { - get - { - return adaptor; - } - set - { - adaptor = value; - } - } - - public virtual bool UniqueNavigationNodes - { - get - { - return uniqueNavigationNodes; - } - set - { - uniqueNavigationNodes = value; - } - } - - public virtual IToken LastToken - { - get - { - return TreeAdaptor.GetToken(LB(1)); - } - } - - public virtual IToken LastRealToken - { - get - { - int i = 0; - IToken token; - do - { - i++; - token = TreeAdaptor.GetToken(LB(i)); - } while (token != null && token.Line <= 0); - - return token; - } - } - - public virtual int MaxLookBehind - { - get - { - return int.MaxValue; - } - } - - #endregion - - /** Walk tree with depth-first-search and fill nodes buffer. - * Don't do DOWN, UP nodes if its a list (t is isNil). - */ - protected virtual void FillBuffer() - { - FillBuffer( root ); - //Console.Out.WriteLine( "revIndex=" + tokenTypeToStreamIndexesMap ); - p = 0; // buffer of nodes intialized now - } - - public virtual void FillBuffer( object t ) - { - bool nil = adaptor.IsNil( t ); - if ( !nil ) - { - nodes.Add( t ); // add this node - } - // add DOWN node if t has children - int n = adaptor.GetChildCount( t ); - if ( !nil && n > 0 ) - { - AddNavigationNode( TokenTypes.Down ); - } - // and now add all its children - for ( int c = 0; c < n; c++ ) - { - object child = adaptor.GetChild( t, c ); - FillBuffer( child ); - } - // add UP node if t has children - if ( !nil && n > 0 ) - { - AddNavigationNode( TokenTypes.Up ); - } - } - - /** What is the stream index for node? 0..n-1 - * Return -1 if node not found. - */ - protected virtual int GetNodeIndex( object node ) - { - if ( p == -1 ) - { - FillBuffer(); - } - for ( int i = 0; i < nodes.Count; i++ ) - { - object t = nodes[i]; - if ( t == node ) - { - return i; - } - } - return -1; - } - - /** As we flatten the tree, we use UP, DOWN nodes to represent - * the tree structure. When debugging we need unique nodes - * so instantiate new ones when uniqueNavigationNodes is true. - */ - protected virtual void AddNavigationNode( int ttype ) - { - object navNode = null; - if ( ttype == TokenTypes.Down ) - { - if ( UniqueNavigationNodes ) - { - navNode = adaptor.Create( TokenTypes.Down, "DOWN" ); - } - else - { - navNode = down; - } - } - else - { - if ( UniqueNavigationNodes ) - { - navNode = adaptor.Create( TokenTypes.Up, "UP" ); - } - else - { - navNode = up; - } - } - nodes.Add( navNode ); - } - - public virtual object this[int i] - { - get - { - if ( p == -1 ) - { - throw new InvalidOperationException( "Cannot get the node at index i before the buffer is filled." ); - } - return nodes[i]; - } - } - - public virtual object LT( int k ) - { - if ( p == -1 ) - { - FillBuffer(); - } - if ( k == 0 ) - { - return null; - } - if ( k < 0 ) - { - return LB( -k ); - } - //System.out.print("LT(p="+p+","+k+")="); - if ( ( p + k - 1 ) >= nodes.Count ) - { - return eof; - } - return nodes[p + k - 1]; - } - - public virtual object GetCurrentSymbol() - { - return LT( 1 ); - } - -#if false - public virtual object getLastTreeNode() - { - int i = Index; - if ( i >= size() ) - { - i--; // if at EOF, have to start one back - } - Console.Out.WriteLine( "start last node: " + i + " size==" + nodes.Count ); - while ( i >= 0 && - ( adaptor.getType( this[i] ) == TokenTypes.EOF || - adaptor.getType( this[i] ) == TokenTypes.UP || - adaptor.getType( this[i] ) == TokenTypes.DOWN ) ) - { - i--; - } - Console.Out.WriteLine( "stop at node: " + i + " " + nodes[i] ); - return nodes[i]; - } -#endif - - /** <summary>Look backwards k nodes</summary> */ - protected virtual object LB( int k ) - { - if ( k == 0 ) - { - return null; - } - if ( ( p - k ) < 0 ) - { - return null; - } - return nodes[p - k]; - } - - public virtual void Consume() - { - if ( p == -1 ) - { - FillBuffer(); - } - p++; - } - - public virtual int LA( int i ) - { - return adaptor.GetType( LT( i ) ); - } - - public virtual int Mark() - { - if ( p == -1 ) - { - FillBuffer(); - } - lastMarker = Index; - return lastMarker; - } - - public virtual void Release( int marker ) - { - // no resources to release - } - - public virtual int Index - { - get - { - return p; - } - } - - public virtual void Rewind( int marker ) - { - Seek( marker ); - } - - public virtual void Rewind() - { - Seek( lastMarker ); - } - - public virtual void Seek( int index ) - { - if ( p == -1 ) - { - FillBuffer(); - } - p = index; - } - - /** <summary> - * Make stream jump to a new location, saving old location. - * Switch back with pop(). - * </summary> - */ - public virtual void Push( int index ) - { - if ( calls == null ) - { - calls = new Stack<int>(); - } - calls.Push( p ); // save current index - Seek( index ); - } - - /** <summary> - * Seek back to previous index saved during last push() call. - * Return top of stack (return index). - * </summary> - */ - public virtual int Pop() - { - int ret = calls.Pop(); - Seek( ret ); - return ret; - } - - public virtual void Reset() - { - p = 0; - lastMarker = 0; - if ( calls != null ) - { - calls.Clear(); - } - } - - public virtual IEnumerator<object> Iterator() - { - if ( p == -1 ) - { - FillBuffer(); - } - - return new StreamIterator( this ); - } - - // TREE REWRITE INTERFACE - - public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ) - { - if ( parent != null ) - { - adaptor.ReplaceChildren( parent, startChildIndex, stopChildIndex, t ); - } - } - - /** <summary>Used for testing, just return the token type stream</summary> */ - public virtual string ToTokenTypeString() - { - if ( p == -1 ) - { - FillBuffer(); - } - StringBuilder buf = new StringBuilder(); - for ( int i = 0; i < nodes.Count; i++ ) - { - object t = nodes[i]; - buf.Append( " " ); - buf.Append( adaptor.GetType( t ) ); - } - return buf.ToString(); - } - - /** <summary>Debugging</summary> */ - public virtual string ToTokenString( int start, int stop ) - { - if ( p == -1 ) - { - FillBuffer(); - } - StringBuilder buf = new StringBuilder(); - for ( int i = start; i < nodes.Count && i <= stop; i++ ) - { - object t = nodes[i]; - buf.Append( " " ); - buf.Append( adaptor.GetToken( t ) ); - } - return buf.ToString(); - } - - public virtual string ToString( object start, object stop ) - { - Console.Out.WriteLine( "toString" ); - if ( start == null || stop == null ) - { - return null; - } - if ( p == -1 ) - { - throw new InvalidOperationException( "Buffer is not yet filled." ); - } - //Console.Out.WriteLine( "stop: " + stop ); - if ( start is CommonTree ) - Console.Out.Write( "toString: " + ( (CommonTree)start ).Token + ", " ); - else - Console.Out.WriteLine( start ); - if ( stop is CommonTree ) - Console.Out.WriteLine( ( (CommonTree)stop ).Token ); - else - Console.Out.WriteLine( stop ); - // if we have the token stream, use that to dump text in order - if ( tokens != null ) - { - int beginTokenIndex = adaptor.GetTokenStartIndex( start ); - int endTokenIndex = adaptor.GetTokenStopIndex( stop ); - // if it's a tree, use start/stop index from start node - // else use token range from start/stop nodes - if ( adaptor.GetType( stop ) == TokenTypes.Up ) - { - endTokenIndex = adaptor.GetTokenStopIndex( start ); - } - else if ( adaptor.GetType( stop ) == TokenTypes.EndOfFile ) - { - endTokenIndex = Count - 2; // don't use EOF - } - return tokens.ToString( beginTokenIndex, endTokenIndex ); - } - // walk nodes looking for start - object t = null; - int i = 0; - for ( ; i < nodes.Count; i++ ) - { - t = nodes[i]; - if ( t == start ) - { - break; - } - } - // now walk until we see stop, filling string buffer with text - StringBuilder buf = new StringBuilder(); - t = nodes[i]; - while ( t != stop ) - { - string text = adaptor.GetText( t ); - if ( text == null ) - { - text = " " + adaptor.GetType( t ).ToString(); - } - buf.Append( text ); - i++; - t = nodes[i]; - } - // include stop node too - string text2 = adaptor.GetText( stop ); - if ( text2 == null ) - { - text2 = " " + adaptor.GetType( stop ).ToString(); - } - buf.Append( text2 ); - return buf.ToString(); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonErrorNode.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonErrorNode.cs deleted file mode 100644 index 3bd9138..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonErrorNode.cs +++ /dev/null @@ -1,142 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - - /** <summary>A node representing erroneous token range in token stream</summary> */ - [System.Serializable] - public class CommonErrorNode : CommonTree - { - public IIntStream input; - public IToken start; - public IToken stop; - public RecognitionException trappedException; - - public CommonErrorNode( ITokenStream input, IToken start, IToken stop, - RecognitionException e ) - { - //System.out.println("start: "+start+", stop: "+stop); - if ( stop == null || - ( stop.TokenIndex < start.TokenIndex && - stop.Type != TokenTypes.EndOfFile ) ) - { - // sometimes resync does not consume a token (when LT(1) is - // in follow set. So, stop will be 1 to left to start. adjust. - // Also handle case where start is the first token and no token - // is consumed during recovery; LT(-1) will return null. - stop = start; - } - this.input = input; - this.start = start; - this.stop = stop; - this.trappedException = e; - } - - #region Properties - public override bool IsNil - { - get - { - return false; - } - } - public override string Text - { - get - { - string badText = null; - if ( start is IToken ) - { - int i = ( (IToken)start ).TokenIndex; - int j = ( (IToken)stop ).TokenIndex; - if ( ( (IToken)stop ).Type == TokenTypes.EndOfFile ) - { - j = ( (ITokenStream)input ).Count; - } - badText = ( (ITokenStream)input ).ToString( i, j ); - } - else if ( start is ITree ) - { - badText = ( (ITreeNodeStream)input ).ToString( start, stop ); - } - else - { - // people should subclass if they alter the tree type so this - // next one is for sure correct. - badText = "<unknown>"; - } - return badText; - } - set - { - } - } - public override int Type - { - get - { - return TokenTypes.Invalid; - } - set - { - } - } - #endregion - - public override string ToString() - { - if ( trappedException is MissingTokenException ) - { - return "<missing type: " + - ( (MissingTokenException)trappedException ).MissingType + - ">"; - } - else if ( trappedException is UnwantedTokenException ) - { - return "<extraneous: " + - ( (UnwantedTokenException)trappedException ).UnexpectedToken + - ", resync=" + Text + ">"; - } - else if ( trappedException is MismatchedTokenException ) - { - return "<mismatched token: " + trappedException.Token + ", resync=" + Text + ">"; - } - else if ( trappedException is NoViableAltException ) - { - return "<unexpected: " + trappedException.Token + - ", resync=" + Text + ">"; - } - return "<error: " + Text + ">"; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTree.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTree.cs deleted file mode 100644 index 33f9bda..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTree.cs +++ /dev/null @@ -1,296 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using ArgumentNullException = System.ArgumentNullException; - - /** <summary> - * A tree node that is wrapper for a Token object. After 3.0 release - * while building tree rewrite stuff, it became clear that computing - * parent and child index is very difficult and cumbersome. Better to - * spend the space in every tree node. If you don't want these extra - * fields, it's easy to cut them out in your own BaseTree subclass. - * </summary> - */ - [System.Serializable] - public class CommonTree : BaseTree - { - /** <summary>A single token is the payload</summary> */ - private IToken _token; - - /** <summary> - * What token indexes bracket all tokens associated with this node - * and below? - * </summary> - */ - protected int startIndex = -1; - protected int stopIndex = -1; - - /** <summary>Who is the parent node of this node; if null, implies node is root</summary> */ - CommonTree parent; - - /** <summary>What index is this node in the child list? Range: 0..n-1</summary> */ - int childIndex = -1; - - public CommonTree() - { - } - - public CommonTree( CommonTree node ) - : base( node ) - { - if (node == null) - throw new ArgumentNullException("node"); - - this.Token = node.Token; - this.startIndex = node.startIndex; - this.stopIndex = node.stopIndex; - } - - public CommonTree( IToken t ) - { - this.Token = t; - } - - #region Properties - - public override int CharPositionInLine - { - get - { - if ( Token == null || Token.CharPositionInLine == -1 ) - { - if ( ChildCount > 0 ) - return Children[0].CharPositionInLine; - - return 0; - } - return Token.CharPositionInLine; - } - - set - { - base.CharPositionInLine = value; - } - } - - public override int ChildIndex - { - get - { - return childIndex; - } - - set - { - childIndex = value; - } - } - - public override bool IsNil - { - get - { - return Token == null; - } - } - - public override int Line - { - get - { - if ( Token == null || Token.Line == 0 ) - { - if ( ChildCount > 0 ) - return Children[0].Line; - - return 0; - } - - return Token.Line; - } - - set - { - base.Line = value; - } - } - - public override ITree Parent - { - get - { - return parent; - } - - set - { - parent = (CommonTree)value; - } - } - - public override string Text - { - get - { - if ( Token == null ) - return null; - - return Token.Text; - } - - set - { - } - } - - public IToken Token - { - get - { - return _token; - } - - set - { - _token = value; - } - } - - public override int TokenStartIndex - { - get - { - if ( startIndex == -1 && Token != null ) - return Token.TokenIndex; - - return startIndex; - } - - set - { - startIndex = value; - } - } - - public override int TokenStopIndex - { - get - { - if ( stopIndex == -1 && Token != null ) - { - return Token.TokenIndex; - } - return stopIndex; - } - - set - { - stopIndex = value; - } - } - - public override int Type - { - get - { - if ( Token == null ) - return TokenTypes.Invalid; - - return Token.Type; - } - - set - { - } - } - - #endregion - - public override ITree DupNode() - { - return new CommonTree( this ); - } - - /** <summary> - * For every node in this subtree, make sure it's start/stop token's - * are set. Walk depth first, visit bottom up. Only updates nodes - * with at least one token index < 0. - * </summary> - */ - public virtual void SetUnknownTokenBoundaries() - { - if ( Children == null ) - { - if ( startIndex < 0 || stopIndex < 0 ) - startIndex = stopIndex = Token.TokenIndex; - - return; - } - - foreach (ITree childTree in Children) - { - CommonTree commonTree = childTree as CommonTree; - if (commonTree == null) - continue; - - commonTree.SetUnknownTokenBoundaries(); - } - - if ( startIndex >= 0 && stopIndex >= 0 ) - return; // already set - - if ( Children.Count > 0 ) - { - ITree firstChild = Children[0]; - ITree lastChild = Children[Children.Count - 1]; - startIndex = firstChild.TokenStartIndex; - stopIndex = lastChild.TokenStopIndex; - } - } - - public override string ToString() - { - if (IsNil) - return "nil"; - - if (Type == TokenTypes.Invalid) - return "<errornode>"; - - if (Token == null) - return string.Empty; - - return Token.Text; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeAdaptor.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeAdaptor.cs deleted file mode 100644 index 18c7505..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeAdaptor.cs +++ /dev/null @@ -1,112 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - - /** <summary> - * A TreeAdaptor that works with any Tree implementation. It provides - * really just factory methods; all the work is done by BaseTreeAdaptor. - * If you would like to have different tokens created than ClassicToken - * objects, you need to override this and then set the parser tree adaptor to - * use your subclass. - * </summary> - * - * <remarks> - * To get your parser to build nodes of a different type, override - * create(Token), errorNode(), and to be safe, YourTreeClass.dupNode(). - * dupNode is called to duplicate nodes during rewrite operations. - * </remarks> - */ - public class CommonTreeAdaptor : BaseTreeAdaptor - { - public override object Create( IToken payload ) - { - return new CommonTree( payload ); - } - - /** <summary> - * Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * </summary> - * - * <remarks> - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - * </remarks> - */ - public override IToken CreateToken( int tokenType, string text ) - { - return new CommonToken( tokenType, text ); - } - - /** <summary> - * Tell me how to create a token for use with imaginary token nodes. - * For example, there is probably no input symbol associated with imaginary - * token DECL, but you need to create it as a payload or whatever for - * the DECL node as in ^(DECL type ID). - * </summary> - * - * <remarks> - * This is a variant of createToken where the new token is derived from - * an actual real input token. Typically this is for converting '{' - * tokens to BLOCK etc... You'll see - * - * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; - * - * If you care what the token payload objects' type is, you should - * override this method and any other createToken variant. - * </remarks> - */ - public override IToken CreateToken( IToken fromToken ) - { - return new CommonToken( fromToken ); - } - - /** <summary> - * What is the Token associated with this node? If - * you are not using CommonTree, then you must - * override this in your own adaptor. - * </summary> - */ - public override IToken GetToken( object t ) - { - if ( t is CommonTree ) - { - return ( (CommonTree)t ).Token; - } - return null; // no idea what to do - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs deleted file mode 100644 index f9cb0a7..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/CommonTreeNodeStream.cs +++ /dev/null @@ -1,312 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - using Antlr.Runtime.Misc; - - using StringBuilder = System.Text.StringBuilder; - - [System.Serializable] - public class CommonTreeNodeStream : LookaheadStream<object>, ITreeNodeStream, IPositionTrackingStream - { - public const int DEFAULT_INITIAL_BUFFER_SIZE = 100; - public const int INITIAL_CALL_STACK_SIZE = 10; - - /** <summary>Pull nodes from which tree?</summary> */ - private readonly object _root; - - /** <summary>If this tree (root) was created from a token stream, track it.</summary> */ - protected ITokenStream tokens; - - /** <summary>What tree adaptor was used to build these trees</summary> */ - [System.NonSerialized] - private ITreeAdaptor _adaptor; - - /** The tree iterator we are using */ - private readonly TreeIterator _it; - - /** <summary>Stack of indexes used for push/pop calls</summary> */ - private Stack<int> _calls; - - /** <summary>Tree (nil A B C) trees like flat A B C streams</summary> */ - private bool _hasNilRoot = false; - - /** <summary>Tracks tree depth. Level=0 means we're at root node level.</summary> */ - private int _level = 0; - - /** - * Tracks the last node before the start of {@link #data} which contains - * position information to provide information for error reporting. This is - * tracked in addition to {@link #prevElement} which may or may not contain - * position information. - * - * @see #hasPositionInformation - * @see RecognitionException#extractInformationFromTreeNodeStream - */ - private object _previousLocationElement; - - public CommonTreeNodeStream( object tree ) - : this( new CommonTreeAdaptor(), tree ) - { - } - - public CommonTreeNodeStream( ITreeAdaptor adaptor, object tree ) - { - this._root = tree; - this._adaptor = adaptor; - _it = new TreeIterator( adaptor, _root ); - } - - #region Properties - - public virtual string SourceName - { - get - { - if ( TokenStream == null ) - return null; - - return TokenStream.SourceName; - } - } - - public virtual ITokenStream TokenStream - { - get - { - return tokens; - } - - set - { - tokens = value; - } - } - - public virtual ITreeAdaptor TreeAdaptor - { - get - { - return _adaptor; - } - - set - { - _adaptor = value; - } - } - - public virtual object TreeSource - { - get - { - return _root; - } - } - - public virtual bool UniqueNavigationNodes - { - get - { - return false; - } - - set - { - } - } - - #endregion - - public override void Reset() - { - base.Reset(); - _it.Reset(); - _hasNilRoot = false; - _level = 0; - _previousLocationElement = null; - if ( _calls != null ) - _calls.Clear(); - } - - public override object NextElement() - { - _it.MoveNext(); - object t = _it.Current; - //System.out.println("pulled "+adaptor.getType(t)); - if ( t == _it.up ) - { - _level--; - if ( _level == 0 && _hasNilRoot ) - { - _it.MoveNext(); - return _it.Current; // don't give last UP; get EOF - } - } - else if ( t == _it.down ) - { - _level++; - } - - if ( _level == 0 && TreeAdaptor.IsNil( t ) ) - { - // if nil root, scarf nil, DOWN - _hasNilRoot = true; - _it.MoveNext(); - t = _it.Current; // t is now DOWN, so get first real node next - _level++; - _it.MoveNext(); - t = _it.Current; - } - - return t; - } - - public override object Dequeue() - { - object result = base.Dequeue(); - if (_p == 0 && HasPositionInformation(PreviousElement)) - _previousLocationElement = PreviousElement; - - return result; - } - - public override bool IsEndOfFile(object o) - { - return TreeAdaptor.GetType(o) == CharStreamConstants.EndOfFile; - } - - public virtual int LA( int i ) - { - return TreeAdaptor.GetType( LT( i ) ); - } - - /** Make stream jump to a new location, saving old location. - * Switch back with pop(). - */ - public virtual void Push( int index ) - { - if ( _calls == null ) - _calls = new Stack<int>(); - - _calls.Push( _p ); // save current index - Seek( index ); - } - - /** Seek back to previous index saved during last push() call. - * Return top of stack (return index). - */ - public virtual int Pop() - { - int ret = _calls.Pop(); - Seek( ret ); - return ret; - } - - /** - * Returns an element containing position information. If {@code allowApproximateLocation} is {@code false}, then - * this method will return the {@code LT(1)} element if it contains position information, and otherwise return {@code null}. - * If {@code allowApproximateLocation} is {@code true}, then this method will return the last known element containing position information. - * - * @see #hasPositionInformation - */ - public object GetKnownPositionElement(bool allowApproximateLocation) - { - object node = _data[_p]; - if (HasPositionInformation(node)) - return node; - - if (!allowApproximateLocation) - return null; - - for (int index = _p - 1; index >= 0; index--) - { - node = _data[index]; - if (HasPositionInformation(node)) - return node; - } - - return _previousLocationElement; - } - - public bool HasPositionInformation(object node) - { - IToken token = TreeAdaptor.GetToken(node); - if (token == null) - return false; - - if (token.Line <= 0) - return false; - - return true; - } - - #region Tree rewrite interface - - public virtual void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ) - { - if ( parent != null ) - { - TreeAdaptor.ReplaceChildren( parent, startChildIndex, stopChildIndex, t ); - } - } - - #endregion - - public virtual string ToString( object start, object stop ) - { - // we'll have to walk from start to stop in tree; we're not keeping - // a complete node stream buffer - return "n/a"; - } - - /** <summary>For debugging; destructive: moves tree iterator to end.</summary> */ - public virtual string ToTokenTypeString() - { - Reset(); - StringBuilder buf = new StringBuilder(); - object o = LT( 1 ); - int type = TreeAdaptor.GetType( o ); - while ( type != TokenTypes.EndOfFile ) - { - buf.Append( " " ); - buf.Append( type ); - Consume(); - o = LT( 1 ); - type = TreeAdaptor.GetType( o ); - } - return buf.ToString(); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/DotTreeGenerator.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/DotTreeGenerator.cs deleted file mode 100644 index 8a35b45..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/DotTreeGenerator.cs +++ /dev/null @@ -1,216 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - using StringBuilder = System.Text.StringBuilder; - - /** A utility class to generate DOT diagrams (graphviz) from - * arbitrary trees. You can pass in your own templates and - * can pass in any kind of tree or use Tree interface method. - * I wanted this separator so that you don't have to include - * ST just to use the org.antlr.runtime.tree.* package. - * This is a set of non-static methods so you can subclass - * to override. For example, here is an invocation: - * - * CharStream input = new ANTLRInputStream(System.in); - * TLexer lex = new TLexer(input); - * CommonTokenStream tokens = new CommonTokenStream(lex); - * TParser parser = new TParser(tokens); - * TParser.e_return r = parser.e(); - * Tree t = (Tree)r.tree; - * System.out.println(t.toStringTree()); - * DOTTreeGenerator gen = new DOTTreeGenerator(); - * StringTemplate st = gen.toDOT(t); - * System.out.println(st); - */ - public class DotTreeGenerator - { - readonly string[] HeaderLines = - { - "digraph {", - "", - "\tordering=out;", - "\tranksep=.4;", - "\tbgcolor=\"lightgrey\"; node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"", - "\t\twidth=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];", - "\tedge [arrowsize=.5, color=\"black\", style=\"bold\"]", - "" - }; - const string Footer = "}"; - const string NodeFormat = " {0} [label=\"{1}\"];"; - const string EdgeFormat = " {0} -> {1} // \"{2}\" -> \"{3}\""; - - /** Track node to number mapping so we can get proper node name back */ - Dictionary<object, int> nodeToNumberMap = new Dictionary<object, int>(); - - /** Track node number so we can get unique node names */ - int nodeNumber = 0; - - /** Generate DOT (graphviz) for a whole tree not just a node. - * For example, 3+4*5 should generate: - * - * digraph { - * node [shape=plaintext, fixedsize=true, fontsize=11, fontname="Courier", - * width=.4, height=.2]; - * edge [arrowsize=.7] - * "+"->3 - * "+"->"*" - * "*"->4 - * "*"->5 - * } - * - * Takes a Tree interface object. - */ - public virtual string ToDot( object tree, ITreeAdaptor adaptor ) - { - StringBuilder builder = new StringBuilder(); - foreach ( string line in HeaderLines ) - builder.AppendLine( line ); - - nodeNumber = 0; - var nodes = DefineNodes( tree, adaptor ); - nodeNumber = 0; - var edges = DefineEdges( tree, adaptor ); - - foreach ( var s in nodes ) - builder.AppendLine( s ); - - builder.AppendLine(); - - foreach ( var s in edges ) - builder.AppendLine( s ); - - builder.AppendLine(); - - builder.AppendLine( Footer ); - return builder.ToString(); - } - - public virtual string ToDot( ITree tree ) - { - return ToDot( tree, new CommonTreeAdaptor() ); - } - protected virtual IEnumerable<string> DefineNodes( object tree, ITreeAdaptor adaptor ) - { - if ( tree == null ) - yield break; - - int n = adaptor.GetChildCount( tree ); - if ( n == 0 ) - { - // must have already dumped as child from previous - // invocation; do nothing - yield break; - } - - // define parent node - yield return GetNodeText( adaptor, tree ); - - // for each child, do a "<unique-name> [label=text]" node def - for ( int i = 0; i < n; i++ ) - { - object child = adaptor.GetChild( tree, i ); - yield return GetNodeText( adaptor, child ); - foreach ( var t in DefineNodes( child, adaptor ) ) - yield return t; - } - } - - protected virtual IEnumerable<string> DefineEdges( object tree, ITreeAdaptor adaptor ) - { - if ( tree == null ) - yield break; - - int n = adaptor.GetChildCount( tree ); - if ( n == 0 ) - { - // must have already dumped as child from previous - // invocation; do nothing - yield break; - } - - string parentName = "n" + GetNodeNumber( tree ); - - // for each child, do a parent -> child edge using unique node names - string parentText = adaptor.GetText( tree ); - for ( int i = 0; i < n; i++ ) - { - object child = adaptor.GetChild( tree, i ); - string childText = adaptor.GetText( child ); - string childName = "n" + GetNodeNumber( child ); - yield return string.Format( EdgeFormat, parentName, childName, FixString( parentText ), FixString( childText ) ); - foreach ( var t in DefineEdges( child, adaptor ) ) - yield return t; - } - } - - protected virtual string GetNodeText( ITreeAdaptor adaptor, object t ) - { - string text = adaptor.GetText( t ); - string uniqueName = "n" + GetNodeNumber( t ); - return string.Format( NodeFormat, uniqueName, FixString( text ) ); - } - - protected virtual int GetNodeNumber( object t ) - { - int i; - if ( nodeToNumberMap.TryGetValue( t, out i ) ) - { - return i; - } - else - { - nodeToNumberMap[t] = nodeNumber; - nodeNumber++; - return nodeNumber - 1; - } - } - - protected virtual string FixString( string text ) - { - if ( text != null ) - { - text = System.Text.RegularExpressions.Regex.Replace( text, "\"", "\\\\\"" ); - text = System.Text.RegularExpressions.Regex.Replace( text, "\\t", " " ); - text = System.Text.RegularExpressions.Regex.Replace( text, "\\n", "\\\\n" ); - text = System.Text.RegularExpressions.Regex.Replace( text, "\\r", "\\\\r" ); - - if ( text.Length > 20 ) - text = text.Substring( 0, 8 ) + "..." + text.Substring( text.Length - 8 ); - } - - return text; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/IPositionTrackingStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/IPositionTrackingStream.cs deleted file mode 100644 index 8bc8945..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/IPositionTrackingStream.cs +++ /dev/null @@ -1,59 +0,0 @@ -/* - [The "BSD license"] - Copyright (c) 2012 Terence Parr - Copyright (c) 2012 Sam Harwell - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - /** - * - * @author Sam Harwell - */ - public interface IPositionTrackingStream - { - /** - * Returns an element containing concrete information about the current - * position in the stream. - * - * @param allowApproximateLocation if {@code false}, this method returns - * {@code null} if an element containing exact information about the current - * position is not available - */ - object GetKnownPositionElement(bool allowApproximateLocation); - - /** - * Determines if the specified {@code element} contains concrete position - * information. - * - * @param element the element to check - * @return {@code true} if {@code element} contains concrete position - * information, otherwise {@code false} - */ - bool HasPositionInformation(object element); - - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITree.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITree.cs deleted file mode 100644 index a3eb034..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITree.cs +++ /dev/null @@ -1,173 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - - /** <summary> - * What does a tree look like? ANTLR has a number of support classes - * such as CommonTreeNodeStream that work on these kinds of trees. You - * don't have to make your trees implement this interface, but if you do, - * you'll be able to use more support code. - * </summary> - * - * <remarks> - * NOTE: When constructing trees, ANTLR can build any kind of tree; it can - * even use Token objects as trees if you add a child list to your tokens. - * - * This is a tree node without any payload; just navigation and factory stuff. - * </remarks> - */ - public interface ITree - { - - ITree GetChild( int i ); - - int ChildCount - { - get; - } - - // Tree tracks parent and child index now > 3.0 - - ITree Parent - { - get; - set; - } - - /** <summary>Is there is a node above with token type ttype?</summary> */ - bool HasAncestor( int ttype ); - - /** <summary>Walk upwards and get first ancestor with this token type.</summary> */ - ITree GetAncestor( int ttype ); - - /** <summary> - * Return a list of all ancestors of this node. The first node of - * list is the root and the last is the parent of this node. - * </summary> - */ - IList<ITree> GetAncestors(); - - /** <summary>This node is what child index? 0..n-1</summary> */ - int ChildIndex - { - get; - set; - } - - /** <summary>Set the parent and child index values for all children</summary> */ - void FreshenParentAndChildIndexes(); - - /** <summary> - * Add t as a child to this node. If t is null, do nothing. If t - * is nil, add all children of t to this' children. - * </summary> - */ - void AddChild( ITree t ); - - /** <summary>Set ith child (0..n-1) to t; t must be non-null and non-nil node</summary> */ - void SetChild( int i, ITree t ); - - object DeleteChild( int i ); - - /** <summary> - * Delete children from start to stop and replace with t even if t is - * a list (nil-root tree). num of children can increase or decrease. - * For huge child lists, inserting children can force walking rest of - * children to set their childindex; could be slow. - * </summary> - */ - void ReplaceChildren( int startChildIndex, int stopChildIndex, object t ); - - /** <summary> - * Indicates the node is a nil node but may still have children, meaning - * the tree is a flat list. - * </summary> - */ - bool IsNil - { - get; - } - - /** <summary> - * What is the smallest token index (indexing from 0) for this node - * and its children? - * </summary> - */ - int TokenStartIndex - { - get; - set; - } - - /** <summary> - * What is the largest token index (indexing from 0) for this node - * and its children? - * </summary> - */ - int TokenStopIndex - { - get; - set; - } - - ITree DupNode(); - - /** <summary>Return a token type; needed for tree parsing</summary> */ - int Type - { - get; - } - - string Text - { - get; - } - - /** <summary>In case we don't have a token payload, what is the line for errors?</summary> */ - int Line - { - get; - } - - int CharPositionInLine - { - get; - } - - string ToStringTree(); - - string ToString(); - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeAdaptor.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeAdaptor.cs deleted file mode 100644 index 30230cc..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeAdaptor.cs +++ /dev/null @@ -1,346 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - - /** <summary> - * How to create and navigate trees. Rather than have a separate factory - * and adaptor, I've merged them. Makes sense to encapsulate. - * </summary> - * - * <remarks> - * This takes the place of the tree construction code generated in the - * generated code in 2.x and the ASTFactory. - * - * I do not need to know the type of a tree at all so they are all - * generic Objects. This may increase the amount of typecasting needed. :( - * </remarks> - */ - public interface ITreeAdaptor - { - #region Construction - - /** <summary> - * Create a tree node from Token object; for CommonTree type trees, - * then the token just becomes the payload. This is the most - * common create call. - * </summary> - * - * <remarks> - * Override if you want another kind of node to be built. - * </remarks> - */ - object Create(IToken payload); - - /** <summary> - * Create a new node derived from a token, with a new token type. - * This is invoked from an imaginary node ref on right side of a - * rewrite rule as IMAG[$tokenLabel]. - * </summary> - * - * <remarks> - * This should invoke createToken(Token). - * </remarks> - */ - object Create(int tokenType, IToken fromToken); - - /** <summary> - * Same as create(tokenType,fromToken) except set the text too. - * This is invoked from an imaginary node ref on right side of a - * rewrite rule as IMAG[$tokenLabel, "IMAG"]. - * </summary> - * - * <remarks> - * This should invoke createToken(Token). - * </remarks> - */ - object Create(int tokenType, IToken fromToken, string text); - - /** <summary> - * Same as create(fromToken) except set the text too. - * This is invoked when the <c>text</c> terminal option is set, as in - * IMAG<text='IMAG'>. - * </summary> - * - * <remarks> - * This should invoke createToken(Token). - * </remarks> - */ - object Create(IToken fromToken, string text); - - /** <summary> - * Create a new node derived from a token, with a new token type. - * This is invoked from an imaginary node ref on right side of a - * rewrite rule as IMAG["IMAG"]. - * </summary> - * - * <remarks> - * This should invoke createToken(int,String). - * </remarks> - */ - object Create(int tokenType, string text); - - /** <summary>Duplicate a single tree node.</summary> - * <remarks>Override if you want another kind of node to be built.</remarks> - */ - object DupNode(object treeNode); - - object DupNode(int type, object treeNode); - - object DupNode(object treeNode, string text); - - object DupNode(int type, object treeNode, string text); - - /** <summary>Duplicate tree recursively, using dupNode() for each node</summary> */ - object DupTree( object tree ); - - /** <summary> - * Return a nil node (an empty but non-null node) that can hold - * a list of element as the children. If you want a flat tree (a list) - * use "t=adaptor.nil(); t.addChild(x); t.addChild(y);" - * </summary> - */ - object Nil(); - - /** <summary> - * Return a tree node representing an error. This node records the - * tokens consumed during error recovery. The start token indicates the - * input symbol at which the error was detected. The stop token indicates - * the last symbol consumed during recovery. - * </summary> - * - * </remarks> - * You must specify the input stream so that the erroneous text can - * be packaged up in the error node. The exception could be useful - * to some applications; default implementation stores ptr to it in - * the CommonErrorNode. - * - * This only makes sense during token parsing, not tree parsing. - * Tree parsing should happen only when parsing and tree construction - * succeed. - * </remarks> - */ - object ErrorNode( ITokenStream input, IToken start, IToken stop, RecognitionException e ); - - /** <summary>Is tree considered a nil node used to make lists of child nodes?</summary> */ - bool IsNil( object tree ); - - /** <summary> - * Add a child to the tree t. If child is a flat tree (a list), make all - * in list children of t. Warning: if t has no children, but child does - * and child isNil then you can decide it is ok to move children to t via - * t.children = child.children; i.e., without copying the array. Just - * make sure that this is consistent with have the user will build - * ASTs. Do nothing if t or child is null. - * </summary> - */ - void AddChild( object t, object child ); - - /** <summary> - * If oldRoot is a nil root, just copy or move the children to newRoot. - * If not a nil root, make oldRoot a child of newRoot. - * </summary> - * - * <remarks> - * old=^(nil a b c), new=r yields ^(r a b c) - * old=^(a b c), new=r yields ^(r ^(a b c)) - * - * If newRoot is a nil-rooted single child tree, use the single - * child as the new root node. - * - * old=^(nil a b c), new=^(nil r) yields ^(r a b c) - * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) - * - * If oldRoot was null, it's ok, just return newRoot (even if isNil). - * - * old=null, new=r yields r - * old=null, new=^(nil r) yields ^(nil r) - * - * Return newRoot. Throw an exception if newRoot is not a - * simple node or nil root with a single child node--it must be a root - * node. If newRoot is ^(nil x) return x as newRoot. - * - * Be advised that it's ok for newRoot to point at oldRoot's - * children; i.e., you don't have to copy the list. We are - * constructing these nodes so we should have this control for - * efficiency. - * </remarks> - */ - object BecomeRoot( object newRoot, object oldRoot ); - - /** <summary> - * Given the root of the subtree created for this rule, post process - * it to do any simplifications or whatever you want. A required - * behavior is to convert ^(nil singleSubtree) to singleSubtree - * as the setting of start/stop indexes relies on a single non-nil root - * for non-flat trees. - * </summary> - * - * <remarks> - * Flat trees such as for lists like "idlist : ID+ ;" are left alone - * unless there is only one ID. For a list, the start/stop indexes - * are set in the nil node. - * - * This method is executed after all rule tree construction and right - * before setTokenBoundaries(). - * </remarks> - */ - object RulePostProcessing( object root ); - - /** <summary>For identifying trees.</summary> - * - * <remarks> - * How to identify nodes so we can say "add node to a prior node"? - * Even becomeRoot is an issue. Use System.identityHashCode(node) - * usually. - * </remarks> - */ - int GetUniqueID( object node ); - - - // R e w r i t e R u l e s - - /** <summary> - * Create a node for newRoot make it the root of oldRoot. - * If oldRoot is a nil root, just copy or move the children to newRoot. - * If not a nil root, make oldRoot a child of newRoot. - * </summary> - * - * <returns> - * Return node created for newRoot. - * </returns> - * - * <remarks> - * Be advised: when debugging ASTs, the DebugTreeAdaptor manually - * calls create(Token child) and then plain becomeRoot(node, node) - * because it needs to trap calls to create, but it can't since it delegates - * to not inherits from the TreeAdaptor. - * </remarks> - */ - object BecomeRoot( IToken newRoot, object oldRoot ); - - #endregion - - - #region Content - - /** <summary>For tree parsing, I need to know the token type of a node</summary> */ - int GetType( object t ); - - /** <summary>Node constructors can set the type of a node</summary> */ - void SetType( object t, int type ); - - string GetText( object t ); - - /** <summary>Node constructors can set the text of a node</summary> */ - void SetText( object t, string text ); - - /** <summary> - * Return the token object from which this node was created. - * Currently used only for printing an error message. - * The error display routine in BaseRecognizer needs to - * display where the input the error occurred. If your - * tree of limitation does not store information that can - * lead you to the token, you can create a token filled with - * the appropriate information and pass that back. See - * BaseRecognizer.getErrorMessage(). - * </summary> - */ - IToken GetToken( object t ); - - /** <summary> - * Where are the bounds in the input token stream for this node and - * all children? Each rule that creates AST nodes will call this - * method right before returning. Flat trees (i.e., lists) will - * still usually have a nil root node just to hold the children list. - * That node would contain the start/stop indexes then. - * </summary> - */ - void SetTokenBoundaries( object t, IToken startToken, IToken stopToken ); - - /** <summary>Get the token start index for this subtree; return -1 if no such index</summary> */ - int GetTokenStartIndex( object t ); - - /** <summary>Get the token stop index for this subtree; return -1 if no such index</summary> */ - int GetTokenStopIndex( object t ); - - #endregion - - - #region Navigation / Tree Parsing - - /** <summary>Get a child 0..n-1 node</summary> */ - object GetChild( object t, int i ); - - /** <summary>Set ith child (0..n-1) to t; t must be non-null and non-nil node</summary> */ - void SetChild( object t, int i, object child ); - - /** <summary>Remove ith child and shift children down from right.</summary> */ - object DeleteChild( object t, int i ); - - /** <summary>How many children? If 0, then this is a leaf node</summary> */ - int GetChildCount( object t ); - - /** <summary> - * Who is the parent node of this node; if null, implies node is root. - * If your node type doesn't handle this, it's ok but the tree rewrites - * in tree parsers need this functionality. - * </summary> - */ - object GetParent( object t ); - void SetParent( object t, object parent ); - - /** <summary> - * What index is this node in the child list? Range: 0..n-1 - * If your node type doesn't handle this, it's ok but the tree rewrites - * in tree parsers need this functionality. - * </summary> - */ - int GetChildIndex( object t ); - void SetChildIndex( object t, int index ); - - /** <summary> - * Replace from start to stop child index of parent with t, which might - * be a list. Number of children may be different after this call. - * </summary> - * - * <remarks> - * If parent is null, don't do anything; must be at root of overall tree. - * Can't replace whatever points to the parent externally. Do nothing. - * </remarks> - */ - void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ); - - #endregion - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs deleted file mode 100644 index 8f3f30a..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeNodeStream.cs +++ /dev/null @@ -1,146 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - /** <summary>A stream of tree nodes, accessing nodes from a tree of some kind</summary> */ - public interface ITreeNodeStream : IIntStream - { - /** <summary> - * Get a tree node at an absolute index i; 0..n-1. - * If you don't want to buffer up nodes, then this method makes no - * sense for you. - * </summary> - */ - object this[int i] - { - get; - } - - /** <summary> - * Get tree node at current input pointer + {@code k} ahead where - * {@code k==1} is next node. {@code k<0} indicates nodes in the past. So - * {@code LT(-1)} is previous node, but implementations are not required to - * provide results for {@code k < -1}. {@code LT(0)} is undefined. For - * {@code k<=n}, return {@code null}. Return {@code null} for {@code LT(0)} - * and any index that results in an absolute address that is negative. - * </summary> - * - * <remarks> - * This is analogous to {@link TokenStream#LT}, but this returns a tree node - * instead of a {@link Token}. Makes code generation identical for both - * parser and tree grammars. - * </remarks> - */ - object LT( int k ); - - /** <summary> - * Where is this stream pulling nodes from? This is not the name, but - * the object that provides node objects. - * </summary> - */ - object TreeSource - { - get; - } - - /** <summary> - * If the tree associated with this stream was created from a - * {@link TokenStream}, you can specify it here. Used to do rule - * {@code $text} attribute in tree parser. Optional unless you use tree - * parser rule {@code $text} attribute or {@code output=template} and - * {@code rewrite=true} options. - * </summary> - */ - ITokenStream TokenStream - { - get; - } - - /** <summary> - * What adaptor can tell me how to interpret/navigate nodes and - * trees. E.g., get text of a node. - * </summary> - */ - ITreeAdaptor TreeAdaptor - { - get; - } - - /** <summary> - * As we flatten the tree, we use {@link Token#UP}, {@link Token#DOWN} nodes - * to represent the tree structure. When debugging we need unique nodes so - * we have to instantiate new ones. When doing normal tree parsing, it's - * slow and a waste of memory to create unique navigation nodes. Default - * should be {@code false}. - * </summary> - */ - bool UniqueNavigationNodes - { - get; - set; - } - - /** <summary> - * Return the text of all nodes from {@code start} to {@code stop}, - * inclusive. If the stream does not buffer all the nodes then it can still - * walk recursively from start until stop. You can always return - * {@code null} or {@code ""} too, but users should not access - * {@code $ruleLabel.text} in an action of course in that case. - * </summary> - */ - string ToString( object start, object stop ); - - - #region REWRITING TREES (used by tree parser) - - /** <summary> - * Replace children of {@code parent} from index {@code startChildIndex} to - * {@code stopChildIndex} with {@code t}, which might be a list. Number of - * children may be different after this call. The stream is notified because - * it is walking the tree and might need to know you are monkeying with the - * underlying tree. Also, it might be able to modify the node stream to - * avoid restreaming for future phases. - * </summary> - * - * <remarks> - * If {@code parent} is {@code null}, don't do anything; must be at root of - * overall tree. Can't replace whatever points to the parent externally. Do - * nothing. - * </remarks> - */ - void ReplaceChildren( object parent, int startChildIndex, int stopChildIndex, object t ); - - #endregion - - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeVisitorAction.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeVisitorAction.cs deleted file mode 100644 index bcf0156..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ITreeVisitorAction.cs +++ /dev/null @@ -1,90 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using Antlr.Runtime.Misc; - - /** <summary> - * How to execute code for node t when a visitor visits node t. Execute - * pre() before visiting children and execute post() after visiting children. - * </summary> - */ - public interface ITreeVisitorAction - { - /** <summary> - * Execute an action before visiting children of t. Return t or - * a rewritten t. It is up to the visitor to decide what to do - * with the return value. Children of returned value will be - * visited if using TreeVisitor.visit(). - * </summary> - */ - object Pre( object t ); - - /** <summary> - * Execute an action after visiting children of t. Return t or - * a rewritten t. It is up to the visitor to decide what to do - * with the return value. - * </summary> - */ - object Post( object t ); - } - - public class TreeVisitorAction - : ITreeVisitorAction - { - private readonly Func<object, object> _preAction; - private readonly Func<object, object> _postAction; - - public TreeVisitorAction( Func<object, object> preAction, Func<object, object> postAction ) - { - _preAction = preAction; - _postAction = postAction; - } - - public object Pre( object t ) - { - if ( _preAction != null ) - return _preAction( t ); - - return t; - } - - public object Post( object t ) - { - if ( _postAction != null ) - return _postAction( t ); - - return t; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ParseTree.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ParseTree.cs deleted file mode 100644 index 149269a..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/ParseTree.cs +++ /dev/null @@ -1,167 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - - using StringBuilder = System.Text.StringBuilder; - - /** <summary> - * A record of the rules used to match a token sequence. The tokens - * end up as the leaves of this tree and rule nodes are the interior nodes. - * This really adds no functionality, it is just an alias for CommonTree - * that is more meaningful (specific) and holds a String to display for a node. - * </summary> - */ - [System.Serializable] - public class ParseTree : BaseTree - { - public object payload; - public List<IToken> hiddenTokens; - - public ParseTree( object label ) - { - this.payload = label; - } - - #region Properties - public override string Text - { - get - { - return ToString(); - } - set - { - } - } - public override int TokenStartIndex - { - get - { - return 0; - } - set - { - } - } - public override int TokenStopIndex - { - get - { - return 0; - } - set - { - } - } - public override int Type - { - get - { - return 0; - } - set - { - } - } - #endregion - - public override ITree DupNode() - { - return null; - } - - public override string ToString() - { - if ( payload is IToken ) - { - IToken t = (IToken)payload; - if ( t.Type == TokenTypes.EndOfFile ) - { - return "<EOF>"; - } - return t.Text; - } - return payload.ToString(); - } - - /** <summary> - * Emit a token and all hidden nodes before. EOF node holds all - * hidden tokens after last real token. - * </summary> - */ - public virtual string ToStringWithHiddenTokens() - { - StringBuilder buf = new StringBuilder(); - if ( hiddenTokens != null ) - { - for ( int i = 0; i < hiddenTokens.Count; i++ ) - { - IToken hidden = (IToken)hiddenTokens[i]; - buf.Append( hidden.Text ); - } - } - string nodeText = this.ToString(); - if ( !nodeText.Equals( "<EOF>" ) ) - buf.Append( nodeText ); - return buf.ToString(); - } - - /** <summary> - * Print out the leaves of this tree, which means printing original - * input back out. - * </summary> - */ - public virtual string ToInputString() - { - StringBuilder buf = new StringBuilder(); - ToStringLeaves( buf ); - return buf.ToString(); - } - - protected virtual void ToStringLeaves( StringBuilder buf ) - { - if ( payload is IToken ) - { // leaf node token? - buf.Append( this.ToStringWithHiddenTokens() ); - return; - } - for ( int i = 0; Children != null && i < Children.Count; i++ ) - { - ParseTree t = (ParseTree)Children[i]; - t.ToStringLeaves( buf ); - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteCardinalityException.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteCardinalityException.cs deleted file mode 100644 index 4590287..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteCardinalityException.cs +++ /dev/null @@ -1,96 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using ArgumentNullException = System.ArgumentNullException; - using Exception = System.Exception; - using SerializationInfo = System.Runtime.Serialization.SerializationInfo; - using StreamingContext = System.Runtime.Serialization.StreamingContext; - - /** <summary> - * Base class for all exceptions thrown during AST rewrite construction. - * This signifies a case where the cardinality of two or more elements - * in a subrule are different: (ID INT)+ where |ID|!=|INT| - * </summary> - */ - [System.Serializable] - public class RewriteCardinalityException : Exception - { - private readonly string _elementDescription; - - public RewriteCardinalityException() - { - } - - public RewriteCardinalityException(string elementDescription) - : this(elementDescription, elementDescription) - { - this._elementDescription = elementDescription; - } - - public RewriteCardinalityException(string elementDescription, Exception innerException) - : this(elementDescription, elementDescription, innerException) - { - } - - public RewriteCardinalityException(string message, string elementDescription) - : base(message) - { - _elementDescription = elementDescription; - } - - public RewriteCardinalityException(string message, string elementDescription, Exception innerException) - : base(message, innerException) - { - _elementDescription = elementDescription; - } - - protected RewriteCardinalityException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - if (info == null) - throw new ArgumentNullException("info"); - - _elementDescription = info.GetString("ElementDescription"); - } - - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - if (info == null) - throw new ArgumentNullException("info"); - - base.GetObjectData(info, context); - info.AddValue("ElementDescription", _elementDescription); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteEarlyExitException.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteEarlyExitException.cs deleted file mode 100644 index 43c51fe..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteEarlyExitException.cs +++ /dev/null @@ -1,72 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using Exception = System.Exception; - using SerializationInfo = System.Runtime.Serialization.SerializationInfo; - using StreamingContext = System.Runtime.Serialization.StreamingContext; - - /** <summary>No elements within a (...)+ in a rewrite rule</summary> */ - [System.Serializable] - public class RewriteEarlyExitException : RewriteCardinalityException - { - public RewriteEarlyExitException() - { - } - - public RewriteEarlyExitException(string elementDescription) - : base(elementDescription) - { - } - - public RewriteEarlyExitException(string elementDescription, Exception innerException) - : base(elementDescription, innerException) - { - } - - public RewriteEarlyExitException(string message, string elementDescription) - : base(message, elementDescription) - { - } - - public RewriteEarlyExitException(string message, string elementDescription, Exception innerException) - : base(message, elementDescription, innerException) - { - } - - protected RewriteEarlyExitException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteEmptyStreamException.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteEmptyStreamException.cs deleted file mode 100644 index 8390cd6..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteEmptyStreamException.cs +++ /dev/null @@ -1,72 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using Exception = System.Exception; - using SerializationInfo = System.Runtime.Serialization.SerializationInfo; - using StreamingContext = System.Runtime.Serialization.StreamingContext; - - /** <summary>Ref to ID or expr but no tokens in ID stream or subtrees in expr stream</summary> */ - [System.Serializable] - public class RewriteEmptyStreamException : RewriteCardinalityException - { - public RewriteEmptyStreamException() - { - } - - public RewriteEmptyStreamException(string elementDescription) - : base(elementDescription) - { - } - - public RewriteEmptyStreamException(string elementDescription, Exception innerException) - : base(elementDescription, innerException) - { - } - - public RewriteEmptyStreamException(string message, string elementDescription) - : base(message, elementDescription) - { - } - - public RewriteEmptyStreamException(string message, string elementDescription, Exception innerException) - : base(message, elementDescription, innerException) - { - } - - protected RewriteEmptyStreamException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs deleted file mode 100644 index cb76ace..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleElementStream.cs +++ /dev/null @@ -1,253 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - using IList = System.Collections.IList; - - /** <summary> - * A generic list of elements tracked in an alternative to be used in - * a -> rewrite rule. We need to subclass to fill in the next() method, - * which returns either an AST node wrapped around a token payload or - * an existing subtree. - * </summary> - * - * <remarks> - * Once you start next()ing, do not try to add more elements. It will - * break the cursor tracking I believe. - * - * TODO: add mechanism to detect/puke on modification after reading from stream - * </remarks> - * - * <see cref="RewriteRuleSubtreeStream"/> - * <see cref="RewriteRuleTokenStream"/> - */ - [System.Serializable] - public abstract class RewriteRuleElementStream - { - /** <summary> - * Cursor 0..n-1. If singleElement!=null, cursor is 0 until you next(), - * which bumps it to 1 meaning no more elements. - * </summary> - */ - protected int cursor = 0; - - /** <summary>Track single elements w/o creating a list. Upon 2nd add, alloc list */ - protected object singleElement; - - /** <summary>The list of tokens or subtrees we are tracking */ - protected IList elements; - - /** <summary>Once a node / subtree has been used in a stream, it must be dup'd - * from then on. Streams are reset after subrules so that the streams - * can be reused in future subrules. So, reset must set a dirty bit. - * If dirty, then next() always returns a dup.</summary> - */ - protected bool dirty = false; - - /** <summary>The element or stream description; usually has name of the token or - * rule reference that this list tracks. Can include rulename too, but - * the exception would track that info. - */ - protected string elementDescription; - protected ITreeAdaptor adaptor; - - public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription ) - { - this.elementDescription = elementDescription; - this.adaptor = adaptor; - } - - /** <summary>Create a stream with one element</summary> */ - public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) - : this( adaptor, elementDescription ) - { - Add( oneElement ); - } - - /** <summary>Create a stream, but feed off an existing list</summary> */ - public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, IList elements ) - : this( adaptor, elementDescription ) - { - this.singleElement = null; - this.elements = elements; - } - - /** <summary> - * Reset the condition of this stream so that it appears we have - * not consumed any of its elements. Elements themselves are untouched. - * Once we reset the stream, any future use will need duplicates. Set - * the dirty bit. - * </summary> - */ - public virtual void Reset() - { - cursor = 0; - dirty = true; - } - - public virtual void Add( object el ) - { - //System.out.println("add '"+elementDescription+"' is "+el); - if ( el == null ) - { - return; - } - if ( elements != null ) - { // if in list, just add - elements.Add( el ); - return; - } - if ( singleElement == null ) - { // no elements yet, track w/o list - singleElement = el; - return; - } - // adding 2nd element, move to list - elements = new List<object>( 5 ); - elements.Add( singleElement ); - singleElement = null; - elements.Add( el ); - } - - /** <summary> - * Return the next element in the stream. If out of elements, throw - * an exception unless size()==1. If size is 1, then return elements[0]. - * Return a duplicate node/subtree if stream is out of elements and - * size==1. If we've already used the element, dup (dirty bit set). - * </summary> - */ - public virtual object NextTree() - { - int n = Count; - if ( dirty || ( cursor >= n && n == 1 ) ) - { - // if out of elements and size is 1, dup - object el = NextCore(); - return Dup( el ); - } - // test size above then fetch - object el2 = NextCore(); - return el2; - } - - /** <summary> - * Do the work of getting the next element, making sure that it's - * a tree node or subtree. Deal with the optimization of single- - * element list versus list of size > 1. Throw an exception - * if the stream is empty or we're out of elements and size>1. - * protected so you can override in a subclass if necessary. - * </summary> - */ - protected virtual object NextCore() - { - int n = Count; - if ( n == 0 ) - { - throw new RewriteEmptyStreamException( elementDescription ); - } - if ( cursor >= n ) - { // out of elements? - if ( n == 1 ) - { // if size is 1, it's ok; return and we'll dup - return ToTree( singleElement ); - } - // out of elements and size was not 1, so we can't dup - throw new RewriteCardinalityException( elementDescription ); - } - // we have elements - if ( singleElement != null ) - { - cursor++; // move cursor even for single element list - return ToTree( singleElement ); - } - // must have more than one in list, pull from elements - object o = ToTree( elements[cursor] ); - cursor++; - return o; - } - - /** <summary> - * When constructing trees, sometimes we need to dup a token or AST - * subtree. Dup'ing a token means just creating another AST node - * around it. For trees, you must call the adaptor.dupTree() unless - * the element is for a tree root; then it must be a node dup. - * </summary> - */ - protected abstract object Dup( object el ); - - /** <summary> - * Ensure stream emits trees; tokens must be converted to AST nodes. - * AST nodes can be passed through unmolested. - * </summary> - */ - protected virtual object ToTree( object el ) - { - return el; - } - - public virtual bool HasNext - { - get - { - return ( singleElement != null && cursor < 1 ) || - ( elements != null && cursor < elements.Count ); - } - } - - public virtual int Count - { - get - { - int n = 0; - if ( singleElement != null ) - { - n = 1; - } - if ( elements != null ) - { - return elements.Count; - } - return n; - } - } - - public virtual string Description - { - get - { - return elementDescription; - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleNodeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleNodeStream.cs deleted file mode 100644 index eb434cc..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleNodeStream.cs +++ /dev/null @@ -1,81 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using IList = System.Collections.IList; - using NotSupportedException = System.NotSupportedException; - - /** <summary> - * Queues up nodes matched on left side of -> in a tree parser. This is - * the analog of RewriteRuleTokenStream for normal parsers. - * </summary> - */ - [System.Serializable] - public class RewriteRuleNodeStream : RewriteRuleElementStream - { - - public RewriteRuleNodeStream( ITreeAdaptor adaptor, string elementDescription ) - : base( adaptor, elementDescription ) - { - } - - /** <summary>Create a stream with one element</summary> */ - public RewriteRuleNodeStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) - : base( adaptor, elementDescription, oneElement ) - { - } - - /** <summary>Create a stream, but feed off an existing list</summary> */ - public RewriteRuleNodeStream( ITreeAdaptor adaptor, string elementDescription, IList elements ) - : base( adaptor, elementDescription, elements ) - { - } - - public virtual object NextNode() - { - return NextCore(); - } - - protected override object ToTree( object el ) - { - return adaptor.DupNode( el ); - } - - protected override object Dup( object el ) - { - // we dup every node, so don't have to worry about calling dup; short- - // circuited next() so it doesn't call. - throw new NotSupportedException( "dup can't be called for a node stream." ); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleSubtreeStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleSubtreeStream.cs deleted file mode 100644 index 504b891..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleSubtreeStream.cs +++ /dev/null @@ -1,99 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using IList = System.Collections.IList; - - [System.Serializable] - public class RewriteRuleSubtreeStream : RewriteRuleElementStream - { - public RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription ) - : base( adaptor, elementDescription ) - { - } - - /** <summary>Create a stream with one element</summary> */ - public RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) - : base( adaptor, elementDescription, oneElement ) - { - } - - /** <summary>Create a stream, but feed off an existing list</summary> */ - public RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription, IList elements ) - : base( adaptor, elementDescription, elements ) - { - } - - /** <summary> - * Treat next element as a single node even if it's a subtree. - * This is used instead of next() when the result has to be a - * tree root node. Also prevents us from duplicating recently-added - * children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration - * must dup the type node, but ID has been added. - * </summary> - * - * <remarks> - * Referencing a rule result twice is ok; dup entire tree as - * we can't be adding trees as root; e.g., expr expr. - * - * Hideous code duplication here with super.next(). Can't think of - * a proper way to refactor. This needs to always call dup node - * and super.next() doesn't know which to call: dup node or dup tree. - * </remarks> - */ - public virtual object NextNode() - { - //System.Console.WriteLine("nextNode: elements={0}, singleElement={1}", elements, ((ITree)singleElement).ToStringTree()); - int n = Count; - if ( dirty || ( cursor >= n && n == 1 ) ) - { - // if out of elements and size is 1, dup (at most a single node - // since this is for making root nodes). - object el = NextCore(); - return adaptor.DupNode( el ); - } - // test size above then fetch - object tree = NextCore(); - while (adaptor.IsNil(tree) && adaptor.GetChildCount(tree) == 1) - tree = adaptor.GetChild(tree, 0); - //System.Console.WriteLine("_next={0}", ((ITree)tree).ToStringTree()); - object el2 = adaptor.DupNode(tree); // dup just the root (want node here) - return el2; - } - - protected override object Dup( object el ) - { - return adaptor.DupTree( el ); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleTokenStream.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleTokenStream.cs deleted file mode 100644 index 4441e47..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/RewriteRuleTokenStream.cs +++ /dev/null @@ -1,86 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using IList = System.Collections.IList; - using NotSupportedException = System.NotSupportedException; - - [System.Serializable] - public class RewriteRuleTokenStream : RewriteRuleElementStream - { - - public RewriteRuleTokenStream( ITreeAdaptor adaptor, string elementDescription ) - : base( adaptor, elementDescription ) - { - } - - /** <summary>Create a stream with one element</summary> */ - public RewriteRuleTokenStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) - : base( adaptor, elementDescription, oneElement ) - { - } - - /** <summary>Create a stream, but feed off an existing list</summary> */ - public RewriteRuleTokenStream( ITreeAdaptor adaptor, string elementDescription, IList elements ) - : base( adaptor, elementDescription, elements ) - { - } - - /** <summary>Get next token from stream and make a node for it</summary> */ - public virtual object NextNode() - { - IToken t = (IToken)NextCore(); - return adaptor.Create( t ); - } - - public virtual IToken NextToken() - { - return (IToken)NextCore(); - } - - /** <summary> - * Don't convert to a tree unless they explicitly call nextTree. - * This way we can do hetero tree nodes in rewrite. - * </summary> - */ - protected override object ToTree( object el ) - { - return el; - } - - protected override object Dup( object el ) - { - throw new NotSupportedException( "dup can't be called for a token stream." ); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TemplateTreeRuleReturnScope`2.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TemplateTreeRuleReturnScope`2.cs deleted file mode 100644 index 96ac54f..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TemplateTreeRuleReturnScope`2.cs +++ /dev/null @@ -1,60 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - public class TemplateTreeRuleReturnScope<TTemplate, TTree> : TreeRuleReturnScope<TTree>, ITemplateRuleReturnScope<TTemplate>, ITemplateRuleReturnScope - { - private TTemplate _template; - - public TTemplate Template - { - get - { - return _template; - } - - set - { - _template = value; - } - } - - object ITemplateRuleReturnScope.Template - { - get - { - return Template; - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs deleted file mode 100644 index ba44e2d..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeFilter.cs +++ /dev/null @@ -1,99 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using Antlr.Runtime.Misc; - - public class TreeFilter : TreeParser - { - protected ITokenStream originalTokenStream; - protected ITreeAdaptor originalAdaptor; - - public TreeFilter( ITreeNodeStream input ) - : this( input, new RecognizerSharedState() ) - { - } - public TreeFilter( ITreeNodeStream input, RecognizerSharedState state ) - : base( input, state ) - { - originalAdaptor = input.TreeAdaptor; - originalTokenStream = input.TokenStream; - } - - public virtual void ApplyOnce( object t, Action whichRule ) - { - if ( t == null ) - return; - - try - { - // share TreeParser object but not parsing-related state - SetState(new RecognizerSharedState()); - SetTreeNodeStream(new CommonTreeNodeStream(originalAdaptor, t)); - ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream; - BacktrackingLevel = 1; - whichRule(); - BacktrackingLevel = 0; - } - catch ( RecognitionException ) - { - } - } - - public virtual void Downup( object t ) - { - TreeVisitor v = new TreeVisitor( new CommonTreeAdaptor() ); - Func<object, object> pre = ( o ) => - { - ApplyOnce( o, Topdown ); - return o; - }; - Func<object, object> post = ( o ) => - { - ApplyOnce( o, Bottomup ); - return o; - }; - v.Visit( t, pre, post ); - } - - // methods the downup strategy uses to do the up and down rules. - // to override, just define tree grammar rule topdown and turn on - // filter=true. - protected virtual void Topdown() - { - } - protected virtual void Bottomup() - { - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeIterator.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeIterator.cs deleted file mode 100644 index 02549d5..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeIterator.cs +++ /dev/null @@ -1,180 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - - /** Return a node stream from a doubly-linked tree whose nodes - * know what child index they are. No remove() is supported. - * - * Emit navigation nodes (DOWN, UP, and EOF) to let show tree structure. - */ - public class TreeIterator : IEnumerator<object> - { - protected ITreeAdaptor adaptor; - protected object root; - protected object tree; - protected bool firstTime = true; - private bool reachedEof; - - // navigation nodes to return during walk and at end - public object up; - public object down; - public object eof; - - /** If we emit UP/DOWN nodes, we need to spit out multiple nodes per - * next() call. - */ - protected Queue<object> nodes; - - public TreeIterator( CommonTree tree ) - : this( new CommonTreeAdaptor(), tree ) - { - } - - public TreeIterator( ITreeAdaptor adaptor, object tree ) - { - this.adaptor = adaptor; - this.tree = tree; - this.root = tree; - nodes = new Queue<object>(); - down = adaptor.Create( TokenTypes.Down, "DOWN" ); - up = adaptor.Create( TokenTypes.Up, "UP" ); - eof = adaptor.Create( TokenTypes.EndOfFile, "EOF" ); - } - - #region IEnumerator<object> Members - - public object Current - { - get; - private set; - } - - #endregion - - #region IDisposable Members - - public void Dispose() - { - } - - #endregion - - #region IEnumerator Members - - public bool MoveNext() - { - if ( firstTime ) - { - // initial condition - firstTime = false; - if ( adaptor.GetChildCount( tree ) == 0 ) - { - // single node tree (special) - nodes.Enqueue( eof ); - } - Current = tree; - } - else - { - // if any queued up, use those first - if ( nodes != null && nodes.Count > 0 ) - { - Current = nodes.Dequeue(); - } - else - { - // no nodes left? - if ( tree == null ) - { - Current = eof; - } - else - { - // next node will be child 0 if any children - if ( adaptor.GetChildCount( tree ) > 0 ) - { - tree = adaptor.GetChild( tree, 0 ); - nodes.Enqueue( tree ); // real node is next after DOWN - Current = down; - } - else - { - // if no children, look for next sibling of tree or ancestor - object parent = adaptor.GetParent( tree ); - // while we're out of siblings, keep popping back up towards root - while ( parent != null && - adaptor.GetChildIndex( tree ) + 1 >= adaptor.GetChildCount( parent ) ) - { - nodes.Enqueue( up ); // we're moving back up - tree = parent; - parent = adaptor.GetParent( tree ); - } - - // no nodes left? - if ( parent == null ) - { - tree = null; // back at root? nothing left then - nodes.Enqueue( eof ); // add to queue, might have UP nodes in there - Current = nodes.Dequeue(); - } - else - { - // must have found a node with an unvisited sibling - // move to it and return it - int nextSiblingIndex = adaptor.GetChildIndex( tree ) + 1; - tree = adaptor.GetChild( parent, nextSiblingIndex ); - nodes.Enqueue( tree ); // add to queue, might have UP nodes in there - Current = nodes.Dequeue(); - } - } - } - } - } - - bool result = Current != eof || !reachedEof; - reachedEof = Current == eof; - return result; - } - - public void Reset() - { - firstTime = true; - tree = root; - nodes.Clear(); - } - - #endregion - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs deleted file mode 100644 index f5a1508..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeParser.cs +++ /dev/null @@ -1,209 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using ConditionalAttribute = System.Diagnostics.ConditionalAttribute; - using Regex = System.Text.RegularExpressions.Regex; - using RegexOptions = System.Text.RegularExpressions.RegexOptions; - - /** <summary> - * A parser for a stream of tree nodes. "tree grammars" result in a subclass - * of this. All the error reporting and recovery is shared with Parser via - * the BaseRecognizer superclass. - * </summary> - */ - public class TreeParser : BaseRecognizer - { - public const int DOWN = TokenTypes.Down; - public const int UP = TokenTypes.Up; - - // precompiled regex used by inContext - static string dotdot = ".*[^.]\\.\\.[^.].*"; - static string doubleEtc = ".*\\.\\.\\.\\s+\\.\\.\\..*"; - static Regex dotdotPattern = new Regex( dotdot, RegexOptions.Compiled ); - static Regex doubleEtcPattern = new Regex( doubleEtc, RegexOptions.Compiled ); - - protected ITreeNodeStream input; - - public TreeParser( ITreeNodeStream input ) - : base() // highlight that we go to super to set state object - { - this.input = input; - } - - public TreeParser( ITreeNodeStream input, RecognizerSharedState state ) - : base( state ) // share the state object with another parser - { - this.input = input; - } - - public override void Reset() - { - base.Reset(); // reset all recognizer state variables - if ( input != null ) - { - input.Seek( 0 ); // rewind the input - } - } - - /** <summary>Set the input stream</summary> */ - public virtual void SetTreeNodeStream( ITreeNodeStream input ) - { - this.input = input; - } - - public virtual ITreeNodeStream GetTreeNodeStream() - { - return input; - } - - public override string SourceName - { - get - { - return input.SourceName; - } - } - - protected override object GetCurrentInputSymbol( IIntStream input ) - { - return ( (ITreeNodeStream)input ).LT( 1 ); - } - - protected override object GetMissingSymbol( IIntStream input, - RecognitionException e, - int expectedTokenType, - BitSet follow ) - { - string tokenText = - "<missing " + TokenNames[expectedTokenType] + ">"; - ITreeAdaptor adaptor = ((ITreeNodeStream)e.Input).TreeAdaptor; - return adaptor.Create(new CommonToken(expectedTokenType, tokenText)); - } - - /** <summary> - * Match '.' in tree parser has special meaning. Skip node or - * entire tree if node has children. If children, scan until - * corresponding UP node. - * </summary> - */ - public override void MatchAny( IIntStream ignore ) - { - state.errorRecovery = false; - state.failed = false; - // always consume the current node - input.Consume(); - // if the next node is DOWN, then the current node is a subtree: - // skip to corresponding UP. must count nesting level to get right UP - int look = input.LA( 1 ); - if ( look == DOWN ) - { - input.Consume(); - int level = 1; - while ( level > 0 ) - { - switch ( input.LA( 1 ) ) - { - case DOWN: - level++; - break; - case UP: - level--; - break; - case TokenTypes.EndOfFile: - return; - default: - break; - } - input.Consume(); - } - } - } - - /** <summary> - * We have DOWN/UP nodes in the stream that have no line info; override. - * plus we want to alter the exception type. Don't try to recover - * from tree parser errors inline... - * </summary> - */ - protected override object RecoverFromMismatchedToken( IIntStream input, int ttype, BitSet follow ) - { - throw new MismatchedTreeNodeException( ttype, (ITreeNodeStream)input ); - } - - /** <summary> - * Prefix error message with the grammar name because message is - * always intended for the programmer because the parser built - * the input tree not the user. - * </summary> - */ - public override string GetErrorHeader( RecognitionException e ) - { - return GrammarFileName + ": node from " + - ( e.ApproximateLineInfo ? "after " : "" ) + "line " + e.Line + ":" + e.CharPositionInLine; - } - - /** <summary> - * Tree parsers parse nodes they usually have a token object as - * payload. Set the exception token and do the default behavior. - * </summary> - */ - public override string GetErrorMessage( RecognitionException e, string[] tokenNames ) - { - if ( this is TreeParser ) - { - ITreeAdaptor adaptor = ( (ITreeNodeStream)e.Input ).TreeAdaptor; - e.Token = adaptor.GetToken( e.Node ); - if ( e.Token == null ) - { // could be an UP/DOWN node - e.Token = new CommonToken( adaptor.GetType( e.Node ), - adaptor.GetText( e.Node ) ); - } - } - return base.GetErrorMessage( e, tokenNames ); - } - - [Conditional("ANTLR_TRACE")] - public virtual void TraceIn( string ruleName, int ruleIndex ) - { - base.TraceIn( ruleName, ruleIndex, input.LT( 1 ) ); - } - - [Conditional("ANTLR_TRACE")] - public virtual void TraceOut( string ruleName, int ruleIndex ) - { - base.TraceOut( ruleName, ruleIndex, input.LT( 1 ) ); - } - - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreePatternLexer.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreePatternLexer.cs deleted file mode 100644 index e742205..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreePatternLexer.cs +++ /dev/null @@ -1,162 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using StringBuilder = System.Text.StringBuilder; - - public class TreePatternLexer - { - public const int Begin = 1; - public const int End = 2; - public const int Id = 3; - public const int Arg = 4; - public const int Percent = 5; - public const int Colon = 6; - public const int Dot = 7; - - /** <summary>The tree pattern to lex like "(A B C)"</summary> */ - protected string pattern; - - /** <summary>Index into input string</summary> */ - protected int p = -1; - - /** <summary>Current char</summary> */ - protected int c; - - /** <summary>How long is the pattern in char?</summary> */ - protected int n; - - /** <summary>Set when token type is ID or ARG (name mimics Java's StreamTokenizer)</summary> */ - public StringBuilder sval = new StringBuilder(); - - public bool error = false; - - public TreePatternLexer( string pattern ) - { - this.pattern = pattern; - this.n = pattern.Length; - Consume(); - } - - public virtual int NextToken() - { - sval.Length = 0; // reset, but reuse buffer - while ( c != CharStreamConstants.EndOfFile ) - { - if ( c == ' ' || c == '\n' || c == '\r' || c == '\t' ) - { - Consume(); - continue; - } - if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || c == '_' ) - { - sval.Append( (char)c ); - Consume(); - while ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || - ( c >= '0' && c <= '9' ) || c == '_' ) - { - sval.Append( (char)c ); - Consume(); - } - return Id; - } - if ( c == '(' ) - { - Consume(); - return Begin; - } - if ( c == ')' ) - { - Consume(); - return End; - } - if ( c == '%' ) - { - Consume(); - return Percent; - } - if ( c == ':' ) - { - Consume(); - return Colon; - } - if ( c == '.' ) - { - Consume(); - return Dot; - } - if ( c == '[' ) - { - // grab [x] as a string, returning x - Consume(); - while ( c != ']' ) - { - if ( c == '\\' ) - { - Consume(); - if ( c != ']' ) - { - sval.Append( '\\' ); - } - sval.Append( (char)c ); - } - else - { - sval.Append( (char)c ); - } - Consume(); - } - Consume(); - return Arg; - } - Consume(); - error = true; - return CharStreamConstants.EndOfFile; - } - return CharStreamConstants.EndOfFile; - } - - protected virtual void Consume() - { - p++; - if ( p >= n ) - { - c = CharStreamConstants.EndOfFile; - } - else - { - c = pattern[p]; - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreePatternParser.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreePatternParser.cs deleted file mode 100644 index 5112335..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreePatternParser.cs +++ /dev/null @@ -1,183 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using InvalidOperationException = System.InvalidOperationException; - - public class TreePatternParser - { - protected TreePatternLexer tokenizer; - protected int ttype; - protected TreeWizard wizard; - protected ITreeAdaptor adaptor; - - public TreePatternParser( TreePatternLexer tokenizer, TreeWizard wizard, ITreeAdaptor adaptor ) - { - this.tokenizer = tokenizer; - this.wizard = wizard; - this.adaptor = adaptor; - ttype = tokenizer.NextToken(); // kickstart - } - - public virtual object Pattern() - { - if ( ttype == TreePatternLexer.Begin ) - { - return ParseTree(); - } - else if ( ttype == TreePatternLexer.Id ) - { - object node = ParseNode(); - if ( ttype == CharStreamConstants.EndOfFile ) - { - return node; - } - return null; // extra junk on end - } - return null; - } - - public virtual object ParseTree() - { - if ( ttype != TreePatternLexer.Begin ) - throw new InvalidOperationException("No beginning."); - - ttype = tokenizer.NextToken(); - object root = ParseNode(); - if ( root == null ) - { - return null; - } - while ( ttype == TreePatternLexer.Begin || - ttype == TreePatternLexer.Id || - ttype == TreePatternLexer.Percent || - ttype == TreePatternLexer.Dot ) - { - if ( ttype == TreePatternLexer.Begin ) - { - object subtree = ParseTree(); - adaptor.AddChild( root, subtree ); - } - else - { - object child = ParseNode(); - if ( child == null ) - { - return null; - } - adaptor.AddChild( root, child ); - } - } - - if ( ttype != TreePatternLexer.End ) - throw new InvalidOperationException("No end."); - - ttype = tokenizer.NextToken(); - return root; - } - - public virtual object ParseNode() - { - // "%label:" prefix - string label = null; - if ( ttype == TreePatternLexer.Percent ) - { - ttype = tokenizer.NextToken(); - if ( ttype != TreePatternLexer.Id ) - { - return null; - } - label = tokenizer.sval.ToString(); - ttype = tokenizer.NextToken(); - if ( ttype != TreePatternLexer.Colon ) - { - return null; - } - ttype = tokenizer.NextToken(); // move to ID following colon - } - - // Wildcard? - if ( ttype == TreePatternLexer.Dot ) - { - ttype = tokenizer.NextToken(); - IToken wildcardPayload = new CommonToken( 0, "." ); - TreeWizard.TreePattern node = - new TreeWizard.WildcardTreePattern( wildcardPayload ); - if ( label != null ) - { - node.label = label; - } - return node; - } - - // "ID" or "ID[arg]" - if ( ttype != TreePatternLexer.Id ) - { - return null; - } - string tokenName = tokenizer.sval.ToString(); - ttype = tokenizer.NextToken(); - if ( tokenName.Equals( "nil" ) ) - { - return adaptor.Nil(); - } - string text = tokenName; - // check for arg - string arg = null; - if ( ttype == TreePatternLexer.Arg ) - { - arg = tokenizer.sval.ToString(); - text = arg; - ttype = tokenizer.NextToken(); - } - - // create node - int treeNodeType = wizard.GetTokenType( tokenName ); - if ( treeNodeType == TokenTypes.Invalid ) - { - return null; - } - object node2; - node2 = adaptor.Create( treeNodeType, text ); - if ( label != null && node2.GetType() == typeof( TreeWizard.TreePattern ) ) - { - ( (TreeWizard.TreePattern)node2 ).label = label; - } - if ( arg != null && node2.GetType() == typeof( TreeWizard.TreePattern ) ) - { - ( (TreeWizard.TreePattern)node2 ).hasTextArg = true; - } - return node2; - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs deleted file mode 100644 index 16a38a2..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRewriter.cs +++ /dev/null @@ -1,144 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using Antlr.Runtime.Misc; - - using Console = System.Console; - - public class TreeRewriter : TreeParser - { - protected bool showTransformations; - - protected ITokenStream originalTokenStream; - protected ITreeAdaptor originalAdaptor; - - Func<IAstRuleReturnScope> topdown_func; - Func<IAstRuleReturnScope> bottomup_func; - - public TreeRewriter( ITreeNodeStream input ) - : this( input, new RecognizerSharedState() ) - { - } - public TreeRewriter( ITreeNodeStream input, RecognizerSharedState state ) - : base( input, state ) - { - originalAdaptor = input.TreeAdaptor; - originalTokenStream = input.TokenStream; - topdown_func = () => Topdown(); - bottomup_func = () => Bottomup(); - } - - public virtual object ApplyOnce( object t, Func<IAstRuleReturnScope> whichRule ) - { - if ( t == null ) - return null; - - try - { - // share TreeParser object but not parsing-related state - SetState(new RecognizerSharedState()); - SetTreeNodeStream(new CommonTreeNodeStream(originalAdaptor, t)); - ( (CommonTreeNodeStream)input ).TokenStream = originalTokenStream; - BacktrackingLevel = 1; - IAstRuleReturnScope r = whichRule(); - BacktrackingLevel = 0; - if ( Failed ) - return t; - - if (showTransformations && r != null && !t.Equals(r.Tree) && r.Tree != null) - ReportTransformation(t, r.Tree); - - if ( r != null && r.Tree != null ) - return r.Tree; - else - return t; - } - catch ( RecognitionException ) - { - } - - return t; - } - - public virtual object ApplyRepeatedly( object t, Func<IAstRuleReturnScope> whichRule ) - { - bool treeChanged = true; - while ( treeChanged ) - { - object u = ApplyOnce( t, whichRule ); - treeChanged = !t.Equals( u ); - t = u; - } - return t; - } - - public virtual object Downup( object t ) - { - return Downup( t, false ); - } - - public virtual object Downup( object t, bool showTransformations ) - { - this.showTransformations = showTransformations; - TreeVisitor v = new TreeVisitor( new CommonTreeAdaptor() ); - t = v.Visit( t, ( o ) => ApplyOnce( o, topdown_func ), ( o ) => ApplyRepeatedly( o, bottomup_func ) ); - return t; - } - - // methods the downup strategy uses to do the up and down rules. - // to override, just define tree grammar rule topdown and turn on - // filter=true. - protected virtual IAstRuleReturnScope Topdown() - { - return null; - } - - protected virtual IAstRuleReturnScope Bottomup() - { - return null; - } - - /** Override this if you need transformation tracing to go somewhere - * other than stdout or if you're not using ITree-derived trees. - */ - protected virtual void ReportTransformation(object oldTree, object newTree) - { - ITree old = oldTree as ITree; - ITree @new = newTree as ITree; - string oldMessage = old != null ? old.ToStringTree() : "??"; - string newMessage = @new != null ? @new.ToStringTree() : "??"; - Console.WriteLine("{0} -> {1}", oldMessage, newMessage); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRuleReturnScope`1.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRuleReturnScope`1.cs deleted file mode 100644 index 2a928e2..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeRuleReturnScope`1.cs +++ /dev/null @@ -1,84 +0,0 @@ -/* - * [The "BSD license"] - * Copyright (c) 2011 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2011 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - /** <summary> - * This is identical to the ParserRuleReturnScope except that - * the start property is a tree nodes not Token object - * when you are parsing trees. - * </summary> - */ - [System.Serializable] - public class TreeRuleReturnScope<TTree> : IRuleReturnScope<TTree> - { - private TTree _start; - - /** <summary>Gets the first node or root node of tree matched for this rule.</summary> */ - public TTree Start - { - get - { - return _start; - } - - set - { - _start = value; - } - } - - object IRuleReturnScope.Start - { - get - { - return Start; - } - } - - TTree IRuleReturnScope<TTree>.Stop - { - get - { - return default(TTree); - } - } - - object IRuleReturnScope.Stop - { - get - { - return default(TTree); - } - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeVisitor.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeVisitor.cs deleted file mode 100644 index f6e49ea..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeVisitor.cs +++ /dev/null @@ -1,83 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -namespace Antlr.Runtime.Tree -{ - using Antlr.Runtime.Misc; - - /** <summary>Do a depth first walk of a tree, applying pre() and post() actions as we go.</summary> */ - public class TreeVisitor - { - protected ITreeAdaptor adaptor; - - public TreeVisitor( ITreeAdaptor adaptor ) - { - this.adaptor = adaptor; - } - public TreeVisitor() - : this( new CommonTreeAdaptor() ) - { - } - - /** <summary> - * Visit every node in tree t and trigger an action for each node - * before/after having visited all of its children. Bottom up walk. - * Execute both actions even if t has no children. Ignore return - * results from transforming children since they will have altered - * the child list of this node (their parent). Return result of - * applying post action to this node. - * </summary> - */ - public object Visit( object t, ITreeVisitorAction action ) - { - // System.out.println("visit "+((Tree)t).toStringTree()); - bool isNil = adaptor.IsNil( t ); - if ( action != null && !isNil ) - { - t = action.Pre( t ); // if rewritten, walk children of new t - } - for ( int i = 0; i < adaptor.GetChildCount(t); i++ ) - { - object child = adaptor.GetChild( t, i ); - Visit( child, action ); - } - if ( action != null && !isNil ) - t = action.Post( t ); - return t; - } - - public object Visit( object t, Func<object, object> preAction, Func<object, object> postAction ) - { - return Visit( t, new TreeVisitorAction( preAction, postAction ) ); - } - } -} diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeWizard.cs b/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeWizard.cs deleted file mode 100644 index 98c759b..0000000 --- a/runtime/CSharp3/Sources/Antlr3.Runtime/Tree/TreeWizard.cs +++ /dev/null @@ -1,736 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2005-2008 Terence Parr - * All rights reserved. - * - * Conversion to C#: - * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -// TODO: build indexes for wizard -//#define BUILD_INDEXES - -namespace Antlr.Runtime.Tree -{ - using System.Collections.Generic; - - using IList = System.Collections.IList; -#if BUILD_INDEXES - using IDictionary = System.Collections.IDictionary; -#endif - - /** <summary> - * Build and navigate trees with this object. Must know about the names - * of tokens so you have to pass in a map or array of token names (from which - * this class can build the map). I.e., Token DECL means nothing unless the - * class can translate it to a token type. - * </summary> - * - * <remarks> - * In order to create nodes and navigate, this class needs a TreeAdaptor. - * - * This class can build a token type -> node index for repeated use or for - * iterating over the various nodes with a particular type. - * - * This class works in conjunction with the TreeAdaptor rather than moving - * all this functionality into the adaptor. An adaptor helps build and - * navigate trees using methods. This class helps you do it with string - * patterns like "(A B C)". You can create a tree from that pattern or - * match subtrees against it. - * </remarks> - */ - public class TreeWizard - { - protected ITreeAdaptor adaptor; - protected IDictionary<string, int> tokenNameToTypeMap; - - public interface IContextVisitor - { - // TODO: should this be called visit or something else? - void Visit( object t, object parent, int childIndex, IDictionary<string, object> labels ); - } - - public abstract class Visitor : IContextVisitor - { - public virtual void Visit( object t, object parent, int childIndex, IDictionary<string, object> labels ) - { - Visit( t ); - } - public abstract void Visit( object t ); - } - - class ActionVisitor : Visitor - { - System.Action<object> _action; - - public ActionVisitor( System.Action<object> action ) - { - _action = action; - } - - public override void Visit( object t ) - { - _action( t ); - } - } - - /** <summary> - * When using %label:TOKENNAME in a tree for parse(), we must - * track the label. - * </summary> - */ - public class TreePattern : CommonTree - { - public string label; - public bool hasTextArg; - public TreePattern( IToken payload ) : - base( payload ) - { - } - public override string ToString() - { - if ( label != null ) - { - return "%" + label + ":"; //+ base.ToString(); - } - else - { - return base.ToString(); - } - } - } - - public class WildcardTreePattern : TreePattern - { - public WildcardTreePattern( IToken payload ) : - base( payload ) - { - } - } - - /** <summary>This adaptor creates TreePattern objects for use during scan()</summary> */ - public class TreePatternTreeAdaptor : CommonTreeAdaptor - { - public override object Create( IToken payload ) - { - return new TreePattern( payload ); - } - } - -#if BUILD_INDEXES - // TODO: build indexes for the wizard - - /** <summary> - * During fillBuffer(), we can make a reverse index from a set - * of token types of interest to the list of indexes into the - * node stream. This lets us convert a node pointer to a - * stream index semi-efficiently for a list of interesting - * nodes such as function definition nodes (you'll want to seek - * to their bodies for an interpreter). Also useful for doing - * dynamic searches; i.e., go find me all PLUS nodes. - * </summary> - */ - protected IDictionary<int, IList<int>> tokenTypeToStreamIndexesMap; - - /** <summary> - * If tokenTypesToReverseIndex set to INDEX_ALL then indexing - * occurs for all token types. - * </summary> - */ - public static readonly HashSet<int> INDEX_ALL = new HashSet<int>(); - - /** <summary> - * A set of token types user would like to index for faster lookup. - * If this is INDEX_ALL, then all token types are tracked. If null, - * then none are indexed. - * </summary> - */ - protected HashSet<int> tokenTypesToReverseIndex = null; -#endif - - public TreeWizard( ITreeAdaptor adaptor ) - { - this.adaptor = adaptor; - } - - public TreeWizard( ITreeAdaptor adaptor, IDictionary<string, int> tokenNameToTypeMap ) - { - this.adaptor = adaptor; - this.tokenNameToTypeMap = tokenNameToTypeMap; - } - - public TreeWizard( ITreeAdaptor adaptor, string[] tokenNames ) - { - this.adaptor = adaptor; - this.tokenNameToTypeMap = ComputeTokenTypes( tokenNames ); - } - - public TreeWizard( string[] tokenNames ) - : this( new CommonTreeAdaptor(), tokenNames ) - { - } - - /** <summary> - * Compute a Map<String, Integer> that is an inverted index of - * tokenNames (which maps int token types to names). - * </summary> - */ - public virtual IDictionary<string, int> ComputeTokenTypes( string[] tokenNames ) - { - IDictionary<string, int> m = new Dictionary<string, int>(); - if ( tokenNames == null ) - { - return m; - } - for ( int ttype = TokenTypes.Min; ttype < tokenNames.Length; ttype++ ) - { - string name = tokenNames[ttype]; - m[name] = ttype; - } - return m; - } - - /** <summary>Using the map of token names to token types, return the type.</summary> */ - public virtual int GetTokenType( string tokenName ) - { - if ( tokenNameToTypeMap == null ) - { - return TokenTypes.Invalid; - } - - int value; - if ( tokenNameToTypeMap.TryGetValue( tokenName, out value ) ) - return value; - - return TokenTypes.Invalid; - } - - /** <summary> - * Walk the entire tree and make a node name to nodes mapping. - * For now, use recursion but later nonrecursive version may be - * more efficient. Returns Map<Integer, List> where the List is - * of your AST node type. The Integer is the token type of the node. - * </summary> - * - * <remarks> - * TODO: save this index so that find and visit are faster - * </remarks> - */ - public IDictionary<int, IList> Index( object t ) - { - IDictionary<int, IList> m = new Dictionary<int, IList>(); - IndexCore( t, m ); - return m; - } - - /** <summary>Do the work for index</summary> */ - protected virtual void IndexCore( object t, IDictionary<int, IList> m ) - { - if ( t == null ) - { - return; - } - int ttype = adaptor.GetType( t ); - IList elements; - if ( !m.TryGetValue( ttype, out elements ) || elements == null ) - { - elements = new List<object>(); - m[ttype] = elements; - } - elements.Add( t ); - int n = adaptor.GetChildCount( t ); - for ( int i = 0; i < n; i++ ) - { - object child = adaptor.GetChild( t, i ); - IndexCore( child, m ); - } - } - - class FindTreeWizardVisitor : TreeWizard.Visitor - { - IList _nodes; - public FindTreeWizardVisitor( IList nodes ) - { - _nodes = nodes; - } - public override void Visit( object t ) - { - _nodes.Add( t ); - } - } - class FindTreeWizardContextVisitor : TreeWizard.IContextVisitor - { - TreeWizard _outer; - TreePattern _tpattern; - IList _subtrees; - public FindTreeWizardContextVisitor( TreeWizard outer, TreePattern tpattern, IList subtrees ) - { - _outer = outer; - _tpattern = tpattern; - _subtrees = subtrees; - } - - public void Visit( object t, object parent, int childIndex, IDictionary<string, object> labels ) - { - if ( _outer.ParseCore( t, _tpattern, null ) ) - { - _subtrees.Add( t ); - } - } - } - - /** <summary>Return a List of tree nodes with token type ttype</summary> */ - public virtual IList Find( object t, int ttype ) - { - IList nodes = new List<object>(); - Visit( t, ttype, new FindTreeWizardVisitor( nodes ) ); - return nodes; - } - - /** <summary>Return a List of subtrees matching pattern.</summary> */ - public virtual IList Find( object t, string pattern ) - { - IList subtrees = new List<object>(); - // Create a TreePattern from the pattern - TreePatternLexer tokenizer = new TreePatternLexer( pattern ); - TreePatternParser parser = - new TreePatternParser( tokenizer, this, new TreePatternTreeAdaptor() ); - TreePattern tpattern = (TreePattern)parser.Pattern(); - // don't allow invalid patterns - if ( tpattern == null || - tpattern.IsNil || - tpattern.GetType() == typeof( WildcardTreePattern ) ) - { - return null; - } - int rootTokenType = tpattern.Type; - Visit( t, rootTokenType, new FindTreeWizardContextVisitor( this, tpattern, subtrees ) ); - return subtrees; - } - - public virtual object FindFirst( object t, int ttype ) - { - return null; - } - - public virtual object FindFirst( object t, string pattern ) - { - return null; - } - - /** <summary> - * Visit every ttype node in t, invoking the visitor. This is a quicker - * version of the general visit(t, pattern) method. The labels arg - * of the visitor action method is never set (it's null) since using - * a token type rather than a pattern doesn't let us set a label. - * </summary> - */ - public void Visit( object t, int ttype, IContextVisitor visitor ) - { - VisitCore( t, null, 0, ttype, visitor ); - } - - public void Visit( object t, int ttype, System.Action<object> action ) - { - Visit( t, ttype, new ActionVisitor( action ) ); - } - - /** <summary>Do the recursive work for visit</summary> */ - protected virtual void VisitCore( object t, object parent, int childIndex, int ttype, IContextVisitor visitor ) - { - if ( t == null ) - { - return; - } - if ( adaptor.GetType( t ) == ttype ) - { - visitor.Visit( t, parent, childIndex, null ); - } - int n = adaptor.GetChildCount( t ); - for ( int i = 0; i < n; i++ ) - { - object child = adaptor.GetChild( t, i ); - VisitCore( child, t, i, ttype, visitor ); - } - } - - class VisitTreeWizardContextVisitor : TreeWizard.IContextVisitor - { - TreeWizard _outer; - IContextVisitor _visitor; - IDictionary<string, object> _labels; - TreePattern _tpattern; - - public VisitTreeWizardContextVisitor( TreeWizard outer, IContextVisitor visitor, IDictionary<string, object> labels, TreePattern tpattern ) - { - _outer = outer; - _visitor = visitor; - _labels = labels; - _tpattern = tpattern; - } - - public void Visit( object t, object parent, int childIndex, IDictionary<string, object> unusedlabels ) - { - // the unusedlabels arg is null as visit on token type doesn't set. - _labels.Clear(); - if ( _outer.ParseCore( t, _tpattern, _labels ) ) - { - _visitor.Visit( t, parent, childIndex, _labels ); - } - } - } - - /** <summary> - * For all subtrees that match the pattern, execute the visit action. - * The implementation uses the root node of the pattern in combination - * with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. - * Patterns with wildcard roots are also not allowed. - * </summary> - */ - public void Visit( object t, string pattern, IContextVisitor visitor ) - { - // Create a TreePattern from the pattern - TreePatternLexer tokenizer = new TreePatternLexer( pattern ); - TreePatternParser parser = - new TreePatternParser( tokenizer, this, new TreePatternTreeAdaptor() ); - TreePattern tpattern = (TreePattern)parser.Pattern(); - // don't allow invalid patterns - if ( tpattern == null || - tpattern.IsNil || - tpattern.GetType() == typeof( WildcardTreePattern ) ) - { - return; - } - IDictionary<string, object> labels = new Dictionary<string, object>(); // reused for each _parse - int rootTokenType = tpattern.Type; - Visit( t, rootTokenType, new VisitTreeWizardContextVisitor( this, visitor, labels, tpattern ) ); - } - - /** <summary> - * Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels - * on the various nodes and '.' (dot) as the node/subtree wildcard, - * return true if the pattern matches and fill the labels Map with - * the labels pointing at the appropriate nodes. Return false if - * the pattern is malformed or the tree does not match. - * </summary> - * - * <remarks> - * If a node specifies a text arg in pattern, then that must match - * for that node in t. - * - * TODO: what's a better way to indicate bad pattern? Exceptions are a hassle - * </remarks> - */ - public bool Parse( object t, string pattern, IDictionary<string, object> labels ) - { - TreePatternLexer tokenizer = new TreePatternLexer( pattern ); - TreePatternParser parser = - new TreePatternParser( tokenizer, this, new TreePatternTreeAdaptor() ); - TreePattern tpattern = (TreePattern)parser.Pattern(); - /* - System.out.println("t="+((Tree)t).toStringTree()); - System.out.println("scant="+tpattern.toStringTree()); - */ - bool matched = ParseCore( t, tpattern, labels ); - return matched; - } - - public bool Parse( object t, string pattern ) - { - return Parse( t, pattern, null ); - } - - /** <summary> - * Do the work for parse. Check to see if the t2 pattern fits the - * structure and token types in t1. Check text if the pattern has - * text arguments on nodes. Fill labels map with pointers to nodes - * in tree matched against nodes in pattern with labels. - * </summary> - */ - protected virtual bool ParseCore( object t1, TreePattern tpattern, IDictionary<string, object> labels ) - { - // make sure both are non-null - if ( t1 == null || tpattern == null ) - { - return false; - } - // check roots (wildcard matches anything) - if ( tpattern.GetType() != typeof( WildcardTreePattern ) ) - { - if ( adaptor.GetType( t1 ) != tpattern.Type ) - { - return false; - } - // if pattern has text, check node text - if ( tpattern.hasTextArg && !adaptor.GetText( t1 ).Equals( tpattern.Text ) ) - { - return false; - } - } - if ( tpattern.label != null && labels != null ) - { - // map label in pattern to node in t1 - labels[tpattern.label] = t1; - } - // check children - int n1 = adaptor.GetChildCount( t1 ); - int n2 = tpattern.ChildCount; - if ( n1 != n2 ) - { - return false; - } - for ( int i = 0; i < n1; i++ ) - { - object child1 = adaptor.GetChild( t1, i ); - TreePattern child2 = (TreePattern)tpattern.GetChild( i ); - if ( !ParseCore( child1, child2, labels ) ) - { - return false; - } - } - return true; - } - - /** <summary> - * Create a tree or node from the indicated tree pattern that closely - * follows ANTLR tree grammar tree element syntax: - * - * (root child1 ... child2). - * </summary> - * - * <remarks> - * You can also just pass in a node: ID - * - * Any node can have a text argument: ID[foo] - * (notice there are no quotes around foo--it's clear it's a string). - * - * nil is a special name meaning "give me a nil node". Useful for - * making lists: (nil A B C) is a list of A B C. - * </remarks> - */ - public virtual object Create( string pattern ) - { - TreePatternLexer tokenizer = new TreePatternLexer( pattern ); - TreePatternParser parser = new TreePatternParser( tokenizer, this, adaptor ); - object t = parser.Pattern(); - return t; - } - - /** <summary> - * Compare t1 and t2; return true if token types/text, structure match exactly. - * The trees are examined in their entirety so that (A B) does not match - * (A B C) nor (A (B C)). - * </summary> - * - * <remarks> - * TODO: allow them to pass in a comparator - * TODO: have a version that is nonstatic so it can use instance adaptor - * - * I cannot rely on the tree node's equals() implementation as I make - * no constraints at all on the node types nor interface etc... - * </remarks> - */ - public static bool Equals( object t1, object t2, ITreeAdaptor adaptor ) - { - return EqualsCore( t1, t2, adaptor ); - } - - /** <summary> - * Compare type, structure, and text of two trees, assuming adaptor in - * this instance of a TreeWizard. - * </summary> - */ - public new bool Equals( object t1, object t2 ) - { - return EqualsCore( t1, t2, adaptor ); - } - - protected static bool EqualsCore( object t1, object t2, ITreeAdaptor adaptor ) - { - // make sure both are non-null - if ( t1 == null || t2 == null ) - { - return false; - } - // check roots - if ( adaptor.GetType( t1 ) != adaptor.GetType( t2 ) ) - { - return false; - } - if ( !adaptor.GetText( t1 ).Equals( adaptor.GetText( t2 ) ) ) - { - return false; - } - // check children - int n1 = adaptor.GetChildCount( t1 ); - int n2 = adaptor.GetChildCount( t2 ); - if ( n1 != n2 ) - { - return false; - } - for ( int i = 0; i < n1; i++ ) - { - object child1 = adaptor.GetChild( t1, i ); - object child2 = adaptor.GetChild( t2, i ); - if ( !EqualsCore( child1, child2, adaptor ) ) - { - return false; - } - } - return true; - } - -#if BUILD_INDEXES - // TODO: next stuff taken from CommonTreeNodeStream - - /** <summary> - * Given a node, add this to the reverse index tokenTypeToStreamIndexesMap. - * You can override this method to alter how indexing occurs. The - * default is to create a - * - * Map<Integer token type,ArrayList<Integer stream index>> - * </summary> - * - * <remarks> - * This data structure allows you to find all nodes with type INT in order. - * - * If you really need to find a node of type, say, FUNC quickly then perhaps - * - * Map<Integertoken type,Map<Object tree node,Integer stream index>> - * - * would be better for you. The interior maps map a tree node to - * the index so you don't have to search linearly for a specific node. - * - * If you change this method, you will likely need to change - * getNodeIndex(), which extracts information. - * </remarks> - */ - protected void fillReverseIndex( object node, int streamIndex ) - { - //System.out.println("revIndex "+node+"@"+streamIndex); - if ( tokenTypesToReverseIndex == null ) - { - return; // no indexing if this is empty (nothing of interest) - } - if ( tokenTypeToStreamIndexesMap == null ) - { - tokenTypeToStreamIndexesMap = new Dictionary<int, IList<int>>(); // first indexing op - } - int tokenType = adaptor.getType( node ); - if ( !( tokenTypesToReverseIndex == INDEX_ALL || - tokenTypesToReverseIndex.Contains( tokenType ) ) ) - { - return; // tokenType not of interest - } - IList<int> indexes; - - if ( !tokenTypeToStreamIndexesMap.TryGetValue( tokenType, out indexes ) || indexes == null ) - { - indexes = new List<int>(); // no list yet for this token type - indexes.Add( streamIndex ); // not there yet, add - tokenTypeToStreamIndexesMap[tokenType] = indexes; - } - else - { - if ( !indexes.Contains( streamIndex ) ) - { - indexes.Add( streamIndex ); // not there yet, add - } - } - } - - /** <summary> - * Track the indicated token type in the reverse index. Call this - * repeatedly for each type or use variant with Set argument to - * set all at once. - * </summary> - * - * <param name="tokenType" /> - */ - public void reverseIndex( int tokenType ) - { - if ( tokenTypesToReverseIndex == null ) - { - tokenTypesToReverseIndex = new HashSet<int>(); - } - else if ( tokenTypesToReverseIndex == INDEX_ALL ) - { - return; - } - tokenTypesToReverseIndex.add( tokenType ); - } - - /** <summary> - * Track the indicated token types in the reverse index. Set - * to INDEX_ALL to track all token types. - * </summary> - */ - public void reverseIndex( HashSet<int> tokenTypes ) - { - tokenTypesToReverseIndex = tokenTypes; - } - - /** <summary> - * Given a node pointer, return its index into the node stream. - * This is not its Token stream index. If there is no reverse map - * from node to stream index or the map does not contain entries - * for node's token type, a linear search of entire stream is used. - * </summary> - * - * <remarks> - * Return -1 if exact node pointer not in stream. - * </remarks> - */ - public int getNodeIndex( object node ) - { - //System.out.println("get "+node); - if ( tokenTypeToStreamIndexesMap == null ) - { - return getNodeIndexLinearly( node ); - } - int tokenType = adaptor.getType( node ); - IList<int> indexes; - if ( !tokenTypeToStreamIndexesMap.TryGetValue( tokenType, out indexes ) || indexes == null ) - { - //System.out.println("found linearly; stream index = "+getNodeIndexLinearly(node)); - return getNodeIndexLinearly( node ); - } - for ( int i = 0; i < indexes.size(); i++ ) - { - int streamIndex = indexes[i]; - object n = get( streamIndex ); - if ( n == node ) - { - //System.out.println("found in index; stream index = "+streamIndexI); - return streamIndex; // found it! - } - } - return -1; - } -#endif - - } -} |