1package json
2
3import (
4	"sync"
5
6	"github.com/yudai/hcl/hcl"
7	"github.com/hashicorp/go-multierror"
8)
9
10// jsonErrors are the errors built up from parsing. These should not
11// be accessed directly.
12var jsonErrors []error
13var jsonLock sync.Mutex
14var jsonResult *hcl.Object
15
16// Parse parses the given string and returns the result.
17func Parse(v string) (*hcl.Object, error) {
18	jsonLock.Lock()
19	defer jsonLock.Unlock()
20	jsonErrors = nil
21	jsonResult = nil
22
23	// Parse
24	lex := &jsonLex{Input: v}
25	jsonParse(lex)
26
27	// If we have an error in the lexer itself, return it
28	if lex.err != nil {
29		return nil, lex.err
30	}
31
32	// Build up the errors
33	var err error
34	if len(jsonErrors) > 0 {
35		err = &multierror.Error{Errors: jsonErrors}
36		jsonResult = nil
37	}
38
39	return jsonResult, err
40}
41