Example #1
0
        public void TestList() /*throws Exception*/
        {
            ITree root = new CommonTree((IToken)null);

            ITree t = new CommonTree(new CommonToken(101));

            t.AddChild(new CommonTree(new CommonToken(102)));
            t.GetChild(0).AddChild(new CommonTree(new CommonToken(103)));
            t.AddChild(new CommonTree(new CommonToken(104)));

            ITree u = new CommonTree(new CommonToken(105));

            root.AddChild(t);
            root.AddChild(u);

            ITreeNodeStream stream    = newStream(root);
            string          expecting = " 101 102 103 104 105";
            string          found     = ToNodesOnlyString(stream);

            assertEquals(expecting, found);

            expecting = " 101 2 102 2 103 3 104 3 105";
            found     = ToTokenTypeString(stream);
            assertEquals(expecting, found);
        }
Example #2
0
        public void TestAddListToExistChildren() /*throws Exception*/
        {
            // Add child ^(nil 101 102 103) to root ^(5 6)
            // should add 101 102 103 to end of 5's child list
            CommonTree root = new CommonTree(new CommonToken(5));

            root.AddChild(new CommonTree(new CommonToken(6)));

            // child tree
            CommonTree r0 = new CommonTree((IToken)null);
            CommonTree c0, c1, c2;

            r0.AddChild(c0 = new CommonTree(new CommonToken(101)));
            r0.AddChild(c1 = new CommonTree(new CommonToken(102)));
            r0.AddChild(c2 = new CommonTree(new CommonToken(103)));

            root.AddChild(r0);

            Assert.IsNull(root.Parent);
            Assert.AreEqual(-1, root.ChildIndex);
            // check children of root all point at root
            Assert.AreEqual(root, c0.Parent);
            Assert.AreEqual(1, c0.ChildIndex);
            Assert.AreEqual(root, c0.Parent);
            Assert.AreEqual(2, c1.ChildIndex);
            Assert.AreEqual(root, c0.Parent);
            Assert.AreEqual(3, c2.ChildIndex);
        }
Example #3
0
        public void TestList2() /*throws Exception*/
        {
            // Add child ^(nil 101 102 103) to root 5
            // should pull 101 102 103 directly to become 5's child list
            CommonTree root = new CommonTree(new CommonToken(5));

            // child tree
            CommonTree r0 = new CommonTree((IToken)null);
            CommonTree c0, c1, c2;

            r0.AddChild(c0 = new CommonTree(new CommonToken(101)));
            r0.AddChild(c1 = new CommonTree(new CommonToken(102)));
            r0.AddChild(c2 = new CommonTree(new CommonToken(103)));

            root.AddChild(r0);

            Assert.IsNull(root.Parent);
            Assert.AreEqual(-1, root.ChildIndex);
            // check children of root all point at root
            Assert.AreEqual(root, c0.Parent);
            Assert.AreEqual(0, c0.ChildIndex);
            Assert.AreEqual(root, c0.Parent);
            Assert.AreEqual(1, c1.ChildIndex);
            Assert.AreEqual(root, c0.Parent);
            Assert.AreEqual(2, c2.ChildIndex);
        }
        public void TestSeek()
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree(new CommonToken(101));
            ITree r1 = new CommonTree(new CommonToken(102));

            r0.AddChild(r1);
            r1.AddChild(new CommonTree(new CommonToken(103)));
            ITree r2 = new CommonTree(new CommonToken(106));

            r2.AddChild(new CommonTree(new CommonToken(107)));
            r1.AddChild(r2);
            r0.AddChild(new CommonTree(new CommonToken(104)));
            r0.AddChild(new CommonTree(new CommonToken(105)));

            ITreeNodeStream stream = newStream(r0);

            stream.Consume(); // consume 101
            stream.Consume(); // consume DN
            stream.Consume(); // consume 102
            stream.Seek(7);   // seek to 107
            Assert.AreEqual(107, ((ITree)stream.LT(1)).Type);
            stream.Consume(); // consume 107
            stream.Consume(); // consume UP
            stream.Consume(); // consume UP
            Assert.AreEqual(104, ((ITree)stream.LT(1)).Type);
        }
