1
#ifndef INC_InputBuffer_hpp__
2
#define INC_InputBuffer_hpp__
5
* <b>SOFTWARE RIGHTS</b>
7
* ANTLR 2.6.0 MageLang Insitute, 1999
9
* We reserve no legal rights to the ANTLR--it is fully in the
10
* public domain. An individual or company may do whatever
11
* they wish with source code distributed with ANTLR or the
12
* code generated by ANTLR, including the incorporation of
13
* ANTLR, or its output, into commerical software.
15
* We encourage users to develop software with ANTLR. However,
16
* we do ask that credit is given to us for developing
17
* ANTLR. By "credit", we mean that if you use ANTLR or
18
* incorporate any source code into one of your programs
19
* (commercial product, research project, or otherwise) that
20
* you acknowledge this fact somewhere in the documentation,
21
* research report, etc... If you like ANTLR and have
22
* developed a nice tool with the output, please mention that
23
* you developed it using ANTLR. In addition, we ask that the
24
* headers remain intact in our source code. As long as these
25
* guidelines are kept, we expect to continue enhancing this
26
* system and expect to make other tools available as they are
30
* @version ANTLR 2.6.0 MageLang Insitute, 1999
31
* @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
32
* @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
33
* @author <br><a href="mailto:pete@yamuna.demon.co.uk">Pete Wells</a>
36
/**A Stream of characters fed to the lexer from a InputStream that can
37
* be rewound via mark()/rewind() methods.
39
* A dynamic array is used to buffer up all the input characters. Normally,
40
* "k" characters are stored in the buffer. More characters may be stored during
41
* guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
42
* Consumption of characters is deferred. In other words, reading the next
43
* character is not done by conume(), but deferred until needed by LA or LT.
46
* @see antlr.CharQueue
49
#include "antlr/config.hpp"
50
#include "antlr/antlr_export.h"
51
#include "antlr/CircularQueue.hpp"
54
ANTLR_BEGIN_NAMESPACE(antlr)
56
class ANTLR_EXPORT InputBuffer {
59
// leave to subclasses
61
// Number of active markers
64
// Additional offset used when markers are active
65
int markerOffset; // = 0;
67
// Number of calls to consume() since last LA() or LT() call
68
int numToConsume; // = 0;
71
CircularQueue<int> queue;
74
/** Create a character buffer */
77
virtual ~InputBuffer() {}
79
/** This method updates the state of the input buffer so that
80
* the text matched since the most recent mark() is no longer
81
* held by the buffer. So, you either do a mark/rewind for
82
* failed predicate or mark/commit to keep on parsing without
83
* rewinding the input.
87
/** Mark another character for deferred consumption */
88
virtual void consume();
90
/** Ensure that the character buffer is sufficiently full */
91
virtual void fill(int amount);
93
/** Override this in subclasses to get the next character */
94
virtual int getChar()=0;
96
ANTLR_USE_NAMESPACE(std)string getLAChars() const;
98
ANTLR_USE_NAMESPACE(std)string getMarkedChars() const;
100
virtual bool isMarked() const;
102
/** Get a lookahead character */
103
virtual int LA(int i);
105
/**Return an integer marker that can be used to rewind the buffer to
110
/**Rewind the character buffer to a marker.
111
* @param mark Marker returned previously from mark()
113
virtual void rewind(int mark);
116
/** Sync up deferred consumption */
120
InputBuffer(const InputBuffer& other);
121
InputBuffer& operator=(const InputBuffer& other);
124
/** Sync up deferred consumption */
125
inline void InputBuffer::syncConsume() {
127
while (numToConsume > 0) {
130
// guess mode -- leave leading characters and bump offset.
133
// normal mode -- remove first character
140
if (numToConsume > 0) {
142
markerOffset += numToConsume;
144
queue.removeItems( numToConsume );
150
/** Get a lookahead character */
151
inline int InputBuffer::LA(int i)
154
return queue.elementAt(markerOffset + i - 1);
159
#endif //INC_InputBuffer_hpp__