Skip to content

Commit 6a87eb8

Browse files
committed
fix: failed to parsing ternary statement in some error cases, #483
1 parent e4d51b6 commit 6a87eb8

5 files changed

Lines changed: 40 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,11 @@ public Token<?> scan(final boolean analyse) {
442442
}
443443

444444
public String getScanString() {
445-
return this.expression.substring(0, this.iterator.getIndex());
445+
Token<?> firstPushbackToken = this.tokenBuffer != null ? this.tokenBuffer.peekFirst() : null;
446+
return this.expression.substring(0,
447+
(firstPushbackToken != null && firstPushbackToken.getStartIndex() > 0)
448+
? firstPushbackToken.getEndIndex()
449+
: this.iterator.getIndex());
446450
}
447451

448452
private String getBigNumberLexeme(final StringBuilder sb) {

src/main/java/com/googlecode/aviator/lexer/token/AbstractToken.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public int getStartIndex() {
107107
return this.lineIndex;
108108
}
109109

110+
@Override
111+
public int getEndIndex() {
112+
return this.lineIndex + (this.lexeme != null ? this.lexeme.length() : 0);
113+
}
114+
110115

111116
@Override
112117
public String toString() {

src/main/java/com/googlecode/aviator/lexer/token/Token.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ enum TokenType {
4848

4949
int getStartIndex();
5050

51+
int getEndIndex();
52+
5153
int getLineNo();
5254
}

src/main/java/com/googlecode/aviator/parser/ExpressionParser.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ private void pattern() {
10511051
this.inPattern = true;
10521052
StringBuilder sb = new StringBuilder();
10531053
while (this.lookhead != null) {
1054-
while (!expectChar('/')) {
1054+
while (!expectChar('/') && this.lookhead != null) {
10551055
sb.append(this.lookhead.getLexeme());
10561056
move(false);
10571057
}
@@ -1886,6 +1886,16 @@ private StatementType statements() {
18861886
ensureDepthState();
18871887
}
18881888
ensureNoStatementAfterReturn(stmtType);
1889+
// If the last statement is ternary,it must be ended with END TOKEN such as null token, '}',
1890+
// 'end' keyword, or ';'
1891+
// Otherwise report syntax error.
1892+
if (stmtType == StatementType.Ternary) {
1893+
if (lookhead != null && !expectChar(';') && !expectChar('}') && lookhead != Variable.END) {
1894+
this.back();
1895+
reportSyntaxError("unexpect token '" + currentTokenLexeme()
1896+
+ "', maybe forget to insert ';' to complete last expression ");
1897+
}
1898+
}
18891899

18901900
return stmtType;
18911901
}

src/test/java/com/googlecode/aviator/test/function/GrammarUnitTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ public void testCompareWithVariableSyntaxSuger() {
143143
assertTrue((boolean) this.instance.execute("data == nil"));
144144
}
145145

146+
@Test
147+
public void testIssue483() {
148+
try {
149+
this.instance
150+
.execute("if(true){\n" + "return false;\n" + "}\n" + "//注释xxxx\n" + "return true;");
151+
fail();
152+
} catch (ExpressionSyntaxErrorException e) {
153+
}
154+
155+
try {
156+
this.instance.execute("if(true){\n" + "return false;\n" + "}\n" + "//\n" + "return true;");
157+
fail();
158+
} catch (ExpressionSyntaxErrorException e) {
159+
e.printStackTrace();
160+
}
161+
}
162+
146163
@Test
147164
public void testEscapeStringInterpolation() {
148165
this.instance.disableFeature(Feature.StringInterpolation);

0 commit comments

Comments
 (0)