From aa13eab21fc415f16b4b376ffd57b17000ae64b1 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 20 May 2017 09:30:04 -0700 Subject: [PATCH] json: Don't suppress parser return value on errors Even if errors were encountered during parsing, it is helpful to still return a partial AST if possible since that allows for the source code analysis features to still (partially) work in the face of errors. --- zcl/json/parser.go | 4 ---- zcl/json/parser_test.go | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/zcl/json/parser.go b/zcl/json/parser.go index 1c3924f..1272673 100644 --- a/zcl/json/parser.go +++ b/zcl/json/parser.go @@ -27,10 +27,6 @@ func parseFileContent(buf []byte, filename string) (node, zcl.Diagnostics) { Subject: p.Peek().Range.Ptr(), }) } - if diags.HasErrors() { - // Don't return a node if there were errors during parsing. - return nil, diags - } return node, diags } diff --git a/zcl/json/parser_test.go b/zcl/json/parser_test.go index 8bb7a93..62e5fd0 100644 --- a/zcl/json/parser_test.go +++ b/zcl/json/parser_test.go @@ -190,7 +190,13 @@ func TestParse(t *testing.T) { }, { `1 2`, - nil, + &numberVal{ + Value: mustBigFloat("1"), + SrcRange: zcl.Range{ + Start: zcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: zcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + }, 1, }, @@ -306,7 +312,32 @@ func TestParse(t *testing.T) { }, { `{"hello": true, "hello": true}`, - nil, + &objectVal{ + Attrs: map[string]*objectAttr{ + "hello": { + Name: "hello", + Value: &booleanVal{ + Value: true, + SrcRange: zcl.Range{ + Start: zcl.Pos{Line: 1, Column: 26, Byte: 25}, + End: zcl.Pos{Line: 1, Column: 30, Byte: 29}, + }, + }, + NameRange: zcl.Range{ + Start: zcl.Pos{Line: 1, Column: 17, Byte: 16}, + End: zcl.Pos{Line: 1, Column: 24, Byte: 23}, + }, + }, + }, + SrcRange: zcl.Range{ + Start: zcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: zcl.Pos{Line: 1, Column: 31, Byte: 30}, + }, + OpenRange: zcl.Range{ + Start: zcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: zcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + }, 1, }, {