Beispiel #1
0
        public virtual void ParseAndRewrite()
        {
            ProcessArgs(args);
            ICharStream input = null;

            if (filename != null)
            {
                input = new ANTLRFileStream(filename);
            }
            else
            {
                input = new ANTLRReaderStream(Console.In, ANTLRReaderStream.InitialBufferSize, ANTLRReaderStream.ReadBufferSize);
            }
            // BUILD AST
            ANTLRLexer lex = new ANTLRLexer(input);

            tokens = new TokenRewriteStream(lex);
            ANTLRParser g       = new ANTLRParser(tokens);
            Grammar     grammar = new Grammar();
            var         r       = g.grammar_(grammar);
            CommonTree  t       = (CommonTree)r.Tree;

            if (tree_option)
            {
                Console.Out.WriteLine(t.ToStringTree());
            }
            Rewrite(g.TreeAdaptor, t, g.TokenNames);
        }
        public void testDoubleLevelTree()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(A (B C) (B D) E)");
            string       actual   = t.ToStringTree();
            string       expected = "(A (B C) (B D) E)";

            Assert.AreEqual(expected, actual);
        }
        public void testListTree()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("(nil A B C)");
            string       actual   = t.ToStringTree();
            string       expected = "A B C";

            Assert.AreEqual(expected, actual);
        }
        public void testSingleNodeWithArg()
        {
            ITreeAdaptor adaptor  = new CommonTreeAdaptor();
            TreeWizard   wiz      = new TreeWizard(adaptor, tokens);
            CommonTree   t        = (CommonTree)wiz.Create("ID[foo]");
            string       actual   = t.ToStringTree();
            string       expected = "foo";

            Assert.AreEqual(expected, actual);
        }
Beispiel #5
0
        public void testReplaceAllWithOne()
        {
            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 expected = "(a x)";

            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #6
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 expected = "(a b x d)";

            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #7
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 expected = "(a c)";

            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #8
0
        public void testReplaceAllWithTwo()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            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.GetNilNode();

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

            t.ReplaceChildren(0, 2, newChildren);
            String expected = "(a x y)";

            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #9
0
        public void testReplaceOneWithTwoInMiddle()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            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 expected = "(a b x y d)";

            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #10
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 expected = "(a c)";
            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #11
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 expected = "(a b x)";
            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #12
0
        public void testReplaceOneWithTwoInMiddle()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            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.GetNilNode();
            newChildren.AddChild(new CommonTree(new CommonToken(99, "x")));
            newChildren.AddChild(new CommonTree(new CommonToken(99, "y")));

            t.ReplaceChildren(1, 1, newChildren);
            String expected = "(a b x y d)";
            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Beispiel #13
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 expected = "(a b x d)";
            Assert.AreEqual(expected, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
		public void testReplaceAllWithTwo()
		{
			ITreeAdaptor adaptor = new CommonTreeAdaptor();
			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 expected = "(a x y)";
			Assert.AreEqual(expected, t.ToStringTree());
			t.SanityCheckParentAndChildIndexes();
		}