%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.web.util.sql.SQLToken |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: chetan.mehrotra (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | ||
23 | package net.sf.infrared.web.util.sql; |
|
24 | ||
25 | ||
26 | /** |
|
27 | * A SQLToken is a token that is returned by a lexer that is lexing an SQL |
|
28 | * source file. It has several attributes describing the token: |
|
29 | * The type of token, the text of the token, the line number on which it |
|
30 | * occurred, the number of characters into the input at which it started, and |
|
31 | * similarly, the number of characters into the input at which it ended. <br> |
|
32 | */ |
|
33 | public class SQLToken extends Token { |
|
34 | /** |
|
35 | * A reserved word (keyword) |
|
36 | */ |
|
37 | public final static int RESERVED_WORD = 0x100; |
|
38 | ||
39 | /** |
|
40 | * A variable, name, or other identifier |
|
41 | */ |
|
42 | public final static int IDENTIFIER = 0x200; |
|
43 | ||
44 | /** |
|
45 | * A string literal |
|
46 | */ |
|
47 | public final static int QUOTED_STRING = 0x600; |
|
48 | ||
49 | /** |
|
50 | * A string literal |
|
51 | */ |
|
52 | public final static int LITERAL_STRING = 0x300; |
|
53 | ||
54 | /** |
|
55 | * A bit-string |
|
56 | */ |
|
57 | public final static int LITERAL_BIT_STRING = 0x310; |
|
58 | /** |
|
59 | * An integer |
|
60 | */ |
|
61 | public final static int LITERAL_INTEGER = 0x320; |
|
62 | /** |
|
63 | * A floating point |
|
64 | */ |
|
65 | public final static int LITERAL_FLOAT = 0x330; |
|
66 | ||
67 | /** |
|
68 | * A separator |
|
69 | */ |
|
70 | public final static int SEPARATOR = 0x400; |
|
71 | ||
72 | /** |
|
73 | * An operator |
|
74 | */ |
|
75 | public final static int OPERATOR = 0x500; |
|
76 | ||
77 | /** |
|
78 | * C style comment, (except possibly nested) |
|
79 | */ |
|
80 | public final static int COMMENT_TRADITIONAL = 0xD00; |
|
81 | ||
82 | /** |
|
83 | * a -- to end of line comment. |
|
84 | */ |
|
85 | public final static int COMMENT_END_OF_LINE = 0xD10; |
|
86 | ||
87 | /** |
|
88 | * White space |
|
89 | */ |
|
90 | public final static int WHITE_SPACE = 0xE00; |
|
91 | ||
92 | /** |
|
93 | * An error |
|
94 | */ |
|
95 | public final static int ERROR = 0xF00; |
|
96 | /** |
|
97 | * An comment start embedded in an operator |
|
98 | */ |
|
99 | public final static int ERROR_UNCLOSED_COMMENT = 0xF02; |
|
100 | /** |
|
101 | * An comment start embedded in an operator |
|
102 | */ |
|
103 | public final static int ERROR_UNCLOSED_STRING = 0xF03; |
|
104 | /** |
|
105 | * An comment start embedded in an operator |
|
106 | */ |
|
107 | public final static int ERROR_UNCLOSED_BIT_STRING = 0xF04; |
|
108 | /** |
|
109 | * An comment start embedded in an operator |
|
110 | */ |
|
111 | public final static int ERROR_BAD_BIT_STRING = 0xF05; |
|
112 | ||
113 | private int ID; |
|
114 | private String contents; |
|
115 | private int lineNumber; |
|
116 | private int charBegin; |
|
117 | private int charEnd; |
|
118 | private int state; |
|
119 | ||
120 | /** |
|
121 | * Create a new token. |
|
122 | * The constructor is typically called by the lexer |
|
123 | * |
|
124 | * @param ID the id number of the token |
|
125 | * @param contents A string representing the text of the token |
|
126 | * @param lineNumber the line number of the input on which this token started |
|
127 | * @param charBegin the offset into the input in characters at which this token started |
|
128 | * @param charEnd the offset into the input in characters at which this token ended |
|
129 | */ |
|
130 | public SQLToken(int ID, String contents, class="keyword">int lineNumber, class="keyword">int charBegin, class="keyword">int charEnd){ |
|
131 | 0 | this (ID, contents, lineNumber, charBegin, charEnd, Token.UNDEFINED_STATE); |
132 | 0 | } |
133 | ||
134 | /** |
|
135 | * Create a new token. |
|
136 | * The constructor is typically called by the lexer |
|
137 | * |
|
138 | * @param ID the id number of the token |
|
139 | * @param contents A string representing the text of the token |
|
140 | * @param lineNumber the line number of the input on which this token started |
|
141 | * @param charBegin the offset into the input in characters at which this token started |
|
142 | * @param charEnd the offset into the input in characters at which this token ended |
|
143 | * @param state the state the tokenizer is in after returning this token. |
|
144 | */ |
|
145 | 0 | public SQLToken(int ID, String contents, class="keyword">int lineNumber, class="keyword">int charBegin, class="keyword">int charEnd, class="keyword">int state){ |
146 | 0 | this.ID = ID; |
147 | 0 | this.contents = new String(contents); |
148 | 0 | this.lineNumber = lineNumber; |
149 | 0 | this.charBegin = charBegin; |
150 | 0 | this.charEnd = charEnd; |
151 | 0 | this.state = state; |
152 | 0 | } |
153 | ||
154 | /** |
|
155 | * Get an integer representing the state the tokenizer is in after |
|
156 | * returning this token. |
|
157 | * Those who are interested in incremental tokenizing for performance |
|
158 | * reasons will want to use this method to figure out where the tokenizer |
|
159 | * may be restarted. The tokenizer starts in Token.INITIAL_STATE, so |
|
160 | * any time that it reports that it has returned to this state, the |
|
161 | * tokenizer may be restarted from there. |
|
162 | */ |
|
163 | public int getState(){ |
|
164 | 0 | return state; |
165 | } |
|
166 | ||
167 | /** |
|
168 | * get the ID number of this token |
|
169 | * |
|
170 | * @return the id number of the token |
|
171 | */ |
|
172 | public int getID(){ |
|
173 | 0 | return ID; |
174 | } |
|
175 | ||
176 | /** |
|
177 | * get the contents of this token |
|
178 | * |
|
179 | * @return A string representing the text of the token |
|
180 | */ |
|
181 | public String getContents(){ |
|
182 | 0 | return (new String(contents)); |
183 | } |
|
184 | ||
185 | /** |
|
186 | * get the line number of the input on which this token started |
|
187 | * |
|
188 | * @return the line number of the input on which this token started |
|
189 | */ |
|
190 | public int getLineNumber(){ |
|
191 | 0 | return lineNumber; |
192 | } |
|
193 | ||
194 | /** |
|
195 | * get the offset into the input in characters at which this token started |
|
196 | * |
|
197 | * @return the offset into the input in characters at which this token started |
|
198 | */ |
|
199 | public int getCharBegin(){ |
|
200 | 0 | return charBegin; |
201 | } |
|
202 | ||
203 | /** |
|
204 | * get the offset into the input in characters at which this token ended |
|
205 | * |
|
206 | * @return the offset into the input in characters at which this token ended |
|
207 | */ |
|
208 | public int getCharEnd(){ |
|
209 | 0 | return charEnd; |
210 | } |
|
211 | ||
212 | /** |
|
213 | * Checks this token to see if it is a reserved word. |
|
214 | * Reserved words are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java |
|
215 | * Language Specification</A>. |
|
216 | * |
|
217 | * @return true if this token is a reserved word, false otherwise |
|
218 | */ |
|
219 | public boolean isReservedWord(){ |
|
220 | 0 | return((ID >> 8) == 0x1); |
221 | } |
|
222 | ||
223 | /** |
|
224 | * Checks this token to see if it is an identifier. |
|
225 | * Identifiers are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java |
|
226 | * Language Specification</A>. |
|
227 | * |
|
228 | * @return true if this token is an identifier, false otherwise |
|
229 | */ |
|
230 | public boolean isIdentifier(){ |
|
231 | 0 | return((ID >> 8) == 0x2); |
232 | } |
|
233 | ||
234 | /** |
|
235 | * Checks this token to see if it is a literal. |
|
236 | * Literals are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java |
|
237 | * Language Specification</A>. |
|
238 | * |
|
239 | * @return true if this token is a literal, false otherwise |
|
240 | */ |
|
241 | public boolean isLiteral(){ |
|
242 | 0 | return((ID >> 8) == 0x3); |
243 | } |
|
244 | ||
245 | /** |
|
246 | * Checks this token to see if it is a Separator. |
|
247 | * Separators are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java |
|
248 | * Language Specification</A>. |
|
249 | * |
|
250 | * @return true if this token is a Separator, false otherwise |
|
251 | */ |
|
252 | public boolean isSeparator(){ |
|
253 | 0 | return((ID >> 8) == 0x4); |
254 | } |
|
255 | ||
256 | /** |
|
257 | * Checks this token to see if it is a Operator. |
|
258 | * Operators are explained in <A Href=http://java.sun.com/docs/books/jls/html/>Java |
|
259 | * Language Specification</A>. |
|
260 | * |
|
261 | * @return true if this token is a Operator, false otherwise |
|
262 | */ |
|
263 | public boolean isOperator(){ |
|
264 | 0 | return((ID >> 8) == 0x5); |
265 | } |
|
266 | ||
267 | /** |
|
268 | * Checks this token to see if it is a comment. |
|
269 | * |
|
270 | * @return true if this token is a comment, false otherwise |
|
271 | */ |
|
272 | public boolean isComment(){ |
|
273 | 0 | return((ID >> 8) == 0xD); |
274 | } |
|
275 | ||
276 | /** |
|
277 | * Checks this token to see if it is White Space. |
|
278 | * Usually tabs, line breaks, form feed, spaces, etc. |
|
279 | * |
|
280 | * @return true if this token is White Space, false otherwise |
|
281 | */ |
|
282 | public boolean isWhiteSpace(){ |
|
283 | 0 | return((ID >> 8) == 0xE); |
284 | } |
|
285 | ||
286 | /** |
|
287 | * Checks this token to see if it is an Error. |
|
288 | * Unfinished comments, numbers that are too big, unclosed strings, etc. |
|
289 | * |
|
290 | * @return true if this token is an Error, false otherwise |
|
291 | */ |
|
292 | public boolean isError(){ |
|
293 | 0 | return((ID >> 8) == 0xF); |
294 | } |
|
295 | ||
296 | ||
297 | ||
298 | /** |
|
299 | * Checks this token to see if it is an Quoted string. |
|
300 | * |
|
301 | * @return true if this token is an Quoted String, false otherwise |
|
302 | */ |
|
303 | public boolean isQuotedString(){ |
|
304 | 0 | return((ID >> 8) == 0x6); |
305 | } |
|
306 | ||
307 | /** |
|
308 | * A description of this token. The description should |
|
309 | * be appropriate for syntax highlighting. For example |
|
310 | * "comment" is returned for a comment. |
|
311 | * |
|
312 | * @return a description of this token. |
|
313 | */ |
|
314 | public String getDescription(){ |
|
315 | 0 | if (isReservedWord()){ |
316 | 0 | return("reservedWord"); |
317 | 0 | } else if (isIdentclass="keyword">ifier()){ |
318 | 0 | return("identifier"); |
319 | 0 | } else if (isLiteral()){ |
320 | 0 | return("literal"); |
321 | 0 | } else if (isSeparator()){ |
322 | 0 | return("separator"); |
323 | 0 | } else if (isOperator()){ |
324 | 0 | return("operator"); |
325 | 0 | } else if (isComment()){ |
326 | 0 | return("comment"); |
327 | 0 | } else if (isWhiteSpace()){ |
328 | 0 | return("whitespace"); |
329 | 0 | } else if (isError()){ |
330 | 0 | return("error"); |
331 | 0 | } else if (isQuotedString()){ |
332 | 0 | return("quoted"); |
333 | } else { |
|
334 | 0 | return("unknown"); |
335 | } |
|
336 | } |
|
337 | ||
338 | /** |
|
339 | * get a String that explains the error, if this token is an error. |
|
340 | * |
|
341 | * @return a String that explains the error, if this token is an error, null otherwise. |
|
342 | */ |
|
343 | public String errorString(){ |
|
344 | String s; |
|
345 | 0 | if (isError()){ |
346 | 0 | s = "Error on line " + lineNumber + ": "; |
347 | 0 | switch (ID){ |
348 | case ERROR: |
|
349 | 0 | s += "Unexpected token: " + contents; |
350 | 0 | break; |
351 | case ERROR_UNCLOSED_COMMENT: |
|
352 | 0 | s += "Unclosed comment: " + contents; |
353 | 0 | break; |
354 | case ERROR_UNCLOSED_STRING: |
|
355 | 0 | s += "Unclosed string literal: " + contents; |
356 | 0 | break; |
357 | case ERROR_UNCLOSED_BIT_STRING: |
|
358 | 0 | s += "Unclosed bit-string literal: " + contents; |
359 | 0 | break; |
360 | case ERROR_BAD_BIT_STRING: |
|
361 | 0 | s += "Bit-strings can only contain 0 and 1: " + contents; |
362 | 0 | break; |
363 | } |
|
364 | ||
365 | } else { |
|
366 | 0 | s = null; |
367 | } |
|
368 | 0 | return (s); |
369 | } |
|
370 | ||
371 | /** |
|
372 | * get a representation of this token as a human readable string. |
|
373 | * The format of this string is subject to change and should only be used |
|
374 | * for debugging purposes. |
|
375 | * |
|
376 | * @return a string representation of this token |
|
377 | */ |
|
378 | public String toString() { |
|
379 | 0 | return ("Token #" + Integer.toHexString(ID) + ": " + getDescription() + " Line " + |
380 | lineNumber + " from " +charBegin + " to " + charEnd + " : " + contents); |
|
381 | } |
|
382 | ||
383 | } |
|
384 |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |