Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing issue inside if conditions #180

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,18 @@ func TestCrashKeys(t *testing.T) {
t.Errorf("should not have errored: %v", err)
}
}

func TestParenInIf(t *testing.T) {
inp := `if (1+2)==3 {42}`
s := eval.NewState()
res, err := eval.EvalString(s, inp, false)
if err != nil {
t.Fatalf("should not have errored: %v", err)
}
if res.Type() != object.INTEGER {
t.Fatalf("should have returned an integer, got %#v", res)
}
if res.Inspect() != "42" {
t.Errorf("wrong result, got %q", res.Inspect())
}
}
4 changes: 4 additions & 0 deletions main_test.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ cmp stdout json_output
grol -quiet -c '()=>{{"a":1,"b":2}}'
stdout '^\(\)=>{{"a":1,"b":2}}$'

# if extra paren are needed (like for a[x] in the left part of if condition) it should still parse.
grol -quiet -c '()=> if 1+2==3 {4}'
stdout '^\(\)=>if \(1\+2\)==3\{4}$'

-- json_output --
{
"63": 63,
Expand Down
10 changes: 0 additions & 10 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,9 @@ func (p *Parser) parseIfExpression() ast.Node {
expression := &ast.IfExpression{}
expression.Token = p.curToken

needCloseParen := false
if p.peekTokenIs(token.LPAREN) {
needCloseParen = true
p.nextToken()
}

p.nextToken()
expression.Condition = p.parseExpression(LOWEST)

if needCloseParen && !p.expectPeek(token.RPAREN) {
return nil
}

if !p.expectPeek(token.LBRACE) {
return nil
}
Expand Down