1 | using System; |
---|
2 | |
---|
3 | namespace antlr |
---|
4 | { |
---|
5 | /*ANTLR Translator Generator |
---|
6 | * Project led by Terence Parr at http://www.jGuru.com |
---|
7 | * Software rights: http://www.antlr.org/license.html |
---|
8 | * |
---|
9 | * $Id:$ |
---|
10 | */ |
---|
11 | |
---|
12 | // |
---|
13 | // ANTLR C# Code Generator by Micheal Jordan |
---|
14 | // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com |
---|
15 | // Anthony Oguntimehin |
---|
16 | // |
---|
17 | // With many thanks to Eric V. Smith from the ANTLR list. |
---|
18 | // |
---|
19 | |
---|
20 | /*A Stream of Token objects fed to the parser from a Tokenizer that can |
---|
21 | * be rewound via mark()/rewind() methods. |
---|
22 | * <p> |
---|
23 | * A dynamic array is used to buffer up all the input tokens. Normally, |
---|
24 | * "k" tokens are stored in the buffer. More tokens may be stored during |
---|
25 | * guess mode (testing syntactic predicate), or when LT(i>k) is referenced. |
---|
26 | * Consumption of tokens is deferred. In other words, reading the next |
---|
27 | * token is not done by conume(), but deferred until needed by LA or LT. |
---|
28 | * <p> |
---|
29 | * |
---|
30 | * @see antlr.Token |
---|
31 | * @see antlr.Tokenizer |
---|
32 | * @see antlr.TokenQueue |
---|
33 | */ |
---|
34 | |
---|
35 | public class TokenBuffer |
---|
36 | { |
---|
37 | |
---|
38 | // Token source |
---|
39 | protected internal TokenStream input; |
---|
40 | |
---|
41 | // Number of active markers |
---|
42 | protected internal int nMarkers = 0; |
---|
43 | |
---|
44 | // Additional offset used when markers are active |
---|
45 | protected internal int markerOffset = 0; |
---|
46 | |
---|
47 | // Number of calls to consume() since last LA() or LT() call |
---|
48 | protected internal int numToConsume = 0; |
---|
49 | |
---|
50 | // Circular queue |
---|
51 | internal TokenQueue queue; |
---|
52 | |
---|
53 | /*Create a token buffer */ |
---|
54 | public TokenBuffer(TokenStream input_) |
---|
55 | { |
---|
56 | input = input_; |
---|
57 | queue = new TokenQueue(1); |
---|
58 | } |
---|
59 | |
---|
60 | /*Reset the input buffer to empty state */ |
---|
61 | public virtual void reset() |
---|
62 | { |
---|
63 | nMarkers = 0; |
---|
64 | markerOffset = 0; |
---|
65 | numToConsume = 0; |
---|
66 | queue.reset(); |
---|
67 | } |
---|
68 | |
---|
69 | /*Mark another token for deferred consumption */ |
---|
70 | public virtual void consume() |
---|
71 | { |
---|
72 | numToConsume++; |
---|
73 | } |
---|
74 | |
---|
75 | /*Ensure that the token buffer is sufficiently full */ |
---|
76 | protected virtual void fill(int amount) |
---|
77 | { |
---|
78 | syncConsume(); |
---|
79 | // Fill the buffer sufficiently to hold needed tokens |
---|
80 | while (queue.nbrEntries < (amount + markerOffset)) |
---|
81 | { |
---|
82 | // Append the next token |
---|
83 | queue.append(input.nextToken()); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /*return the Tokenizer (needed by ParseView) */ |
---|
88 | public virtual TokenStream getInput() |
---|
89 | { |
---|
90 | return input; |
---|
91 | } |
---|
92 | |
---|
93 | /*Get a lookahead token value */ |
---|
94 | public virtual int LA(int i) |
---|
95 | { |
---|
96 | fill(i); |
---|
97 | return queue.elementAt(markerOffset + i - 1).Type; |
---|
98 | } |
---|
99 | |
---|
100 | /*Get a lookahead token */ |
---|
101 | public virtual IToken LT(int i) |
---|
102 | { |
---|
103 | fill(i); |
---|
104 | return queue.elementAt(markerOffset + i - 1); |
---|
105 | } |
---|
106 | |
---|
107 | /*Return an integer marker that can be used to rewind the buffer to |
---|
108 | * its current state. |
---|
109 | */ |
---|
110 | public virtual int mark() |
---|
111 | { |
---|
112 | syncConsume(); |
---|
113 | nMarkers++; |
---|
114 | return markerOffset; |
---|
115 | } |
---|
116 | |
---|
117 | /*Rewind the token buffer to a marker. |
---|
118 | * @param mark Marker returned previously from mark() |
---|
119 | */ |
---|
120 | public virtual void rewind(int mark) |
---|
121 | { |
---|
122 | syncConsume(); |
---|
123 | markerOffset = mark; |
---|
124 | nMarkers--; |
---|
125 | } |
---|
126 | |
---|
127 | /*Sync up deferred consumption */ |
---|
128 | protected virtual void syncConsume() |
---|
129 | { |
---|
130 | while (numToConsume > 0) |
---|
131 | { |
---|
132 | if (nMarkers > 0) |
---|
133 | { |
---|
134 | // guess mode -- leave leading tokens and bump offset. |
---|
135 | markerOffset++; |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | // normal mode -- remove first token |
---|
140 | queue.removeFirst(); |
---|
141 | } |
---|
142 | numToConsume--; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|