Repository: benhoyt/goawk (go)

Issue:
Parser doesn't allow null ; (semicolon)
GoAWK fails with a parse error on `{}` blocks with just semicolons in them (other AWKs support that, so presumably it's allowed by the grammar). For example:

```
$ goawk 'BEGIN {;}'
<cmdline>:1:9: expected expression instead of }
BEGIN {;}
        ^
$ goawk 'BEGIN { while (0) {;;} }'
<cmdline>:1:22: expected expression instead of }
BEGIN { while (0) {;;} }
                     ^
```

Thanks @raygard for the bug report.

Pull request:
Allow { blocks } with just semicolons in them
Fixes #188

Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 72b3e33..3ffcdfc 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -217,6 +217,10 @@ func (p *parser) stmtsBrace() ast.Stmts {
 	p.optionalNewlines()
 	ss := []ast.Stmt{}
 	for p.tok != RBRACE && p.tok != EOF {
+		if p.matches(SEMICOLON, NEWLINE) {
+			p.next()
+			continue
+		}
 		ss = append(ss, p.stmt())
 	}
 	p.expect(RBRACE)
@@ -278,9 +282,6 @@ func (p *parser) simpleStmt() ast.Stmt {
 
 // Parse any top-level statement.
 func (p *parser) stmt() ast.Stmt {
-	for p.matches(SEMICOLON, NEWLINE) {
-		p.next()
-	}
 	var s ast.Stmt
 	startPos := p.pos
 	switch p.tok {


Test patch:
diff --git a/interp/interp_test.go b/interp/interp_test.go
index a20d31d..e32af7b 100644
--- a/interp/interp_test.go
+++ b/interp/interp_test.go
@@ -869,6 +869,10 @@ BEGIN { x[1]=3; f5(x); print x[1] }
 	{"{}             ;  ", "", "", "", ""},
 	{"1              ;  ", "", "", "", ""},
 
+	// Extra semicolons are allowed in blocks
+	{`BEGIN {;}`, "", "", "", ""},
+	{`BEGIN { while (0) {;;} }`, "", "", "", ""},
+
 	// Grammar should allow blocks wherever statements are allowed
 	{`BEGIN { if (1) printf "x"; else printf "y" }`, "", "x", "", ""},
 	{`BEGIN { printf "x"; { printf "y"; printf "z" } }`, "", "xyz", "", ""},