Example #5
0
        public void Test4Nodes() /*throws Exception*/
        {
            // ^(101 ^(102 103) 104)
            CommonTree r0 = new CommonTree(new CommonToken(101));

            r0.AddChild(new CommonTree(new CommonToken(102)));
            r0.GetChild(0).AddChild(new CommonTree(new CommonToken(103)));
            r0.AddChild(new CommonTree(new CommonToken(104)));

            Assert.IsNull(r0.Parent);
            Assert.AreEqual(-1, r0.ChildIndex);
        }
Example #6
0
        public void TestMarkRewindInMiddle() /*throws Exception*/
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree(new CommonToken(101));
            ITree r1 = new CommonTree(new CommonToken(102));

            r0.AddChild(r1);
            r1.AddChild(new CommonTree(new CommonToken(103)));
            ITree r2 = new CommonTree(new CommonToken(106));

            r2.AddChild(new CommonTree(new CommonToken(107)));
            r1.AddChild(r2);
            r0.AddChild(new CommonTree(new CommonToken(104)));
            r0.AddChild(new CommonTree(new CommonToken(105)));

            ITreeNodeStream stream = newStream(r0);

            for (int k = 1; k <= 7; k++)
            { // consume til middle
                //System.out.println(((Tree)stream.LT(1)).getType());
                stream.Consume();
            }
            assertEquals(107, ((ITree)stream.LT(1)).Type);
            stream.Mark();    // MARK
            stream.Consume(); // consume 107
            stream.Consume(); // consume UP
            stream.Consume(); // consume UP
            stream.Consume(); // consume 104
            stream.Rewind();  // REWIND
            stream.Mark();    // keep saving nodes though

            assertEquals(107, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.Up, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.Up, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(104, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            // now we're past rewind position
            assertEquals(105, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.Up, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(1)).Type);
            assertEquals(TokenTypes.Up, ((ITree)stream.LT(-1)).Type);
        }
