aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-12 01:30:17 +0200
committerMel <einebeere@gmail.com>2022-07-12 01:30:17 +0200
commitff1372bafb279193b00ac7563eb0d04cac930ab0 (patch)
tree2e7ea7e8f8e6b66fa0f863a9d8720afdae07fa2f
parente5ae4ea1288a555c4019dad43ee27e960eec46b9 (diff)
downloadjinx-ff1372bafb279193b00ac7563eb0d04cac930ab0.tar.zst
jinx-ff1372bafb279193b00ac7563eb0d04cac930ab0.zip
Fix empty for parsing and test regression
-rw-r--r--pkg/lang/parser/parser_test.go44
-rw-r--r--pkg/lang/parser/stmts.go9
2 files changed, 50 insertions, 3 deletions
diff --git a/pkg/lang/parser/parser_test.go b/pkg/lang/parser/parser_test.go
index e02ab5a..8cecfa8 100644
--- a/pkg/lang/parser/parser_test.go
+++ b/pkg/lang/parser/parser_test.go
@@ -521,6 +521,50 @@ func TestForCondStmt(t *testing.T) {
}, program.Stmts[0])
}
+func TestEmptyForStmt(t *testing.T) {
+ src := sourceify(
+ `for {`,
+ ` "spam"`,
+ `}`,
+ )
+
+ p := cheatWithScanner(t, src)
+ program, err := p.Parse()
+ require.NoError(t, err)
+
+ require.Equal(t, 1, len(program.Stmts))
+ require.Equal(t, ast.Stmt{
+ At: source.NewLoc(0, 0),
+ Kind: ast.StmtKindForCond,
+ Value: ast.StmtForCond{
+ Cond: ast.Expr{},
+ Do: ast.BlockNode{
+ At: source.NewLoc(0, 4),
+ Stmts: []ast.Stmt{
+ {
+ At: source.NewLoc(0, 5),
+ Kind: ast.StmtKindEmpty,
+ Value: ast.StmtEmpty{},
+ },
+ {
+ At: source.NewLoc(1, 1),
+ Kind: ast.StmtKindExpr,
+ Value: ast.StmtExpr{
+ Value: ast.Expr{
+ At: source.NewLoc(1, 1),
+ Kind: ast.ExprKindStringLit,
+ Value: ast.ExprStringLit{
+ Value: "spam",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ }, program.Stmts[0])
+}
+
func TestForInStmt(t *testing.T) {
src := sourceify(
`for x in [1, 2, 3] {`,
diff --git a/pkg/lang/parser/stmts.go b/pkg/lang/parser/stmts.go
index 2a32906..713b1aa 100644
--- a/pkg/lang/parser/stmts.go
+++ b/pkg/lang/parser/stmts.go
@@ -231,9 +231,12 @@ func (p *Parser) parseForStmt() (ast.Stmt, error) {
return ast.Stmt{}, err
}
- expr, err := p.parseExpr()
- if err != nil {
- return ast.Stmt{}, err
+ expr := ast.Expr{}
+ if p.peek().Kind != token.LBrace {
+ expr, err = p.parseExpr()
+ if err != nil {
+ return ast.Stmt{}, err
+ }
}
// ForInStmt