Example #1
0
        public virtual void SanityCheckParentAndChildIndexes(ITree parent, int i)
        {
            if (parent != this.Parent)
            {
                throw new ArgumentException("parents don't match; expected " + parent + " found " + this.Parent);
            }
            if (i != this.ChildIndex)
            {
                throw new NotSupportedException("child indexes don't match; expected " + i + " found " + this.ChildIndex);
            }
            int n = this.ChildCount;

            for (int c = 0; c < n; c++)
            {
                CommonTree child = (CommonTree)this.GetChild(c);
                child.SanityCheckParentAndChildIndexes(this, c);
            }
        }
Example #2
0
        public void TestReplaceWithOneChildren()
        {
            // assume token type 99 and use text
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            CommonTree c0 = new CommonTree( new CommonToken( 99, "b" ) );
            t.AddChild( c0 );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "c" ) );
            t.ReplaceChildren( 0, 0, newChild );
            string expecting = "(a c)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
Example #3
0
        public void TestReplaceOneWithTwoInMiddle()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChildren = (CommonTree)adaptor.Nil();
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "x" ) ) );
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "y" ) ) );

            t.ReplaceChildren( 1, 1, newChildren );
            string expecting = "(a b x y d)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
Example #4
0
        public void TestReplaceTwoWithOneAtRight()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );

            t.ReplaceChildren( 1, 2, newChild );
            string expecting = "(a b x)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
Example #5
0
        public void TestReplaceInMiddle()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) ); // index 1
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );
            t.ReplaceChildren( 1, 1, newChild );
            string expecting = "(a b x d)";
            assertEquals( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
Example #6
0
        public void TestBecomeRoot5()
        {
            // ^(nil 5) becomes new root of ^(101 102 103)
            CommonTree newRoot = new CommonTree( (IToken)null );
            newRoot.AddChild( new CommonTree( new CommonToken( 5 ) ) );

            CommonTree oldRoot = new CommonTree( new CommonToken( 101 ) );
            oldRoot.AddChild( new CommonTree( new CommonToken( 102 ) ) );
            oldRoot.AddChild( new CommonTree( new CommonToken( 103 ) ) );

            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            adaptor.BecomeRoot( newRoot, oldRoot );
            newRoot.SanityCheckParentAndChildIndexes();
        }
Example #7
0
        public void TestReplaceTwoWithOneAtLeft()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );

            t.ReplaceChildren( 0, 1, newChild );
            string expecting = "(a x d)";
            Assert.AreEqual( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
Example #8
0
        public void TestReplaceOneWithTwoAtRight()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) );

            CommonTree newChildren = (CommonTree)adaptor.Nil();
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "x" ) ) );
            newChildren.AddChild( new CommonTree( new CommonToken( 99, "y" ) ) );

            t.ReplaceChildren( 2, 2, newChildren );
            string expecting = "(a b c x y)";
            Assert.AreEqual( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }
Example #9
0
        public void TestReplaceAtRight()
        {
            CommonTree t = new CommonTree( new CommonToken( 99, "a" ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "b" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "c" ) ) );
            t.AddChild( new CommonTree( new CommonToken( 99, "d" ) ) ); // index 2

            CommonTree newChild = new CommonTree( new CommonToken( 99, "x" ) );
            t.ReplaceChildren( 2, 2, newChild );
            string expecting = "(a b c x)";
            Assert.AreEqual( expecting, t.ToStringTree() );
            t.SanityCheckParentAndChildIndexes();
        }