KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > InputBuffer


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/InputBuffer.java#7 $
8  */

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

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

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

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

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

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

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