• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2009 Sam Harwell
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 Antlr.Runtime.Misc;
35     using CLSCompliant = System.CLSCompliantAttribute;
36     using NotSupportedException = System.NotSupportedException;
37     using IndexOutOfRangeException = System.IndexOutOfRangeException;
38 
39     /** A token stream that pulls tokens from the code source on-demand and
40      *  without tracking a complete buffer of the tokens. This stream buffers
41      *  the minimum number of tokens possible.  It's the same as
42      *  OnDemandTokenStream except that OnDemandTokenStream buffers all tokens.
43      *
44      *  You can't use this stream if you pass whitespace or other off-channel
45      *  tokens to the parser. The stream can't ignore off-channel tokens.
46      *
47      *  You can only look backwards 1 token: LT(-1).
48      *
49      *  Use this when you need to read from a socket or other infinite stream.
50      *
51      *  @see BufferedTokenStream
52      *  @see CommonTokenStream
53      */
54     public class UnbufferedTokenStream : LookaheadStream<IToken>, ITokenStream {
55         [CLSCompliant(false)]
56         protected ITokenSource tokenSource;
57         protected int tokenIndex; // simple counter to set token index in tokens
58 
59         /** Skip tokens on any channel but this one; this is how we skip whitespace... */
60         protected int channel = TokenChannels.Default;
61 
UnbufferedTokenStream(ITokenSource tokenSource)62         public UnbufferedTokenStream(ITokenSource tokenSource) {
63             this.tokenSource = tokenSource;
64         }
65 
66         public ITokenSource TokenSource {
67             get {
68                 return this.tokenSource;
69             }
70         }
71 
72         public string SourceName {
73             get {
74                 return TokenSource.SourceName;
75             }
76         }
77 
NextElement()78         public override IToken NextElement() {
79             IToken t = this.tokenSource.NextToken();
80             t.TokenIndex = this.tokenIndex++;
81             return t;
82         }
83 
IsEndOfFile(IToken o)84         public override bool IsEndOfFile(IToken o) {
85             return o.Type == CharStreamConstants.EndOfFile;
86         }
87 
Get(int i)88         public IToken Get(int i) {
89             throw new NotSupportedException("Absolute token indexes are meaningless in an unbuffered stream");
90         }
91 
LA(int i)92         public int LA(int i) {
93             return LT(i).Type;
94         }
95 
ToString(int start, int stop)96         public string ToString(int start, int stop) {
97             return "n/a";
98         }
99 
ToString(IToken start, IToken stop)100         public string ToString(IToken start, IToken stop) {
101             return "n/a";
102         }
103     }
104 }
105