KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > template > parser > InputBuffer


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.template.parser;
19
20 /**
21  * Class InputBuffer
22  *
23  *
24  * @author
25  * @version %I%, %G%
26  */

27 public abstract class InputBuffer {
28
29    // Number of active markers
30

31    /** Field nMarkers */
32    protected int nMarkers = 0;
33
34    // Additional offset used when markers are active
35

36    /** Field markerOffset */
37    protected int markerOffset = 0;
38
39    // Number of calls to consume() since last LA()
40

41    /** Field numToConsume */
42    protected int numToConsume = 0;
43
44    // Circular queue
45

46    /** Field queue */
47    protected CharQueue queue;
48
49
50    /** Create an input buffer */
51    public InputBuffer() {
52       queue = new CharQueue(1);
53    }
54
55
56    /**
57     * This method updates the state of the input buffer so that
58     * the text matched since the most recent mark() is no longer
59     * held by the buffer. So, you either do a mark/rewind for
60     * failed predicate or mark/commit to keep on parsing without
61     * rewinding the input.
62     */

63    public void commit() {
64       nMarkers = (nMarkers > 0) ? nMarkers - 1 : 0;
65    }
66
67
68    /** Mark another character for deferred consumption */
69    public void consume() {
70       numToConsume++;
71    }
72
73
74    /**
75     * Ensure that the input buffer is sufficiently full
76     *
77     * @param amount
78     *
79     * @throws CharStreamException
80     */

81    public abstract void fill(int amount) throws CharStreamException;
82
83
84    /**
85     * Method getLAChars
86     *
87     *
88     * @return
89     *
90     */

91    public String JavaDoc getLAChars() {
92       StringBuffer JavaDoc la = new StringBuffer JavaDoc();
93       for (int i = markerOffset; i < queue.nbrEntries; i++) {
94          la.append(queue.elementAt(i));
95       }
96
97       return la.toString();
98    }
99
100
101    /**
102     * Method getCharsFromMark
103     *
104     *
105     * @param mark
106     *
107     * @return
108     *
109     */

110    public String JavaDoc getCharsFromMark(int mark) {
111       StringBuffer JavaDoc la = new StringBuffer JavaDoc();
112       int i = mark;
113
114       mark();
115       for (; i < markerOffset; i++) {
116          la.append(queue.elementAt(i));
117       }
118       commit();
119
120       return la.toString();
121    }
122
123
124    /**
125     * Method LAChars
126     *
127     *
128     * @param n
129     *
130     * @return
131     *
132     * @throws CharStreamException
133     *
134     */

135    public String JavaDoc LAChars(int n) throws CharStreamException {
136       StringBuffer JavaDoc la = new StringBuffer JavaDoc();
137
138       for (int i = 0; i < n; i++) {
139          la.append(LA(i + 1));
140       }
141
142       return la.toString();
143    }
144
145
146    /**
147     * Method getMarkedChars
148     *
149     *
150     * @return
151     *
152     */

153    public String JavaDoc getMarkedChars() {
154       StringBuffer JavaDoc marked = new StringBuffer JavaDoc();
155
156       for (int i = 0; i < markerOffset; i++) {
157          marked.append(queue.elementAt(i));
158       }
159
160       return marked.toString();
161    }
162
163
164    /**
165     * Method isMarked
166     *
167     *
168     * @return
169     *
170     */

171    public boolean isMarked() {
172       return (nMarkers != 0);
173    }
174
175
176    /**
177     * Get a lookahead character
178     *
179     * @param i
180     *
181     * @return
182     *
183     * @throws CharStreamException
184     */

185    public char LA(int i) throws CharStreamException {
186       fill(i);
187       return queue.elementAt(markerOffset + i - 1);
188    }
189
190
191    /**
192     * Return an integer marker that can be used to rewind the buffer to
193     * its current state.
194     *
195     * @return
196     */

197    public int mark() {
198       syncConsume();
199       nMarkers++;
200       return markerOffset;
201    }
202
203
204    /**
205     * Rewind the character buffer to a marker.
206     * @param mark Marker returned previously from mark()
207     */

208    public void rewind(int mark) {
209       syncConsume();
210       markerOffset = mark;
211       nMarkers--;
212    }
213
214
215    /** Sync up deferred consumption */
216    protected void syncConsume() {
217       while (numToConsume > 0) {
218          if (nMarkers > 0) {
219             // guess mode -- leave leading characters and bump offset.
220
markerOffset++;
221          }
222          else {
223             // normal mode -- remove first character
224
queue.removeFirst();
225          }
226          numToConsume--;
227       }
228    }
229 }
Popular Tags