1 | /* ANTLR Translator Generator |
---|
2 | * Project led by Terence Parr at http://www.jGuru.com |
---|
3 | * Software rights: http://www.antlr.org/license.html |
---|
4 | * |
---|
5 | * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/src/InputBuffer.cpp#2 $ |
---|
6 | */ |
---|
7 | |
---|
8 | #include "antlr/config.hpp" |
---|
9 | #include "antlr/InputBuffer.hpp" |
---|
10 | |
---|
11 | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
---|
12 | namespace antlr { |
---|
13 | #endif |
---|
14 | |
---|
15 | /** Ensure that the character buffer is sufficiently full */ |
---|
16 | void InputBuffer::fill(unsigned int amount) |
---|
17 | { |
---|
18 | syncConsume(); |
---|
19 | // Fill the buffer sufficiently to hold needed characters |
---|
20 | while (queue.entries() < amount + markerOffset) |
---|
21 | { |
---|
22 | // Append the next character |
---|
23 | queue.append(getChar()); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | /** get the current lookahead characters as a string |
---|
28 | * @warning it may treat 0 and EOF values wrong |
---|
29 | */ |
---|
30 | ANTLR_USE_NAMESPACE(std)string InputBuffer::getLAChars( void ) const |
---|
31 | { |
---|
32 | ANTLR_USE_NAMESPACE(std)string ret; |
---|
33 | |
---|
34 | for(unsigned int i = markerOffset; i < queue.entries(); i++) |
---|
35 | ret += queue.elementAt(i); |
---|
36 | |
---|
37 | return ret; |
---|
38 | } |
---|
39 | |
---|
40 | /** get the current marked characters as a string |
---|
41 | * @warning it may treat 0 and EOF values wrong |
---|
42 | */ |
---|
43 | ANTLR_USE_NAMESPACE(std)string InputBuffer::getMarkedChars( void ) const |
---|
44 | { |
---|
45 | ANTLR_USE_NAMESPACE(std)string ret; |
---|
46 | |
---|
47 | for(unsigned int i = 0; i < markerOffset; i++) |
---|
48 | ret += queue.elementAt(i); |
---|
49 | |
---|
50 | return ret; |
---|
51 | } |
---|
52 | |
---|
53 | /** Return an integer marker that can be used to rewind the buffer to |
---|
54 | * its current state. |
---|
55 | */ |
---|
56 | unsigned int InputBuffer::mark() |
---|
57 | { |
---|
58 | syncConsume(); |
---|
59 | nMarkers++; |
---|
60 | return markerOffset; |
---|
61 | } |
---|
62 | |
---|
63 | /** Rewind the character buffer to a marker. |
---|
64 | * @param mark Marker returned previously from mark() |
---|
65 | */ |
---|
66 | void InputBuffer::rewind(unsigned int mark) |
---|
67 | { |
---|
68 | syncConsume(); |
---|
69 | markerOffset = mark; |
---|
70 | nMarkers--; |
---|
71 | } |
---|
72 | |
---|
73 | unsigned int InputBuffer::entries() const |
---|
74 | { |
---|
75 | //assert(queue.entries() >= markerOffset); |
---|
76 | return queue.entries() - markerOffset; |
---|
77 | } |
---|
78 | |
---|
79 | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
---|
80 | } |
---|
81 | #endif |
---|