Skip to content

Commit 7b11573

Browse files
committed
(feat) refactor lexer
1 parent 34ee209 commit 7b11573

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/main/java/com/googlecode/aviator/lexer/ExpressionLexer.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
23+
import java.math.MathContext;
2324
import java.text.CharacterIterator;
2425
import java.text.StringCharacterIterator;
2526
import java.util.Stack;
@@ -83,7 +84,7 @@ public void prevChar() {
8384
}
8485

8586
static final char[] VALID_HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'a', 'B', 'b', 'C',
86-
'c', 'D', 'd', 'E', 'e', 'F', 'f' };
87+
'c', 'D', 'd', 'E', 'e', 'F', 'f' };
8788

8889

8990
public boolean isValidHexChar(char ch) {
@@ -237,21 +238,23 @@ else if (hasDot) {
237238
|| this.peek == 'M' || this.peek == 'N');
238239
Number value;
239240
if (isBigDecimal) {
240-
value = getDecimalValue(sb);
241+
value =
242+
new BigDecimal(this.getBigNumberLexeme(sb),
243+
(MathContext) AviatorEvaluator.getOption(Options.MATH_CONTEXT));
241244
}
242245
else if (isBigInt) {
243-
String lexeme = this.getBigNumberLexeme(sb);
244-
value = new BigInteger(lexeme);
246+
value = new BigInteger(this.getBigNumberLexeme(sb));
245247
}
246248
else if (hasDot) {
247-
boolean alwaysUseDecimalAsDouble = AviatorEvaluator
248-
.getOption(Options.ALWAYS_USE_DOUBLE_AS_DECIMAL);
249-
if (alwaysUseDecimalAsDouble) {
250-
value = new BigDecimal(sb.toString(),
251-
AviatorEvaluator.getMathContext());
252-
} else {
253-
value = dval;
254-
}
249+
boolean alwaysUseDecimalAsDouble = AviatorEvaluator.getOption(Options.ALWAYS_USE_DOUBLE_AS_DECIMAL);
250+
if (alwaysUseDecimalAsDouble) {
251+
value =
252+
new BigDecimal(sb.toString(),
253+
(MathContext) AviatorEvaluator.getOption(Options.MATH_CONTEXT));
254+
}
255+
else {
256+
value = dval;
257+
}
255258
}
256259
else {
257260
// if the long value is out of range,then it must be negative,so
@@ -318,15 +321,6 @@ else if (hasDot) {
318321
return token;
319322
}
320323

321-
322-
private Number getDecimalValue(StringBuffer sb) {
323-
Number value;
324-
String lexeme = this.getBigNumberLexeme(sb);
325-
value = new BigDecimal(lexeme, AviatorEvaluator.getMathContext());
326-
return value;
327-
}
328-
329-
330324
private String getBigNumberLexeme(StringBuffer sb) {
331325
String lexeme = sb.toString();
332326
lexeme = lexeme.substring(0, lexeme.length() - 1);

0 commit comments

Comments
 (0)