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);
        }
        protected virtual void ExtractInformationFromTreeNodeStream(IIntStream input)
        {
            ITreeNodeStream nodes = (ITreeNodeStream)input;

            this.node = nodes.LT(1);
            ITreeAdaptor adaptor = nodes.TreeAdaptor;
            IToken       payload = adaptor.GetToken(node);

            if (payload != null)
            {
                this.token = payload;
                if (payload.Line <= 0)
                {
                    // imaginary node; no line/pos info; scan backwards
                    int    i         = -1;
                    object priorNode = nodes.LT(i);
                    while (priorNode != null)
                    {
                        IToken priorPayload = adaptor.GetToken(priorNode);
                        if (priorPayload != null && priorPayload.Line > 0)
                        {
                            // we found the most recent real line / pos info
                            this.line = priorPayload.Line;
                            this.charPositionInLine  = priorPayload.CharPositionInLine;
                            this.approximateLineInfo = true;
                            break;
                        }
                        --i;
                        priorNode = nodes.LT(i);
                    }
                }
                else
                { // node created from real token
                    this.line = payload.Line;
                    this.charPositionInLine = payload.CharPositionInLine;
                }
            }
            else if (this.node is Tree.ITree)
            {
                this.line = ((Tree.ITree) this.node).Line;
                this.charPositionInLine = ((Tree.ITree) this.node).CharPositionInLine;
                if (this.node is CommonTree)
                {
                    this.token = ((CommonTree)this.node).token;
                }
            }
            else
            {
                int    type = adaptor.GetType(this.node);
                string text = adaptor.GetText(this.node);
                this.token = new CommonToken(type, text);
            }
        }
Пример #3
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);
        }
Пример #4
0
        public string ToNodesOnlyString(ITreeNodeStream nodes)
        {
            ITreeAdaptor  adaptor = nodes.TreeAdaptor;
            StringBuilder buf     = new StringBuilder();
            object        o       = nodes.LT(1);
            int           type    = adaptor.GetType(o);

            while (o != null && type != TokenTypes.EndOfFile)
            {
                if (!(type == TokenTypes.Down || type == TokenTypes.Up))
                {
                    buf.Append(" ");
                    buf.Append(type);
                }
                nodes.Consume();
                o    = nodes.LT(1);
                type = adaptor.GetType(o);
            }
            return(buf.ToString());
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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);
        }
        protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input)
        {
            this._node = input.LT(1);
            ITokenStreamInformation streamInformation = input as ITokenStreamInformation;

            if (streamInformation != null)
            {
                IToken lastToken     = streamInformation.LastToken;
                IToken lastRealToken = streamInformation.LastRealToken;
                if (lastRealToken != null)
                {
                    this._token = lastRealToken;
                    this._line  = lastRealToken.Line;
                    this._charPositionInLine  = lastRealToken.CharPositionInLine;
                    this._approximateLineInfo = lastRealToken.Equals(lastToken);
                }
            }
            else
            {
                ITreeAdaptor adaptor = input.TreeAdaptor;
                IToken       payload = adaptor.GetToken(_node);
                if (payload != null)
                {
                    this._token = payload;
                    if (payload.Line <= 0)
                    {
                        // imaginary node; no line/pos info; scan backwards
                        int    i         = -1;
                        object priorNode = input.LT(i);
                        while (priorNode != null)
                        {
                            IToken priorPayload = adaptor.GetToken(priorNode);
                            if (priorPayload != null && priorPayload.Line > 0)
                            {
                                // we found the most recent real line / pos info
                                this._line = priorPayload.Line;
                                this._charPositionInLine  = priorPayload.CharPositionInLine;
                                this._approximateLineInfo = true;
                                break;
                            }
                            --i;
                            try
                            {
                                priorNode = input.LT(i);
                            }
                            catch (ArgumentException)
                            {
                                priorNode = null;
                            }
                        }
                    }
                    else
                    {
                        // node created from real token
                        this._line = payload.Line;
                        this._charPositionInLine = payload.CharPositionInLine;
                    }
                }
                else if (this._node is Tree.ITree)
                {
                    this._line = ((Tree.ITree) this._node).Line;
                    this._charPositionInLine = ((Tree.ITree) this._node).CharPositionInLine;
                    if (this._node is CommonTree)
                    {
                        this._token = ((CommonTree)this._node).Token;
                    }
                }
                else
                {
                    int    type = adaptor.GetType(this._node);
                    string text = adaptor.GetText(this._node);
                    this._token = new CommonToken(type, text);
                }
            }
        }
