Skip to content

Commit

Permalink
json: Add ParseExpression function
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed May 24, 2020
1 parent 50eda8b commit d55b62d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ func parseFileContent(buf []byte, filename string) (node, hcl.Diagnostics) {
return node, diags
}

func parseExpression(buf []byte, filename string, start hcl.Pos) (node, hcl.Diagnostics) {
tokens := scan(buf, pos{Filename: filename, Pos: start})
p := newPeeker(tokens)
node, diags := parseValue(p)
if len(diags) == 0 && p.Peek().Type != tokenEOF {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Extraneous data after value",
Detail: "Extra characters appear after the JSON value.",
Subject: p.Peek().Range.Ptr(),
})
}
return node, diags
}

func parseValue(p *peeker) (node, hcl.Diagnostics) {
tok := p.Peek()

Expand Down
7 changes: 7 additions & 0 deletions json/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func Parse(src []byte, filename string) (*hcl.File, hcl.Diagnostics) {
return file, diags
}

// ParseExpression parses the given buffer as a standalone JSON expression,
// returning it as an instance of Expression.
func ParseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression, hcl.Diagnostics) {
node, diags := parseExpression(src, filename, start)
return &expression{src: node}, diags
}

// ParseFile is a convenience wrapper around Parse that first attempts to load
// data from the given filename, passing the result to Parse if successful.
//
Expand Down

0 comments on commit d55b62d

Please sign in to comment.