json: Detect and report extraneous data at EOF

This commit is contained in:
Martin Atkins 2017-05-17 07:59:41 -07:00
parent 0ef4932962
commit eca76d650c
2 changed files with 13 additions and 0 deletions

View File

@ -19,6 +19,14 @@ func parseFileContent(buf []byte, filename string) (node, zcl.Diagnostics) {
}) })
p := newPeeker(tokens) p := newPeeker(tokens)
node, diags := parseValue(p) node, diags := parseValue(p)
if len(diags) == 0 && p.Peek().Type != tokenEOF {
diags = diags.Append(&zcl.Diagnostic{
Severity: zcl.DiagError,
Summary: "Extraneous data after value",
Detail: "Extra characters appear after the JSON value.",
Subject: p.Peek().Range.Ptr(),
})
}
if diags.HasErrors() { if diags.HasErrors() {
// Don't return a node if there were errors during parsing. // Don't return a node if there were errors during parsing.
return nil, diags return nil, diags

View File

@ -188,6 +188,11 @@ func TestParse(t *testing.T) {
nil, nil,
1, 1,
}, },
{
`1 2`,
nil,
1,
},
// Objects // Objects
{ {