Example #7
0
        public void TestBecomeRoot2() /*throws Exception*/
        {
            // 5 becomes new root of ^(101 102 103)
            CommonTree newRoot = 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 #8
0
        public void TestReplaceAllWithOne() /*throws Exception*/
        {
            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, 2, newChild);
            string expecting = "(a x)";

            Assert.AreEqual(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Example #9
0
        public void TestReplaceTwoWithOneAtRight() /*throws Exception*/
        {
            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 #10
0
        public void TestReplaceAtLeft() /*throws Exception*/
        {
            CommonTree t = new CommonTree(new CommonToken(99, "a"));

            t.AddChild(new CommonTree(new CommonToken(99, "b")));       // index 0
            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, 0, newChild);
            string expecting = "(a x c d)";

            assertEquals(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Example #11
0
        public void TestFlatList() /*throws Exception*/
        {
            ITree root = new CommonTree((IToken)null);

            root.AddChild(new CommonTree(new CommonToken(101)));
            root.AddChild(new CommonTree(new CommonToken(102)));
            root.AddChild(new CommonTree(new CommonToken(103)));

            ITreeNodeStream stream    = newStream(root);
            string          expecting = " 101 102 103";
            string          found     = ToNodesOnlyString(stream);

            Assert.AreEqual(expecting, found);

            expecting = " 101 102 103";
            found     = ToTokenTypeString(stream);
            Assert.AreEqual(expecting, found);
        }
Example #12
0
        public void TestMarkRewindNested() /*throws Exception*/
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree(new CommonToken(101));
            ITree r1 = new CommonTree(new CommonToken(102));

            r0.AddChild(r1);
            r1.AddChild(new CommonTree(new CommonToken(103)));
            ITree r2 = new CommonTree(new CommonToken(106));

            r2.AddChild(new CommonTree(new CommonToken(107)));
            r1.AddChild(r2);
            r0.AddChild(new CommonTree(new CommonToken(104)));
            r0.AddChild(new CommonTree(new CommonToken(105)));

            ITreeNodeStream stream = newStream(r0);
            int             m      = stream.Mark(); // MARK at start

            stream.Consume();                       // consume 101
            stream.Consume();                       // consume DN
            int m2 = stream.Mark();                 // MARK on 102

            stream.Consume();                       // consume 102
            stream.Consume();                       // consume DN
            stream.Consume();                       // consume 103
            stream.Consume();                       // consume 106
            stream.Rewind(m2);                      // REWIND to 102
            assertEquals(102, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.Down, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            // stop at 103 and rewind to start
            stream.Rewind(m);   // REWIND to 101
            assertEquals(101, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.Down, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(102, ((ITree)stream.LT(1)).Type);
            stream.Consume();
            assertEquals(TokenTypes.Down, ((ITree)stream.LT(1)).Type);
        }
Example #13
0
        public void TestReplaceAllWithTwo() /*throws Exception*/
        {
            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(0, 2, newChildren);
            string expecting = "(a x y)";

            assertEquals(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Example #14
0
        public void TestList() /*throws Exception*/
        {
            // ^(nil 101 102 103)
            CommonTree r0 = new CommonTree((IToken)null);
            CommonTree c0, c1, c2;

            r0.AddChild(c0 = new CommonTree(new CommonToken(101)));
            r0.AddChild(c1 = new CommonTree(new CommonToken(102)));
            r0.AddChild(c2 = new CommonTree(new CommonToken(103)));

            Assert.IsNull(r0.Parent);
            Assert.AreEqual(-1, r0.ChildIndex);
            Assert.AreEqual(r0, c0.Parent);
            Assert.AreEqual(0, c0.ChildIndex);
            Assert.AreEqual(r0, c1.Parent);
            Assert.AreEqual(1, c1.ChildIndex);
            Assert.AreEqual(r0, c2.Parent);
            Assert.AreEqual(2, c2.ChildIndex);
        }
Example #15
0
        public void Test4Nodes() /*throws Exception*/
        {
            // ^(101 ^(102 103) 104)
            ITree t = new CommonTree(new CommonToken(101));

            t.AddChild(new CommonTree(new CommonToken(102)));
            t.GetChild(0).AddChild(new CommonTree(new CommonToken(103)));
            t.AddChild(new CommonTree(new CommonToken(104)));

            ITreeNodeStream stream    = newStream(t);
            string          expecting = " 101 102 103 104";
            string          found     = ToNodesOnlyString(stream);

            assertEquals(expecting, found);

            expecting = " 101 2 102 2 103 3 104 3";
            found     = ToTokenTypeString(stream);
            assertEquals(expecting, found);
        }
Example #16
0
        public void TestReplaceOneWithTwoInMiddle() /*throws Exception*/
        {
            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)";

            Assert.AreEqual(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Example #17
0
        public void TestDupTree() /*throws Exception*/
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            CommonTree r0 = new CommonTree(new CommonToken(101));
            CommonTree r1 = new CommonTree(new CommonToken(102));

            r0.AddChild(r1);
            r1.AddChild(new CommonTree(new CommonToken(103)));
            ITree r2 = new CommonTree(new CommonToken(106));

            r2.AddChild(new CommonTree(new CommonToken(107)));
            r1.AddChild(r2);
            r0.AddChild(new CommonTree(new CommonToken(104)));
            r0.AddChild(new CommonTree(new CommonToken(105)));

            CommonTree dup = (CommonTree)(new CommonTreeAdaptor()).DupTree(r0);

            Assert.IsNull(dup.Parent);
            Assert.AreEqual(-1, dup.ChildIndex);
            dup.SanityCheckParentAndChildIndexes();
        }
Example #18
0
        private CommonTree Combine(List <SourceFile> sourceFiles)
        {
            var root = new CommonTree(new CommonToken(0, ""));

            foreach (var item in sourceFiles)
            {
                foreach (var node in item.Tree.Children)
                {
                    root.AddChild(node);
                }
            }

            return(root);
        }
Example #19
0
        public void TestLT() /*throws Exception*/
        {
            // ^(101 ^(102 103) 104)
            ITree t = new CommonTree(new CommonToken(101));

            t.AddChild(new CommonTree(new CommonToken(102)));
            t.GetChild(0).AddChild(new CommonTree(new CommonToken(103)));
            t.AddChild(new CommonTree(new CommonToken(104)));

            ITreeNodeStream stream = newStream(t);

            assertEquals(101, ((ITree)stream.LT(1)).Type);
            assertEquals(TokenTypes.Down, ((ITree)stream.LT(2)).Type);
            assertEquals(102, ((ITree)stream.LT(3)).Type);
            assertEquals(TokenTypes.Down, ((ITree)stream.LT(4)).Type);
            assertEquals(103, ((ITree)stream.LT(5)).Type);
            assertEquals(TokenTypes.Up, ((ITree)stream.LT(6)).Type);
            assertEquals(104, ((ITree)stream.LT(7)).Type);
            assertEquals(TokenTypes.Up, ((ITree)stream.LT(8)).Type);
            assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(9)).Type);
            // check way ahead
            assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(100)).Type);
        }
Example #20
0
        public void TestMarkRewindEntire() /*throws Exception*/
        {
            // ^(101 ^(102 103 ^(106 107) ) 104 105)
            // stream has 7 real + 6 nav nodes
            // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
            ITree r0 = new CommonTree(new CommonToken(101));
            ITree r1 = new CommonTree(new CommonToken(102));

            r0.AddChild(r1);
            r1.AddChild(new CommonTree(new CommonToken(103)));
            ITree r2 = new CommonTree(new CommonToken(106));

            r2.AddChild(new CommonTree(new CommonToken(107)));
            r1.AddChild(r2);
            r0.AddChild(new CommonTree(new CommonToken(104)));
            r0.AddChild(new CommonTree(new CommonToken(105)));

            ITreeNodeStream stream = newStream(r0);
            int             m      = stream.Mark(); // MARK

            for (int k = 1; k <= 13; k++)
            { // consume til end
                stream.LT(1);
                stream.Consume();
            }
            assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(1)).Type);
            stream.Rewind(m);        // REWIND

            // consume til end again :)
            for (int k = 1; k <= 13; k++)
            { // consume til end
                stream.LT(1);
                stream.Consume();
            }
            assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(1)).Type);
        }
