Repository navigation
Expand file tree
/
Copy pathExpressionLexer.java
More file actions
641 lines (561 loc) · 18.8 KB
/
Copy pathExpressionLexer.java
File metadata and controls
641 lines (561 loc) · 18.8 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
/**
* Copyright (C) 2010 dennis zhuang (killme2008@gmail.com)
*
* This library is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
**/
package com.googlecode.aviator.lexer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.LinkedList;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Feature;
import com.googlecode.aviator.Options;
import com.googlecode.aviator.exception.CompileExpressionErrorException;
import com.googlecode.aviator.exception.ExpressionSyntaxErrorException;
import com.googlecode.aviator.lexer.token.CharToken;
import com.googlecode.aviator.lexer.token.NumberToken;
import com.googlecode.aviator.lexer.token.StringToken;
import com.googlecode.aviator.lexer.token.Token;
import com.googlecode.aviator.lexer.token.Variable;
import com.googlecode.aviator.utils.Constants;
/**
* Expression Lexer,scan tokens from string
*
* @author dennis
*
*/
public class ExpressionLexer {
private static final long OVERFLOW_FLAG = Long.MAX_VALUE / 10;
private static final long OVERFLOW_SINGLE = Long.MAX_VALUE % 10;
// current char
private char peek;
// Char iterator for string
private final CharacterIterator iterator;
private int lineNo;
// symbol table
private final SymbolTable symbolTable;
// Tokens buffer
private LinkedList<Token<?>> tokenBuffer;
private final AviatorEvaluatorInstance instance;
private final String expression;
private final MathContext mathContext;
private final boolean parseFloatIntoDecimal;
private final boolean parseIntegralNumberIntoDecimal;
public ExpressionLexer(final AviatorEvaluatorInstance instance, final String expression) {
this.iterator = new StringCharacterIterator(expression);
this.expression = expression;
this.symbolTable = new SymbolTable();
this.peek = this.iterator.current();
this.instance = instance;
this.lineNo = 1;
this.mathContext = this.instance.getOptionValue(Options.MATH_CONTEXT).mathContext;
this.parseFloatIntoDecimal =
this.instance.getOptionValue(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL).bool;
this.parseIntegralNumberIntoDecimal =
this.instance.getOptionValue(Options.ALWAYS_PARSE_INTEGRAL_NUMBER_INTO_DECIMAL).bool;
}
public SymbolTable getSymbolTable() {
return this.symbolTable;
}
public void setLineNo(final int lineNo) {
this.lineNo = lineNo;
}
public int getLineNo() {
return this.lineNo;
}
/**
* Push back token
*
* @param token
*/
public void pushback(final Token<?> token) {
if (this.tokenBuffer == null) {
this.tokenBuffer = new LinkedList<>();
}
this.tokenBuffer.push(token);
}
public Token<?> scan() {
return this.scan(true);
}
public void nextChar() {
this.peek = this.iterator.next();
}
public void prevChar() {
this.peek = this.iterator.previous();
}
// Character constants for lexical analysis
private static final String HEX_CHARS = "0123456789AaBbCcDdEeFf";
private static final String OPERATOR_CHARS = "=><+-*/%!&|";
/** Suffix for BigInteger literals, e.g., 100N */
static final char BIGINT_SUFFIX = 'N';
/** Suffix for BigDecimal literals, e.g., 3.14M */
static final char DECIMAL_SUFFIX = 'M';
/** Hex number prefix after '0', e.g., 0xFF */
static final char HEX_PREFIX = 'x';
public boolean isValidHexChar(final char ch) {
return HEX_CHARS.indexOf(ch) >= 0;
}
public int getCurrentIndex() {
return this.iterator.getIndex();
}
public Token<?> scan(final boolean analyse) {
// If buffer is not empty, return buffered token
if (this.tokenBuffer != null && !this.tokenBuffer.isEmpty()) {
return this.tokenBuffer.pop();
}
// Skip whitespace or return raw char when not analysing
for (;; nextChar()) {
if (this.peek == CharacterIterator.DONE) {
return null;
}
if (analyse) {
if (this.peek == ' ' || this.peek == '\t' || this.peek == '\r' || this.peek == '\n') {
if (this.peek == '\n') {
this.lineNo++;
}
continue;
}
break;
} else {
char ch = this.peek;
int index = this.iterator.getIndex();
nextChar();
return new CharToken(ch, this.lineNo, index);
}
}
// Try each token type in order
Token<?> token;
if ((token = scanHexNumber()) != null) {
return token;
}
if ((token = scanNumber()) != null) {
return token;
}
if ((token = scanQuoteVariable(analyse)) != null) {
return token;
}
if ((token = scanVariable()) != null) {
return token;
}
if ((token = scanOperator()) != null) {
return token;
}
if ((token = scanString()) != null) {
return token;
}
// Fallback: return single character token (but not DONE)
if (this.peek == CharacterIterator.DONE) {
return null;
}
token = new CharToken(this.peek, this.lineNo, this.iterator.getIndex());
nextChar();
return token;
}
/**
* Scan hexadecimal number (0x...).
*
* @return NumberToken if hex number found, null otherwise
*/
private Token<?> scanHexNumber() {
if (!(Character.isDigit(this.peek) && this.peek == '0')) {
return null;
}
nextChar();
if (this.peek == HEX_PREFIX || this.peek == 'X') {
nextChar();
StringBuilder sb = new StringBuilder();
int startIndex = this.iterator.getIndex() - 2;
long value = 0L;
do {
sb.append(this.peek);
value = 16 * value + Character.digit(this.peek, 16);
nextChar();
} while (isValidHexChar(this.peek));
return new NumberToken(value, sb.toString(), this.lineNo, startIndex);
} else {
prevChar();
return null;
}
}
/**
* Scan decimal number (integer, float, scientific notation, BigInt, BigDecimal).
*
* @return NumberToken or CharToken('.') if found, null otherwise
*/
private Token<?> scanNumber() {
if (!(Character.isDigit(this.peek) || this.peek == '.')) {
return null;
}
StringBuilder sb = new StringBuilder();
int startIndex = this.iterator.getIndex();
long lval = 0L;
double dval = 0d;
double d = 10.0;
boolean hasDot = false;
boolean isBigInt = false;
boolean isBigDecimal = false;
boolean scientificNotation = false;
boolean negExp = false;
boolean isOverflow = false;
do {
sb.append(this.peek);
if (this.peek == '.') {
if (scientificNotation) {
throw new CompileExpressionErrorException(
"Illegal number " + sb + " at " + this.iterator.getIndex());
}
if (hasDot) {
throw new CompileExpressionErrorException(
"Illegal Number " + sb + " at " + this.iterator.getIndex());
}
hasDot = true;
nextChar();
} else if (this.peek == BIGINT_SUFFIX) {
if (hasDot) {
throw new CompileExpressionErrorException(
"Illegal number " + sb + " at " + this.iterator.getIndex());
}
isBigInt = true;
nextChar();
break;
} else if (this.peek == DECIMAL_SUFFIX) {
isBigDecimal = true;
nextChar();
break;
} else if (this.peek == 'e' || this.peek == 'E') {
if (scientificNotation) {
throw new CompileExpressionErrorException(
"Illegal number " + sb + " at " + this.iterator.getIndex());
}
scientificNotation = true;
nextChar();
if (this.peek == '-') {
negExp = true;
sb.append(this.peek);
nextChar();
}
} else {
int digit = Character.digit(this.peek, 10);
if (scientificNotation) {
int n = digit;
nextChar();
while (Character.isDigit(this.peek)) {
sb.append(this.peek);
n = 10 * n + Character.digit(this.peek, 10);
nextChar();
}
while (n-- > 0) {
if (negExp) {
dval = dval / 10;
} else {
dval = 10 * dval;
}
}
hasDot = true;
} else if (hasDot) {
dval = dval + digit / d;
d = d * 10;
nextChar();
} else {
if (!isOverflow
&& (lval > OVERFLOW_FLAG || (lval == OVERFLOW_FLAG && digit > OVERFLOW_SINGLE))) {
isOverflow = true;
}
lval = 10 * lval + digit;
dval = 10 * dval + digit;
nextChar();
}
}
} while (Character.isDigit(this.peek) || this.peek == '.' || this.peek == 'E'
|| this.peek == 'e' || this.peek == DECIMAL_SUFFIX || this.peek == BIGINT_SUFFIX);
// Build final number value
Number value;
if (isBigDecimal) {
value = new BigDecimal(getBigNumberLexeme(sb), this.mathContext);
} else if (isBigInt) {
value = new BigInteger(getBigNumberLexeme(sb));
} else if (hasDot) {
if (this.parseFloatIntoDecimal && sb.length() > 1) {
value = new BigDecimal(sb.toString(), this.mathContext);
} else if (sb.length() == 1) {
// Only a dot character
return new CharToken('.', this.lineNo, startIndex);
} else {
value = dval;
}
} else {
if (this.parseIntegralNumberIntoDecimal) {
value = new BigDecimal(sb.toString(), this.mathContext);
} else if (isOverflow) {
value = new BigInteger(sb.toString());
} else {
value = lval;
}
}
String lexeme = sb.toString();
if (isBigDecimal || isBigInt) {
lexeme = lexeme.substring(0, lexeme.length() - 1);
}
return new NumberToken(value, lexeme, this.lineNo, startIndex);
}
/**
* Scan quote variable (#var or #`var`).
*
* @param analyse whether to analyse (for recursive call on comment)
* @return Variable token if found, null otherwise
*/
private Token<?> scanQuoteVariable(final boolean analyse) {
if (this.peek != '#') {
return null;
}
int startIndex = this.iterator.getIndex();
nextChar(); // skip '#'
// ## is a comment
if (this.peek == '#') {
while (this.peek != CharacterIterator.DONE && this.peek != '\n') {
nextChar();
}
return this.scan(analyse);
}
// Check for backquote form #`...`
boolean hasBackquote = false;
if (this.peek == '`') {
hasBackquote = true;
nextChar();
}
StringBuilder sb = new StringBuilder();
if (hasBackquote) {
while (this.peek != '`') {
if (this.peek == CharacterIterator.DONE) {
throw new CompileExpressionErrorException(
"EOF while reading string at index: " + this.iterator.getIndex());
}
sb.append(this.peek);
nextChar();
}
nextChar(); // skip closing '`'
} else {
while (Character.isJavaIdentifierPart(this.peek) || this.peek == '.' || this.peek == '['
|| this.peek == ']') {
sb.append(this.peek);
nextChar();
}
}
String lexeme = sb.toString();
if (lexeme.isEmpty()) {
throw new ExpressionSyntaxErrorException("Blank variable name after '#'");
}
Variable variable = new Variable(lexeme, this.lineNo, startIndex);
variable.setQuote(true);
return this.symbolTable.reserve(variable);
}
/**
* Scan normal variable/identifier.
*
* @return Variable token if found, null otherwise
*/
private Token<?> scanVariable() {
if (!Character.isJavaIdentifierStart(this.peek)) {
return null;
}
int startIndex = this.iterator.getIndex();
StringBuilder sb = new StringBuilder();
boolean hasDot = false;
// The first character is a valid Java identifier start, never a dot.
sb.append(this.peek);
nextChar();
while (true) {
if (Character.isJavaIdentifierPart(this.peek) || this.peek == '.') {
if (this.peek == '.') {
hasDot = true;
}
sb.append(this.peek);
nextChar();
} else if (this.peek == '?' && tryAbsorbNullSafeDot(sb)) {
// The "?." text has been appended and the lexer now points at the next
// property segment, which the next iteration consumes.
hasDot = true;
} else if (hasDot && this.peek == '[' && tryAbsorbChainedIndex(sb)) {
// The "[digits]" text has been appended and the lexer now points at the
// following '.' or '?', which the next iteration consumes.
} else {
break;
}
}
String lexeme = sb.toString();
Variable variable = new Variable(lexeme, this.lineNo, startIndex);
return this.symbolTable.reserve(variable);
}
/**
* Try to absorb a mid-chain integer-literal index into the variable lexeme, e.g. the "[0]" in
* "foo.bars[0].name".
*
* <p>
* Only a non-negative integer literal index immediately followed by another property segment
* ('.') is absorbed, because such a chain cannot be expressed through the parser's array-access
* handling. Dynamic indices (foo.bars[i]), expression indices (foo.bars[i + 1]), trailing indices
* (foo.bars[0]) and multi-dimensional indices (foo.bars[0][1]) are left untouched so the parser
* resolves them via {@code getElement}, which preserves dynamic and multi-dimensional indexing.
*
* <p>
* On success the "[digits]" text is appended to {@code sb} and the lexer is positioned at the
* following '.'. On failure the lexer is restored to the original '[' and the method returns
* false.
*
* @param sb the variable lexeme being built, positioned at '['
* @return true if a chained literal index was absorbed
*/
private boolean tryAbsorbChainedIndex(final StringBuilder sb) {
int mark = this.iterator.getIndex(); // points at '['
StringBuilder digits = new StringBuilder();
nextChar(); // skip '['
while (Character.isDigit(this.peek)) {
digits.append(this.peek);
nextChar();
}
if (digits.length() > 0 && this.peek == ']') {
nextChar(); // skip ']'
if (this.peek == '.') {
sb.append('[').append(digits).append(']');
return true;
}
}
// Not a chained literal index: restore the lexer to the original '['.
this.peek = this.iterator.setIndex(mark);
return false;
}
/**
* Try to absorb a null-safe navigation operator "?." into the variable lexeme, e.g. the "?." in
* "a?.b". The '?' is kept as a marker on the preceding segment so that the runtime can tell which
* segments should short-circuit to null instead of throwing on a null intermediate value.
*
* <p>
* Only "?." (a '?' immediately followed by '.') is absorbed. A lone '?' (ternary operator) or
* "??" (null-coalescing operator) is left untouched for the parser to handle.
*
* @param sb the variable lexeme being built, positioned at '?'
* @return true if a null-safe dot was absorbed
*/
private boolean tryAbsorbNullSafeDot(final StringBuilder sb) {
int mark = this.iterator.getIndex(); // points at '?'
nextChar(); // skip '?'
if (this.peek == '.') {
sb.append("?.");
nextChar(); // skip '.'
return true;
}
// Not a null-safe dot (e.g. ternary '?' or '??'): restore the lexer to the original '?'.
this.peek = this.iterator.setIndex(mark);
return false;
}
/**
* Scan operator character.
*
* @return CharToken if operator found, null otherwise
*/
private Token<?> scanOperator() {
if (!isBinaryOP(this.peek)) {
return null;
}
CharToken opToken = new CharToken(this.peek, this.lineNo, this.iterator.getIndex());
nextChar();
return opToken;
}
/**
* Scan string literal ("..." or '...').
*
* @return StringToken if found, null otherwise
*/
private Token<?> scanString() {
if (this.peek != '"' && this.peek != '\'') {
return null;
}
char left = this.peek;
int startIndex = this.iterator.getIndex();
StringBuilder sb = new StringBuilder();
boolean hasInterpolation = false;
while ((this.peek = this.iterator.next()) != left) {
// Check for interpolation marker
if (this.peek == '#' && !hasInterpolation) {
hasInterpolation = true;
}
// Handle escape sequences
if (this.peek == '\\') {
nextChar();
if (this.peek == CharacterIterator.DONE) {
throw new CompileExpressionErrorException(
"EOF while reading string at index: " + this.iterator.getIndex());
}
if (this.peek == left) {
sb.append(this.peek);
continue;
}
switch (this.peek) {
case 't':
this.peek = '\t';
break;
case 'r':
this.peek = '\r';
break;
case 'n':
this.peek = '\n';
break;
case '\\':
break;
case 'b':
this.peek = '\b';
break;
case 'f':
this.peek = '\f';
break;
case '#':
hasInterpolation = true;
if (this.instance.isFeatureEnabled(Feature.StringInterpolation)) {
sb.append('\\');
this.peek = '#';
break;
}
default:
throw new CompileExpressionErrorException(
"Unsupported escape character: \\" + this.peek);
}
}
if (this.peek == CharacterIterator.DONE) {
throw new CompileExpressionErrorException(
"EOF while reading string at index: " + this.iterator.getIndex());
}
sb.append(this.peek);
}
nextChar();
return new StringToken(sb.toString(), this.lineNo, startIndex).withMeta(Constants.INTER_META,
hasInterpolation);
}
public String getScanString() {
Token<?> firstPushbackToken = this.tokenBuffer != null ? this.tokenBuffer.peekFirst() : null;
return this.expression.substring(0,
(firstPushbackToken != null && firstPushbackToken.getStartIndex() > 0)
? firstPushbackToken.getEndIndex()
: this.iterator.getIndex());
}
private String getBigNumberLexeme(final StringBuilder sb) {
String lexeme = sb.toString();
lexeme = lexeme.substring(0, lexeme.length() - 1);
return lexeme;
}
public static boolean isBinaryOP(final char ch) {
return OPERATOR_CHARS.indexOf(ch) >= 0;
}
}