1 package antlr; 2 3 9 10 24 25 import java.io.IOException ; 26 27 public class TokenBuffer { 28 29 protected TokenStream input; 31 32 int nMarkers = 0; 34 35 int markerOffset = 0; 37 38 int numToConsume = 0; 40 41 TokenQueue queue; 43 44 45 public TokenBuffer(TokenStream input_) { 46 input = input_; 47 queue = new TokenQueue(1); 48 } 49 50 51 public final void reset() { 52 nMarkers = 0; 53 markerOffset = 0; 54 numToConsume = 0; 55 queue.reset(); 56 } 57 58 59 public final void consume() { 60 numToConsume++; 61 } 62 63 64 private final void fill(int amount) throws TokenStreamException { 65 syncConsume(); 66 while (queue.nbrEntries < amount + markerOffset) { 68 queue.append(input.nextToken()); 70 } 71 } 72 73 74 public TokenStream getInput() { 75 return input; 76 } 77 78 79 public final int LA(int i) throws TokenStreamException { 80 fill(i); 81 return queue.elementAt(markerOffset + i - 1).type; 82 } 83 84 85 public final Token LT(int i) throws TokenStreamException { 86 fill(i); 87 return queue.elementAt(markerOffset + i - 1); 88 } 89 90 93 public final int mark() { 94 syncConsume(); 95 nMarkers++; 98 return markerOffset; 99 } 100 101 104 public final void rewind(int mark) { 105 syncConsume(); 106 markerOffset = mark; 107 nMarkers--; 108 } 111 112 113 private final void syncConsume() { 114 while (numToConsume > 0) { 115 if (nMarkers > 0) { 116 markerOffset++; 118 } 119 else { 120 queue.removeFirst(); 122 } 123 numToConsume--; 124 } 125 } 126 } 127 | Popular Tags |