about summary refs log tree commit diff
diff options
context:
space:
mode:
-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