Пример #9
0
 public virtual void TraceIn(string ruleName, int ruleIndex)
 {
     base.TraceIn(ruleName, ruleIndex, input.LT(1));
 }
Пример #10
0
 protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input)
 {
     this._node = input.LT(1);
     ITokenStreamInformation streamInformation = input as ITokenStreamInformation;
     if (streamInformation != null)
     {
         IToken lastToken = streamInformation.LastToken;
         IToken lastRealToken = streamInformation.LastRealToken;
         if (lastRealToken != null)
         {
             this._token = lastRealToken;
             this._line = lastRealToken.Line;
             this._charPositionInLine = lastRealToken.CharPositionInLine;
             this._approximateLineInfo = lastRealToken.Equals(lastToken);
         }
     }
     else
     {
         ITreeAdaptor adaptor = input.TreeAdaptor;
         IToken payload = adaptor.GetToken(_node);
         if (payload != null)
         {
             this._token = payload;
             if (payload.Line <= 0)
             {
                 // imaginary node; no line/pos info; scan backwards
                 int i = -1;
                 object priorNode = input.LT(i);
                 while (priorNode != null)
                 {
                     IToken priorPayload = adaptor.GetToken(priorNode);
                     if (priorPayload != null && priorPayload.Line > 0)
                     {
                         // we found the most recent real line / pos info
                         this._line = priorPayload.Line;
                         this._charPositionInLine = priorPayload.CharPositionInLine;
                         this._approximateLineInfo = true;
                         break;
                     }
                     --i;
                     try
                     {
                         priorNode = input.LT(i);
                     }
                     catch (ArgumentException)
                     {
                         priorNode = null;
                     }
                 }
             }
             else
             {
                 // node created from real token
                 this._line = payload.Line;
                 this._charPositionInLine = payload.CharPositionInLine;
             }
         }
         else if (this._node is Tree.ITree)
         {
             this._line = ((Tree.ITree)this._node).Line;
             this._charPositionInLine = ((Tree.ITree)this._node).CharPositionInLine;
             if (this._node is CommonTree)
             {
                 this._token = ((CommonTree)this._node).Token;
             }
         }
         else
         {
             int type = adaptor.GetType(this._node);
             string text = adaptor.GetText(this._node);
             this._token = new CommonToken(type, text);
         }
     }
 }
