1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 namespace Antlr.Runtime {
34     using System.Collections.Generic;
35 
36     using InvalidOperationException = System.InvalidOperationException;
37     using StringBuilder = System.Text.StringBuilder;
38 
39     /** <summary>
40      *  The most common stream of tokens is one where every token is buffered up
41      *  and tokens are prefiltered for a certain channel (the parser will only
42      *  see these tokens and cannot change the filter channel number during the
43      *  parse).
44      *  </summary>
45      *
46      *  <remarks>TODO: how to access the full token stream?  How to track all tokens matched per rule?</remarks>
47      */
48     [System.Serializable]
49     public class CommonTokenStream : BufferedTokenStream {
50         /** Skip tokens on any channel but this one; this is how we skip whitespace... */
51         private int _channel;
52 
CommonTokenStream()53         public CommonTokenStream() {
54         }
55 
CommonTokenStream(ITokenSource tokenSource)56         public CommonTokenStream(ITokenSource tokenSource)
57             : this(tokenSource, TokenChannels.Default) {
58         }
59 
CommonTokenStream(ITokenSource tokenSource, int channel)60         public CommonTokenStream(ITokenSource tokenSource, int channel)
61             : base(tokenSource) {
62             this._channel = channel;
63         }
64 
65         public int Channel {
66             get {
67                 return _channel;
68             }
69         }
70 
71         /** Reset this token stream by setting its token source. */
72         public override ITokenSource TokenSource {
73             get {
74                 return base.TokenSource;
75             }
76             set {
77                 base.TokenSource = value;
78                 _channel = TokenChannels.Default;
79             }
80         }
81 
82         /** Always leave p on an on-channel token. */
Consume()83         public override void Consume() {
84             if (_p == -1)
85                 Setup();
86             _p++;
87             Sync(_p);
88             while (_tokens[_p].Channel != _channel) {
89                 _p++;
90                 Sync(_p);
91             }
92         }
93 
LB(int k)94         protected override IToken LB(int k) {
95             if (k == 0 || (_p - k) < 0)
96                 return null;
97 
98             int i = _p;
99             int n = 1;
100             // find k good tokens looking backwards
101             while (n <= k) {
102                 // skip off-channel tokens
103                 i = SkipOffTokenChannelsReverse(i - 1);
104                 n++;
105             }
106             if (i < 0)
107                 return null;
108             return _tokens[i];
109         }
110 
LT(int k)111         public override IToken LT(int k) {
112             if (_p == -1)
113                 Setup();
114             if (k == 0)
115                 return null;
116             if (k < 0)
117                 return LB(-k);
118             int i = _p;
119             int n = 1; // we know tokens[p] is a good one
120             // find k good tokens
121             while (n < k) {
122                 // skip off-channel tokens
123                 i = SkipOffTokenChannels(i + 1);
124                 n++;
125             }
126 
127             if (i > Range)
128                 Range = i;
129 
130             return _tokens[i];
131         }
132 
133         /** Given a starting index, return the index of the first on-channel
134          *  token.
135          */
SkipOffTokenChannels(int i)136         protected virtual int SkipOffTokenChannels(int i) {
137             Sync(i);
138             while (_tokens[i].Channel != _channel) {
139                 // also stops at EOF (it's on channel)
140                 i++;
141                 Sync(i);
142             }
143             return i;
144         }
145 
SkipOffTokenChannelsReverse(int i)146         protected virtual int SkipOffTokenChannelsReverse(int i) {
147             while (i >= 0 && ((IToken)_tokens[i]).Channel != _channel) {
148                 i--;
149             }
150 
151             return i;
152         }
153 
Setup()154         protected override void Setup() {
155             _p = 0;
156             Sync(0);
157             int i = 0;
158             while (_tokens[i].Channel != _channel) {
159                 i++;
160                 Sync(i);
161             }
162             _p = i;
163         }
164     }
165 }
166