1 package persistence.antlr; 2 3 8 9 15 import java.io.IOException ; 16 17 29 public abstract class InputBuffer { 30 protected int nMarkers = 0; 32 33 protected int markerOffset = 0; 35 36 protected int numToConsume = 0; 38 39 protected CharQueue queue; 41 42 43 public InputBuffer() { 44 queue = new CharQueue(1); 45 } 46 47 53 public void commit() { 54 nMarkers--; 55 } 56 57 58 public void consume() { 59 numToConsume++; 60 } 61 62 63 public abstract void fill(int amount) throws CharStreamException; 64 65 public String getLAChars() { 66 StringBuffer la = new StringBuffer (); 67 for (int i = markerOffset; i < queue.nbrEntries; i++) 68 la.append(queue.elementAt(i)); 69 return la.toString(); 70 } 71 72 public String getMarkedChars() { 73 StringBuffer marked = new StringBuffer (); 74 for (int i = 0; i < markerOffset; i++) 75 marked.append(queue.elementAt(i)); 76 return marked.toString(); 77 } 78 79 public boolean isMarked() { 80 return (nMarkers != 0); 81 } 82 83 84 public char LA(int i) throws CharStreamException { 85 fill(i); 86 return queue.elementAt(markerOffset + i - 1); 87 } 88 89 92 public int mark() { 93 syncConsume(); 94 nMarkers++; 95 return markerOffset; 96 } 97 98 101 public void rewind(int mark) { 102 syncConsume(); 103 markerOffset = mark; 104 nMarkers--; 105 } 106 107 109 public void reset() { 110 nMarkers = 0; 111 markerOffset = 0; 112 numToConsume = 0; 113 queue.reset(); 114 } 115 116 117 protected void syncConsume() { 118 while (numToConsume > 0) { 119 if (nMarkers > 0) { 120 markerOffset++; 122 } 123 else { 124 queue.removeFirst(); 126 } 127 numToConsume--; 128 } 129 } 130 } 131 | Popular Tags |