Repository navigation
Expand file tree
/
Copy pathSimpleCharStream.java
More file actions
650 lines (575 loc) · 21.5 KB
/
Copy pathSimpleCharStream.java
File metadata and controls
650 lines (575 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2025 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
/* Generated By:JavaCC: Do not edit this line by hand. SimpleCharStream.java Version 8.0.0 */
/*
* Warning: this file is maintained by hand on top of the JavaCC template. Among other manual
* adjustments it carries the in-buffer '#' and blank line rewrites in BeginToken(), which
* Feature.allowHashLineComments and Feature.allowBlankLinesAsStatementSeparator depend on. Never
* overwrite it with a freshly generated SimpleCharStream, that would silently drop the rewrites and
* break both features.
*/
package net.sf.jsqlparser.parser;
import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.parser.feature.FeatureConfiguration;
/**
* An implementation of interface CharStream, where the stream is assumed to contain only ASCII
* characters (without unicode processing).
*/
public class SimpleCharStream {
/**
* Whether parser is static.
*/
public static final boolean staticFlag = false;
/**
* Position in buffer.
*/
public int bufpos = -1;
protected long[] buflinecolumn;
protected int column = 0;
protected int line = 1;
protected boolean prevCharIsCR = false;
protected boolean prevCharIsLF = false;
protected Provider inputStream;
protected char[] buffer;
protected int maxNextCharInd = 0;
protected int inBuf = 0;
protected int tabSize = 1;
protected boolean trackLineColumn = true;
protected int totalCharsRead = 0;
protected int absoluteTokenBegin = 0;
// line, column and absolute position of each match begun since the last positionSpecialTokens()
private int[] matchBegins = new int[24];
private int matchBeginCount = 0;
int bufsize;
int available;
int tokenBegin;
// MySQL `#` line comments (#2499): under the flag a token-start `#` is
// rewritten in the buffer to HASH_SUBSTITUTION, a character no other
// lexical rule starts with, so the dedicated HASH_LINE_COMMENT production
// wins the match for every `#` form (`# c`, `#c`, `#>`, ...). Rewriting
// the buffer itself (instead of synthesizing reads) keeps the matcher's
// backup / re-read arithmetic intact; the production's action restores
// the `#` in the token image. Wired (with the token manager's
// configuration) before parsing, null keeps this inert.
static final char HASH_SUBSTITUTION = '\u0001';
static final char MYSQL_MINUS_SUBSTITUTION = '\u0002';
static final char MYSQL_COMMENT_ERROR = '\u0003';
FeatureConfiguration featureConfiguration;
private boolean mySqlExecutableComment;
String mySqlCommentError;
boolean mySqlMinusSubstitution;
/**
* Constructor.
*/
public SimpleCharStream(Provider dstream, int startline, int startcolumn, int buffersize) {
inputStream = dstream;
line = startline;
column = startcolumn - 1;
available = bufsize = buffersize;
buffer = new char[buffersize];
buflinecolumn = new long[buffersize];
}
/**
* Constructor.
*/
public SimpleCharStream(Provider dstream, int startline, int startcolumn) {
this(dstream, startline, startcolumn, 4096);
}
/**
* Constructor.
*/
public SimpleCharStream(Provider dstream) {
this(dstream, 1, 1, 4096);
}
public int getTabSize() {
return tabSize;
}
public void setTabSize(int i) {
tabSize = i;
}
public final int getAbsoluteTokenBegin() {
return absoluteTokenBegin;
}
/**
* Gives the comments in front of {@code token} the positions where their matches began, from
* the begins recorded since the last call, and forgets those. Comments come through no token
* action, so this is how they get a position. The comments chain backwards from the token while
* the begins are recorded forwards, so one backwards pass pairs them.
*/
public final void positionSpecialTokens(Token token) {
int i = matchBeginCount - 3;
for (Token comment = token.specialToken; comment != null; comment = comment.specialToken) {
while (i >= 0 && (matchBegins[i] != comment.beginLine
|| matchBegins[i + 1] != comment.beginColumn)) {
i -= 3;
}
if (i < 0) {
break;
}
comment.absoluteBegin = matchBegins[i + 2];
comment.absoluteEnd = comment.absoluteBegin + comment.image.length();
i -= 3;
}
matchBeginCount = 0;
}
private void recordMatchBegin() {
if (matchBeginCount + 3 > matchBegins.length) {
matchBegins = java.util.Arrays.copyOf(matchBegins, matchBegins.length * 2);
}
matchBegins[matchBeginCount++] = getBeginLine();
matchBegins[matchBeginCount++] = getBeginColumn();
matchBegins[matchBeginCount++] = absoluteTokenBegin;
}
protected void ExpandBuff(boolean wrapAround) {
final int newSize = bufsize * 2;
char[] newbuffer = new char[newSize];
long[] newbuflinecolumn = new long[newSize];
if (wrapAround) {
System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
buffer = newbuffer;
System.arraycopy(buflinecolumn, tokenBegin, newbuflinecolumn, 0, bufsize - tokenBegin);
System.arraycopy(buflinecolumn, 0, newbuflinecolumn, bufsize - tokenBegin, bufpos);
buflinecolumn = newbuflinecolumn;
maxNextCharInd = bufpos += bufsize - tokenBegin;
} else {
System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
buffer = newbuffer;
System.arraycopy(buflinecolumn, tokenBegin, newbuflinecolumn, 0, bufsize - tokenBegin);
buflinecolumn = newbuflinecolumn;
maxNextCharInd = bufpos -= tokenBegin;
}
bufsize = newSize;
available = bufsize;
tokenBegin = 0;
}
protected void FillBuff() throws java.io.IOException {
if (maxNextCharInd == available) {
if (available == bufsize) {
if (tokenBegin > 2048) {
bufpos = maxNextCharInd = 0;
available = tokenBegin;
} else if (tokenBegin < 0) {
bufpos = maxNextCharInd = 0;
} else {
ExpandBuff(false);
}
} else if (available > tokenBegin) {
available = bufsize;
} else if ((tokenBegin - available) < 2048) {
ExpandBuff(true);
} else {
available = tokenBegin;
}
}
int i;
try {
if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
inputStream.close();
throw new java.io.IOException();
} else {
maxNextCharInd += i;
}
} catch (java.io.IOException e) {
--bufpos;
backup(0);
if (tokenBegin == -1) {
tokenBegin = bufpos;
}
throw e;
}
}
/**
* Start.
*/
public final char BeginToken() throws java.io.IOException {
mySqlMinusSubstitution = false;
if (mySqlCommentError != null) {
// Parser lookahead may catch lexical errors. Keep this failure sticky until ReInit
// rather than allowing a later token request to continue after the rejected marker.
return MYSQL_COMMENT_ERROR;
}
tokenBegin = -1;
char c;
try {
c = readChar();
} catch (java.io.IOException e) {
if (!mySqlExecutableComment) {
throw e;
}
// JavaCC treats exceptions from BeginToken as EOF. Emit a real token whose lexical
// action reports the error, so an unclosed executable comment cannot succeed as SQL.
c = mySqlCommentError("Unterminated MySQL executable comment");
}
tokenBegin = bufpos;
absoluteTokenBegin = totalCharsRead;
recordMatchBegin();
if (c == '#' && featureConfiguration != null
&& featureConfiguration.getAsBoolean(Feature.allowHashLineComments)) {
buffer[bufpos] = HASH_SUBSTITUTION;
c = HASH_SUBSTITUTION;
} else if ((c == '-' || c == '/' || c == '*') && featureConfiguration != null
&& isMySqlCommentDialect(featureConfiguration.getValue(Feature.dialect))) {
c = mySqlCommentBoundary(c);
} else if (c == '\n' && featureConfiguration != null
&& !featureConfiguration
.getAsBoolean(Feature.allowBlankLinesAsStatementSeparator)
&& peekCharacters(2).equals("\n\n")) {
// Blanking the first newline of the run stops ST_SEMICOLON matching "\n\n\n";
// line and column tracking was already updated when the newline was read.
buffer[bufpos] = ' ';
c = ' ';
}
return c;
}
/** MariaDB shares the MySQL rules for {@code --} and executable comments. */
private static boolean isMySqlCommentDialect(Object dialect) {
return AbstractJSqlParser.Dialect.MYSQL.name().equals(dialect)
|| AbstractJSqlParser.Dialect.MARIADB.name().equals(dialect);
}
/** Changes only token boundaries, never characters being scanned inside strings or names. */
private char mySqlCommentBoundary(char c) throws java.io.IOException {
if (c == '-') {
String following = peekCharacters(2);
if (following.length() == 2 && following.charAt(0) == '-'
&& !isMySqlCommentSpace(following.charAt(1))) {
mySqlMinusSubstitution = true;
buffer[bufpos] = MYSQL_MINUS_SUBSTITUTION;
return MYSQL_MINUS_SUBSTITUTION;
}
} else if (c == '*' && mySqlExecutableComment && peekCharacters(1).equals("/")) {
eraseCommentMarker(2);
mySqlExecutableComment = false;
return ' ';
} else if (c == '/') {
String following = peekCharacters(9);
boolean mariaDb = AbstractJSqlParser.Dialect.MARIADB.name()
.equals(featureConfiguration.getValue(Feature.dialect));
// MariaDB also runs its own /*M! ... */ comments, which MySQL ignores.
int marker = following.startsWith("*!") ? 2
: mariaDb && following.startsWith("*M!") ? 3 : 0;
if (marker > 0) {
if (mySqlExecutableComment) {
return mySqlCommentError("Nested MySQL executable comments are not supported");
}
int digits = mariaDb ? mariaDbCommentVersionLength(following, marker)
: mySqlCommentVersionLength(following);
if (digits > 0) {
long target = featureConfiguration.getAsLong(Feature.mySqlServerVersion);
if (target < 0 || target > 999999) {
return mySqlCommentError("MySQL conditional executable comments require "
+ "withMySqlServerVersion(major * 10000 + minor * 100 + patch)");
}
int minimum = Integer.parseInt(following.substring(marker, marker + digits));
// MariaDB skips /*! comments for MySQL 5.7 and later, but not /*M! ones.
if (minimum > target
|| mariaDb && marker == 2 && minimum >= 50700 && minimum <= 99999) {
return c;
}
}
eraseCommentMarker(marker + 1 + digits);
mySqlExecutableComment = true;
return ' ';
}
}
return c;
}
private static boolean isMySqlCommentSpace(char c) {
return c <= ' ' || c == '\u007f';
}
private static int mySqlCommentVersionLength(String following) {
if (following.length() < 7) {
return 0;
}
for (int i = 2; i < 7; i++) {
if (following.charAt(i) < '0' || following.charAt(i) > '9') {
return 0;
}
}
// MySQL recognizes a sixth digit only when followed by whitespace. Otherwise the
// five-digit prefix is the version and the remaining characters belong to the SQL body.
return following.length() >= 9 && following.charAt(7) >= '0'
&& following.charAt(7) <= '9' && (following.charAt(8) == ' '
|| following.charAt(8) >= '\t' && following.charAt(8) <= '\r')
? 6
: 5;
}
private static int mariaDbCommentVersionLength(String following, int marker) {
if (following.length() < marker + 5) {
return 0;
}
for (int i = marker; i < marker + 5; i++) {
if (following.charAt(i) < '0' || following.charAt(i) > '9') {
return 0;
}
}
// MariaDB takes a sixth digit whenever there is one, whatever follows it.
return following.length() > marker + 5 && following.charAt(marker + 5) >= '0'
&& following.charAt(marker + 5) <= '9' ? 6 : 5;
}
private String peekCharacters(int length) {
StringBuilder result = new StringBuilder(length);
try {
for (int i = 0; i < length; i++) {
result.append(readChar());
}
} catch (java.io.IOException ignored) {
// Only the successfully read characters need to be rewound, including at EOF.
}
backup(result.length());
return result.toString();
}
private void eraseCommentMarker(int length) throws java.io.IOException {
buffer[bufpos] = ' ';
for (int i = 1; i < length; i++) {
readChar();
buffer[bufpos] = ' ';
}
backup(length - 1);
}
private char mySqlCommentError(String message) {
mySqlCommentError = message;
return MYSQL_COMMENT_ERROR;
}
protected final void UpdateLineColumn(char c) {
column++;
if (c >= ' ' && !prevCharIsLF && !prevCharIsCR) {
// Fast path: printable ASCII, no pending newline
buflinecolumn[bufpos] = ((long) line << 32) | (column & 0xFFFFFFFFL);
return;
}
// Slow path: newlines, carriage returns, tabs, control chars
if (prevCharIsLF) {
prevCharIsLF = false;
line += column = 1;
} else if (prevCharIsCR) {
prevCharIsCR = false;
if (c == '\n') {
prevCharIsLF = true;
} else {
line += column = 1;
}
}
switch (c) {
case '\r':
prevCharIsCR = true;
break;
case '\n':
prevCharIsLF = true;
break;
case '\t':
column--;
column += tabSize - (column % tabSize);
break;
default:
break;
}
buflinecolumn[bufpos] = ((long) line << 32) | (column & 0xFFFFFFFFL);
}
/**
* Read a character.
*/
public final char readChar() throws java.io.IOException {
if (inBuf > 0) {
--inBuf;
if (++bufpos == bufsize) {
bufpos = 0;
}
totalCharsRead++;
return buffer[bufpos];
}
if (++bufpos >= maxNextCharInd) {
FillBuff();
}
totalCharsRead++;
char c = buffer[bufpos];
UpdateLineColumn(c);
return c;
}
@Deprecated
/**
* @deprecated
* @see #getEndColumn
*/
public int getColumn() {
return (int) buflinecolumn[bufpos];
}
@Deprecated
/**
* @deprecated
* @see #getEndLine
*/
public int getLine() {
return (int) (buflinecolumn[bufpos] >>> 32);
}
/**
* Get token end column number.
*/
public int getEndColumn() {
return (int) buflinecolumn[bufpos];
}
/**
* Get token end line number.
*/
public int getEndLine() {
return (int) (buflinecolumn[bufpos] >>> 32);
}
/**
* Get token beginning column number.
*/
public int getBeginColumn() {
return (int) buflinecolumn[tokenBegin];
}
/**
* Get token beginning line number.
*/
public int getBeginLine() {
return (int) (buflinecolumn[tokenBegin] >>> 32);
}
/**
* Backup a number of characters.
*/
public final void backup(int amount) {
inBuf += amount;
totalCharsRead -= amount;
if ((bufpos -= amount) < 0) {
bufpos += bufsize;
}
}
/**
* Reinitialise.
*/
public void ReInit(Provider dstream, int startline, int startcolumn, int buffersize) {
matchBeginCount = 0;
inputStream = dstream;
line = startline;
column = startcolumn - 1;
if (buffer == null || buffersize != buffer.length) {
available = bufsize = buffersize;
buffer = new char[buffersize];
buflinecolumn = new long[buffersize];
}
prevCharIsLF = prevCharIsCR = false;
tokenBegin = inBuf = maxNextCharInd = 0;
bufpos = -1;
mySqlExecutableComment = false;
mySqlCommentError = null;
}
/**
* Reinitialise.
*/
public void ReInit(Provider dstream, int startline, int startcolumn) {
ReInit(dstream, startline, startcolumn, 4096);
}
/**
* Reinitialise.
*/
public void ReInit(Provider dstream) {
ReInit(dstream, 1, 1, 4096);
}
/**
* Get token literal value.
*/
public String GetImage() {
// restore the `#` rewritten to HASH_SUBSTITUTION: only the comment
// token's own window starts with the sentinel (token-start rewrites
// only), so this is exact and covers every consumer of GetImage.
// The wiring check keeps parses that never opted in on the original
// cost (the stream is only wired through the feature consumers /
// withConfiguration)
if (featureConfiguration == null) {
return doGetImage();
}
String image = doGetImage();
if (!image.isEmpty() && image.charAt(0) == HASH_SUBSTITUTION) {
image = "#" + image.substring(1);
} else if (!image.isEmpty() && image.charAt(0) == MYSQL_MINUS_SUBSTITUTION) {
image = "-" + image.substring(1);
}
return image;
}
private String doGetImage() {
if (bufpos >= tokenBegin) {
return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
} else {
return new String(buffer, tokenBegin, bufsize - tokenBegin)
+ new String(buffer, 0, bufpos + 1);
}
}
/**
* Get the suffix.
*/
public char[] GetSuffix(int len) {
char[] ret = new char[len];
if ((bufpos + 1) >= len) {
System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
} else {
System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
}
return ret;
}
/**
* Reset buffer when finished.
*/
public void Done() {
buffer = null;
buflinecolumn = null;
}
/**
* Method to adjust line and column numbers for the start of a token.
*/
public void adjustBeginLineColumn(int newLine, int newCol) {
int start = tokenBegin;
int len;
if (bufpos >= tokenBegin) {
len = bufpos - tokenBegin + inBuf + 1;
} else {
len = bufsize - tokenBegin + bufpos + 1 + inBuf;
}
int i = 0;
int j = 0;
int k;
int nextColDiff;
int columnDiff = 0;
while (i < len &&
(int) (buflinecolumn[j = start % bufsize] >>> 32) == (int) (buflinecolumn[k =
++start % bufsize] >>> 32)) {
int colJ = (int) buflinecolumn[j];
int colK = (int) buflinecolumn[k];
nextColDiff = columnDiff + colK - colJ;
buflinecolumn[j] = ((long) newLine << 32) | ((newCol + columnDiff) & 0xFFFFFFFFL);
columnDiff = nextColDiff;
i++;
}
if (i < len) {
buflinecolumn[j] = ((long) (newLine++) << 32) | ((newCol + columnDiff) & 0xFFFFFFFFL);
while (i++ < len) {
int lineJ = (int) (buflinecolumn[j = start % bufsize] >>> 32);
int lineNext = (int) (buflinecolumn[++start % bufsize] >>> 32);
if (lineJ != lineNext) {
buflinecolumn[j] =
((long) (newLine++) << 32) | (buflinecolumn[j] & 0xFFFFFFFFL);
} else {
buflinecolumn[j] = ((long) newLine << 32) | (buflinecolumn[j] & 0xFFFFFFFFL);
}
}
}
line = (int) (buflinecolumn[j] >>> 32);
column = (int) buflinecolumn[j];
}
boolean getTrackLineColumn() {
return trackLineColumn;
}
void setTrackLineColumn(boolean tlc) {
trackLineColumn = tlc;
}
}
/* JavaCC - OriginalChecksum=0cd74e5ad7a4ccb9188541ab8f8b35eb (do not edit this line) */