Debian Codegraph @

src/main/org/h2/command/Tokenizer.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.command;
7
8 import static org.h2.command.Token.ASTERISK;
9 import static org.h2.command.Token.AT;
10 import static org.h2.command.Token.BIGGER;
11 import static org.h2.command.Token.BIGGER_EQUAL;
12 import static org.h2.command.Token.CLOSE_BRACE;
13 import static org.h2.command.Token.CLOSE_BRACKET;
14 import static org.h2.command.Token.CLOSE_PAREN;
15 import static org.h2.command.Token.COLON;
16 import static org.h2.command.Token.COLON_COLON;
17 import static org.h2.command.Token.COLON_EQ;
18 import static org.h2.command.Token.COMMA;
19 import static org.h2.command.Token.CONCATENATION;
20 import static org.h2.command.Token.DOT;
21 import static org.h2.command.Token.EQUAL;
22 import static org.h2.command.Token.MINUS_SIGN;
23 import static org.h2.command.Token.NOT_EQUAL;
24 import static org.h2.command.Token.NOT_TILDE;
25 import static org.h2.command.Token.OPEN_BRACE;
26 import static org.h2.command.Token.OPEN_BRACKET;
27 import static org.h2.command.Token.OPEN_PAREN;
28 import static org.h2.command.Token.PERCENT;
29 import static org.h2.command.Token.PLUS_SIGN;
30 import static org.h2.command.Token.SEMICOLON;
31 import static org.h2.command.Token.SLASH;
32 import static org.h2.command.Token.SMALLER;
33 import static org.h2.command.Token.SMALLER_EQUAL;
34 import static org.h2.command.Token.SPATIAL_INTERSECTS;
35 import static org.h2.command.Token.TILDE;
36 import static org.h2.util.ParserUtil.ALL;
37 import static org.h2.util.ParserUtil.AND;
38 import static org.h2.util.ParserUtil.ANY;
39 import static org.h2.util.ParserUtil.ARRAY;
40 import static org.h2.util.ParserUtil.AS;
41 import static org.h2.util.ParserUtil.ASYMMETRIC;
42 import static org.h2.util.ParserUtil.AUTHORIZATION;
43 import static org.h2.util.ParserUtil.BETWEEN;
44 import static org.h2.util.ParserUtil.CASE;
45 import static org.h2.util.ParserUtil.CAST;
46 import static org.h2.util.ParserUtil.CHECK;
47 import static org.h2.util.ParserUtil.CONSTRAINT;
48 import static org.h2.util.ParserUtil.CROSS;
49 import static org.h2.util.ParserUtil.CURRENT_CATALOG;
50 import static org.h2.util.ParserUtil.CURRENT_DATE;
51 import static org.h2.util.ParserUtil.CURRENT_PATH;
52 import static org.h2.util.ParserUtil.CURRENT_ROLE;
53 import static org.h2.util.ParserUtil.CURRENT_SCHEMA;
54 import static org.h2.util.ParserUtil.CURRENT_TIME;
55 import static org.h2.util.ParserUtil.CURRENT_TIMESTAMP;
56 import static org.h2.util.ParserUtil.CURRENT_USER;
57 import static org.h2.util.ParserUtil.DAY;
58 import static org.h2.util.ParserUtil.DEFAULT;
59 import static org.h2.util.ParserUtil.DISTINCT;
60 import static org.h2.util.ParserUtil.ELSE;
61 import static org.h2.util.ParserUtil.END;
62 import static org.h2.util.ParserUtil.EXCEPT;
63 import static org.h2.util.ParserUtil.EXISTS;
64 import static org.h2.util.ParserUtil.FALSE;
65 import static org.h2.util.ParserUtil.FETCH;
66 import static org.h2.util.ParserUtil.FOR;
67 import static org.h2.util.ParserUtil.FOREIGN;
68 import static org.h2.util.ParserUtil.FROM;
69 import static org.h2.util.ParserUtil.FULL;
70 import static org.h2.util.ParserUtil.GROUP;
71 import static org.h2.util.ParserUtil.HAVING;
72 import static org.h2.util.ParserUtil.HOUR;
73 import static org.h2.util.ParserUtil.IDENTIFIER;
74 import static org.h2.util.ParserUtil.IF;
75 import static org.h2.util.ParserUtil.IN;
76 import static org.h2.util.ParserUtil.INNER;
77 import static org.h2.util.ParserUtil.INTERSECT;
78 import static org.h2.util.ParserUtil.INTERVAL;
79 import static org.h2.util.ParserUtil.IS;
80 import static org.h2.util.ParserUtil.JOIN;
81 import static org.h2.util.ParserUtil.KEY;
82 import static org.h2.util.ParserUtil.LEFT;
83 import static org.h2.util.ParserUtil.LIKE;
84 import static org.h2.util.ParserUtil.LIMIT;
85 import static org.h2.util.ParserUtil.LOCALTIME;
86 import static org.h2.util.ParserUtil.LOCALTIMESTAMP;
87 import static org.h2.util.ParserUtil.MINUS;
88 import static org.h2.util.ParserUtil.MINUTE;
89 import static org.h2.util.ParserUtil.MONTH;
90 import static org.h2.util.ParserUtil.NATURAL;
91 import static org.h2.util.ParserUtil.NOT;
92 import static org.h2.util.ParserUtil.NULL;
93 import static org.h2.util.ParserUtil.OFFSET;
94 import static org.h2.util.ParserUtil.ON;
95 import static org.h2.util.ParserUtil.OR;
96 import static org.h2.util.ParserUtil.ORDER;
97 import static org.h2.util.ParserUtil.PRIMARY;
98 import static org.h2.util.ParserUtil.QUALIFY;
99 import static org.h2.util.ParserUtil.RIGHT;
100 import static org.h2.util.ParserUtil.ROW;
101 import static org.h2.util.ParserUtil.ROWNUM;
102 import static org.h2.util.ParserUtil.SECOND;
103 import static org.h2.util.ParserUtil.SELECT;
104 import static org.h2.util.ParserUtil.SESSION_USER;
105 import static org.h2.util.ParserUtil.SET;
106 import static org.h2.util.ParserUtil.SOME;
107 import static org.h2.util.ParserUtil.SYMMETRIC;
108 import static org.h2.util.ParserUtil.SYSTEM_USER;
109 import static org.h2.util.ParserUtil.TABLE;
110 import static org.h2.util.ParserUtil.TO;
111 import static org.h2.util.ParserUtil.TRUE;
112 import static org.h2.util.ParserUtil.UESCAPE;
113 import static org.h2.util.ParserUtil.UNION;
114 import static org.h2.util.ParserUtil.UNIQUE;
115 import static org.h2.util.ParserUtil.UNKNOWN;
116 import static org.h2.util.ParserUtil.USER;
117 import static org.h2.util.ParserUtil.USING;
118 import static org.h2.util.ParserUtil.VALUE;
119 import static org.h2.util.ParserUtil.VALUES;
120 import static org.h2.util.ParserUtil.WHEN;
121 import static org.h2.util.ParserUtil.WHERE;
122 import static org.h2.util.ParserUtil.WINDOW;
123 import static org.h2.util.ParserUtil.WITH;
124 import static org.h2.util.ParserUtil.YEAR;
125 import static org.h2.util.ParserUtil._ROWID_;
126
127 import java.io.ByteArrayOutputStream;
128 import java.math.BigDecimal;
129 import java.math.BigInteger;
130 import java.util.ArrayList;
131 import java.util.BitSet;
132 import java.util.ListIterator;
133
134 import org.h2.api.ErrorCode;
135 import org.h2.engine.CastDataProvider;
136 import org.h2.message.DbException;
137 import org.h2.util.StringUtils;
138 import org.h2.value.ValueBigint;
139 import org.h2.value.ValueDecfloat;
140 import org.h2.value.ValueNumeric;
141
142 /**
143 * Tokenizer.
144 */
145 public final class Tokenizer {
146
147 private final CastDataProvider provider;
148
149 private final boolean identifiersToUpper;
150
151 private final boolean identifiersToLower;
152
153 private final BitSet nonKeywords;
154
155 Tokenizer(CastDataProvider provider, boolean identifiersToUpper, boolean identifiersToLower, BitSet nonKeywords) {
156 this.provider = provider;
157 this.identifiersToUpper = identifiersToUpper;
158 this.identifiersToLower = identifiersToLower;
159 this.nonKeywords = nonKeywords;
160 }
161
162 ArrayList<Token> tokenize(String sql, boolean stopOnCloseParen, BitSet parameters) {
163 ArrayList<Token> tokens = new ArrayList<>();
164 int end = sql.length() - 1;
165 boolean foundUnicode = false;
166 int lastParameter = 0;
167 loop: for (int i = 0; i <= end;) {
168 char c = sql.charAt(i);
169 Token token;
170 switch (c) {
171 case '!':
172 if (i < end) {
173 char c2 = sql.charAt(i + 1);
174 if (c2 == '=') {
175 token = new Token.KeywordToken(i++, NOT_EQUAL);
176 break;
177 }
178 if (c2 == '~') {
179 token = new Token.KeywordToken(i++, NOT_TILDE);
180 break;
181 }
182 }
183 throw DbException.getSyntaxError(sql, i);
184 case '"':
185 case '`':
186 i = readQuotedIdentifier(sql, end, i, i, c, false, tokens);
187 continue loop;
188 case '#':
189 if (provider.getMode().supportPoundSymbolForColumnNames) {
190 i = readIdentifier(sql, end, i, i, tokens);
191 continue loop;
192 }
193 throw DbException.getSyntaxError(sql, i);
194 case '$':
195 if (i < end) {
196 char c2 = sql.charAt(i + 1);
197 if (c2 == '$') {
198 int stringStart = i + 2;
199 int stringEnd = sql.indexOf("$$", stringStart);
200 if (stringEnd < 0) {
201 throw DbException.getSyntaxError(sql, i);
202 }
203 token = new Token.CharacterStringToken(i, sql.substring(stringStart, stringEnd), false);
204 i = stringEnd + 1;
205 } else {
206 i = parseParameterIndex(sql, end, i, tokens);
207 lastParameter = assignParameterIndex(tokens, lastParameter, parameters);
208 continue loop;
209 }
210 } else {
211 token = new Token.ParameterToken(i, 0);
212 }
213 break;
214 case '%':
215 token = new Token.KeywordToken(i, PERCENT);
216 break;
217 case '&':
218 if (i < end && sql.charAt(i + 1) == '&') {
219 token = new Token.KeywordToken(i++, SPATIAL_INTERSECTS);
220 break;
221 }
222 throw DbException.getSyntaxError(sql, i);
223 case '\'':
224 i = readCharacterString(sql, i, end, i, false, tokens);
225 continue loop;
226 case '(':
227 token = new Token.KeywordToken(i, OPEN_PAREN);
228 break;
229 case ')':
230 token = new Token.KeywordToken(i, CLOSE_PAREN);
231 if (stopOnCloseParen) {
232 tokens.add(token);
233 end = skipWhitespace(sql, end, i + 1) - 1;
234 break loop;
235 }
236 break;
237 case '*':
238 token = new Token.KeywordToken(i, ASTERISK);
239 break;
240 case '+':
241 token = new Token.KeywordToken(i, PLUS_SIGN);
242 break;
243 case ',':
244 token = new Token.KeywordToken(i, COMMA);
245 break;
246 case '-':
247 if (i < end && sql.charAt(i + 1) == '-') {
248 i = skipSimpleComment(sql, end, i);
249 continue loop;
250 } else {
251 token = new Token.KeywordToken(i, MINUS_SIGN);
252 }
253 break;
254 case '.':
255 if (i < end) {
256 char c2 = sql.charAt(i + 1);
257 if (c2 >= '0' && c2 <= '9') {
258 i = readFloat(sql, i, end, i + 1, false, tokens);
259 continue loop;
260 }
261 }
262 token = new Token.KeywordToken(i, DOT);
263 break;
264 case '/':
265 if (i < end) {
266 char c2 = sql.charAt(i + 1);
267 if (c2 == '*') {
268 i = skipBracketedComment(sql, end, i);
269 continue loop;
270 } else if (c2 == '/') {
271 i = skipSimpleComment(sql, end, i);
272 continue loop;
273 }
274 }
275 token = new Token.KeywordToken(i, SLASH);
276 break;
277 case '0':
278 if (i < end) {
279 switch (sql.charAt(i + 1) & 0xffdf) {
280 case 'B':
281 i = readIntegerNumber(sql, i, end, i + 2, tokens, "Binary number", 2);
282 continue loop;
283 case 'O':
284 i = readIntegerNumber(sql, i, end, i + 2, tokens, "Octal number", 8);
285 continue loop;
286 case 'X':
287 if (provider.getMode().zeroExLiteralsAreBinaryStrings) {
288 i = read0xBinaryString(sql, end, i + 2, tokens);
289 } else {
290 i = readIntegerNumber(sql, i, end, i + 2, tokens, "Hex number", 16);
291 }
292 continue loop;
293 }
294 }
295 //$FALL-THROUGH$
296 case '1':
297 case '2':
298 case '3':
299 case '4':
300 case '5':
301 case '6':
302 case '7':
303 case '8':
304 case '9':
305 i = readNumeric(sql, i, end, i + 1, c, tokens);
306 continue loop;
307 case ':':
308 if (i < end) {
309 char c2 = sql.charAt(i + 1);
310 if (c2 == ':') {
311 token = new Token.KeywordToken(i++, COLON_COLON);
312 break;
313 } else if (c2 == '=') {
314 token = new Token.KeywordToken(i++, COLON_EQ);
315 break;
316 }
317 }
318 token = new Token.KeywordToken(i, COLON);
319 break;
320 case ';':
321 token = new Token.KeywordToken(i, SEMICOLON);
322 break;
323 case '<':
324 if (i < end) {
325 char c2 = sql.charAt(i + 1);
326 if (c2 == '=') {
327 token = new Token.KeywordToken(i++, SMALLER_EQUAL);
328 break;
329 }
330 if (c2 == '>') {
331 token = new Token.KeywordToken(i++, NOT_EQUAL);
332 break;
333 }
334 }
335 token = new Token.KeywordToken(i, SMALLER);
336 break;
337 case '=':
338 token = new Token.KeywordToken(i, EQUAL);
339 break;
340 case '>':
341 if (i < end && sql.charAt(i + 1) == '=') {
342 token = new Token.KeywordToken(i++, BIGGER_EQUAL);
343 break;
344 }
345 token = new Token.KeywordToken(i, BIGGER);
346 break;
347 case '?': {
348 if (i + 1 < end && sql.charAt(i + 1) == '?') {
349 char c3 = sql.charAt(i + 2);
350 if (c3 == '(') {
351 token = new Token.KeywordToken(i, OPEN_BRACKET);
352 i += 2;
353 break;
354 }
355 if (c3 == ')') {
356 token = new Token.KeywordToken(i, CLOSE_BRACKET);
357 i += 2;
358 break;
359 }
360 }
361 i = parseParameterIndex(sql, end, i, tokens);
362 lastParameter = assignParameterIndex(tokens, lastParameter, parameters);
363 continue loop;
364 }
365 case '@':
366 token = new Token.KeywordToken(i, AT);
367 break;
368 case 'A':
369 case 'a':
370 i = readA(sql, end, i, tokens);
371 continue loop;
372 case 'B':
373 case 'b':
374 i = readB(sql, end, i, tokens);
375 continue loop;
376 case 'C':
377 case 'c':
378 i = readC(sql, end, i, tokens);
379 continue loop;
380 case 'D':
381 case 'd':
382 i = readD(sql, end, i, tokens);
383 continue loop;
384 case 'E':
385 case 'e':
386 i = readE(sql, end, i, tokens);
387 continue loop;
388 case 'F':
389 case 'f':
390 i = readF(sql, end, i, tokens);
391 continue loop;
392 case 'G':
393 case 'g':
394 i = readG(sql, end, i, tokens);
395 continue loop;
396 case 'H':
397 case 'h':
398 i = readH(sql, end, i, tokens);
399 continue loop;
400 case 'I':
401 case 'i':
402 i = readI(sql, end, i, tokens);
403 continue loop;
404 case 'J':
405 case 'j':
406 i = readJ(sql, end, i, tokens);
407 continue loop;
408 case 'K':
409 case 'k':
410 i = readK(sql, end, i, tokens);
411 continue loop;
412 case 'L':
413 case 'l':
414 i = readL(sql, end, i, tokens);
415 continue loop;
416 case 'M':
417 case 'm':
418 i = readM(sql, end, i, tokens);
419 continue loop;
420 case 'N':
421 case 'n':
422 if (i < end && sql.charAt(i + 1) == '\'') {
423 i = readCharacterString(sql, i, end, i + 1, false, tokens);
424 } else {
425 i = readN(sql, end, i, tokens);
426 }
427 continue loop;
428 case 'O':
429 case 'o':
430 i = readO(sql, end, i, tokens);
431 continue loop;
432 case 'P':
433 case 'p':
434 i = readP(sql, end, i, tokens);
435 continue loop;
436 case 'Q':
437 case 'q':
438 i = readQ(sql, end, i, tokens);
439 continue loop;
440 case 'R':
441 case 'r':
442 i = readR(sql, end, i, tokens);
443 continue loop;
444 case 'S':
445 case 's':
446 i = readS(sql, end, i, tokens);
447 continue loop;
448 case 'T':
449 case 't':
450 i = readT(sql, end, i, tokens);
451 continue loop;
452 case 'U':
453 case 'u':
454 if (i + 1 < end && sql.charAt(i + 1) == '&') {
455 char c3 = sql.charAt(i + 2);
456 if (c3 == '"') {
457 i = readQuotedIdentifier(sql, end, i, i + 2, '"', true, tokens);
458 foundUnicode = true;
459 continue loop;
460 } else if (c3 == '\'') {
461 i = readCharacterString(sql, i, end, i + 2, true, tokens);
462 foundUnicode = true;
463 continue loop;
464 }
465 }
466 i = readU(sql, end, i, tokens);
467 continue loop;
468 case 'V':
469 case 'v':
470 i = readV(sql, end, i, tokens);
471 continue loop;
472 case 'W':
473 case 'w':
474 i = readW(sql, end, i, tokens);
475 continue loop;
476 case 'X':
477 case 'x':
478 if (i < end && sql.charAt(i + 1) == '\'') {
479 i = readBinaryString(sql, i, end, i + 1, tokens);
480 } else {
481 i = readIdentifier(sql, end, i, i, tokens);
482 }
483 continue loop;
484 case 'Y':
485 case 'y':
486 i = readY(sql, end, i, tokens);
487 continue loop;
488 case 'Z':
489 case 'z':
490 i = readIdentifier(sql, end, i, i, tokens);
491 continue loop;
492 case '[':
493 if (provider.getMode().squareBracketQuotedNames) {
494 int identifierStart = i + 1;
495 int identifierEnd = sql.indexOf(']', identifierStart);
496 if (identifierEnd < 0) {
497 throw DbException.getSyntaxError(sql, i);
498 }
499 token = new Token.IdentifierToken(i, sql.substring(identifierStart, identifierEnd), true, false);
500 i = identifierEnd;
501 } else {
502 token = new Token.KeywordToken(i, OPEN_BRACKET);
503 }
504 break;
505 case ']':
506 token = new Token.KeywordToken(i, CLOSE_BRACKET);
507 break;
508 case '_':
509 i = read_(sql, end, i, tokens);
510 continue loop;
511 case '{':
512 token = new Token.KeywordToken(i, OPEN_BRACE);
513 break;
514 case '|':
515 if (i < end && sql.charAt(i + 1) == '|') {
516 token = new Token.KeywordToken(i++, CONCATENATION);
517 break;
518 }
519 throw DbException.getSyntaxError(sql, i);
520 case '}':
521 token = new Token.KeywordToken(i, CLOSE_BRACE);
522 break;
523 case '~':
524 token = new Token.KeywordToken(i, TILDE);
525 break;
526 default:
527 if (c <= ' ') {
528 i++;
529 continue loop;
530 } else {
531 int tokenStart = i;
532 int cp = Character.isHighSurrogate(c) ? sql.codePointAt(i++) : c;
533 if (Character.isSpaceChar(cp)) {
534 i++;
535 continue loop;
536 }
537 if (Character.isJavaIdentifierStart(cp)) {
538 i = readIdentifier(sql, end, tokenStart, i, tokens);
539 continue loop;
540 }
541 throw DbException.getSyntaxError(sql, tokenStart);
542 }
543 }
544 tokens.add(token);
545 i++;
546 }
547 if (foundUnicode) {
548 processUescape(sql, tokens);
549 }
550 tokens.add(new Token.EndOfInputToken(end + 1));
551 return tokens;
552 }
553
554 private int readIdentifier(String sql, int end, int tokenStart, int i, ArrayList<Token> tokens) {
555 int endIndex = findIdentifierEnd(sql, end, i);
556 tokens.add(new Token.IdentifierToken(tokenStart, extractIdentifier(sql, tokenStart, endIndex), false, false));
557 return endIndex;
558 }
559
560 private int readA(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
561 int endIndex = findIdentifierEnd(sql, end, tokenStart);
562 int length = endIndex - tokenStart;
563 int type;
564 if (length == 2) {
565 type = (sql.charAt(tokenStart + 1) & 0xffdf) == 'S' ? AS : IDENTIFIER;
566 } else {
567 if (eq("ALL", sql, tokenStart, length)) {
568 type = ALL;
569 } else if (eq("AND", sql, tokenStart, length)) {
570 type = AND;
571 } else if (eq("ANY", sql, tokenStart, length)) {
572 type = ANY;
573 } else if (eq("ARRAY", sql, tokenStart, length)) {
574 type = ARRAY;
575 } else if (eq("ASYMMETRIC", sql, tokenStart, length)) {
576 type = ASYMMETRIC;
577 } else if (eq("AUTHORIZATION", sql, tokenStart, length)) {
578 type = AUTHORIZATION;
579 } else {
580 type = IDENTIFIER;
581 }
582 }
583 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
584 }
585
586 private int readB(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
587 int endIndex = findIdentifierEnd(sql, end, tokenStart);
588 int length = endIndex - tokenStart;
589 int type = eq("BETWEEN", sql, tokenStart, length) ? BETWEEN : IDENTIFIER;
590 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
591 }
592
593 private int readC(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
594 int endIndex = findIdentifierEnd(sql, end, tokenStart);
595 int length = endIndex - tokenStart;
596 int type;
597 if (eq("CASE", sql, tokenStart, length)) {
598 type = CASE;
599 } else if (eq("CAST", sql, tokenStart, length)) {
600 type = CAST;
601 } else if (eq("CHECK", sql, tokenStart, length)) {
602 type = CHECK;
603 } else if (eq("CONSTRAINT", sql, tokenStart, length)) {
604 type = CONSTRAINT;
605 } else if (eq("CROSS", sql, tokenStart, length)) {
606 type = CROSS;
607 } else if (length >= 12 && eq("CURRENT_", sql, tokenStart, 8)) {
608 type = getTokenTypeCurrent(sql, tokenStart, length);
609 } else {
610 type = IDENTIFIER;
611 }
612 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
613 }
614
615 private static int getTokenTypeCurrent(String s, int tokenStart, int length) {
616 tokenStart += 8;
617 switch (length) {
618 case 12:
619 if (eqCurrent("CURRENT_DATE", s, tokenStart, length)) {
620 return CURRENT_DATE;
621 } else if (eqCurrent("CURRENT_PATH", s, tokenStart, length)) {
622 return CURRENT_PATH;
623 } else if (eqCurrent("CURRENT_ROLE", s, tokenStart, length)) {
624 return CURRENT_ROLE;
625 } else if (eqCurrent("CURRENT_TIME", s, tokenStart, length)) {
626 return CURRENT_TIME;
627 } else if (eqCurrent("CURRENT_USER", s, tokenStart, length)) {
628 return CURRENT_USER;
629 }
630 break;
631 case 14:
632 if (eqCurrent("CURRENT_SCHEMA", s, tokenStart, length)) {
633 return CURRENT_SCHEMA;
634 }
635 break;
636 case 15:
637 if (eqCurrent("CURRENT_CATALOG", s, tokenStart, length)) {
638 return CURRENT_CATALOG;
639 }
640 break;
641 case 17:
642 if (eqCurrent("CURRENT_TIMESTAMP", s, tokenStart, length)) {
643 return CURRENT_TIMESTAMP;
644 }
645 }
646 return IDENTIFIER;
647 }
648
649 private static boolean eqCurrent(String expected, String s, int start, int length) {
650 for (int i = 8; i < length; i++) {
651 if (expected.charAt(i) != (s.charAt(start++) & 0xffdf)) {
652 return false;
653 }
654 }
655 return true;
656 }
657
658 private int readD(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
659 int endIndex = findIdentifierEnd(sql, end, tokenStart);
660 int length = endIndex - tokenStart;
661 int type;
662 if (eq("DAY", sql, tokenStart, length)) {
663 type = DAY;
664 } else if (eq("DEFAULT", sql, tokenStart, length)) {
665 type = DEFAULT;
666 } else if (eq("DISTINCT", sql, tokenStart, length)) {
667 type = DISTINCT;
668 } else {
669 type = IDENTIFIER;
670 }
671 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
672 }
673
674 private int readE(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
675 int endIndex = findIdentifierEnd(sql, end, tokenStart);
676 int length = endIndex - tokenStart;
677 int type;
678 if (eq("ELSE", sql, tokenStart, length)) {
679 type = ELSE;
680 } else if (eq("END", sql, tokenStart, length)) {
681 type = END;
682 } else if (eq("EXCEPT", sql, tokenStart, length)) {
683 type = EXCEPT;
684 } else if (eq("EXISTS", sql, tokenStart, length)) {
685 type = EXISTS;
686 } else {
687 type = IDENTIFIER;
688 }
689 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
690 }
691
692 private int readF(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
693 int endIndex = findIdentifierEnd(sql, end, tokenStart);
694 int length = endIndex - tokenStart;
695 int type;
696 if (eq("FETCH", sql, tokenStart, length)) {
697 type = FETCH;
698 } else if (eq("FROM", sql, tokenStart, length)) {
699 type = FROM;
700 } else if (eq("FOR", sql, tokenStart, length)) {
701 type = FOR;
702 } else if (eq("FOREIGN", sql, tokenStart, length)) {
703 type = FOREIGN;
704 } else if (eq("FULL", sql, tokenStart, length)) {
705 type = FULL;
706 } else if (eq("FALSE", sql, tokenStart, length)) {
707 type = FALSE;
708 } else {
709 type = IDENTIFIER;
710 }
711 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
712 }
713
714 private int readG(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
715 int endIndex = findIdentifierEnd(sql, end, tokenStart);
716 int length = endIndex - tokenStart;
717 int type = eq("GROUP", sql, tokenStart, length) ? GROUP : IDENTIFIER;
718 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
719 }
720
721 private int readH(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
722 int endIndex = findIdentifierEnd(sql, end, tokenStart);
723 int length = endIndex - tokenStart;
724 int type;
725 if (eq("HAVING", sql, tokenStart, length)) {
726 type = HAVING;
727 } else if (eq("HOUR", sql, tokenStart, length)) {
728 type = HOUR;
729 } else {
730 type = IDENTIFIER;
731 }
732 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
733 }
734
735 private int readI(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
736 int endIndex = findIdentifierEnd(sql, end, tokenStart);
737 int length = endIndex - tokenStart;
738 int type;
739 if (length == 2) {
740 switch ((sql.charAt(tokenStart + 1) & 0xffdf)) {
741 case 'F':
742 type = IF;
743 break;
744 case 'N':
745 type = IN;
746 break;
747 case 'S':
748 type = IS;
749 break;
750 default:
751 type = IDENTIFIER;
752 }
753 } else {
754 if (eq("INNER", sql, tokenStart, length)) {
755 type = INNER;
756 } else if (eq("INTERSECT", sql, tokenStart, length)) {
757 type = INTERSECT;
758 } else if (eq("INTERVAL", sql, tokenStart, length)) {
759 type = INTERVAL;
760 } else {
761 type = IDENTIFIER;
762 }
763 }
764 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
765 }
766
767 private int readJ(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
768 int endIndex = findIdentifierEnd(sql, end, tokenStart);
769 int length = endIndex - tokenStart;
770 int type = eq("JOIN", sql, tokenStart, length) ? JOIN : IDENTIFIER;
771 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
772 }
773
774 private int readK(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
775 int endIndex = findIdentifierEnd(sql, end, tokenStart);
776 int length = endIndex - tokenStart;
777 int type = eq("KEY", sql, tokenStart, length) ? KEY : IDENTIFIER;
778 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
779 }
780
781 private int readL(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
782 int endIndex = findIdentifierEnd(sql, end, tokenStart);
783 int length = endIndex - tokenStart;
784 int type;
785 if (eq("LEFT", sql, tokenStart, length)) {
786 type = LEFT;
787 } else if (eq("LIMIT", sql, tokenStart, length)) {
788 type = provider.getMode().limit ? LIMIT : IDENTIFIER;
789 } else if (eq("LIKE", sql, tokenStart, length)) {
790 type = LIKE;
791 } else if (eq("LOCALTIME", sql, tokenStart, length)) {
792 type = LOCALTIME;
793 } else if (eq("LOCALTIMESTAMP", sql, tokenStart, length)) {
794 type = LOCALTIMESTAMP;
795 } else {
796 type = IDENTIFIER;
797 }
798 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
799 }
800
801 private int readM(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
802 int endIndex = findIdentifierEnd(sql, end, tokenStart);
803 int length = endIndex - tokenStart;
804 int type;
805 if (eq("MINUS", sql, tokenStart, length)) {
806 type = provider.getMode().minusIsExcept ? MINUS : IDENTIFIER;
807 } else if (eq("MINUTE", sql, tokenStart, length)) {
808 type = MINUTE;
809 } else if (eq("MONTH", sql, tokenStart, length)) {
810 type = MONTH;
811 } else {
812 type = IDENTIFIER;
813 }
814 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
815 }
816
817 private int readN(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
818 int endIndex = findIdentifierEnd(sql, end, tokenStart);
819 int length = endIndex - tokenStart;
820 int type;
821 if (eq("NOT", sql, tokenStart, length)) {
822 type = NOT;
823 } else if (eq("NATURAL", sql, tokenStart, length)) {
824 type = NATURAL;
825 } else if (eq("NULL", sql, tokenStart, length)) {
826 type = NULL;
827 } else {
828 type = IDENTIFIER;
829 }
830 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
831 }
832
833 private int readO(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
834 int endIndex = findIdentifierEnd(sql, end, tokenStart);
835 int length = endIndex - tokenStart;
836 int type;
837 if (length == 2) {
838 switch ((sql.charAt(tokenStart + 1) & 0xffdf)) {
839 case 'N':
840 type = ON;
841 break;
842 case 'R':
843 type = OR;
844 break;
845 default:
846 type = IDENTIFIER;
847 }
848 } else {
849 if (eq("OFFSET", sql, tokenStart, length)) {
850 type = OFFSET;
851 } else if (eq("ORDER", sql, tokenStart, length)) {
852 type = ORDER;
853 } else {
854 type = IDENTIFIER;
855 }
856 }
857 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
858 }
859
860 private int readP(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
861 int endIndex = findIdentifierEnd(sql, end, tokenStart);
862 int length = endIndex - tokenStart;
863 int type = eq("PRIMARY", sql, tokenStart, length) ? PRIMARY : IDENTIFIER;
864 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
865 }
866
867 private int readQ(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
868 int endIndex = findIdentifierEnd(sql, end, tokenStart);
869 int length = endIndex - tokenStart;
870 int type = eq("QUALIFY", sql, tokenStart, length) ? QUALIFY : IDENTIFIER;
871 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
872 }
873
874 private int readR(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
875 int endIndex = findIdentifierEnd(sql, end, tokenStart);
876 int length = endIndex - tokenStart;
877 int type;
878 if (eq("RIGHT", sql, tokenStart, length)) {
879 type = RIGHT;
880 } else if (eq("ROW", sql, tokenStart, length)) {
881 type = ROW;
882 } else if (eq("ROWNUM", sql, tokenStart, length)) {
883 type = ROWNUM;
884 } else {
885 type = IDENTIFIER;
886 }
887 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
888 }
889
890 private int readS(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
891 int endIndex = findIdentifierEnd(sql, end, tokenStart);
892 int length = endIndex - tokenStart;
893 int type;
894 if (eq("SECOND", sql, tokenStart, length)) {
895 type = SECOND;
896 } else if (eq("SELECT", sql, tokenStart, length)) {
897 type = SELECT;
898 } else if (eq("SESSION_USER", sql, tokenStart, length)) {
899 type = SESSION_USER;
900 } else if (eq("SET", sql, tokenStart, length)) {
901 type = SET;
902 } else if (eq("SOME", sql, tokenStart, length)) {
903 type = SOME;
904 } else if (eq("SYMMETRIC", sql, tokenStart, length)) {
905 type = SYMMETRIC;
906 } else if (eq("SYSTEM_USER", sql, tokenStart, length)) {
907 type = SYSTEM_USER;
908 } else {
909 type = IDENTIFIER;
910 }
911 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
912 }
913
914 private int readT(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
915 int endIndex = findIdentifierEnd(sql, end, tokenStart);
916 int length = endIndex - tokenStart;
917 int type;
918 if (length == 2) {
919 type = (sql.charAt(tokenStart + 1) & 0xffdf) == 'O' ? TO : IDENTIFIER;
920 } else {
921 if (eq("TABLE", sql, tokenStart, length)) {
922 type = TABLE;
923 } else if (eq("TRUE", sql, tokenStart, length)) {
924 type = TRUE;
925 } else {
926 type = IDENTIFIER;
927 }
928 }
929 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
930 }
931
932 private int readU(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
933 int endIndex = findIdentifierEnd(sql, end, tokenStart);
934 int length = endIndex - tokenStart;
935 int type;
936 if (eq("UESCAPE", sql, tokenStart, length)) {
937 type = UESCAPE;
938 } else if (eq("UNION", sql, tokenStart, length)) {
939 type = UNION;
940 } else if (eq("UNIQUE", sql, tokenStart, length)) {
941 type = UNIQUE;
942 } else if (eq("UNKNOWN", sql, tokenStart, length)) {
943 type = UNKNOWN;
944 } else if (eq("USER", sql, tokenStart, length)) {
945 type = USER;
946 } else if (eq("USING", sql, tokenStart, length)) {
947 type = USING;
948 } else {
949 type = IDENTIFIER;
950 }
951 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
952 }
953
954 private int readV(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
955 int endIndex = findIdentifierEnd(sql, end, tokenStart);
956 int length = endIndex - tokenStart;
957 int type;
958 if (eq("VALUE", sql, tokenStart, length)) {
959 type = VALUE;
960 } else if (eq("VALUES", sql, tokenStart, length)) {
961 type = VALUES;
962 } else {
963 type = IDENTIFIER;
964 }
965 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
966 }
967
968 private int readW(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
969 int endIndex = findIdentifierEnd(sql, end, tokenStart);
970 int length = endIndex - tokenStart;
971 int type;
972 if (eq("WHEN", sql, tokenStart, length)) {
973 type = WHEN;
974 } else if (eq("WHERE", sql, tokenStart, length)) {
975 type = WHERE;
976 } else if (eq("WINDOW", sql, tokenStart, length)) {
977 type = WINDOW;
978 } else if (eq("WITH", sql, tokenStart, length)) {
979 type = WITH;
980 } else {
981 type = IDENTIFIER;
982 }
983 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
984 }
985
986 private int readY(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
987 int endIndex = findIdentifierEnd(sql, end, tokenStart);
988 int length = endIndex - tokenStart;
989 int type = eq("YEAR", sql, tokenStart, length) ? YEAR : IDENTIFIER;
990 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
991 }
992
993 private int read_(String sql, int end, int tokenStart, ArrayList<Token> tokens) {
994 int endIndex = findIdentifierEnd(sql, end, tokenStart);
995 int type = endIndex - tokenStart == 7 && "_ROWID_".regionMatches(true, 1, sql, tokenStart + 1, 6) ? _ROWID_
996 : IDENTIFIER;
997 return readIdentifierOrKeyword(sql, tokenStart, tokens, endIndex, type);
998 }
999
1000 private int readIdentifierOrKeyword(String sql, int tokenStart, ArrayList<Token> tokens, int endIndex, int type) {
1001 Token token;
1002 if (type == IDENTIFIER) {
1003 token = new Token.IdentifierToken(tokenStart, extractIdentifier(sql, tokenStart, endIndex), false, false);
1004 } else if (nonKeywords != null && nonKeywords.get(type)) {
1005 token = new Token.KeywordOrIdentifierToken(tokenStart, type, extractIdentifier(sql, tokenStart, endIndex));
1006 } else {
1007 token = new Token.KeywordToken(tokenStart, type);
1008 }
1009 tokens.add(token);
1010 return endIndex;
1011 }
1012
1013 private static boolean eq(String expected, String s, int start, int length) {
1014 if (length != expected.length()) {
1015 return false;
1016 }
1017 for (int i = 1; i < length; i++) {
1018 if (expected.charAt(i) != (s.charAt(++start) & 0xffdf)) {
1019 return false;
1020 }
1021 }
1022 return true;
1023 }
1024
1025 private int findIdentifierEnd(String sql, int end, int i) {
1026 i++;
1027 for (;;) {
1028 int cp;
1029 if (i > end || (!Character.isJavaIdentifierPart(cp = sql.codePointAt(i))
1030 && (cp != '#' || !provider.getMode().supportPoundSymbolForColumnNames))) {
1031 break;
1032 }
1033 i += Character.charCount(cp);
1034 }
1035 return i;
1036 }
1037
1038 private String extractIdentifier(String sql, int beginIndex, int endIndex) {
1039 return convertCase(sql.substring(beginIndex, endIndex));
1040 }
1041
1042 private int readQuotedIdentifier(String sql, int end, int tokenStart, int i, char c, boolean unicode,
1043 ArrayList<Token> tokens) {
1044 int identifierEnd = sql.indexOf(c, ++i);
1045 if (identifierEnd < 0) {
1046 throw DbException.getSyntaxError(sql, tokenStart);
1047 }
1048 String s = sql.substring(i, identifierEnd);
1049 i = identifierEnd + 1;
1050 if (i <= end && sql.charAt(i) == c) {
1051 StringBuilder builder = new StringBuilder(s);
1052 do {
1053 identifierEnd = sql.indexOf(c, i + 1);
1054 if (identifierEnd < 0) {
1055 throw DbException.getSyntaxError(sql, tokenStart);
1056 }
1057 builder.append(sql, i, identifierEnd);
1058 i = identifierEnd + 1;
1059 } while (i <= end && sql.charAt(i) == c);
1060 s = builder.toString();
1061 }
1062 if (c == '`') {
1063 s = convertCase(s);
1064 }
1065 tokens.add(new Token.IdentifierToken(tokenStart, s, true, unicode));
1066 return i;
1067 }
1068
1069 private String convertCase(String s) {
1070 if (identifiersToUpper) {
1071 s = StringUtils.toUpperEnglish(s);
1072 } else if (identifiersToLower) {
1073 s = StringUtils.toLowerEnglish(s);
1074 }
1075 return s;
1076 }
1077
1078 private static int readBinaryString(String sql, int tokenStart, int end, int i, ArrayList<Token> tokens) {
1079 ByteArrayOutputStream result = new ByteArrayOutputStream();
1080 int stringEnd;
1081 do {
1082 stringEnd = sql.indexOf('\'', ++i);
1083 if (stringEnd < 0 || stringEnd < end && sql.charAt(stringEnd + 1) == '\'') {
1084 throw DbException.getSyntaxError(sql, tokenStart);
1085 }
1086 StringUtils.convertHexWithSpacesToBytes(result, sql, i, stringEnd);
1087 i = skipWhitespace(sql, end, stringEnd + 1);
1088 } while (i <= end && sql.charAt(i) == '\'');
1089 tokens.add(new Token.BinaryStringToken(tokenStart, result.toByteArray()));
1090 return i;
1091 }
1092
1093 private static int readCharacterString(String sql, int tokenStart, int end, int i, boolean unicode,
1094 ArrayList<Token> tokens) {
1095 String s = null;
1096 StringBuilder builder = null;
1097 int stringEnd;
1098 do {
1099 stringEnd = sql.indexOf('\'', ++i);
1100 if (stringEnd < 0) {
1101 throw DbException.getSyntaxError(sql, tokenStart);
1102 }
1103 if (s == null) {
1104 s = sql.substring(i, stringEnd);
1105 } else {
1106 if (builder == null) {
1107 builder = new StringBuilder(s);
1108 }
1109 builder.append(sql, i, stringEnd);
1110 }
1111 i = stringEnd + 1;
1112 if (i <= end && sql.charAt(i) == '\'') {
1113 if (builder == null) {
1114 builder = new StringBuilder(s);
1115 }
1116 do {
1117 stringEnd = sql.indexOf('\'', i + 1);
1118 if (stringEnd < 0) {
1119 throw DbException.getSyntaxError(sql, tokenStart);
1120 }
1121 builder.append(sql, i, stringEnd);
1122 i = stringEnd + 1;
1123 } while (i <= end && sql.charAt(i) == '\'');
1124 }
1125 i = skipWhitespace(sql, end, i);
1126 } while (i <= end && sql.charAt(i) == '\'');
1127 if (builder != null) {
1128 s = builder.toString();
1129 }
1130 tokens.add(new Token.CharacterStringToken(tokenStart, s, unicode));
1131 return i;
1132 }
1133
1134 private static int skipWhitespace(String sql, int end, int i) {
1135 while (i <= end) {
1136 int cp = sql.codePointAt(i);
1137 if (!Character.isWhitespace(cp)) {
1138 if (cp == '/' && i < end) {
1139 char c2 = sql.charAt(i + 1);
1140 if (c2 == '*') {
1141 i = skipBracketedComment(sql, end, i);
1142 continue;
1143 } else if (c2 == '/') {
1144 i = skipSimpleComment(sql, end, i);
1145 continue;
1146 }
1147 }
1148 break;
1149 }
1150 i += Character.charCount(cp);
1151 }
1152 return i;
1153 }
1154
1155 private static int read0xBinaryString(String sql, int end, int i, ArrayList<Token> tokens) {
1156 int start = i;
1157 for (char c; i <= end && (((c = sql.charAt(i)) >= '0' && c <= '9') || ((c &= 0xffdf) >= 'A' && c <= 'F'));) {
1158 i++;
1159 }
1160 if (i <= end && Character.isJavaIdentifierPart(sql.codePointAt(i))) {
1161 throw DbException.get(ErrorCode.HEX_STRING_WRONG_1, sql.substring(start, i + 1));
1162 }
1163 tokens.add(new Token.BinaryStringToken(start, StringUtils.convertHexToBytes(sql.substring(start, i))));
1164 return i;
1165 }
1166
1167 private static int readIntegerNumber(String sql, int tokenStart, int end, int i, ArrayList<Token> tokens,
1168 String name, int radix) {
1169 if (i > end) {
1170 throw DbException.getSyntaxError(sql, tokenStart, name);
1171 }
1172 int maxDigit, maxLetter;
1173 if (radix > 10) {
1174 maxDigit = '9';
1175 maxLetter = ('A' - 11) + radix;
1176 } else {
1177 maxDigit = ('0' - 1) + radix;
1178 maxLetter = -1;
1179 }
1180 int start = i;
1181 long number = 0;
1182 char c;
1183 int lastUnderscore = Integer.MIN_VALUE;
1184 do {
1185 c = sql.charAt(i);
1186 if (c >= '0' && c <= maxDigit) {
1187 number = (number * radix) + c - '0';
1188 } else if (c == '_') {
1189 if (lastUnderscore == i - 1) {
1190 throw DbException.getSyntaxError(sql, tokenStart, name);
1191 }
1192 lastUnderscore = i;
1193 continue;
1194 } else if (maxLetter >= 0 && (c &= 0xffdf) >= 'A' && c <= maxLetter) {
1195 number = (number * radix) + c - ('A' - 10);
1196 } else if (i == start) {
1197 throw DbException.getSyntaxError(sql, tokenStart, name);
1198 } else {
1199 break;
1200 }
1201 if (number > Integer.MAX_VALUE) {
1202 while (++i <= end) {
1203 if ((c = sql.charAt(i)) >= '0' && c <= maxDigit) {
1204 //
1205 } else if (c == '_') {
1206 if (lastUnderscore == i - 1) {
1207 throw DbException.getSyntaxError(sql, tokenStart, name);
1208 }
1209 lastUnderscore = i;
1210 continue;
1211 } else if (maxLetter >= 0 && (c &= 0xffdf) >= 'A' && c <= 'F') {
1212 //
1213 } else {
1214 break;
1215 }
1216 }
1217 if (lastUnderscore == i - 1) {
1218 throw DbException.getSyntaxError(sql, tokenStart, name);
1219 }
1220 return finishBigInteger(sql, tokenStart, end, i, start, i <= end && c == 'L', lastUnderscore >= 0,
1221 radix, tokens);
1222 }
1223 } while (++i <= end);
1224 if (lastUnderscore == i - 1) {
1225 throw DbException.getSyntaxError(sql, tokenStart, name);
1226 }
1227 boolean bigint = i <= end && c == 'L';
1228 if (bigint) {
1229 i++;
1230 }
1231 if (i <= end && Character.isJavaIdentifierPart(sql.codePointAt(i))) {
1232 throw DbException.getSyntaxError(sql, tokenStart, name);
1233 }
1234 tokens.add(bigint ? new Token.BigintToken(start, number) : new Token.IntegerToken(start, (int) number));
1235 return i;
1236 }
1237
1238 private static int readNumeric(String sql, int tokenStart, int end, int i, char c, ArrayList<Token> tokens) {
1239 long number = c - '0';
1240 int lastUnderscore = Integer.MIN_VALUE;
1241 for (; i <= end; i++) {
1242 c = sql.charAt(i);
1243 if (c < '0' || c > '9') {
1244 if (lastUnderscore == i - 1) {
1245 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1246 }
1247 switch (c) {
1248 case '.':
1249 return readFloat(sql, tokenStart, end, i, lastUnderscore >= 0, tokens);
1250 case 'E':
1251 case 'e':
1252 return readApproximateNumeric(sql, tokenStart, end, i, lastUnderscore >= 0, tokens);
1253 case 'L':
1254 case 'l':
1255 return finishBigInteger(sql, tokenStart, end, i, tokenStart, true, lastUnderscore >= 0, 10, //
1256 tokens);
1257 case '_':
1258 lastUnderscore = i;
1259 continue;
1260 }
1261 break;
1262 }
1263 number = number * 10 + (c - '0');
1264 if (number > Integer.MAX_VALUE) {
1265 while (++i <= end) {
1266 c = sql.charAt(i);
1267 if (c < '0' || c > '9') {
1268 if (lastUnderscore == i - 1) {
1269 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1270 }
1271 switch (c) {
1272 case '.':
1273 return readFloat(sql, tokenStart, end, i, lastUnderscore >= 0, tokens);
1274 case 'E':
1275 case 'e':
1276 return readApproximateNumeric(sql, tokenStart, end, i, lastUnderscore >= 0, tokens);
1277 case '_':
1278 lastUnderscore = i;
1279 continue;
1280 }
1281 break;
1282 }
1283 }
1284 if (lastUnderscore == i - 1) {
1285 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1286 }
1287 return finishBigInteger(sql, tokenStart, end, i, tokenStart, c == 'L' || c == 'l', lastUnderscore >= 0,
1288 10, tokens);
1289 }
1290 }
1291 if (lastUnderscore == i - 1) {
1292 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1293 }
1294 tokens.add(new Token.IntegerToken(tokenStart, (int) number));
1295 return i;
1296 }
1297
1298 private static int readFloat(String sql, int tokenStart, int end, int i, boolean withUnderscore,
1299 ArrayList<Token> tokens) {
1300 int start = i + 1;
1301 int lastUnderscore = Integer.MIN_VALUE;
1302 while (++i <= end) {
1303 char c = sql.charAt(i);
1304 if (c < '0' || c > '9') {
1305 if (lastUnderscore == i - 1) {
1306 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1307 }
1308 switch (c) {
1309 case 'E':
1310 case 'e':
1311 return readApproximateNumeric(sql, tokenStart, end, i, withUnderscore, tokens);
1312 case '_':
1313 if (i == start) {
1314 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1315 }
1316 lastUnderscore = i;
1317 withUnderscore = true;
1318 continue;
1319 }
1320 break;
1321 }
1322 }
1323 if (lastUnderscore == i - 1) {
1324 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1325 }
1326 tokens.add(new Token.ValueToken(tokenStart, //
1327 ValueNumeric.get(readBigDecimal(sql, tokenStart, i, withUnderscore))));
1328 return i;
1329 }
1330
1331 private static int readApproximateNumeric(String sql, int tokenStart, int end, int i, boolean withUnderscore,
1332 ArrayList<Token> tokens) {
1333 if (i == end) {
1334 throw DbException.getSyntaxError(sql, tokenStart, "Approximate numeric");
1335 }
1336 char c = sql.charAt(++i);
1337 if (c == '+' || c == '-') {
1338 if (i == end) {
1339 throw DbException.getSyntaxError(sql, tokenStart, "Approximate numeric");
1340 }
1341 c = sql.charAt(++i);
1342 }
1343 if (c < '0' || c > '9') {
1344 throw DbException.getSyntaxError(sql, tokenStart, "Approximate numeric");
1345 }
1346 int lastUnderscore = Integer.MIN_VALUE;
1347 while (++i <= end) {
1348 c = sql.charAt(i);
1349 if (c < '0' || c > '9') {
1350 if (lastUnderscore == i - 1) {
1351 throw DbException.getSyntaxError(sql, tokenStart, "Approximate numeric");
1352 }
1353 if (c == '_') {
1354 lastUnderscore = i;
1355 withUnderscore = true;
1356 continue;
1357 }
1358 break;
1359 }
1360 }
1361 if (lastUnderscore == i - 1) {
1362 throw DbException.getSyntaxError(sql, tokenStart, "Approximate numeric");
1363 }
1364 tokens.add(new Token.ValueToken(tokenStart,
1365 ValueDecfloat.get(readBigDecimal(sql, tokenStart, i, withUnderscore))));
1366 return i;
1367 }
1368
1369 private static BigDecimal readBigDecimal(String sql, int tokenStart, int i, boolean withUnderscore) {
1370 String string = readAndRemoveUnderscores(sql, tokenStart, i, withUnderscore);
1371 BigDecimal bd;
1372 try {
1373 bd = new BigDecimal(string);
1374 } catch (NumberFormatException e) {
1375 throw DbException.getSyntaxError(sql, tokenStart, "Numeric");
1376 }
1377 return bd;
1378 }
1379
1380 private static int finishBigInteger(String sql, int tokenStart, int end, int i, int start, boolean asBigint,
1381 boolean withUnderscore, int radix, ArrayList<Token> tokens) {
1382 int endIndex = i;
1383 if (asBigint) {
1384 i++;
1385 }
1386 if (radix == 16 && i <= end && Character.isJavaIdentifierPart(sql.codePointAt(i))) {
1387 throw DbException.getSyntaxError(sql, tokenStart, "Hex number");
1388 }
1389 BigInteger bigInteger = new BigInteger(readAndRemoveUnderscores(sql, start, endIndex, withUnderscore), radix);
1390 Token token;
1391 if (bigInteger.compareTo(ValueBigint.MAX_BI) > 0) {
1392 if (asBigint) {
1393 throw DbException.getSyntaxError(sql, tokenStart, "BIGINT");
1394 }
1395 token = new Token.ValueToken(tokenStart, ValueNumeric.get(bigInteger));
1396 } else {
1397 token = new Token.BigintToken(tokenStart, bigInteger.longValue());
1398 }
1399 tokens.add(token);
1400 return i;
1401 }
1402
1403 private static String readAndRemoveUnderscores(String sql, int start, int endIndex, boolean withUnderscore) {
1404 if (!withUnderscore) {
1405 return sql.substring(start, endIndex);
1406 }
1407 StringBuilder builder = new StringBuilder(endIndex - start - 1);
1408 for (; start < endIndex; start++) {
1409 char c = sql.charAt(start);
1410 if (c != '_') {
1411 builder.append(c);
1412 }
1413 }
1414 return builder.toString();
1415 }
1416
1417 private static int skipBracketedComment(String sql, int end, int i) {
1418 int tokenStart = i;
1419 i += 2;
1420 for (int level = 1; level > 0;) {
1421 for (;;) {
1422 if (i >= end) {
1423 throw DbException.getSyntaxError(sql, tokenStart);
1424 }
1425 char c = sql.charAt(i++);
1426 if (c == '*') {
1427 if (sql.charAt(i) == '/') {
1428 level--;
1429 i++;
1430 break;
1431 }
1432 } else if (c == '/' && sql.charAt(i) == '*') {
1433 level++;
1434 i++;
1435 }
1436 }
1437 }
1438 return i;
1439 }
1440
1441 private static int skipSimpleComment(String sql, int end, int i) {
1442 i += 2;
1443 for (char c; i <= end && (c = sql.charAt(i)) != '\n' && c != '\r'; i++) {
1444 //
1445 }
1446 return i;
1447 }
1448
1449 private static int parseParameterIndex(String sql, int end, int i, ArrayList<Token> tokens) {
1450 int tokenStart = i;
1451 long number = 0;
1452 for (char c; ++i <= end && (c = sql.charAt(i)) >= '0' && c <= '9';) {
1453 number = number * 10 + (c - '0');
1454 if (number > Integer.MAX_VALUE) {
1455 throw DbException.getInvalidValueException("parameter index", number);
1456 }
1457 }
1458 if (i > tokenStart + 1 && number == 0) {
1459 throw DbException.getInvalidValueException("parameter index", number);
1460 }
1461 tokens.add(new Token.ParameterToken(tokenStart, (int) number));
1462 return i;
1463 }
1464
1465 private static int assignParameterIndex(ArrayList<Token> tokens, int lastParameter, BitSet parameters) {
1466 Token.ParameterToken parameter = (Token.ParameterToken) tokens.get(tokens.size() - 1);
1467 int index = parameter.index;
1468 if (index == 0) {
1469 if (lastParameter < 0) {
1470 throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
1471 }
1472 parameter.index = index = ++lastParameter;
1473 } else if (lastParameter > 0) {
1474 throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
1475 } else {
1476 lastParameter = -1;
1477 }
1478 parameters.set(index - 1);
1479 return lastParameter;
1480 }
1481
1482 private static void processUescape(String sql, ArrayList<Token> tokens) {
1483 ListIterator<Token> i = tokens.listIterator();
1484 while (i.hasNext()) {
1485 Token token = i.next();
1486 if (token.needsUnicodeConversion()) {
1487 int uescape = '\\';
1488 condition: if (i.hasNext()) {
1489 Token t2 = i.next();
1490 if (t2.tokenType() == UESCAPE) {
1491 i.remove();
1492 if (i.hasNext()) {
1493 Token t3 = i.next();
1494 i.remove();
1495 if (t3 instanceof Token.CharacterStringToken) {
1496 String s = ((Token.CharacterStringToken) t3).string;
1497 if (s.codePointCount(0, s.length()) == 1) {
1498 int escape = s.codePointAt(0);
1499 if (!Character.isWhitespace(escape) && (escape < '0' || escape > '9')
1500 && (escape < 'A' || escape > 'F') && (escape < 'a' || escape > 'f')) {
1501 switch (escape) {
1502 default:
1503 uescape = escape;
1504 break condition;
1505 case '"':
1506 case '\'':
1507 case '+':
1508 }
1509 }
1510 }
1511 }
1512 }
1513 throw DbException.getSyntaxError(sql, t2.start() + 7, "'<Unicode escape character>'");
1514 }
1515 }
1516 token.convertUnicode(uescape);
1517 }
1518 }
1519 }
1520
1521 }