1 | #ifndef INC_InputBuffer_hpp__ |
---|
2 | #define INC_InputBuffer_hpp__ |
---|
3 | |
---|
4 | /* ANTLR Translator Generator |
---|
5 | * Project led by Terence Parr at http://www.jGuru.com |
---|
6 | * Software rights: http://www.antlr.org/license.html |
---|
7 | * |
---|
8 | * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/InputBuffer.hpp#2 $ |
---|
9 | */ |
---|
10 | |
---|
11 | #include <antlr/config.hpp> |
---|
12 | #include <antlr/CircularQueue.hpp> |
---|
13 | #include <string> |
---|
14 | |
---|
15 | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
---|
16 | namespace antlr { |
---|
17 | #endif |
---|
18 | |
---|
19 | /** A Stream of characters fed to the lexer from a InputStream that can |
---|
20 | * be rewound via mark()/rewind() methods. |
---|
21 | * <p> |
---|
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. |
---|
27 | * <p> |
---|
28 | * |
---|
29 | * @see antlr.CharQueue |
---|
30 | */ |
---|
31 | class ANTLR_API InputBuffer { |
---|
32 | public: |
---|
33 | /** Create a character buffer */ |
---|
34 | InputBuffer() |
---|
35 | : nMarkers(0) |
---|
36 | , markerOffset(0) |
---|
37 | , numToConsume(0) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | virtual ~InputBuffer() |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | /// Reset the input buffer to empty state |
---|
46 | virtual inline void reset( void ) |
---|
47 | { |
---|
48 | nMarkers = 0; |
---|
49 | markerOffset = 0; |
---|
50 | numToConsume = 0; |
---|
51 | queue.clear(); |
---|
52 | } |
---|
53 | |
---|
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. |
---|
59 | */ |
---|
60 | inline void commit( void ) |
---|
61 | { |
---|
62 | nMarkers--; |
---|
63 | } |
---|
64 | |
---|
65 | /** Mark another character for deferred consumption */ |
---|
66 | virtual inline void consume() |
---|
67 | { |
---|
68 | numToConsume++; |
---|
69 | } |
---|
70 | |
---|
71 | /** Ensure that the character buffer is sufficiently full */ |
---|
72 | virtual void fill(unsigned int amount); |
---|
73 | |
---|
74 | /** Override this in subclasses to get the next character */ |
---|
75 | virtual int getChar()=0; |
---|
76 | |
---|
77 | /** Get a lookahead character */ |
---|
78 | virtual inline int LA(unsigned int i) |
---|
79 | { |
---|
80 | fill(i); |
---|
81 | return queue.elementAt(markerOffset + i - 1); |
---|
82 | } |
---|
83 | |
---|
84 | /** Return an integer marker that can be used to rewind the buffer to |
---|
85 | * its current state. |
---|
86 | */ |
---|
87 | virtual unsigned int mark(); |
---|
88 | /// Are there any marks active in the InputBuffer |
---|
89 | virtual inline bool isMarked() const |
---|
90 | { |
---|
91 | return (nMarkers != 0); |
---|
92 | } |
---|
93 | /** Rewind the character buffer to a marker. |
---|
94 | * @param mark Marker returned previously from mark() |
---|
95 | */ |
---|
96 | virtual void rewind(unsigned int mark); |
---|
97 | |
---|
98 | /** Get the number of non-consumed characters |
---|
99 | */ |
---|
100 | virtual unsigned int entries() const; |
---|
101 | |
---|
102 | ANTLR_USE_NAMESPACE(std)string getLAChars() const; |
---|
103 | |
---|
104 | ANTLR_USE_NAMESPACE(std)string getMarkedChars() const; |
---|
105 | |
---|
106 | protected: |
---|
107 | // char source |
---|
108 | // leave to subclasses |
---|
109 | |
---|
110 | // Number of active markers |
---|
111 | unsigned int nMarkers; // = 0; |
---|
112 | |
---|
113 | // Additional offset used when markers are active |
---|
114 | unsigned int markerOffset; // = 0; |
---|
115 | |
---|
116 | // Number of calls to consume() since last LA() or LT() call |
---|
117 | unsigned int numToConsume; // = 0; |
---|
118 | |
---|
119 | // Circular queue |
---|
120 | CircularQueue<int> queue; |
---|
121 | |
---|
122 | /** Sync up deferred consumption */ |
---|
123 | void syncConsume(); |
---|
124 | |
---|
125 | private: |
---|
126 | InputBuffer(const InputBuffer& other); |
---|
127 | InputBuffer& operator=(const InputBuffer& other); |
---|
128 | }; |
---|
129 | |
---|
130 | /** Sync up deferred consumption */ |
---|
131 | inline void InputBuffer::syncConsume() { |
---|
132 | if (numToConsume > 0) |
---|
133 | { |
---|
134 | if (nMarkers > 0) |
---|
135 | markerOffset += numToConsume; |
---|
136 | else |
---|
137 | queue.removeItems( numToConsume ); |
---|
138 | numToConsume = 0; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
---|
143 | } |
---|
144 | #endif |
---|
145 | |
---|
146 | #endif //INC_InputBuffer_hpp__ |
---|