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