public void CheckRRTokenStreamBehaviourWhileEmpty1()
        {
            string description = "RewriteRuleTokenStream test";
            RewriteRuleTokenStream tokenTest =
                new RewriteRuleTokenStream(CreateTreeAdaptor(), description);

            Assert.IsFalse(tokenTest.HasNext(), "tokenTest has to give back false here.");
            Assert.AreEqual(description.ToString(), tokenTest.Description,
                            "Description strings should be equal.");
            Assert.AreEqual(0, tokenTest.Size(), "The number of elements should be zero.");
            tokenTest.Reset();
            Assert.IsTrue(true, "Reset() shouldn't make any problems here.");
            Assert.AreEqual(0, tokenTest.Size(),
                            "The number of elements should be still zero.");
            tokenTest.NextNode();
        }
Exemplo n.º 2
0
    // $ANTLR start "start"
    // D:\\Szkolenia\\Artykuł ProgramistaMag - DSL\\Code\\DSLSamples\\HelloWorld\\Simple.g:13:0: start : ID EOF -> ^( START ID ) ;
    public SimpleParser.start_return start() // throws RecognitionException [1]
    {
        SimpleParser.start_return retval = new SimpleParser.start_return();
        retval.Start = input.LT(1);

        CommonTree root_0 = null;

        IToken ID1  = null;
        IToken EOF2 = null;

        CommonTree             ID1_tree   = null;
        CommonTree             EOF2_tree  = null;
        RewriteRuleTokenStream stream_ID  = new RewriteRuleTokenStream(adaptor, "token ID");
        RewriteRuleTokenStream stream_EOF = new RewriteRuleTokenStream(adaptor, "token EOF");

        try
        {
            // D:\\Szkolenia\\Artykuł ProgramistaMag - DSL\\Code\\DSLSamples\\HelloWorld\\Simple.g:13:8: ( ID EOF -> ^( START ID ) )
            // D:\\Szkolenia\\Artykuł ProgramistaMag - DSL\\Code\\DSLSamples\\HelloWorld\\Simple.g:13:8: ID EOF
            {
                ID1 = (IToken)Match(input, ID, FOLLOW_ID_in_start50);
                stream_ID.Add(ID1);

                EOF2 = (IToken)Match(input, EOF, FOLLOW_EOF_in_start52);
                stream_EOF.Add(EOF2);



                // AST REWRITE
                // elements:          ID
                // token labels:
                // rule labels:       retval
                // token list labels:
                // rule list labels:
                // wildcard labels:
                retval.Tree = root_0;
                RewriteRuleSubtreeStream stream_retval = new RewriteRuleSubtreeStream(adaptor, "rule retval", retval != null ? retval.Tree : null);

                root_0 = (CommonTree)adaptor.GetNilNode();
                // 13:15: -> ^( START ID )
                {
                    // D:\\Szkolenia\\Artykuł ProgramistaMag - DSL\\Code\\DSLSamples\\HelloWorld\\Simple.g:13:18: ^( START ID )
                    {
                        CommonTree root_1 = (CommonTree)adaptor.GetNilNode();
                        root_1 = (CommonTree)adaptor.BecomeRoot((CommonTree)adaptor.Create(START, "START"), root_1);

                        adaptor.AddChild(root_1, stream_ID.NextNode());

                        adaptor.AddChild(root_0, root_1);
                    }
                }

                retval.Tree = root_0; retval.Tree = root_0;
            }

            retval.Stop = input.LT(-1);

            retval.Tree = (CommonTree)adaptor.RulePostProcessing(root_0);
            adaptor.SetTokenBoundaries(retval.Tree, (IToken)retval.Start, (IToken)retval.Stop);
        }
        catch (RecognitionException re)
        {
            ReportError(re);
            Recover(input, re);
            // Conversion of the second argument necessary, but harmless
            retval.Tree = (CommonTree)adaptor.ErrorNode(input, (IToken)retval.Start, input.LT(-1), re);
        }
        finally
        {
        }
        return(retval);
    }
        public void CheckRRTokenStreamBehaviourWithElements()
        {
            RewriteRuleTokenStream tokenTest = new RewriteRuleTokenStream(CreateTreeAdaptor(),
                                                                          "RewriteRuleTokenStream test");

            IToken token1 = CreateToken(1, "test token without any real context");

            // Test Add()
            tokenTest.Add(token1);
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (1).");
            Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (1).");

            // Test NextNode()
            CommonTree tree = (CommonTree)tokenTest.NextNode();

            Assert.AreEqual(token1, tree.Token,
                            "The returned token should be equal to the given token (1).");
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (2).");
            Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (1).");
            tokenTest.Reset();
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (3).");
            Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (2).");

            // Test NextToken()
            IToken returnedToken = tokenTest.NextToken();

            Assert.AreEqual(token1, returnedToken,
                            "The returned token should be equal to the given token (2).");
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (4).");
            Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (2).");
            tokenTest.Reset();
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (5).");
            Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (3).");

            // Test NextTree()
            returnedToken = (IToken)tokenTest.NextTree();
            Assert.AreEqual(token1, returnedToken,
                            "The returned token should be equal to the given token (3).");
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (6).");
            Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (2).");
            tokenTest.Reset();
            Assert.AreEqual(1, tokenTest.Size(), "tokenTest should have the size 1 (7).");
            Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (3).");

            // Test, what happens with two elements
            IToken token2 = CreateToken(2, "test token without any real context");

            tokenTest.Add(token2);
            Assert.AreEqual(2, tokenTest.Size(), "tokenTest should have the size 2 (1).");
            Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (4).");
            returnedToken = tokenTest.NextToken();
            Assert.AreEqual(token1, returnedToken,
                            "The returned token should be equal to the given token (4).");
            Assert.AreEqual(2, tokenTest.Size(), "tokenTest should have the size 2 (2).");
            Assert.IsTrue(tokenTest.HasNext(), "HasNext() should be true here (5).");
            returnedToken = tokenTest.NextToken();
            Assert.AreEqual(token2, returnedToken,
                            "The returned token should be equal to the given token (5).");
            Assert.IsFalse(tokenTest.HasNext(), "HasNext() should be false here (3).");

            // Test exception
            tokenTest.NextToken();
        }