blob: 3e21653346ef3bb4b7cf17de11addb09412d9f2f [file] [log] [blame]
Till Westmannea8ab392013-06-05 15:17:08 -07001/*
Ian Maxon928bbd12015-09-14 17:12:48 -07002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Ian Maxon032a1782015-06-30 17:10:51 -07009 *
Ian Maxon928bbd12015-09-14 17:12:48 -070010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Maxon032a1782015-06-30 17:10:51 -070011 *
Ian Maxon928bbd12015-09-14 17:12:48 -070012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Till Westmannea8ab392013-06-05 15:17:08 -070018 */
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +000019package [PACKAGE];
20
21import java.io.IOException;
22import [PACKAGE].[LEXER_NAME]Exception;
23
24public class [LEXER_NAME] {
25
26 public static final int
27 TOKEN_EOF = 0, TOKEN_AUX_NOT_FOUND = 1 [TOKENS_CONSTANTS];
28
29 // Human representation of tokens. Useful for debug.
30 // Is possible to convert a TOKEN_CONSTANT in its image through
31 // [LEXER_NAME].tokenKindToString(TOKEN_CONSTANT);
32 private static final String[] tokenImage = {
33 "<EOF>", "<AUX_NOT_FOUND>" [TOKENS_IMAGES]
34 };
35
36 private static final char EOF_CHAR = 4;
37 protected java.io.Reader inputStream;
38 protected int column;
39 protected int line;
40 protected boolean prevCharIsCR;
41 protected boolean prevCharIsLF;
Till Westmann8cdbd392014-04-04 18:31:08 -070042 protected boolean containsEscapes;
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +000043 protected char[] buffer;
44 protected int bufsize;
45 protected int bufpos;
46 protected int tokenBegin;
47 protected int endOf_USED_Buffer;
48 protected int endOf_UNUSED_Buffer;
49 protected int maxUnusedBufferSize;
50
51// ================================================================================
52// Auxiliary functions. Can parse the tokens used in the grammar as partial/auxiliary
53// ================================================================================
54
55 [LEXER_AUXFUNCTIONS]
56
57// ================================================================================
58// Main method. Return a TOKEN_CONSTANT
59// ================================================================================
60
Till Westmann8cdbd392014-04-04 18:31:08 -070061 public int next() throws [LEXER_NAME]Exception, IOException {
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +000062 char currentChar = buffer[bufpos];
63 while (currentChar == ' ' || currentChar=='\t' || currentChar == '\n' || currentChar=='\r')
64 currentChar = readNextChar();
65 tokenBegin = bufpos;
Till Westmann8cdbd392014-04-04 18:31:08 -070066 containsEscapes = false;
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +000067 if (currentChar==EOF_CHAR) return TOKEN_EOF;
68
69 [LEXER_LOGIC]
70 }
71
72// ================================================================================
73// Public interface
74// ================================================================================
75
76 public [LEXER_NAME](java.io.Reader stream) throws IOException{
77 reInit(stream);
78 }
79
80 public void reInit(java.io.Reader stream) throws IOException{
81 done();
82 inputStream = stream;
83 bufsize = 4096;
84 line = 1;
85 column = 0;
86 bufpos = -1;
87 endOf_UNUSED_Buffer = bufsize;
88 endOf_USED_Buffer = 0;
89 prevCharIsCR = false;
90 prevCharIsLF = false;
91 buffer = new char[bufsize];
92 tokenBegin = -1;
93 maxUnusedBufferSize = 4096/2;
94 readNextChar();
95 }
96
97 public String getLastTokenImage() {
98 if (bufpos >= tokenBegin)
99 return new String(buffer, tokenBegin, bufpos - tokenBegin);
100 else
101 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
102 new String(buffer, 0, bufpos);
103 }
104
Till Westmannab40b092014-03-28 15:44:01 -0700105 public int getColumn() {
106 return column;
107 }
108
109 public int getLine() {
110 return line;
111 }
112
Till Westmann8cdbd392014-04-04 18:31:08 -0700113 public boolean containsEscapes() {
114 return containsEscapes;
115 }
116
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +0000117 public static String tokenKindToString(int token) {
118 return tokenImage[token];
119 }
120
121 public void done(){
122 buffer = null;
123 }
124
125// ================================================================================
126// Parse error management
127// ================================================================================
128
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +0000129 protected int parseError(int ... tokens) throws [LEXER_NAME]Exception {
130 StringBuilder message = new StringBuilder();
Till Westmannab40b092014-03-28 15:44:01 -0700131 message.append("Parse error at (").append(line).append(", ").append(column).append(")");
132 if (tokens.length > 0) {
133 message.append(" expecting:");
134 for (int tokenId : tokens){
135 message.append(" ").append([LEXER_NAME].tokenKindToString(tokenId));
136 }
diegogiorgini@gmail.com2de6d342013-02-16 02:41:45 +0000137 }
138 throw new [LEXER_NAME]Exception(message.toString());
139 }
140
141 protected void updateLineColumn(char c){
142 column++;
143
144 if (prevCharIsLF)
145 {
146 prevCharIsLF = false;
147 line += (column = 1);
148 }
149 else if (prevCharIsCR)
150 {
151 prevCharIsCR = false;
152 if (c == '\n')
153 {
154 prevCharIsLF = true;
155 }
156 else
157 {
158 line += (column = 1);
159 }
160 }
161
162 if (c=='\r') {
163 prevCharIsCR = true;
164 } else if(c == '\n') {
165 prevCharIsLF = true;
166 }
167 }
168
169// ================================================================================
170// Read data, buffer management. It uses a circular (and expandable) buffer
171// ================================================================================
172
173 protected char readNextChar() throws IOException {
174 if (++bufpos >= endOf_USED_Buffer)
175 fillBuff();
176 char c = buffer[bufpos];
177 updateLineColumn(c);
178 return c;
179 }
180
181 protected boolean fillBuff() throws IOException {
182 if (endOf_UNUSED_Buffer == endOf_USED_Buffer) // If no more unused buffer space
183 {
184 if (endOf_UNUSED_Buffer == bufsize) // -- If the previous unused space was
185 { // -- at the end of the buffer
186 if (tokenBegin > maxUnusedBufferSize) // -- -- If the first N bytes before
187 { // the current token are enough
188 bufpos = endOf_USED_Buffer = 0; // -- -- -- setup buffer to use that fragment
189 endOf_UNUSED_Buffer = tokenBegin;
190 }
191 else if (tokenBegin < 0) // -- -- If no token yet
192 bufpos = endOf_USED_Buffer = 0; // -- -- -- reuse the whole buffer
193 else
194 ExpandBuff(false); // -- -- Otherwise expand buffer after its end
195 }
196 else if (endOf_UNUSED_Buffer > tokenBegin) // If the endOf_UNUSED_Buffer is after the token
197 endOf_UNUSED_Buffer = bufsize; // -- set endOf_UNUSED_Buffer to the end of the buffer
198 else if ((tokenBegin - endOf_UNUSED_Buffer) < maxUnusedBufferSize)
199 { // If between endOf_UNUSED_Buffer and the token
200 ExpandBuff(true); // there is NOT enough space expand the buffer
201 } // reorganizing it
202 else
203 endOf_UNUSED_Buffer = tokenBegin; // Otherwise there is enough space at the start
204 } // so we set the buffer to use that fragment
205 int i;
206 if ((i = inputStream.read(buffer, endOf_USED_Buffer, endOf_UNUSED_Buffer - endOf_USED_Buffer)) == -1)
207 {
208 inputStream.close();
209 buffer[endOf_USED_Buffer]=(char)EOF_CHAR;
210 endOf_USED_Buffer++;
211 return false;
212 }
213 else
214 endOf_USED_Buffer += i;
215 return true;
216 }
217
218
219 protected void ExpandBuff(boolean wrapAround)
220 {
221 char[] newbuffer = new char[bufsize + maxUnusedBufferSize];
222
223 try {
224 if (wrapAround) {
225 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
226 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
227 buffer = newbuffer;
228 endOf_USED_Buffer = (bufpos += (bufsize - tokenBegin));
229 }
230 else {
231 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
232 buffer = newbuffer;
233 endOf_USED_Buffer = (bufpos -= tokenBegin);
234 }
235 } catch (Throwable t) {
236 throw new Error(t.getMessage());
237 }
238
239 bufsize += maxUnusedBufferSize;
240 endOf_UNUSED_Buffer = bufsize;
241 tokenBegin = 0;
242 }
243}