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