Skip to content

Commit 83658de

Browse files
committed
parser: Fix missing early return on index error
Add missing early return on index expression parse error. This resulted in a "dereferenced nil pointer" panic at run time.
1 parent 7e711cf commit 83658de

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

pkg/parser/expression.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func (p *Parser) parserIndexExpr(scope *scope, left Node) Node {
144144
return nil
145145
}
146146
index := p.parseTopLevelExpr(scope)
147+
if index == nil {
148+
return nil
149+
}
147150
if !p.assertToken(lexer.RBRACKET) {
148151
return nil
149152
}

pkg/parser/expression_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestParseTopLevelExpression(t *testing.T) {
6767
`print (len "abc") 2`: "print(len('abc'), 2)",
6868
`print (len "abc") (len "x")`: "print(len('abc'), len('x'))",
6969
`print s[1]`: "print((s[1]))",
70+
"print map2[s]": "print((map2[s]))",
7071

7172
// Index expression
7273
"arr[1]": "(arr[1])",
@@ -88,6 +89,7 @@ func TestParseTopLevelExpression(t *testing.T) {
8889
"map3.ok[n1]": "((map3.ok)[n1])",
8990
"list[1].x": "((list[1]).x)",
9091
"list[n1][s]": "((list[n1])[s])",
92+
"map2[s]": "(map2[s])",
9193

9294
// Array literals
9395
"[]": "[]",

pkg/parser/parser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func TestParseDeclarationError(t *testing.T) {
7979
"a := {}[": "line 1 column 9: unexpected end of input",
8080
"a :num num": "line 1 column 8: expected end of line, found 'num'",
8181
"a :num{}num": "line 1 column 7: expected end of line, found '{'",
82+
`
83+
m := {name: "Greta"}
84+
s := name
85+
print m[s]`: "line 3 column 6: unknown variable name 'name'",
8286
}
8387
for input, err1 := range tests {
8488
parser := New(input, testBuiltins())

0 commit comments

Comments
 (0)