cue/parser: added fuzzing code and fix one issue

The generated corpus is not included.

Change-Id: I59afb5c4a28b41e3249ad2b4156a4bd2f1b67543
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2622
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/parser/corpus/+commas.cue b/cue/parser/corpus/+commas.cue
new file mode 100644
index 0000000..29ef356
--- /dev/null
+++ b/cue/parser/corpus/+commas.cue
@@ -0,0 +1,18 @@
+package foo
+
+import "path/to/pkg"
+import name "path/to/pkg"
+import . "path/to/pkg"
+import      /* ERROR "expected 'STRING', found newline" */
+import err  /* ERROR "expected 'STRING', found newline" */
+
+foo: [
+	0 // legal JSON
+]
+
+bar: [
+	0,
+	1,
+	2,
+	3
+]
diff --git a/cue/parser/corpus/+comp.cue b/cue/parser/corpus/+comp.cue
new file mode 100644
index 0000000..9c46893
--- /dev/null
+++ b/cue/parser/corpus/+comp.cue
@@ -0,0 +1,3 @@
+"\(x)": y for x, y in {a: 1, b: 2}
+
+z: [ x for x in [1, 2, 3] ]
diff --git a/cue/parser/corpus/+data.cue b/cue/parser/corpus/+data.cue
new file mode 100644
index 0000000..e226892
--- /dev/null
+++ b/cue/parser/corpus/+data.cue
@@ -0,0 +1,3 @@
+package hello
+
+who: "World"
\ No newline at end of file
diff --git a/cue/parser/corpus/+hello.cue b/cue/parser/corpus/+hello.cue
new file mode 100644
index 0000000..b3a6f2d
--- /dev/null
+++ b/cue/parser/corpus/+hello.cue
@@ -0,0 +1,14 @@
+package hello
+
+command echo: {
+    task echo: {
+        kind:   "exec"
+        cmd:    "echo \(message)"
+        stdout: string
+    }
+
+    task display: {
+        kind: "print"
+        text: task.echo.stdout
+    }
+}
\ No newline at end of file
diff --git a/cue/parser/corpus/+import.cue b/cue/parser/corpus/+import.cue
new file mode 100644
index 0000000..d86b5af
--- /dev/null
+++ b/cue/parser/corpus/+import.cue
@@ -0,0 +1,12 @@
+package foo
+
+import (
+	"time.com/now"
+)
+
+
+foo: {
+	bar: 3.4
+}
+
+a b c: [1, 2Gi, 3M]
diff --git a/cue/parser/corpus/+test.cue b/cue/parser/corpus/+test.cue
new file mode 100644
index 0000000..cf9ef15
--- /dev/null
+++ b/cue/parser/corpus/+test.cue
@@ -0,0 +1,4 @@
+import "math"
+
+foo: 1
+bar: "baz"
diff --git a/cue/parser/error_test.go b/cue/parser/error_test.go
index e434d05..887d855 100644
--- a/cue/parser/error_test.go
+++ b/cue/parser/error_test.go
@@ -182,3 +182,17 @@
 		}
 	}
 }
+
+func TestFuzz(t *testing.T) {
+	testCases := []string{
+		"(({\"\\(0)\"(",
+		"{{\"\\(0\xbf\"(",
+		"a:y for x n{b:\"\"(\"\\(" +
+			"\"\"\\\"(",
+	}
+	for _, tc := range testCases {
+		t.Run("", func(t *testing.T) {
+			_, _ = ParseFile("go-fuzz", []byte(tc))
+		})
+	}
+}
diff --git a/cue/parser/fuzz.go b/cue/parser/fuzz.go
new file mode 100644
index 0000000..76d9ff0
--- /dev/null
+++ b/cue/parser/fuzz.go
@@ -0,0 +1,25 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// +build gofuzz
+
+package parser
+
+func Fuzz(b []byte) int {
+	_, err := ParseFile("go-fuzz", b)
+	if err != nil {
+		return 0
+	}
+	return 1
+}
diff --git a/cue/parser/parser.go b/cue/parser/parser.go
index 404d830..474c8f1 100644
--- a/cue/parser/parser.go
+++ b/cue/parser/parser.go
@@ -147,7 +147,10 @@
 	switch c.isList--; {
 	case c.isList < 0:
 		if !p.panicking {
-			panic("unmatched close list")
+			err := errors.Newf(p.pos, "unmatched close list")
+			p.errors = errors.Append(p.errors, err)
+			p.panicking = true
+			panic(err)
 		}
 	case c.isList == 0:
 		parent := c.parent
@@ -160,7 +163,10 @@
 func (c *commentState) closeNode(p *parser, n ast.Node) ast.Node {
 	if p.comments != c {
 		if !p.panicking {
-			panic("unmatched comments")
+			err := errors.Newf(p.pos, "unmatched comments")
+			p.errors = errors.Append(p.errors, err)
+			p.panicking = true
+			panic(err)
 		}
 		return n
 	}