hcl/json/parse.go
2015-11-06 16:48:00 -08:00

41 lines
770 B
Go

package json
import (
"sync"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/hcl/ast"
)
// jsonErrors are the errors built up from parsing. These should not
// be accessed directly.
var jsonErrors []error
var jsonLock sync.Mutex
var jsonResult *ast.File
// Parse parses the given string and returns the result.
func Parse(v string) (*ast.File, error) {
jsonLock.Lock()
defer jsonLock.Unlock()
jsonErrors = nil
jsonResult = nil
// Parse
lex := &jsonLex{Input: v}
jsonParse(lex)
// If we have an error in the lexer itself, return it
if lex.err != nil {
return nil, lex.err
}
// Build up the errors
var err error
if len(jsonErrors) > 0 {
err = &multierror.Error{Errors: jsonErrors}
jsonResult = nil
}
return jsonResult, err
}