Example #21
0
        public void TestListWithOneNode() /*throws Exception*/
        {
            ITree root = new CommonTree((IToken)null);

            root.AddChild(new CommonTree(new CommonToken(101)));

            ITreeNodeStream stream    = newStream(root);
            string          expecting = " 101";
            string          found     = ToNodesOnlyString(stream);

            assertEquals(expecting, found);

            expecting = " 101";
            found     = ToTokenTypeString(stream);
            assertEquals(expecting, found);
        }
Example #22
0
        public void TestAoverB() /*throws Exception*/
        {
            ITree t = new CommonTree(new CommonToken(101));

            t.AddChild(new CommonTree(new CommonToken(102)));

            ITreeNodeStream stream    = newStream(t);
            string          expecting = " 101 102";
            string          found     = ToNodesOnlyString(stream);

            assertEquals(expecting, found);

            expecting = " 101 2 102 3";
            found     = ToTokenTypeString(stream);
            assertEquals(expecting, found);
        }
Example #23
0
        public void TestReplaceWithOneChildren() /*throws Exception*/
        {
            // 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)";

            Assert.AreEqual(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Example #24
0
        /**
         * Recursively copy tree
         */
        private static void copyTreeRecursive(CommonTree copy, CommonTree original, CommonTree modnode, int type)
        {
            if (original.Children != null)
            {
                foreach (var o in original.Children)
                {
                    CommonTree originalChild = (CommonTree)o;

                    // get the token from the original child node
                    CommonToken oTok = (CommonToken)originalChild.Token;

                    // create a new token with the same type & text as 'oTok'
                    //CommonToken cTok = new CommonToken(oTok.Type, oTok.Text);
                    CommonToken cTok;
                    if (o.Equals(modnode))
                    {
                        cTok = new CommonToken(type, oTok.Text);
                    }
                    else
                    {
                        cTok = new CommonToken(oTok.Type, oTok.Text);
                    }

                    // copy all attributes from 'oTok' to 'cTok'
                    cTok.Line = oTok.Line;
                    cTok.CharPositionInLine = oTok.CharPositionInLine;
                    cTok.Channel            = oTok.Channel;
                    cTok.StartIndex         = oTok.StartIndex;
                    cTok.StopIndex          = oTok.StopIndex;
                    cTok.TokenIndex         = oTok.TokenIndex;

                    // create a new tree node with the 'cTok' as token
                    CommonTree copyChild = new CommonTree(cTok);

                    // set the parent node of the child node
                    copyChild.Parent = copy;

                    // add the child to the parent node
                    copy.AddChild(copyChild);

                    // make a recursive call to copy deeper
                    copyTreeRecursive(copyChild, originalChild, modnode, type);
                }
            }
        }