Пример #11
0
 public string ToNodesOnlyString( ITreeNodeStream nodes )
 {
     ITreeAdaptor adaptor = nodes.TreeAdaptor;
     StringBuilder buf = new StringBuilder();
     object o = nodes.LT( 1 );
     int type = adaptor.GetType( o );
     while ( o != null && type != TokenTypes.EndOfFile )
     {
         if ( !( type == TokenTypes.Down || type == TokenTypes.Up ) )
         {
             buf.Append( " " );
             buf.Append( type );
         }
         nodes.Consume();
         o = nodes.LT( 1 );
         type = adaptor.GetType( o );
     }
     return buf.ToString();
 }
        protected virtual void ExtractInformationFromTreeNodeStream(IIntStream input)
        {
            ITokenStreamInformation tokenStreamInformation = input as ITokenStreamInformation;

            if (tokenStreamInformation != null)
            {
                IToken lastToken     = tokenStreamInformation.LastToken;
                IToken lastRealToken = tokenStreamInformation.LastRealToken;
                if (lastRealToken != null)
                {
                    this._token = lastRealToken;
                    this._line  = lastRealToken.Line;
                    this._charPositionInLine  = lastRealToken.CharPositionInLine;
                    this._approximateLineInfo = lastRealToken.Equals(lastToken);
                }
            }
            else
            {
                ITreeNodeStream treeNodeStream = (ITreeNodeStream)input;
                this._node = treeNodeStream.LT(1);
                ITreeAdaptor treeAdaptor = treeNodeStream.TreeAdaptor;
                IToken       token       = treeAdaptor.GetToken(this._node);
                if (token != null)
                {
                    this._token = token;
                    if (token.Line <= 0)
                    {
                        int    num = -1;
                        object obj = treeNodeStream.LT(num);
                        IToken token2;
                        while (true)
                        {
                            if (obj != null)
                            {
                                token2 = treeAdaptor.GetToken(obj);
                                if (token2 != null && token2.Line > 0)
                                {
                                    break;
                                }
                                num--;
                                obj = treeNodeStream.LT(num);
                                continue;
                            }
                            return;
                        }
                        this._line = token2.Line;
                        this._charPositionInLine  = token2.CharPositionInLine;
                        this._approximateLineInfo = true;
                    }
                    else
                    {
                        this._line = token.Line;
                        this._charPositionInLine = token.CharPositionInLine;
                    }
                }
                else if (this._node is ITree)
                {
                    this._line = ((ITree)this._node).Line;
                    this._charPositionInLine = ((ITree)this._node).CharPositionInLine;
                    if (this._node is CommonTree)
                    {
                        this._token = ((CommonTree)this._node).Token;
                    }
                }
                else
                {
                    int    type = treeAdaptor.GetType(this._node);
                    string text = treeAdaptor.GetText(this._node);
                    this._token = new CommonToken(type, text);
                }
            }
        }
 protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input)
 {
     this._node = input.LT(1);
     ITokenStreamInformation information = input as ITokenStreamInformation;
     if (information != null)
     {
         IToken lastToken = information.LastToken;
         IToken lastRealToken = information.LastRealToken;
         if (lastRealToken != null)
         {
             this._token = lastRealToken;
             this._line = lastRealToken.Line;
             this._charPositionInLine = lastRealToken.CharPositionInLine;
             this._approximateLineInfo = lastRealToken.Equals(lastToken);
         }
     }
     else
     {
         ITreeAdaptor treeAdaptor = input.TreeAdaptor;
         IToken token = treeAdaptor.GetToken(this._node);
         if (token == null)
         {
             if (this._node is ITree)
             {
                 this._line = ((ITree) this._node).Line;
                 this._charPositionInLine = ((ITree) this._node).CharPositionInLine;
                 if (this._node is CommonTree)
                 {
                     this._token = ((CommonTree) this._node).Token;
                 }
             }
             else
             {
                 int type = treeAdaptor.GetType(this._node);
                 string text = treeAdaptor.GetText(this._node);
                 this._token = new CommonToken(type, text);
             }
         }
         else
         {
             this._token = token;
             if (token.Line > 0)
             {
                 this._line = token.Line;
                 this._charPositionInLine = token.CharPositionInLine;
             }
             else
             {
                 int k = -1;
                 object t = input.LT(k);
                 while (t != null)
                 {
                     IToken token4 = treeAdaptor.GetToken(t);
                     if ((token4 != null) && (token4.Line > 0))
                     {
                         this._line = token4.Line;
                         this._charPositionInLine = token4.CharPositionInLine;
                         this._approximateLineInfo = true;
                         return;
                     }
                     k--;
                     try
                     {
                         t = input.LT(k);
                         continue;
                     }
                     catch (ArgumentException)
                     {
                         t = null;
                         continue;
                     }
                 }
             }
         }
     }
 }
Пример #14
0
        protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input)
        {
            this._node = input.LT(1);
            ITokenStreamInformation streamInformation = input as ITokenStreamInformation;

            if (streamInformation != null)
            {
                IToken lastToken     = streamInformation.LastToken;
                IToken lastRealToken = streamInformation.LastRealToken;
                if (lastRealToken == null)
                {
                    return;
                }
                this._token = lastRealToken;
                this._line  = lastRealToken.Line;
                this._charPositionInLine  = lastRealToken.CharPositionInLine;
                this._approximateLineInfo = lastRealToken.Equals((object)lastToken);
            }
            else
            {
                ITreeAdaptor treeAdaptor = input.TreeAdaptor;
                IToken       token1      = treeAdaptor.GetToken(this._node);
                if (token1 != null)
                {
                    this._token = token1;
                    if (token1.Line <= 0)
                    {
                        int    k = -1;
                        object t = input.LT(k);
                        while (t != null)
                        {
                            IToken token2 = treeAdaptor.GetToken(t);
                            if (token2 != null && token2.Line > 0)
                            {
                                this._line = token2.Line;
                                this._charPositionInLine  = token2.CharPositionInLine;
                                this._approximateLineInfo = true;
                                break;
                            }
                            --k;
                            try
                            {
                                t = input.LT(k);
                            }
                            catch (ArgumentException ex)
                            {
                                t = (object)null;
                            }
                        }
                    }
                    else
                    {
                        this._line = token1.Line;
                        this._charPositionInLine = token1.CharPositionInLine;
                    }
                }
                else if (this._node is ITree)
                {
                    this._line = ((ITree)this._node).Line;
                    this._charPositionInLine = ((ITree)this._node).CharPositionInLine;
                    if (!(this._node is CommonTree))
                    {
                        return;
                    }
                    this._token = ((CommonTree)this._node).Token;
                }
                else
                {
                    this._token = (IToken) new CommonToken(treeAdaptor.GetType(this._node), treeAdaptor.GetText(this._node));
                }
            }
        }