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