1 #ifndef INC_InputBuffer_hpp__
2 #define INC_InputBuffer_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/InputBuffer.hpp#2 $
11 #include <antlr/config.hpp>
12 #include <antlr/CircularQueue.hpp>
15 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
19 /** A Stream of characters fed to the lexer from a InputStream that can
20 * be rewound via mark()/rewind() methods.
22 * A dynamic array is used to buffer up all the input characters. Normally,
23 * "k" characters are stored in the buffer. More characters may be stored during
24 * guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
25 * Consumption of characters is deferred. In other words, reading the next
26 * character is not done by conume(), but deferred until needed by LA or LT.
29 * @see antlr.CharQueue
31 class ANTLR_API InputBuffer {
33 /** Create a character buffer */
41 virtual ~InputBuffer()
45 /// Reset the input buffer to empty state
46 virtual inline void reset( void )
54 /** This method updates the state of the input buffer so that
55 * the text matched since the most recent mark() is no longer
56 * held by the buffer. So, you either do a mark/rewind for
57 * failed predicate or mark/commit to keep on parsing without
58 * rewinding the input.
60 inline void commit( void )
65 /** Mark another character for deferred consumption */
66 virtual inline void consume()
71 /** Ensure that the character buffer is sufficiently full */
72 virtual void fill(unsigned int amount);
74 /** Override this in subclasses to get the next character */
75 virtual int getChar()=0;
77 /** Get a lookahead character */
78 virtual inline int LA(unsigned int i)
81 return queue.elementAt(markerOffset + i - 1);
84 /** Return an integer marker that can be used to rewind the buffer to
87 virtual unsigned int mark();
88 /// Are there any marks active in the InputBuffer
89 virtual inline bool isMarked() const
91 return (nMarkers != 0);
93 /** Rewind the character buffer to a marker.
94 * @param mark Marker returned previously from mark()
96 virtual void rewind(unsigned int mark);
98 /** Get the number of non-consumed characters
100 virtual unsigned int entries() const;
102 ANTLR_USE_NAMESPACE(std)string getLAChars() const;
104 ANTLR_USE_NAMESPACE(std)string getMarkedChars() const;
108 // leave to subclasses
110 // Number of active markers
111 unsigned int nMarkers; // = 0;
113 // Additional offset used when markers are active
114 unsigned int markerOffset; // = 0;
116 // Number of calls to consume() since last LA() or LT() call
117 unsigned int numToConsume; // = 0;
120 CircularQueue<int> queue;
122 /** Sync up deferred consumption */
126 InputBuffer(const InputBuffer& other);
127 InputBuffer& operator=(const InputBuffer& other);
130 /** Sync up deferred consumption */
131 inline void InputBuffer::syncConsume() {
132 if (numToConsume > 0)
135 markerOffset += numToConsume;
137 queue.removeItems( numToConsume );
142 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
146 #endif //INC_InputBuffer_hpp__