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