1 #ifndef INC_TokenBuffer_hpp__
2 #define INC_TokenBuffer_hpp__
4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/TokenBuffer.hpp#2 $
11 #include <antlr/config.hpp>
12 #include <antlr/TokenStream.hpp>
13 #include <antlr/CircularQueue.hpp>
15 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
19 /**A Stream of Token objects fed to the parser from a TokenStream that can
20 * be rewound via mark()/rewind() methods.
22 * A dynamic array is used to buffer up all the input tokens. Normally,
23 * "k" tokens are stored in the buffer. More tokens may be stored during
24 * guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
25 * Consumption of tokens is deferred. In other words, reading the next
26 * token is not done by conume(), but deferred until needed by LA or LT.
29 * @todo: see if we can integrate this one with InputBuffer into one template
33 * @see antlr.TokenStream
34 * @see antlr.TokenQueue
36 class ANTLR_API TokenBuffer {
38 /** Create a token buffer */
39 TokenBuffer(TokenStream& input_);
40 virtual ~TokenBuffer();
42 /// Reset the input buffer to empty state
43 inline void reset( void )
51 /** Get a lookahead token value */
52 int LA( unsigned int i );
54 /** Get a lookahead token */
55 RefToken LT( unsigned int i );
57 /** Return an integer marker that can be used to rewind the buffer to
62 /**Rewind the token buffer to a marker.
63 * @param mark Marker returned previously from mark()
65 void rewind(unsigned int mark);
67 /** Mark another token for deferred consumption */
73 /// Return the number of entries in the TokenBuffer
74 virtual unsigned int entries() const;
77 /** Ensure that the token buffer is sufficiently full */
78 void fill(unsigned int amount);
79 /** Sync up deferred consumption */
86 /// Number of active markers
87 unsigned int nMarkers;
89 /// Additional offset used when markers are active
90 unsigned int markerOffset;
92 /// Number of calls to consume() since last LA() or LT() call
93 unsigned int numToConsume;
95 /// Circular queue with Tokens
96 CircularQueue<RefToken> queue;
99 TokenBuffer(const TokenBuffer& other);
100 const TokenBuffer& operator=(const TokenBuffer& other);
103 /** Sync up deferred consumption */
104 inline void TokenBuffer::syncConsume()
106 if (numToConsume > 0)
109 markerOffset += numToConsume;
111 queue.removeItems( numToConsume );
117 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
121 #endif //INC_TokenBuffer_hpp__