Skip to content

Commit

Permalink
fix loss/EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
dengsgo committed Sep 11, 2022
1 parent 48f94a2 commit a5a560a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions engine/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (a *AST) parsePrimary() ExprAST {
t := a.getNextToken()
if t == nil {
a.Err = errors.New(
fmt.Sprintf("want '0-9' but nothing at all\n%s",
fmt.Sprintf("want '(' or '0-9' but get EOF\n%s",
ErrPos(a.source, a.currTok.Offset)))
return nil
}
Expand Down Expand Up @@ -242,7 +242,10 @@ func (a *AST) parseBinOpRHS(execPrec int, lhs ExprAST) ExprAST {
}
binOp := a.currTok.Tok
if a.getNextToken() == nil {
return lhs
a.Err = errors.New(
fmt.Sprintf("want '(' or '0-9' but get EOF\n%s",
ErrPos(a.source, a.currTok.Offset)))
return nil
}
rhs := a.parsePrimary()
if rhs == nil {
Expand Down
9 changes: 8 additions & 1 deletion engine/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestParseAndExecSimple(t *testing.T) {
}
exprs := []U{
{"1", 1},
{"1+", 1},
{"--1", 1},
{"1+2", 3},
{"-1+2", 1},
{"-(1+2)", -3},
Expand Down Expand Up @@ -190,6 +190,13 @@ func TestParseAndExecError(t *testing.T) {
"((xscdfddff",
"(1",
"(1+",
"1+",
"1*",
"+2344",
"3+(",
"4+(90-",
"3-(4*7-2)+",
"3-(4*7-2)+98*",
"1#1",
"_123_456_789___",
"1ee3+3",
Expand Down

0 comments on commit a5a560a

Please sign in to comment.