KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > InputBuffer


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 // SAS: Added this class to genericise the input buffers for scanners
10
// This allows a scanner to use a binary (FileInputStream) or
11
// text (FileReader) stream of data; the generated scanner
12
// subclass will define the input stream
13
// There are two subclasses to this: CharBuffer and ByteBuffer
14

15 import java.io.IOException JavaDoc;
16
17 /**A Stream of characters fed to the lexer from a InputStream that can
18  * be rewound via mark()/rewind() methods.
19  * <p>
20  * A dynamic array is used to buffer up all the input characters. Normally,
21  * "k" characters are stored in the buffer. More characters may be stored during
22  * guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
23  * Consumption of characters is deferred. In other words, reading the next
24  * character is not done by conume(), but deferred until needed by LA or LT.
25  * <p>
26  *
27  * @see persistence.antlr.CharQueue
28  */

29 public abstract class InputBuffer {
30     // Number of active markers
31
protected int nMarkers = 0;
32
33     // Additional offset used when markers are active
34
protected int markerOffset = 0;
35
36     // Number of calls to consume() since last LA() or LT() call
37
protected int numToConsume = 0;
38
39     // Circular queue
40
protected CharQueue queue;
41
42     /** Create an input buffer */
43     public InputBuffer() {
44         queue = new CharQueue(1);
45     }
46
47     /** This method updates the state of the input buffer so that
48      * the text matched since the most recent mark() is no longer
49      * held by the buffer. So, you either do a mark/rewind for
50      * failed predicate or mark/commit to keep on parsing without
51      * rewinding the input.
52      */

53     public void commit() {
54         nMarkers--;
55     }
56
57     /** Mark another character for deferred consumption */
58     public void consume() {
59         numToConsume++;
60     }
61
62     /** Ensure that the input buffer is sufficiently full */
63     public abstract void fill(int amount) throws CharStreamException;
64
65     public String JavaDoc getLAChars() {
66         StringBuffer JavaDoc la = new StringBuffer JavaDoc();
67         for (int i = markerOffset; i < queue.nbrEntries; i++)
68             la.append(queue.elementAt(i));
69         return la.toString();
70     }
71
72     public String JavaDoc getMarkedChars() {
73         StringBuffer JavaDoc marked = new StringBuffer JavaDoc();
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     /** Get a lookahead character */
84     public char LA(int i) throws CharStreamException {
85         fill(i);
86         return queue.elementAt(markerOffset + i - 1);
87     }
88
89     /**Return an integer marker that can be used to rewind the buffer to
90      * its current state.
91      */

92     public int mark() {
93         syncConsume();
94         nMarkers++;
95         return markerOffset;
96     }
97
98     /**Rewind the character buffer to a marker.
99      * @param mark Marker returned previously from mark()
100      */

101     public void rewind(int mark) {
102         syncConsume();
103         markerOffset = mark;
104         nMarkers--;
105     }
106
107     /** Reset the input buffer
108      */

109     public void reset() {
110         nMarkers = 0;
111         markerOffset = 0;
112         numToConsume = 0;
113         queue.reset();
114     }
115
116     /** Sync up deferred consumption */
117     protected void syncConsume() {
118         while (numToConsume > 0) {
119             if (nMarkers > 0) {
120                 // guess mode -- leave leading characters and bump offset.
121
markerOffset++;
122             }
123             else {
124                 // normal mode -- remove first character
125
queue.removeFirst();
126             }
127             numToConsume--;
128         }
129     }
130 }
131
Popular Tags