| 1 | tim | 770 | /* 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: TokenBuffer.cpp,v 1.1 2005-12-02 15:38:02 tim Exp $ | 
| 6 |  |  | */ | 
| 7 |  |  |  | 
| 8 |  |  | #include "antlr/TokenBuffer.hpp" | 
| 9 |  |  |  | 
| 10 |  |  | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE | 
| 11 |  |  | namespace antlr { | 
| 12 |  |  | #endif | 
| 13 |  |  |  | 
| 14 |  |  | /**A Stream of Token objects fed to the parser from a TokenStream that can | 
| 15 |  |  | * be rewound via mark()/rewind() methods. | 
| 16 |  |  | * <p> | 
| 17 |  |  | * A dynamic array is used to buffer up all the input tokens.  Normally, | 
| 18 |  |  | * "k" tokens are stored in the buffer.  More tokens may be stored during | 
| 19 |  |  | * guess mode (testing syntactic predicate), or when LT(i>k) is referenced. | 
| 20 |  |  | * Consumption of tokens is deferred.  In other words, reading the next | 
| 21 |  |  | * token is not done by conume(), but deferred until needed by LA or LT. | 
| 22 |  |  | * <p> | 
| 23 |  |  | * | 
| 24 |  |  | * @see antlr.Token | 
| 25 |  |  | * @see antlr.TokenStream | 
| 26 |  |  | * @see antlr.TokenQueue | 
| 27 |  |  | */ | 
| 28 |  |  |  | 
| 29 |  |  | /** Create a token buffer */ | 
| 30 |  |  | TokenBuffer::TokenBuffer( TokenStream& inp ) | 
| 31 |  |  | : input(inp) | 
| 32 |  |  | , nMarkers(0) | 
| 33 |  |  | , markerOffset(0) | 
| 34 |  |  | , numToConsume(0) | 
| 35 |  |  | { | 
| 36 |  |  | } | 
| 37 |  |  |  | 
| 38 |  |  | TokenBuffer::~TokenBuffer( void ) | 
| 39 |  |  | { | 
| 40 |  |  | } | 
| 41 |  |  |  | 
| 42 |  |  | /** Ensure that the token buffer is sufficiently full */ | 
| 43 |  |  | void TokenBuffer::fill(unsigned int amount) | 
| 44 |  |  | { | 
| 45 |  |  | syncConsume(); | 
| 46 |  |  | // Fill the buffer sufficiently to hold needed tokens | 
| 47 |  |  | while (queue.entries() < (amount + markerOffset)) | 
| 48 |  |  | { | 
| 49 |  |  | // Append the next token | 
| 50 |  |  | queue.append(input.nextToken()); | 
| 51 |  |  | } | 
| 52 |  |  | } | 
| 53 |  |  |  | 
| 54 |  |  | /** Get a lookahead token value */ | 
| 55 |  |  | int TokenBuffer::LA(unsigned int i) | 
| 56 |  |  | { | 
| 57 |  |  | fill(i); | 
| 58 |  |  | return queue.elementAt(markerOffset+i-1)->getType(); | 
| 59 |  |  | } | 
| 60 |  |  |  | 
| 61 |  |  | /** Get a lookahead token */ | 
| 62 |  |  | RefToken TokenBuffer::LT(unsigned int i) | 
| 63 |  |  | { | 
| 64 |  |  | fill(i); | 
| 65 |  |  | return queue.elementAt(markerOffset+i-1); | 
| 66 |  |  | } | 
| 67 |  |  |  | 
| 68 |  |  | /** Return an integer marker that can be used to rewind the buffer to | 
| 69 |  |  | * its current state. | 
| 70 |  |  | */ | 
| 71 |  |  | unsigned int TokenBuffer::mark() | 
| 72 |  |  | { | 
| 73 |  |  | syncConsume(); | 
| 74 |  |  | nMarkers++; | 
| 75 |  |  | return markerOffset; | 
| 76 |  |  | } | 
| 77 |  |  |  | 
| 78 |  |  | /**Rewind the token buffer to a marker. | 
| 79 |  |  | * @param mark Marker returned previously from mark() | 
| 80 |  |  | */ | 
| 81 |  |  | void TokenBuffer::rewind(unsigned int mark) | 
| 82 |  |  | { | 
| 83 |  |  | syncConsume(); | 
| 84 |  |  | markerOffset=mark; | 
| 85 |  |  | nMarkers--; | 
| 86 |  |  | } | 
| 87 |  |  |  | 
| 88 |  |  | /// Get number of non-consumed tokens | 
| 89 |  |  | unsigned int TokenBuffer::entries() const | 
| 90 |  |  | { | 
| 91 |  |  | return queue.entries() - markerOffset; | 
| 92 |  |  | } | 
| 93 |  |  |  | 
| 94 |  |  | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE | 
| 95 |  |  | } | 
| 96 |  |  | #endif |