From 708abb8c9750d6555ca45d830c336ac1d9e7e465 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 11 Sep 2017 16:40:37 -0700 Subject: [PATCH] Move the zcl package and its two parsing subpackages to "hcl" names This is a super-invasive update since the "zcl" package in particular is referenced all over. There are probably still a few zcl references hanging around in comments, etc but this takes care of most of it. --- cmd/hclfmt/main.go | 6 +- ext/include/file_resolver.go | 8 +- ext/include/map_resolver.go | 10 +- ext/include/resolver.go | 10 +- ext/include/transformer.go | 22 +- ext/include/transformer_test.go | 38 +- ext/transform/error.go | 36 +- ext/transform/transform.go | 26 +- ext/transform/transform_test.go | 32 +- ext/transform/transformer.go | 10 +- ext/userfunc/README.md | 2 +- ext/userfunc/decode.go | 18 +- ext/userfunc/decode_test.go | 16 +- ext/userfunc/public.go | 6 +- gohcl/decode.go | 38 +- gohcl/decode_test.go | 28 +- gohcl/doc.go | 6 +- gohcl/schema.go | 16 +- gohcl/schema_test.go | 46 +- gohcl/types.go | 12 +- {zcl => hcl}/diagnostic.go | 4 +- {zcl => hcl}/diagnostic_text.go | 2 +- {zcl => hcl}/diagnostic_text_test.go | 2 +- {zcl => hcl}/didyoumean.go | 2 +- hcl/doc.go | 1 + {zcl => hcl}/eval_context.go | 2 +- .../zclsyntax => hcl/hclsyntax}/didyoumean.go | 2 +- .../hclsyntax}/didyoumean_test.go | 2 +- {zcl/zclsyntax => hcl/hclsyntax}/doc.go | 2 +- .../zclsyntax => hcl/hclsyntax}/expression.go | 268 +-- .../hclsyntax}/expression_ops.go | 44 +- .../hclsyntax}/expression_template.go | 44 +- .../hclsyntax}/expression_template_test.go | 10 +- .../hclsyntax}/expression_test.go | 92 +- hcl/hclsyntax/expression_vars.go | 72 + .../hclsyntax}/expression_vars_gen.go | 2 +- {zcl/zclsyntax => hcl/hclsyntax}/file.go | 10 +- {zcl/zclsyntax => hcl/hclsyntax}/generate.go | 2 +- {zcl/zclsyntax => hcl/hclsyntax}/keywords.go | 2 +- .../zclsyntax => hcl/hclsyntax}/navigation.go | 2 +- {zcl/zclsyntax => hcl/hclsyntax}/node.go | 6 +- hcl/hclsyntax/parse_traversal_test.go | 227 +++ {zcl/zclsyntax => hcl/hclsyntax}/parser.go | 374 ++-- .../hclsyntax}/parser_template.go | 156 +- hcl/hclsyntax/parser_test.go | 823 +++++++++ .../hclsyntax}/parser_traversal.go | 70 +- {zcl/zclsyntax => hcl/hclsyntax}/peeker.go | 8 +- .../hclsyntax}/peeker_test.go | 2 +- {zcl/zclsyntax => hcl/hclsyntax}/public.go | 28 +- .../hclsyntax}/scan_tokens.go | 6 +- .../hclsyntax}/scan_tokens.rl | 2 +- hcl/hclsyntax/scan_tokens_test.go | 1506 +++++++++++++++++ {zcl/zclsyntax => hcl/hclsyntax}/spec.md | 2 +- {zcl/zclsyntax => hcl/hclsyntax}/structure.go | 114 +- .../hclsyntax}/structure_test.go | 166 +- {zcl/zclsyntax => hcl/hclsyntax}/token.go | 42 +- .../hclsyntax}/token_type_string.go | 2 +- .../hclsyntax}/unicode2ragel.rb | 0 .../hclsyntax}/unicode_derived.rl | 0 {zcl/zclsyntax => hcl/hclsyntax}/variables.go | 18 +- .../hclsyntax}/variables_test.go | 94 +- {zcl/zclsyntax => hcl/hclsyntax}/walk.go | 20 +- hcl/json/ast.go | 121 ++ {zcl => hcl}/json/didyoumean.go | 0 {zcl => hcl}/json/didyoumean_test.go | 0 {zcl => hcl}/json/doc.go | 2 +- {zcl => hcl}/json/navigation.go | 0 {zcl => hcl}/json/navigation_test.go | 0 {zcl => hcl}/json/parser.go | 130 +- hcl/json/parser_test.go | 609 +++++++ {zcl => hcl}/json/peeker.go | 0 {zcl => hcl}/json/public.go | 30 +- {zcl => hcl}/json/public_test.go | 8 +- {zcl => hcl}/json/scanner.go | 14 +- {zcl => hcl}/json/scanner_test.go | 274 +-- {zcl => hcl}/json/spec.md | 2 +- {zcl => hcl}/json/structure.go | 76 +- {zcl => hcl}/json/structure_test.go | 336 ++-- {zcl => hcl}/json/tokentype_string.go | 0 {zcl => hcl}/merged.go | 2 +- {zcl => hcl}/merged_test.go | 2 +- {zcl => hcl}/ops.go | 2 +- {zcl => hcl}/pos.go | 2 +- {zcl => hcl}/schema.go | 2 +- {zcl => hcl}/spec.md | 2 +- {zcl => hcl}/structure.go | 2 +- {zcl => hcl}/traversal.go | 2 +- hcldec/decode.go | 12 +- hcldec/public.go | 8 +- hcldec/public_test.go | 32 +- hcldec/schema.go | 12 +- hcldec/spec.go | 128 +- hcldec/variables.go | 6 +- hcldec/variables_test.go | 56 +- hcled/navigation.go | 4 +- hclparse/parser.go | 32 +- hcltest/mock.go | 104 +- hcltest/mock_test.go | 172 +- hclwrite/ast.go | 6 +- hclwrite/ast_test.go | 14 +- hclwrite/format.go | 42 +- hclwrite/format_test.go | 76 +- hclwrite/native_node_sorter.go | 4 +- hclwrite/parser.go | 60 +- hclwrite/parser_test.go | 402 ++--- hclwrite/public.go | 4 +- hclwrite/round_trip_test.go | 10 +- hclwrite/tokens.go | 6 +- zcl/doc.go | 1 - zcl/json/ast.go | 121 -- zcl/json/parser_test.go | 609 ------- zcl/zclsyntax/expression_vars.go | 72 - zcl/zclsyntax/parse_traversal_test.go | 227 --- zcl/zclsyntax/parser_test.go | 823 --------- zcl/zclsyntax/scan_tokens_test.go | 1506 ----------------- 115 files changed, 5394 insertions(+), 5392 deletions(-) rename {zcl => hcl}/diagnostic.go (97%) rename {zcl => hcl}/diagnostic_text.go (99%) rename {zcl => hcl}/diagnostic_text_test.go (99%) rename {zcl => hcl}/didyoumean.go (98%) create mode 100644 hcl/doc.go rename {zcl => hcl}/eval_context.go (98%) rename {zcl/zclsyntax => hcl/hclsyntax}/didyoumean.go (97%) rename {zcl/zclsyntax => hcl/hclsyntax}/didyoumean_test.go (98%) rename {zcl/zclsyntax => hcl/hclsyntax}/doc.go (94%) rename {zcl/zclsyntax => hcl/hclsyntax}/expression.go (81%) rename {zcl/zclsyntax => hcl/hclsyntax}/expression_ops.go (86%) rename {zcl/zclsyntax => hcl/hclsyntax}/expression_template.go (80%) rename {zcl/zclsyntax => hcl/hclsyntax}/expression_template_test.go (96%) rename {zcl/zclsyntax => hcl/hclsyntax}/expression_test.go (94%) create mode 100755 hcl/hclsyntax/expression_vars.go rename {zcl/zclsyntax => hcl/hclsyntax}/expression_vars_gen.go (98%) rename {zcl/zclsyntax => hcl/hclsyntax}/file.go (66%) rename {zcl/zclsyntax => hcl/hclsyntax}/generate.go (95%) rename {zcl/zclsyntax => hcl/hclsyntax}/keywords.go (96%) rename {zcl/zclsyntax => hcl/hclsyntax}/navigation.go (97%) rename {zcl/zclsyntax => hcl/hclsyntax}/node.go (88%) create mode 100644 hcl/hclsyntax/parse_traversal_test.go rename {zcl/zclsyntax => hcl/hclsyntax}/parser.go (82%) rename {zcl/zclsyntax => hcl/hclsyntax}/parser_template.go (83%) create mode 100644 hcl/hclsyntax/parser_test.go rename {zcl/zclsyntax => hcl/hclsyntax}/parser_traversal.go (65%) rename {zcl/zclsyntax => hcl/hclsyntax}/peeker.go (94%) rename {zcl/zclsyntax => hcl/hclsyntax}/peeker_test.go (99%) rename {zcl/zclsyntax => hcl/hclsyntax}/public.go (82%) rename {zcl/zclsyntax => hcl/hclsyntax}/scan_tokens.go (99%) rename {zcl/zclsyntax => hcl/hclsyntax}/scan_tokens.rl (99%) create mode 100644 hcl/hclsyntax/scan_tokens_test.go rename {zcl/zclsyntax => hcl/hclsyntax}/spec.md (99%) rename {zcl/zclsyntax => hcl/hclsyntax}/structure.go (77%) rename {zcl/zclsyntax => hcl/hclsyntax}/structure_test.go (73%) rename {zcl/zclsyntax => hcl/hclsyntax}/token.go (90%) rename {zcl/zclsyntax => hcl/hclsyntax}/token_type_string.go (99%) rename {zcl/zclsyntax => hcl/hclsyntax}/unicode2ragel.rb (100%) rename {zcl/zclsyntax => hcl/hclsyntax}/unicode_derived.rl (100%) rename {zcl/zclsyntax => hcl/hclsyntax}/variables.go (85%) rename {zcl/zclsyntax => hcl/hclsyntax}/variables_test.go (66%) rename {zcl/zclsyntax => hcl/hclsyntax}/walk.go (83%) create mode 100644 hcl/json/ast.go rename {zcl => hcl}/json/didyoumean.go (100%) rename {zcl => hcl}/json/didyoumean_test.go (100%) rename {zcl => hcl}/json/doc.go (83%) rename {zcl => hcl}/json/navigation.go (100%) rename {zcl => hcl}/json/navigation_test.go (100%) rename {zcl => hcl}/json/parser.go (80%) create mode 100644 hcl/json/parser_test.go rename {zcl => hcl}/json/peeker.go (100%) rename {zcl => hcl}/json/public.go (82%) rename {zcl => hcl}/json/public_test.go (92%) rename {zcl => hcl}/json/scanner.go (97%) rename {zcl => hcl}/json/scanner_test.go (77%) rename {zcl => hcl}/json/spec.md (99%) rename {zcl => hcl}/json/structure.go (83%) rename {zcl => hcl}/json/structure_test.go (66%) rename {zcl => hcl}/json/tokentype_string.go (100%) rename {zcl => hcl}/merged.go (99%) rename {zcl => hcl}/merged_test.go (99%) rename {zcl => hcl}/ops.go (99%) rename {zcl => hcl}/pos.go (99%) rename {zcl => hcl}/schema.go (97%) rename {zcl => hcl}/spec.md (99%) rename {zcl => hcl}/structure.go (99%) rename {zcl => hcl}/traversal.go (99%) delete mode 100644 zcl/doc.go delete mode 100644 zcl/json/ast.go delete mode 100644 zcl/json/parser_test.go delete mode 100755 zcl/zclsyntax/expression_vars.go delete mode 100644 zcl/zclsyntax/parse_traversal_test.go delete mode 100644 zcl/zclsyntax/parser_test.go delete mode 100644 zcl/zclsyntax/scan_tokens_test.go diff --git a/cmd/hclfmt/main.go b/cmd/hclfmt/main.go index 9e2c494..12f89b0 100644 --- a/cmd/hclfmt/main.go +++ b/cmd/hclfmt/main.go @@ -9,9 +9,9 @@ import ( "os" "strings" + "github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hcl2/hclparse" "github.com/hashicorp/hcl2/hclwrite" - "github.com/hashicorp/hcl2/zcl" "golang.org/x/crypto/ssh/terminal" ) @@ -25,7 +25,7 @@ var ( ) var parser = hclparse.NewParser() -var diagWr zcl.DiagnosticWriter // initialized in init +var diagWr hcl.DiagnosticWriter // initialized in init var checkErrs = false var changed []string @@ -35,7 +35,7 @@ func init() { if err != nil { w = 80 } - diagWr = zcl.NewDiagnosticTextWriter(os.Stderr, parser.Files(), uint(w), color) + diagWr = hcl.NewDiagnosticTextWriter(os.Stderr, parser.Files(), uint(w), color) } func main() { diff --git a/ext/include/file_resolver.go b/ext/include/file_resolver.go index ad31929..fc929b3 100644 --- a/ext/include/file_resolver.go +++ b/ext/include/file_resolver.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/hashicorp/hcl2/hclparse" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // FileResolver creates and returns a Resolver that interprets include paths @@ -35,13 +35,13 @@ type fileResolver struct { Parser *hclparse.Parser } -func (r fileResolver) ResolveBodyPath(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) { +func (r fileResolver) ResolveBodyPath(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) { callerFile := filepath.Join(r.BaseDir, refRange.Filename) callerDir := filepath.Dir(callerFile) targetFile := filepath.Join(callerDir, path) - var f *zcl.File - var diags zcl.Diagnostics + var f *hcl.File + var diags hcl.Diagnostics if strings.HasSuffix(targetFile, ".json") { f, diags = r.Parser.ParseJSONFile(targetFile) } else { diff --git a/ext/include/map_resolver.go b/ext/include/map_resolver.go index 091a3db..31770b7 100644 --- a/ext/include/map_resolver.go +++ b/ext/include/map_resolver.go @@ -3,7 +3,7 @@ package include import ( "fmt" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // MapResolver returns a Resolver that consults the given map for preloaded @@ -11,15 +11,15 @@ import ( // // An error diagnostic is returned if a path is requested that does not appear // as a key in the given map. -func MapResolver(m map[string]zcl.Body) Resolver { - return ResolverFunc(func(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) { +func MapResolver(m map[string]hcl.Body) Resolver { + return ResolverFunc(func(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) { if body, ok := m[path]; ok { return body, nil } - return nil, zcl.Diagnostics{ + return nil, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Invalid include path", Detail: fmt.Sprintf("The include path %q is not recognized.", path), Subject: &refRange, diff --git a/ext/include/resolver.go b/ext/include/resolver.go index 13b0d9f..c477c1f 100644 --- a/ext/include/resolver.go +++ b/ext/include/resolver.go @@ -1,11 +1,11 @@ package include import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // A Resolver maps an include path (an arbitrary string, but usually something -// filepath-like) to a zcl.Body. +// filepath-like) to a hcl.Body. // // The parameter "refRange" is the source range of the expression in the calling // body that provided the given path, for use in generating "invalid path"-type @@ -16,13 +16,13 @@ import ( // Any returned diagnostics will be emitted when content is requested from the // final composed body (after all includes have been dealt with). type Resolver interface { - ResolveBodyPath(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) + ResolveBodyPath(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) } // ResolverFunc is a function type that implements Resolver. -type ResolverFunc func(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) +type ResolverFunc func(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) // ResolveBodyPath is an implementation of Resolver.ResolveBodyPath. -func (f ResolverFunc) ResolveBodyPath(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) { +func (f ResolverFunc) ResolveBodyPath(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) { return f(path, refRange) } diff --git a/ext/include/transformer.go b/ext/include/transformer.go index 03664b3..15c513f 100644 --- a/ext/include/transformer.go +++ b/ext/include/transformer.go @@ -3,7 +3,7 @@ package include import ( "github.com/hashicorp/hcl2/ext/transform" "github.com/hashicorp/hcl2/gohcl" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // Transformer builds a transformer that finds any "include" blocks in a body @@ -30,10 +30,10 @@ import ( // // "body" will now have includes resolved in its own content and that // // of any descendent blocks. // -func Transformer(blockType string, ctx *zcl.EvalContext, resolver Resolver) transform.Transformer { +func Transformer(blockType string, ctx *hcl.EvalContext, resolver Resolver) transform.Transformer { return &transformer{ - Schema: &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + Schema: &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: blockType, }, @@ -45,12 +45,12 @@ func Transformer(blockType string, ctx *zcl.EvalContext, resolver Resolver) tran } type transformer struct { - Schema *zcl.BodySchema - Ctx *zcl.EvalContext + Schema *hcl.BodySchema + Ctx *hcl.EvalContext Resolver Resolver } -func (t *transformer) TransformBody(in zcl.Body) zcl.Body { +func (t *transformer) TransformBody(in hcl.Body) hcl.Body { content, remain, diags := in.PartialContent(t.Schema) if content == nil || len(content.Blocks) == 0 { @@ -58,7 +58,7 @@ func (t *transformer) TransformBody(in zcl.Body) zcl.Body { return transform.BodyWithDiagnostics(remain, diags) } - bodies := make([]zcl.Body, 1, len(content.Blocks)+1) + bodies := make([]hcl.Body, 1, len(content.Blocks)+1) bodies[0] = remain // content in "remain" takes priority over includes for _, block := range content.Blocks { incContent, incDiags := block.Body.Content(includeBlockSchema) @@ -79,11 +79,11 @@ func (t *transformer) TransformBody(in zcl.Body) zcl.Body { bodies = append(bodies, transform.BodyWithDiagnostics(incBody, incDiags)) } - return zcl.MergeBodies(bodies) + return hcl.MergeBodies(bodies) } -var includeBlockSchema = &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ +var includeBlockSchema = &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "path", Required: true, diff --git a/ext/include/transformer_test.go b/ext/include/transformer_test.go index 0edcf4f..99a344b 100644 --- a/ext/include/transformer_test.go +++ b/ext/include/transformer_test.go @@ -7,33 +7,33 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/hashicorp/hcl2/gohcl" "github.com/hashicorp/hcl2/hcltest" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) func TestTransformer(t *testing.T) { - caller := hcltest.MockBody(&zcl.BodyContent{ - Blocks: zcl.Blocks{ + caller := hcltest.MockBody(&hcl.BodyContent{ + Blocks: hcl.Blocks{ { Type: "include", - Body: hcltest.MockBody(&zcl.BodyContent{ - Attributes: hcltest.MockAttrs(map[string]zcl.Expression{ + Body: hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ "path": hcltest.MockExprVariable("var_path"), }), }), }, { Type: "include", - Body: hcltest.MockBody(&zcl.BodyContent{ - Attributes: hcltest.MockAttrs(map[string]zcl.Expression{ + Body: hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ "path": hcltest.MockExprLiteral(cty.StringVal("include2")), }), }), }, { Type: "foo", - Body: hcltest.MockBody(&zcl.BodyContent{ - Attributes: hcltest.MockAttrs(map[string]zcl.Expression{ + Body: hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ "from": hcltest.MockExprLiteral(cty.StringVal("caller")), }), }), @@ -41,25 +41,25 @@ func TestTransformer(t *testing.T) { }, }) - resolver := MapResolver(map[string]zcl.Body{ - "include1": hcltest.MockBody(&zcl.BodyContent{ - Blocks: zcl.Blocks{ + resolver := MapResolver(map[string]hcl.Body{ + "include1": hcltest.MockBody(&hcl.BodyContent{ + Blocks: hcl.Blocks{ { Type: "foo", - Body: hcltest.MockBody(&zcl.BodyContent{ - Attributes: hcltest.MockAttrs(map[string]zcl.Expression{ + Body: hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ "from": hcltest.MockExprLiteral(cty.StringVal("include1")), }), }), }, }, }), - "include2": hcltest.MockBody(&zcl.BodyContent{ - Blocks: zcl.Blocks{ + "include2": hcltest.MockBody(&hcl.BodyContent{ + Blocks: hcl.Blocks{ { Type: "foo", - Body: hcltest.MockBody(&zcl.BodyContent{ - Attributes: hcltest.MockAttrs(map[string]zcl.Expression{ + Body: hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ "from": hcltest.MockExprLiteral(cty.StringVal("include2")), }), }), @@ -68,7 +68,7 @@ func TestTransformer(t *testing.T) { }), }) - ctx := &zcl.EvalContext{ + ctx := &hcl.EvalContext{ Variables: map[string]cty.Value{ "var_path": cty.StringVal("include1"), }, diff --git a/ext/transform/error.go b/ext/transform/error.go index e21024c..d7de418 100644 --- a/ext/transform/error.go +++ b/ext/transform/error.go @@ -1,20 +1,20 @@ package transform import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) -// NewErrorBody returns a zcl.Body that returns the given diagnostics whenever +// NewErrorBody returns a hcl.Body that returns the given diagnostics whenever // any of its content-access methods are called. // // The given diagnostics must have at least one diagnostic of severity -// zcl.DiagError, or this function will panic. +// hcl.DiagError, or this function will panic. // // This can be used to prepare a return value for a Transformer that // can't complete due to an error. While the transform itself will succeed, // the error will be returned as soon as a caller attempts to extract content // from the resulting body. -func NewErrorBody(diags zcl.Diagnostics) zcl.Body { +func NewErrorBody(diags hcl.Diagnostics) hcl.Body { if !diags.HasErrors() { panic("NewErrorBody called without any error diagnostics") } @@ -23,7 +23,7 @@ func NewErrorBody(diags zcl.Diagnostics) zcl.Body { } } -// BodyWithDiagnostics returns a zcl.Body that wraps another zcl.Body +// BodyWithDiagnostics returns a hcl.Body that wraps another hcl.Body // and emits the given diagnostics for any content-extraction method. // // Unlike the result of NewErrorBody, a body with diagnostics still runs @@ -36,7 +36,7 @@ func NewErrorBody(diags zcl.Diagnostics) zcl.Body { // This function is intended for conveniently reporting errors and/or warnings // produced during a transform, ensuring that they will be seen when the // caller eventually extracts content from the returned body. -func BodyWithDiagnostics(body zcl.Body, diags zcl.Diagnostics) zcl.Body { +func BodyWithDiagnostics(body hcl.Body, diags hcl.Diagnostics) hcl.Body { if len(diags) == 0 { // nothing to do! return body @@ -49,60 +49,60 @@ func BodyWithDiagnostics(body zcl.Body, diags zcl.Diagnostics) zcl.Body { } type diagBody struct { - Diags zcl.Diagnostics - Wrapped zcl.Body + Diags hcl.Diagnostics + Wrapped hcl.Body } -func (b diagBody) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostics) { +func (b diagBody) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) { if b.Diags.HasErrors() { return b.emptyContent(), b.Diags } content, wrappedDiags := b.Wrapped.Content(schema) - diags := make(zcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags)) + diags := make(hcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags)) diags = append(diags, b.Diags...) diags = append(diags, wrappedDiags...) return content, diags } -func (b diagBody) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Body, zcl.Diagnostics) { +func (b diagBody) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) { if b.Diags.HasErrors() { return b.emptyContent(), b.Wrapped, b.Diags } content, remain, wrappedDiags := b.Wrapped.PartialContent(schema) - diags := make(zcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags)) + diags := make(hcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags)) diags = append(diags, b.Diags...) diags = append(diags, wrappedDiags...) return content, remain, diags } -func (b diagBody) JustAttributes() (zcl.Attributes, zcl.Diagnostics) { +func (b diagBody) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { if b.Diags.HasErrors() { return nil, b.Diags } attributes, wrappedDiags := b.Wrapped.JustAttributes() - diags := make(zcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags)) + diags := make(hcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags)) diags = append(diags, b.Diags...) diags = append(diags, wrappedDiags...) return attributes, diags } -func (b diagBody) MissingItemRange() zcl.Range { +func (b diagBody) MissingItemRange() hcl.Range { if b.Wrapped != nil { return b.Wrapped.MissingItemRange() } // Placeholder. This should never be seen in practice because decoding // a diagBody without a wrapped body should always produce an error. - return zcl.Range{ + return hcl.Range{ Filename: "", } } -func (b diagBody) emptyContent() *zcl.BodyContent { - return &zcl.BodyContent{ +func (b diagBody) emptyContent() *hcl.BodyContent { + return &hcl.BodyContent{ MissingItemRange: b.MissingItemRange(), } } diff --git a/ext/transform/transform.go b/ext/transform/transform.go index 0c683ba..30e9eed 100644 --- a/ext/transform/transform.go +++ b/ext/transform/transform.go @@ -1,12 +1,12 @@ package transform import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // Shallow is equivalent to calling transformer.TransformBody(body), and // is provided only for completeness of the top-level API. -func Shallow(body zcl.Body, transformer Transformer) zcl.Body { +func Shallow(body hcl.Body, transformer Transformer) hcl.Body { return transformer.TransformBody(body) } @@ -19,36 +19,36 @@ func Shallow(body zcl.Body, transformer Transformer) zcl.Body { // // Due to the wrapping behavior, the body resulting from this function // will not be of the type returned by the transformer. Callers may call -// only the methods defined for interface zcl.Body, and may not type-assert +// only the methods defined for interface hcl.Body, and may not type-assert // to access other methods. -func Deep(body zcl.Body, transformer Transformer) zcl.Body { +func Deep(body hcl.Body, transformer Transformer) hcl.Body { return deepWrapper{ Transformed: transformer.TransformBody(body), Transformer: transformer, } } -// deepWrapper is a zcl.Body implementation that ensures that a given +// deepWrapper is a hcl.Body implementation that ensures that a given // transformer is applied to another given body when content is extracted, // and that it recursively applies to any child blocks that are extracted. type deepWrapper struct { - Transformed zcl.Body + Transformed hcl.Body Transformer Transformer } -func (w deepWrapper) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostics) { +func (w deepWrapper) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) { content, diags := w.Transformed.Content(schema) content = w.transformContent(content) return content, diags } -func (w deepWrapper) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Body, zcl.Diagnostics) { +func (w deepWrapper) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) { content, remain, diags := w.Transformed.PartialContent(schema) content = w.transformContent(content) return content, remain, diags } -func (w deepWrapper) transformContent(content *zcl.BodyContent) *zcl.BodyContent { +func (w deepWrapper) transformContent(content *hcl.BodyContent) *hcl.BodyContent { if len(content.Blocks) == 0 { // Easy path: if there are no blocks then there are no child bodies to wrap return content @@ -57,10 +57,10 @@ func (w deepWrapper) transformContent(content *zcl.BodyContent) *zcl.BodyContent // Since we're going to change things here, we'll be polite and clone the // structure so that we don't risk impacting any internal state of the // original body. - ret := &zcl.BodyContent{ + ret := &hcl.BodyContent{ Attributes: content.Attributes, MissingItemRange: content.MissingItemRange, - Blocks: make(zcl.Blocks, len(content.Blocks)), + Blocks: make(hcl.Blocks, len(content.Blocks)), } for i, givenBlock := range content.Blocks { @@ -73,11 +73,11 @@ func (w deepWrapper) transformContent(content *zcl.BodyContent) *zcl.BodyContent return ret } -func (w deepWrapper) JustAttributes() (zcl.Attributes, zcl.Diagnostics) { +func (w deepWrapper) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { // Attributes can't have bodies or nested blocks, so this is just a thin wrapper. return w.Transformed.JustAttributes() } -func (w deepWrapper) MissingItemRange() zcl.Range { +func (w deepWrapper) MissingItemRange() hcl.Range { return w.Transformed.MissingItemRange() } diff --git a/ext/transform/transform_test.go b/ext/transform/transform_test.go index 718b482..2434418 100644 --- a/ext/transform/transform_test.go +++ b/ext/transform/transform_test.go @@ -6,18 +6,18 @@ import ( "reflect" "github.com/hashicorp/hcl2/hcltest" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) // Assert that deepWrapper implements Body -var deepWrapperIsBody zcl.Body = deepWrapper{} +var deepWrapperIsBody hcl.Body = deepWrapper{} func TestDeep(t *testing.T) { - testTransform := TransformerFunc(func(body zcl.Body) zcl.Body { - _, remain, diags := body.PartialContent(&zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + testTransform := TransformerFunc(func(body hcl.Body) hcl.Body { + _, remain, diags := body.PartialContent(&hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "remove", }, @@ -27,19 +27,19 @@ func TestDeep(t *testing.T) { return BodyWithDiagnostics(remain, diags) }) - src := hcltest.MockBody(&zcl.BodyContent{ - Attributes: hcltest.MockAttrs(map[string]zcl.Expression{ + src := hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcltest.MockAttrs(map[string]hcl.Expression{ "true": hcltest.MockExprLiteral(cty.True), }), - Blocks: []*zcl.Block{ + Blocks: []*hcl.Block{ { Type: "remove", - Body: zcl.EmptyBody(), + Body: hcl.EmptyBody(), }, { Type: "child", - Body: hcltest.MockBody(&zcl.BodyContent{ - Blocks: []*zcl.Block{ + Body: hcltest.MockBody(&hcl.BodyContent{ + Blocks: []*hcl.Block{ { Type: "remove", }, @@ -51,13 +51,13 @@ func TestDeep(t *testing.T) { wrapped := Deep(src, testTransform) - rootContent, diags := wrapped.Content(&zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + rootContent, diags := wrapped.Content(&hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "true", }, }, - Blocks: []zcl.BlockHeaderSchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "child", }, @@ -70,7 +70,7 @@ func TestDeep(t *testing.T) { } } - wantAttrs := hcltest.MockAttrs(map[string]zcl.Expression{ + wantAttrs := hcltest.MockAttrs(map[string]hcl.Expression{ "true": hcltest.MockExprLiteral(cty.True), }) if !reflect.DeepEqual(rootContent.Attributes, wantAttrs) { @@ -85,7 +85,7 @@ func TestDeep(t *testing.T) { } childBlock := rootContent.Blocks[0] - childContent, diags := childBlock.Body.Content(&zcl.BodySchema{}) + childContent, diags := childBlock.Body.Content(&hcl.BodySchema{}) if len(diags) != 0 { t.Errorf("unexpected diagnostics for child content") for _, diag := range diags { diff --git a/ext/transform/transformer.go b/ext/transform/transformer.go index d88c128..7a00d3a 100644 --- a/ext/transform/transformer.go +++ b/ext/transform/transformer.go @@ -1,7 +1,7 @@ package transform import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // A Transformer takes a given body, applies some (possibly no-op) @@ -13,14 +13,14 @@ import ( // returns diagnostics when its methods are called. NewErrorBody is a utility // to help with this. type Transformer interface { - TransformBody(zcl.Body) zcl.Body + TransformBody(hcl.Body) hcl.Body } // TransformerFunc is a function type that implements Transformer. -type TransformerFunc func(zcl.Body) zcl.Body +type TransformerFunc func(hcl.Body) hcl.Body // TransformBody is an implementation of Transformer.TransformBody. -func (f TransformerFunc) TransformBody(in zcl.Body) zcl.Body { +func (f TransformerFunc) TransformBody(in hcl.Body) hcl.Body { return f(in) } @@ -32,7 +32,7 @@ func Chain(c []Transformer) Transformer { return chain(c) } -func (c chain) TransformBody(body zcl.Body) zcl.Body { +func (c chain) TransformBody(body hcl.Body) hcl.Body { for _, t := range c { body = t.TransformBody(body) } diff --git a/ext/userfunc/README.md b/ext/userfunc/README.md index 71f1b8a..c9e353e 100644 --- a/ext/userfunc/README.md +++ b/ext/userfunc/README.md @@ -15,7 +15,7 @@ function "add" { The extension is implemented as a pre-processor for `cty.Body` objects. Given a body that may contain functions, the `DecodeUserFunctions` function searches for blocks that define functions and returns a functions map suitable for -inclusion in a `zcl.EvalContext`. It also returns a new `cty.Body` that +inclusion in a `hcl.EvalContext`. It also returns a new `cty.Body` that contains the remainder of the content from the given body, allowing for further processing of remaining content. diff --git a/ext/userfunc/decode.go b/ext/userfunc/decode.go index 8e477c5..375a1ea 100644 --- a/ext/userfunc/decode.go +++ b/ext/userfunc/decode.go @@ -2,13 +2,13 @@ package userfunc import ( "github.com/hashicorp/hcl2/gohcl" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/function" ) -var funcBodySchema = &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ +var funcBodySchema = &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "params", Required: true, @@ -24,9 +24,9 @@ var funcBodySchema = &zcl.BodySchema{ }, } -func decodeUserFunctions(body zcl.Body, blockType string, contextFunc ContextFunc) (funcs map[string]function.Function, remain zcl.Body, diags zcl.Diagnostics) { - schema := &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ +func decodeUserFunctions(body hcl.Body, blockType string, contextFunc ContextFunc) (funcs map[string]function.Function, remain hcl.Body, diags hcl.Diagnostics) { + schema := &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: blockType, LabelNames: []string{"name"}, @@ -42,8 +42,8 @@ func decodeUserFunctions(body zcl.Body, blockType string, contextFunc ContextFun // first call to getBaseCtx will populate context, and then the same // context will be used for all subsequent calls. It's assumed that // all functions in a given body should see an identical context. - var baseCtx *zcl.EvalContext - getBaseCtx := func() *zcl.EvalContext { + var baseCtx *hcl.EvalContext + getBaseCtx := func() *hcl.EvalContext { if baseCtx == nil { if contextFunc != nil { baseCtx = contextFunc() @@ -64,7 +64,7 @@ func decodeUserFunctions(body zcl.Body, blockType string, contextFunc ContextFun paramsExpr := funcContent.Attributes["params"].Expr resultExpr := funcContent.Attributes["result"].Expr - var varParamExpr zcl.Expression + var varParamExpr hcl.Expression if funcContent.Attributes["variadic_param"] != nil { varParamExpr = funcContent.Attributes["variadic_param"].Expr } diff --git a/ext/userfunc/decode_test.go b/ext/userfunc/decode_test.go index 00be2b1..09d1d07 100644 --- a/ext/userfunc/decode_test.go +++ b/ext/userfunc/decode_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/hashicorp/hcl2/zcl" - "github.com/hashicorp/hcl2/zcl/zclsyntax" + "github.com/hashicorp/hcl2/hcl/hclsyntax" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) @@ -13,7 +13,7 @@ func TestDecodeUserFunctions(t *testing.T) { tests := []struct { src string testExpr string - baseCtx *zcl.EvalContext + baseCtx *hcl.EvalContext want cty.Value diagCount int }{ @@ -98,7 +98,7 @@ function "closure" { } `, `closure()`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "upvalue": cty.True, }, @@ -138,23 +138,23 @@ function "neg" { for i, test := range tests { t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) { - f, diags := zclsyntax.ParseConfig([]byte(test.src), "config", zcl.Pos{Line: 1, Column: 1}) + f, diags := hclsyntax.ParseConfig([]byte(test.src), "config", hcl.Pos{Line: 1, Column: 1}) if f == nil || f.Body == nil { t.Fatalf("got nil file or body") } - funcs, _, funcsDiags := decodeUserFunctions(f.Body, "function", func() *zcl.EvalContext { + funcs, _, funcsDiags := decodeUserFunctions(f.Body, "function", func() *hcl.EvalContext { return test.baseCtx }) diags = append(diags, funcsDiags...) - expr, exprParseDiags := zclsyntax.ParseExpression([]byte(test.testExpr), "testexpr", zcl.Pos{Line: 1, Column: 1}) + expr, exprParseDiags := hclsyntax.ParseExpression([]byte(test.testExpr), "testexpr", hcl.Pos{Line: 1, Column: 1}) diags = append(diags, exprParseDiags...) if expr == nil { t.Fatalf("parsing test expr returned nil") } - got, exprDiags := expr.Value(&zcl.EvalContext{ + got, exprDiags := expr.Value(&hcl.EvalContext{ Functions: funcs, }) diags = append(diags, exprDiags...) diff --git a/ext/userfunc/public.go b/ext/userfunc/public.go index 413e9cc..866cea0 100644 --- a/ext/userfunc/public.go +++ b/ext/userfunc/public.go @@ -1,7 +1,7 @@ package userfunc import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty/function" ) @@ -17,7 +17,7 @@ import ( // configuration language, but more complex applications may use different // contexts to support lexical scoping depending on where in a configuration // structure a function declaration is found, etc. -type ContextFunc func() *zcl.EvalContext +type ContextFunc func() *hcl.EvalContext // DecodeUserFunctions looks for blocks of the given type in the given body // and, for each one found, interprets it as a custom function definition. @@ -37,6 +37,6 @@ type ContextFunc func() *zcl.EvalContext // // If the returned diagnostics set has errors then the function map and // remain body may be nil or incomplete. -func DecodeUserFunctions(body zcl.Body, blockType string, context ContextFunc) (funcs map[string]function.Function, remain zcl.Body, diags zcl.Diagnostics) { +func DecodeUserFunctions(body hcl.Body, blockType string, context ContextFunc) (funcs map[string]function.Function, remain hcl.Body, diags hcl.Diagnostics) { return decodeUserFunctions(body, blockType, context) } diff --git a/gohcl/decode.go b/gohcl/decode.go index 6ef741a..7c9e18b 100644 --- a/gohcl/decode.go +++ b/gohcl/decode.go @@ -4,7 +4,7 @@ import ( "fmt" "reflect" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty/convert" "github.com/zclconf/go-cty/cty/gocty" ) @@ -25,7 +25,7 @@ import ( // are returned then the given value may have been partially-populated but // may still be accessed by a careful caller for static analysis and editor // integration use-cases. -func DecodeBody(body zcl.Body, ctx *zcl.EvalContext, val interface{}) zcl.Diagnostics { +func DecodeBody(body hcl.Body, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics { rv := reflect.ValueOf(val) if rv.Kind() != reflect.Ptr { panic(fmt.Sprintf("target value must be a pointer, not %s", rv.Type().String())) @@ -34,7 +34,7 @@ func DecodeBody(body zcl.Body, ctx *zcl.EvalContext, val interface{}) zcl.Diagno return decodeBodyToValue(body, ctx, rv.Elem()) } -func decodeBodyToValue(body zcl.Body, ctx *zcl.EvalContext, val reflect.Value) zcl.Diagnostics { +func decodeBodyToValue(body hcl.Body, ctx *hcl.EvalContext, val reflect.Value) hcl.Diagnostics { et := val.Type() switch et.Kind() { case reflect.Struct: @@ -46,12 +46,12 @@ func decodeBodyToValue(body zcl.Body, ctx *zcl.EvalContext, val reflect.Value) z } } -func decodeBodyToStruct(body zcl.Body, ctx *zcl.EvalContext, val reflect.Value) zcl.Diagnostics { +func decodeBodyToStruct(body hcl.Body, ctx *hcl.EvalContext, val reflect.Value) hcl.Diagnostics { schema, partial := ImpliedBodySchema(val.Interface()) - var content *zcl.BodyContent - var leftovers zcl.Body - var diags zcl.Diagnostics + var content *hcl.BodyContent + var leftovers hcl.Body + var diags hcl.Diagnostics if partial { content, leftovers, diags = body.PartialContent(schema) } else { @@ -117,8 +117,8 @@ func decodeBodyToStruct(body zcl.Body, ctx *zcl.EvalContext, val reflect.Value) } if len(blocks) > 1 && !isSlice { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Duplicate %s block", typeName), Detail: fmt.Sprintf( "Only one %s block is allowed. Another was defined at %s.", @@ -133,8 +133,8 @@ func decodeBodyToStruct(body zcl.Body, ctx *zcl.EvalContext, val reflect.Value) if isSlice || isPtr { val.Field(fieldIdx).Set(reflect.Zero(field.Type)) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Missing %s block", typeName), Detail: fmt.Sprintf("A %s block is required.", typeName), Subject: body.MissingItemRange().Ptr(), @@ -181,7 +181,7 @@ func decodeBodyToStruct(body zcl.Body, ctx *zcl.EvalContext, val reflect.Value) return diags } -func decodeBodyToMap(body zcl.Body, ctx *zcl.EvalContext, v reflect.Value) zcl.Diagnostics { +func decodeBodyToMap(body hcl.Body, ctx *hcl.EvalContext, v reflect.Value) hcl.Diagnostics { attrs, diags := body.JustAttributes() if attrs == nil { return diags @@ -207,8 +207,8 @@ func decodeBodyToMap(body zcl.Body, ctx *zcl.EvalContext, v reflect.Value) zcl.D return diags } -func decodeBlockToValue(block *zcl.Block, ctx *zcl.EvalContext, v reflect.Value) zcl.Diagnostics { - var diags zcl.Diagnostics +func decodeBlockToValue(block *hcl.Block, ctx *hcl.EvalContext, v reflect.Value) hcl.Diagnostics { + var diags hcl.Diagnostics ty := v.Type() @@ -253,7 +253,7 @@ func decodeBlockToValue(block *zcl.Block, ctx *zcl.EvalContext, v reflect.Value) // are returned then the given value may have been partially-populated but // may still be accessed by a careful caller for static analysis and editor // integration use-cases. -func DecodeExpression(expr zcl.Expression, ctx *zcl.EvalContext, val interface{}) zcl.Diagnostics { +func DecodeExpression(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics { srcVal, diags := expr.Value(ctx) convTy, err := gocty.ImpliedType(val) @@ -263,8 +263,8 @@ func DecodeExpression(expr zcl.Expression, ctx *zcl.EvalContext, val interface{} srcVal, err = convert.Convert(srcVal, convTy) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unsuitable value type", Detail: fmt.Sprintf("Unsuitable value: %s", err.Error()), Subject: expr.StartRange().Ptr(), @@ -275,8 +275,8 @@ func DecodeExpression(expr zcl.Expression, ctx *zcl.EvalContext, val interface{} err = gocty.FromCtyValue(srcVal, val) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unsuitable value type", Detail: fmt.Sprintf("Unsuitable value: %s", err.Error()), Subject: expr.StartRange().Ptr(), diff --git a/gohcl/decode_test.go b/gohcl/decode_test.go index 0372e1e..cf10733 100644 --- a/gohcl/decode_test.go +++ b/gohcl/decode_test.go @@ -7,8 +7,8 @@ import ( "testing" "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/hcl2/zcl" - zclJSON "github.com/hashicorp/hcl2/zcl/json" + zclJSON "github.com/hashicorp/hcl2/hcl/json" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) @@ -83,12 +83,12 @@ func TestDecodeBody(t *testing.T) { }, struct { Name string `zcl:"name"` - Attrs zcl.Attributes `zcl:",remain"` + Attrs hcl.Attributes `zcl:",remain"` }{}, func(gotI interface{}) bool { got := gotI.(struct { Name string `zcl:"name"` - Attrs zcl.Attributes `zcl:",remain"` + Attrs hcl.Attributes `zcl:",remain"` }) return got.Name == "Ermintrude" && len(got.Attrs) == 1 && got.Attrs["age"] != nil }, @@ -101,12 +101,12 @@ func TestDecodeBody(t *testing.T) { }, struct { Name string `zcl:"name"` - Remain zcl.Body `zcl:",remain"` + Remain hcl.Body `zcl:",remain"` }{}, func(gotI interface{}) bool { got := gotI.(struct { Name string `zcl:"name"` - Remain zcl.Body `zcl:",remain"` + Remain hcl.Body `zcl:",remain"` }) attrs, _ := got.Remain.JustAttributes() @@ -420,9 +420,9 @@ func TestDecodeBody(t *testing.T) { "name": "Ermintrude", "age": 89, }, - map[string]*zcl.Attribute(nil), + map[string]*hcl.Attribute(nil), func(gotI interface{}) bool { - got := gotI.(map[string]*zcl.Attribute) + got := gotI.(map[string]*hcl.Attribute) return len(got) == 2 && got["name"] != nil && got["age"] != nil }, 0, @@ -432,9 +432,9 @@ func TestDecodeBody(t *testing.T) { "name": "Ermintrude", "age": 13, }, - map[string]zcl.Expression(nil), + map[string]hcl.Expression(nil), func(gotI interface{}) bool { - got := gotI.(map[string]zcl.Expression) + got := gotI.(map[string]hcl.Expression) return len(got) == 2 && got["name"] != nil && got["age"] != nil }, 0, @@ -561,17 +561,17 @@ type fixedExpression struct { val cty.Value } -func (e *fixedExpression) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *fixedExpression) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { return e.val, nil } -func (e *fixedExpression) Range() (r zcl.Range) { +func (e *fixedExpression) Range() (r hcl.Range) { return } -func (e *fixedExpression) StartRange() (r zcl.Range) { +func (e *fixedExpression) StartRange() (r hcl.Range) { return } -func (e *fixedExpression) Variables() []zcl.Traversal { +func (e *fixedExpression) Variables() []hcl.Traversal { return nil } diff --git a/gohcl/doc.go b/gohcl/doc.go index 82ac545..b7a939f 100644 --- a/gohcl/doc.go +++ b/gohcl/doc.go @@ -19,11 +19,11 @@ // label indicates that the value is to populated from a block label // remain indicates that the value is to be populated from the remaining body after populating other fields // -// "attr" fields may either be of type *zcl.Expression, in which case the raw +// "attr" fields may either be of type *hcl.Expression, in which case the raw // expression is assigned, or of any type accepted by gocty, in which case // gocty will be used to assign the value to a native Go type. // -// "block" fields may be of type *zcl.Block or zcl.Body, in which case the +// "block" fields may be of type *hcl.Block or hcl.Body, in which case the // corresponding raw value is assigned, or may be a struct that recursively // uses the same tags. Block fields may also be slices of any of these types, // in which case multiple blocks of the corresponding type are decoded into @@ -35,7 +35,7 @@ // an identifier for the label in diagnostic messages. // // "remain" can be placed on a single field that may be either of type -// zcl.Body or zcl.Attributes, in which case any remaining body content is +// hcl.Body or hcl.Attributes, in which case any remaining body content is // placed into this field for delayed processing. If no "remain" field is // present then any attributes or blocks not matched by another valid tag // will cause an error diagnostic. diff --git a/gohcl/schema.go b/gohcl/schema.go index 312d495..3d5801f 100644 --- a/gohcl/schema.go +++ b/gohcl/schema.go @@ -6,10 +6,10 @@ import ( "sort" "strings" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) -// ImpliedBodySchema produces a zcl.BodySchema derived from the type of the +// ImpliedBodySchema produces a hcl.BodySchema derived from the type of the // given value, which must be a struct value or a pointer to one. If an // inappropriate value is passed, this function will panic. // @@ -19,7 +19,7 @@ import ( // This uses the tags on the fields of the struct to discover how each // field's value should be expressed within configuration. If an invalid // mapping is attempted, this function will panic. -func ImpliedBodySchema(val interface{}) (schema *zcl.BodySchema, partial bool) { +func ImpliedBodySchema(val interface{}) (schema *hcl.BodySchema, partial bool) { ty := reflect.TypeOf(val) if ty.Kind() == reflect.Ptr { @@ -30,8 +30,8 @@ func ImpliedBodySchema(val interface{}) (schema *zcl.BodySchema, partial bool) { panic(fmt.Sprintf("given value must be struct, not %T", val)) } - var attrSchemas []zcl.AttributeSchema - var blockSchemas []zcl.BlockHeaderSchema + var attrSchemas []hcl.AttributeSchema + var blockSchemas []hcl.BlockHeaderSchema tags := getFieldTags(ty) @@ -43,7 +43,7 @@ func ImpliedBodySchema(val interface{}) (schema *zcl.BodySchema, partial bool) { for _, n := range attrNames { idx := tags.Attributes[n] field := ty.Field(idx) - attrSchemas = append(attrSchemas, zcl.AttributeSchema{ + attrSchemas = append(attrSchemas, hcl.AttributeSchema{ Name: n, Required: field.Type.Kind() != reflect.Ptr, }) @@ -78,14 +78,14 @@ func ImpliedBodySchema(val interface{}) (schema *zcl.BodySchema, partial bool) { } } - blockSchemas = append(blockSchemas, zcl.BlockHeaderSchema{ + blockSchemas = append(blockSchemas, hcl.BlockHeaderSchema{ Type: n, LabelNames: labelNames, }) } partial = tags.Remain != nil - schema = &zcl.BodySchema{ + schema = &hcl.BodySchema{ Attributes: attrSchemas, Blocks: blockSchemas, } diff --git a/gohcl/schema_test.go b/gohcl/schema_test.go index 548be1c..df75630 100644 --- a/gohcl/schema_test.go +++ b/gohcl/schema_test.go @@ -6,25 +6,25 @@ import ( "testing" "github.com/davecgh/go-spew/spew" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) func TestImpliedBodySchema(t *testing.T) { tests := []struct { val interface{} - wantSchema *zcl.BodySchema + wantSchema *hcl.BodySchema wantPartial bool }{ { struct{}{}, - &zcl.BodySchema{}, + &hcl.BodySchema{}, false, }, { struct { Ignored bool }{}, - &zcl.BodySchema{}, + &hcl.BodySchema{}, false, }, { @@ -32,8 +32,8 @@ func TestImpliedBodySchema(t *testing.T) { Attr1 bool `zcl:"attr1"` Attr2 bool `zcl:"attr2"` }{}, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "attr1", Required: true, @@ -50,8 +50,8 @@ func TestImpliedBodySchema(t *testing.T) { struct { Attr *bool `zcl:"attr,attr"` }{}, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "attr", Required: false, @@ -64,8 +64,8 @@ func TestImpliedBodySchema(t *testing.T) { struct { Thing struct{} `zcl:"thing,block"` }{}, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "thing", }, @@ -80,8 +80,8 @@ func TestImpliedBodySchema(t *testing.T) { Name string `zcl:"name,label"` } `zcl:"thing,block"` }{}, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "thing", LabelNames: []string{"type", "name"}, @@ -97,8 +97,8 @@ func TestImpliedBodySchema(t *testing.T) { Name string `zcl:"name,label"` } `zcl:"thing,block"` }{}, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "thing", LabelNames: []string{"type", "name"}, @@ -114,8 +114,8 @@ func TestImpliedBodySchema(t *testing.T) { Name string `zcl:"name,label"` } `zcl:"thing,block"` }{}, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "thing", LabelNames: []string{"type", "name"}, @@ -131,8 +131,8 @@ func TestImpliedBodySchema(t *testing.T) { Something string `zcl:"something"` } `zcl:"thing,block"` }{}, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "thing", LabelNames: []string{"name"}, @@ -148,14 +148,14 @@ func TestImpliedBodySchema(t *testing.T) { Name string `zcl:"name,label"` } `zcl:"thing,block"` }{}, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "doodad", Required: true, }, }, - Blocks: []zcl.BlockHeaderSchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "thing", LabelNames: []string{"name"}, @@ -169,8 +169,8 @@ func TestImpliedBodySchema(t *testing.T) { Doodad string `zcl:"doodad"` Config string `zcl:",remain"` }{}, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "doodad", Required: true, diff --git a/gohcl/types.go b/gohcl/types.go index b1e1e2c..a94f275 100644 --- a/gohcl/types.go +++ b/gohcl/types.go @@ -3,14 +3,14 @@ package gohcl import ( "reflect" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) -var victimExpr zcl.Expression -var victimBody zcl.Body +var victimExpr hcl.Expression +var victimBody hcl.Body var exprType = reflect.TypeOf(&victimExpr).Elem() var bodyType = reflect.TypeOf(&victimBody).Elem() -var blockType = reflect.TypeOf((*zcl.Block)(nil)) -var attrType = reflect.TypeOf((*zcl.Attribute)(nil)) -var attrsType = reflect.TypeOf(zcl.Attributes(nil)) +var blockType = reflect.TypeOf((*hcl.Block)(nil)) +var attrType = reflect.TypeOf((*hcl.Attribute)(nil)) +var attrsType = reflect.TypeOf(hcl.Attributes(nil)) diff --git a/zcl/diagnostic.go b/hcl/diagnostic.go similarity index 97% rename from zcl/diagnostic.go rename to hcl/diagnostic.go index e80e233..6ecf744 100644 --- a/zcl/diagnostic.go +++ b/hcl/diagnostic.go @@ -1,4 +1,4 @@ -package zcl +package hcl import ( "fmt" @@ -67,7 +67,7 @@ func (d Diagnostics) Error() string { // This is provided as a convenience for returning from a function that // collects and then returns a set of diagnostics: // -// return nil, diags.Append(&zcl.Diagnostic{ ... }) +// return nil, diags.Append(&hcl.Diagnostic{ ... }) // // Note that this modifies the array underlying the diagnostics slice, so // must be used carefully within a single codepath. It is incorrect (and rude) diff --git a/zcl/diagnostic_text.go b/hcl/diagnostic_text.go similarity index 99% rename from zcl/diagnostic_text.go rename to hcl/diagnostic_text.go index 7a455b7..9776f04 100644 --- a/zcl/diagnostic_text.go +++ b/hcl/diagnostic_text.go @@ -1,4 +1,4 @@ -package zcl +package hcl import ( "bufio" diff --git a/zcl/diagnostic_text_test.go b/hcl/diagnostic_text_test.go similarity index 99% rename from zcl/diagnostic_text_test.go rename to hcl/diagnostic_text_test.go index 9d51731..40c03df 100644 --- a/zcl/diagnostic_text_test.go +++ b/hcl/diagnostic_text_test.go @@ -1,4 +1,4 @@ -package zcl +package hcl import ( "bytes" diff --git a/zcl/didyoumean.go b/hcl/didyoumean.go similarity index 98% rename from zcl/didyoumean.go rename to hcl/didyoumean.go index cca5d55..c128334 100644 --- a/zcl/didyoumean.go +++ b/hcl/didyoumean.go @@ -1,4 +1,4 @@ -package zcl +package hcl import ( "github.com/agext/levenshtein" diff --git a/hcl/doc.go b/hcl/doc.go new file mode 100644 index 0000000..01318c9 --- /dev/null +++ b/hcl/doc.go @@ -0,0 +1 @@ +package hcl diff --git a/zcl/eval_context.go b/hcl/eval_context.go similarity index 98% rename from zcl/eval_context.go rename to hcl/eval_context.go index d2e4961..915910a 100644 --- a/zcl/eval_context.go +++ b/hcl/eval_context.go @@ -1,4 +1,4 @@ -package zcl +package hcl import ( "github.com/zclconf/go-cty/cty" diff --git a/zcl/zclsyntax/didyoumean.go b/hcl/hclsyntax/didyoumean.go similarity index 97% rename from zcl/zclsyntax/didyoumean.go rename to hcl/hclsyntax/didyoumean.go index 9ee893e..ccc1c0a 100644 --- a/zcl/zclsyntax/didyoumean.go +++ b/hcl/hclsyntax/didyoumean.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax import ( "github.com/agext/levenshtein" diff --git a/zcl/zclsyntax/didyoumean_test.go b/hcl/hclsyntax/didyoumean_test.go similarity index 98% rename from zcl/zclsyntax/didyoumean_test.go rename to hcl/hclsyntax/didyoumean_test.go index 896e2f4..9d04b7b 100644 --- a/zcl/zclsyntax/didyoumean_test.go +++ b/hcl/hclsyntax/didyoumean_test.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax import "testing" diff --git a/zcl/zclsyntax/doc.go b/hcl/hclsyntax/doc.go similarity index 94% rename from zcl/zclsyntax/doc.go rename to hcl/hclsyntax/doc.go index d1eb04e..c4ec785 100644 --- a/zcl/zclsyntax/doc.go +++ b/hcl/hclsyntax/doc.go @@ -4,4 +4,4 @@ // In normal use applications should rarely depend on this package directly, // instead preferring the higher-level interface of the main zcl page and // its companion zclparse. -package zclsyntax +package hclsyntax diff --git a/zcl/zclsyntax/expression.go b/hcl/hclsyntax/expression.go similarity index 81% rename from zcl/zclsyntax/expression.go rename to hcl/hclsyntax/expression.go index 2669ee0..e90ac2b 100644 --- a/zcl/zclsyntax/expression.go +++ b/hcl/hclsyntax/expression.go @@ -1,9 +1,9 @@ -package zclsyntax +package hclsyntax import ( "fmt" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/convert" "github.com/zclconf/go-cty/cty/function" @@ -13,60 +13,60 @@ import ( type Expression interface { Node - // The zcl.Expression methods are duplicated here, rather than simply - // embedded, because both Node and zcl.Expression have a Range method + // The hcl.Expression methods are duplicated here, rather than simply + // embedded, because both Node and hcl.Expression have a Range method // and so they conflict. - Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) - Variables() []zcl.Traversal - StartRange() zcl.Range + Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) + Variables() []hcl.Traversal + StartRange() hcl.Range } -// Assert that Expression implements zcl.Expression -var assertExprImplExpr zcl.Expression = Expression(nil) +// Assert that Expression implements hcl.Expression +var assertExprImplExpr hcl.Expression = Expression(nil) // LiteralValueExpr is an expression that just always returns a given value. type LiteralValueExpr struct { Val cty.Value - SrcRange zcl.Range + SrcRange hcl.Range } func (e *LiteralValueExpr) walkChildNodes(w internalWalkFunc) { // Literal values have no child nodes } -func (e *LiteralValueExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *LiteralValueExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { return e.Val, nil } -func (e *LiteralValueExpr) Range() zcl.Range { +func (e *LiteralValueExpr) Range() hcl.Range { return e.SrcRange } -func (e *LiteralValueExpr) StartRange() zcl.Range { +func (e *LiteralValueExpr) StartRange() hcl.Range { return e.SrcRange } // ScopeTraversalExpr is an Expression that retrieves a value from the scope // using a traversal. type ScopeTraversalExpr struct { - Traversal zcl.Traversal - SrcRange zcl.Range + Traversal hcl.Traversal + SrcRange hcl.Range } func (e *ScopeTraversalExpr) walkChildNodes(w internalWalkFunc) { // Scope traversals have no child nodes } -func (e *ScopeTraversalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *ScopeTraversalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { return e.Traversal.TraverseAbs(ctx) } -func (e *ScopeTraversalExpr) Range() zcl.Range { +func (e *ScopeTraversalExpr) Range() hcl.Range { return e.SrcRange } -func (e *ScopeTraversalExpr) StartRange() zcl.Range { +func (e *ScopeTraversalExpr) StartRange() hcl.Range { return e.SrcRange } @@ -74,26 +74,26 @@ func (e *ScopeTraversalExpr) StartRange() zcl.Range { // value using a _relative_ traversal. type RelativeTraversalExpr struct { Source Expression - Traversal zcl.Traversal - SrcRange zcl.Range + Traversal hcl.Traversal + SrcRange hcl.Range } func (e *RelativeTraversalExpr) walkChildNodes(w internalWalkFunc) { // Scope traversals have no child nodes } -func (e *RelativeTraversalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *RelativeTraversalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { src, diags := e.Source.Value(ctx) ret, travDiags := e.Traversal.TraverseRel(src) diags = append(diags, travDiags...) return ret, diags } -func (e *RelativeTraversalExpr) Range() zcl.Range { +func (e *RelativeTraversalExpr) Range() hcl.Range { return e.SrcRange } -func (e *RelativeTraversalExpr) StartRange() zcl.Range { +func (e *RelativeTraversalExpr) StartRange() hcl.Range { return e.SrcRange } @@ -107,9 +107,9 @@ type FunctionCallExpr struct { // expand to be one argument per element. ExpandFinal bool - NameRange zcl.Range - OpenParenRange zcl.Range - CloseParenRange zcl.Range + NameRange hcl.Range + OpenParenRange hcl.Range + CloseParenRange hcl.Range } func (e *FunctionCallExpr) walkChildNodes(w internalWalkFunc) { @@ -118,8 +118,8 @@ func (e *FunctionCallExpr) walkChildNodes(w internalWalkFunc) { } } -func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { - var diags zcl.Diagnostics +func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { + var diags hcl.Diagnostics var f function.Function exists := false @@ -140,9 +140,9 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti if !exists { if !hasNonNilMap { - return cty.DynamicVal, zcl.Diagnostics{ + return cty.DynamicVal, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Function calls not allowed", Detail: "Functions may not be called here.", Subject: e.Range().Ptr(), @@ -159,9 +159,9 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti suggestion = fmt.Sprintf(" Did you mean %q?", suggestion) } - return cty.DynamicVal, zcl.Diagnostics{ + return cty.DynamicVal, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Call to unknown function", Detail: fmt.Sprintf("There is no function named %q.%s", e.Name, suggestion), Subject: &e.NameRange, @@ -189,8 +189,8 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti switch { case expandVal.Type().IsTupleType() || expandVal.Type().IsListType() || expandVal.Type().IsSetType(): if expandVal.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid expanding argument value", Detail: "The expanding argument (indicated by ...) must not be null.", Context: expandExpr.Range().Ptr(), @@ -214,8 +214,8 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti } args = newArgs default: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid expanding argument value", Detail: "The expanding argument (indicated by ...) must be of a tuple, list, or set type.", Context: expandExpr.Range().Ptr(), @@ -231,9 +231,9 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti if varParam != nil { qual = " at least" } - return cty.DynamicVal, zcl.Diagnostics{ + return cty.DynamicVal, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Not enough function arguments", Detail: fmt.Sprintf( "Function %q expects%s %d argument(s). Missing value for %q.", @@ -246,9 +246,9 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti } if varParam == nil && len(args) > len(params) { - return cty.DynamicVal, zcl.Diagnostics{ + return cty.DynamicVal, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Too many function arguments", Detail: fmt.Sprintf( "Function %q expects only %d argument(s).", @@ -278,8 +278,8 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti // Try to convert our value to the parameter type val, err := convert.Convert(val, param.Type) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid function argument", Detail: fmt.Sprintf( "Invalid value for %q parameter: %s.", @@ -315,8 +315,8 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti // TODO: we should also unpick a PathError here and show the // path to the deep value where the error was detected. - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid function argument", Detail: fmt.Sprintf( "Invalid value for %q parameter: %s.", @@ -327,8 +327,8 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti }) default: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Error in function call", Detail: fmt.Sprintf( "Call to function %q failed: %s.", @@ -345,12 +345,12 @@ func (e *FunctionCallExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti return resultVal, diags } -func (e *FunctionCallExpr) Range() zcl.Range { - return zcl.RangeBetween(e.NameRange, e.CloseParenRange) +func (e *FunctionCallExpr) Range() hcl.Range { + return hcl.RangeBetween(e.NameRange, e.CloseParenRange) } -func (e *FunctionCallExpr) StartRange() zcl.Range { - return zcl.RangeBetween(e.NameRange, e.OpenParenRange) +func (e *FunctionCallExpr) StartRange() hcl.Range { + return hcl.RangeBetween(e.NameRange, e.OpenParenRange) } type ConditionalExpr struct { @@ -358,7 +358,7 @@ type ConditionalExpr struct { TrueResult Expression FalseResult Expression - SrcRange zcl.Range + SrcRange hcl.Range } func (e *ConditionalExpr) walkChildNodes(w internalWalkFunc) { @@ -367,17 +367,17 @@ func (e *ConditionalExpr) walkChildNodes(w internalWalkFunc) { e.FalseResult = w(e.FalseResult).(Expression) } -func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { trueResult, trueDiags := e.TrueResult.Value(ctx) falseResult, falseDiags := e.FalseResult.Value(ctx) - var diags zcl.Diagnostics + var diags hcl.Diagnostics // Try to find a type that both results can be converted to. resultType, convs := convert.UnifyUnsafe([]cty.Type{trueResult.Type(), falseResult.Type()}) if resultType == cty.NilType { - return cty.DynamicVal, zcl.Diagnostics{ + return cty.DynamicVal, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Inconsistent conditional result types", Detail: fmt.Sprintf( // FIXME: Need a helper function for showing natural-language type diffs, @@ -387,7 +387,7 @@ func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostic "The true and false result expressions must have consistent types. The given expressions are %s and %s, respectively.", trueResult.Type(), falseResult.Type(), ), - Subject: zcl.RangeBetween(e.TrueResult.Range(), e.FalseResult.Range()).Ptr(), + Subject: hcl.RangeBetween(e.TrueResult.Range(), e.FalseResult.Range()).Ptr(), Context: &e.SrcRange, }, } @@ -396,8 +396,8 @@ func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostic condResult, condDiags := e.Condition.Value(ctx) diags = append(diags, condDiags...) if condResult.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Null condition", Detail: "The condition value is null. Conditions must either be true or false.", Subject: e.Condition.Range().Ptr(), @@ -410,8 +410,8 @@ func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostic } condResult, err := convert.Convert(condResult, cty.Bool) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Incorrect condition type", Detail: fmt.Sprintf("The condition expression must be of type bool."), Subject: e.Condition.Range().Ptr(), @@ -427,8 +427,8 @@ func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostic trueResult, err = convs[0](trueResult) if err != nil { // Unsafe conversion failed with the concrete result value - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Inconsistent conditional result types", Detail: fmt.Sprintf( "The true result value has the wrong type: %s.", @@ -448,8 +448,8 @@ func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostic falseResult, err = convs[1](falseResult) if err != nil { // Unsafe conversion failed with the concrete result value - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Inconsistent conditional result types", Detail: fmt.Sprintf( "The false result value has the wrong type: %s.", @@ -465,11 +465,11 @@ func (e *ConditionalExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostic } } -func (e *ConditionalExpr) Range() zcl.Range { +func (e *ConditionalExpr) Range() hcl.Range { return e.SrcRange } -func (e *ConditionalExpr) StartRange() zcl.Range { +func (e *ConditionalExpr) StartRange() hcl.Range { return e.Condition.StartRange() } @@ -477,8 +477,8 @@ type IndexExpr struct { Collection Expression Key Expression - SrcRange zcl.Range - OpenRange zcl.Range + SrcRange hcl.Range + OpenRange hcl.Range } func (e *IndexExpr) walkChildNodes(w internalWalkFunc) { @@ -486,29 +486,29 @@ func (e *IndexExpr) walkChildNodes(w internalWalkFunc) { e.Key = w(e.Key).(Expression) } -func (e *IndexExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { - var diags zcl.Diagnostics +func (e *IndexExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { + var diags hcl.Diagnostics coll, collDiags := e.Collection.Value(ctx) key, keyDiags := e.Key.Value(ctx) diags = append(diags, collDiags...) diags = append(diags, keyDiags...) - return zcl.Index(coll, key, &e.SrcRange) + return hcl.Index(coll, key, &e.SrcRange) } -func (e *IndexExpr) Range() zcl.Range { +func (e *IndexExpr) Range() hcl.Range { return e.SrcRange } -func (e *IndexExpr) StartRange() zcl.Range { +func (e *IndexExpr) StartRange() hcl.Range { return e.OpenRange } type TupleConsExpr struct { Exprs []Expression - SrcRange zcl.Range - OpenRange zcl.Range + SrcRange hcl.Range + OpenRange hcl.Range } func (e *TupleConsExpr) walkChildNodes(w internalWalkFunc) { @@ -517,9 +517,9 @@ func (e *TupleConsExpr) walkChildNodes(w internalWalkFunc) { } } -func (e *TupleConsExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *TupleConsExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { var vals []cty.Value - var diags zcl.Diagnostics + var diags hcl.Diagnostics vals = make([]cty.Value, len(e.Exprs)) for i, expr := range e.Exprs { @@ -531,19 +531,19 @@ func (e *TupleConsExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) return cty.TupleVal(vals), diags } -func (e *TupleConsExpr) Range() zcl.Range { +func (e *TupleConsExpr) Range() hcl.Range { return e.SrcRange } -func (e *TupleConsExpr) StartRange() zcl.Range { +func (e *TupleConsExpr) StartRange() hcl.Range { return e.OpenRange } type ObjectConsExpr struct { Items []ObjectConsItem - SrcRange zcl.Range - OpenRange zcl.Range + SrcRange hcl.Range + OpenRange hcl.Range } type ObjectConsItem struct { @@ -558,9 +558,9 @@ func (e *ObjectConsExpr) walkChildNodes(w internalWalkFunc) { } } -func (e *ObjectConsExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *ObjectConsExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { var vals map[string]cty.Value - var diags zcl.Diagnostics + var diags hcl.Diagnostics // This will get set to true if we fail to produce any of our keys, // either because they are actually unknown or if the evaluation produces @@ -586,8 +586,8 @@ func (e *ObjectConsExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics } if key.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Null value as key", Detail: "Can't use a null value as a key.", Subject: item.ValueExpr.Range().Ptr(), @@ -599,8 +599,8 @@ func (e *ObjectConsExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics var err error key, err = convert.Convert(key, cty.String) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Incorrect key type", Detail: fmt.Sprintf("Can't use this value as a key: %s.", err.Error()), Subject: item.ValueExpr.Range().Ptr(), @@ -626,11 +626,11 @@ func (e *ObjectConsExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics return cty.ObjectVal(vals), diags } -func (e *ObjectConsExpr) Range() zcl.Range { +func (e *ObjectConsExpr) Range() hcl.Range { return e.SrcRange } -func (e *ObjectConsExpr) StartRange() zcl.Range { +func (e *ObjectConsExpr) StartRange() hcl.Range { return e.OpenRange } @@ -651,20 +651,20 @@ type ForExpr struct { Group bool // set if the ellipsis is used on the value in an object for - SrcRange zcl.Range - OpenRange zcl.Range - CloseRange zcl.Range + SrcRange hcl.Range + OpenRange hcl.Range + CloseRange hcl.Range } -func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { - var diags zcl.Diagnostics +func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { + var diags hcl.Diagnostics collVal, collDiags := e.CollExpr.Value(ctx) diags = append(diags, collDiags...) if collVal.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Iteration over null value", Detail: "A null value cannot be used as the collection in a 'for' expression.", Subject: e.CollExpr.Range().Ptr(), @@ -676,8 +676,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { return cty.DynamicVal, diags } if !collVal.CanIterateElements() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Iteration over non-iterable value", Detail: fmt.Sprintf( "A value of type %s cannot be used as the collection in a 'for' expression.", @@ -708,8 +708,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { result, condDiags := e.CondExpr.Value(childCtx) diags = append(diags, condDiags...) if result.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Condition is null", Detail: "The value of the 'if' clause must not be null.", Subject: e.CondExpr.Range().Ptr(), @@ -719,8 +719,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { } _, err := convert.Convert(result, cty.Bool) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' condition", Detail: fmt.Sprintf("The 'if' clause value is invalid: %s.", err.Error()), Subject: e.CondExpr.Range().Ptr(), @@ -758,8 +758,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { diags = append(diags, condDiags...) if includeRaw.IsNull() { if known { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Condition is null", Detail: "The value of the 'if' clause must not be null.", Subject: e.CondExpr.Range().Ptr(), @@ -772,8 +772,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { include, err := convert.Convert(includeRaw, cty.Bool) if err != nil { if known { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' condition", Detail: fmt.Sprintf("The 'if' clause value is invalid: %s.", err.Error()), Subject: e.CondExpr.Range().Ptr(), @@ -798,8 +798,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { diags = append(diags, keyDiags...) if keyRaw.IsNull() { if known { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid object key", Detail: "Key expression in 'for' expression must not produce a null value.", Subject: e.KeyExpr.Range().Ptr(), @@ -817,8 +817,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { key, err := convert.Convert(keyRaw, cty.String) if err != nil { if known { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid object key", Detail: fmt.Sprintf("The key expression produced an invalid result: %s.", err.Error()), Subject: e.KeyExpr.Range().Ptr(), @@ -838,8 +838,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { } else { k := key.AsString() if _, exists := vals[k]; exists { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Duplicate object key", Detail: fmt.Sprintf( "Two different items produced the key %q in this for expression. If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key.", @@ -886,8 +886,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { diags = append(diags, condDiags...) if includeRaw.IsNull() { if known { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Condition is null", Detail: "The value of the 'if' clause must not be null.", Subject: e.CondExpr.Range().Ptr(), @@ -908,8 +908,8 @@ func (e *ForExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { include, err := convert.Convert(includeRaw, cty.Bool) if err != nil { if known { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' condition", Detail: fmt.Sprintf("The 'if' clause value is invalid: %s.", err.Error()), Subject: e.CondExpr.Range().Ptr(), @@ -968,11 +968,11 @@ func (e *ForExpr) walkChildNodes(w internalWalkFunc) { } } -func (e *ForExpr) Range() zcl.Range { +func (e *ForExpr) Range() hcl.Range { return e.SrcRange } -func (e *ForExpr) StartRange() zcl.Range { +func (e *ForExpr) StartRange() hcl.Range { return e.OpenRange } @@ -981,11 +981,11 @@ type SplatExpr struct { Each Expression Item *AnonSymbolExpr - SrcRange zcl.Range - MarkerRange zcl.Range + SrcRange hcl.Range + MarkerRange hcl.Range } -func (e *SplatExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { sourceVal, diags := e.Source.Value(ctx) if diags.HasErrors() { // We'll evaluate our "Each" expression here just to see if it @@ -998,12 +998,12 @@ func (e *SplatExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { } if sourceVal.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Splat of null value", Detail: "Splat expressions (with the * symbol) cannot be applied to null values.", Subject: e.Source.Range().Ptr(), - Context: zcl.RangeBetween(e.Source.Range(), e.MarkerRange).Ptr(), + Context: hcl.RangeBetween(e.Source.Range(), e.MarkerRange).Ptr(), }) return cty.DynamicVal, diags } @@ -1051,11 +1051,11 @@ func (e *SplatExpr) walkChildNodes(w internalWalkFunc) { e.Each = w(e.Each).(Expression) } -func (e *SplatExpr) Range() zcl.Range { +func (e *SplatExpr) Range() hcl.Range { return e.SrcRange } -func (e *SplatExpr) StartRange() zcl.Range { +func (e *SplatExpr) StartRange() hcl.Range { return e.MarkerRange } @@ -1071,11 +1071,11 @@ func (e *SplatExpr) StartRange() zcl.Range { // in terms of another node (i.e. a splat expression) which temporarily // assigns it a value. type AnonSymbolExpr struct { - SrcRange zcl.Range - values map[*zcl.EvalContext]cty.Value + SrcRange hcl.Range + values map[*hcl.EvalContext]cty.Value } -func (e *AnonSymbolExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *AnonSymbolExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { if ctx == nil { return cty.DynamicVal, nil } @@ -1088,9 +1088,9 @@ func (e *AnonSymbolExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics // setValue sets a temporary local value for the expression when evaluated // in the given context, which must be non-nil. -func (e *AnonSymbolExpr) setValue(ctx *zcl.EvalContext, val cty.Value) { +func (e *AnonSymbolExpr) setValue(ctx *hcl.EvalContext, val cty.Value) { if e.values == nil { - e.values = make(map[*zcl.EvalContext]cty.Value) + e.values = make(map[*hcl.EvalContext]cty.Value) } if ctx == nil { panic("can't setValue for a nil EvalContext") @@ -1098,7 +1098,7 @@ func (e *AnonSymbolExpr) setValue(ctx *zcl.EvalContext, val cty.Value) { e.values[ctx] = val } -func (e *AnonSymbolExpr) clearValue(ctx *zcl.EvalContext) { +func (e *AnonSymbolExpr) clearValue(ctx *hcl.EvalContext) { if e.values == nil { return } @@ -1112,10 +1112,10 @@ func (e *AnonSymbolExpr) walkChildNodes(w internalWalkFunc) { // AnonSymbolExpr is a leaf node in the tree } -func (e *AnonSymbolExpr) Range() zcl.Range { +func (e *AnonSymbolExpr) Range() hcl.Range { return e.SrcRange } -func (e *AnonSymbolExpr) StartRange() zcl.Range { +func (e *AnonSymbolExpr) StartRange() hcl.Range { return e.SrcRange } diff --git a/zcl/zclsyntax/expression_ops.go b/hcl/hclsyntax/expression_ops.go similarity index 86% rename from zcl/zclsyntax/expression_ops.go rename to hcl/hclsyntax/expression_ops.go index a460b90..9a5da04 100644 --- a/zcl/zclsyntax/expression_ops.go +++ b/hcl/hclsyntax/expression_ops.go @@ -1,9 +1,9 @@ -package zclsyntax +package hclsyntax import ( "fmt" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/convert" "github.com/zclconf/go-cty/cty/function" @@ -125,7 +125,7 @@ type BinaryOpExpr struct { Op *Operation RHS Expression - SrcRange zcl.Range + SrcRange hcl.Range } func (e *BinaryOpExpr) walkChildNodes(w internalWalkFunc) { @@ -133,13 +133,13 @@ func (e *BinaryOpExpr) walkChildNodes(w internalWalkFunc) { e.RHS = w(e.RHS).(Expression) } -func (e *BinaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *BinaryOpExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { impl := e.Op.Impl // assumed to be a function taking exactly two arguments params := impl.Params() lhsParam := params[0] rhsParam := params[1] - var diags zcl.Diagnostics + var diags hcl.Diagnostics givenLHSVal, lhsDiags := e.LHS.Value(ctx) givenRHSVal, rhsDiags := e.RHS.Value(ctx) @@ -148,8 +148,8 @@ func (e *BinaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) lhsVal, err := convert.Convert(givenLHSVal, lhsParam.Type) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid operand", Detail: fmt.Sprintf("Unsuitable value for left operand: %s.", err), Subject: e.LHS.Range().Ptr(), @@ -158,8 +158,8 @@ func (e *BinaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) } rhsVal, err := convert.Convert(givenRHSVal, rhsParam.Type) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid operand", Detail: fmt.Sprintf("Unsuitable value for right operand: %s.", err), Subject: e.RHS.Range().Ptr(), @@ -176,9 +176,9 @@ func (e *BinaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) args := []cty.Value{lhsVal, rhsVal} result, err := impl.Call(args) if err != nil { - diags = append(diags, &zcl.Diagnostic{ + diags = append(diags, &hcl.Diagnostic{ // FIXME: This diagnostic is useless. - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Operation failed", Detail: fmt.Sprintf("Error during operation: %s.", err), Subject: &e.SrcRange, @@ -189,11 +189,11 @@ func (e *BinaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) return result, diags } -func (e *BinaryOpExpr) Range() zcl.Range { +func (e *BinaryOpExpr) Range() hcl.Range { return e.SrcRange } -func (e *BinaryOpExpr) StartRange() zcl.Range { +func (e *BinaryOpExpr) StartRange() hcl.Range { return e.LHS.StartRange() } @@ -201,15 +201,15 @@ type UnaryOpExpr struct { Op *Operation Val Expression - SrcRange zcl.Range - SymbolRange zcl.Range + SrcRange hcl.Range + SymbolRange hcl.Range } func (e *UnaryOpExpr) walkChildNodes(w internalWalkFunc) { e.Val = w(e.Val).(Expression) } -func (e *UnaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *UnaryOpExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { impl := e.Op.Impl // assumed to be a function taking exactly one argument params := impl.Params() param := params[0] @@ -218,8 +218,8 @@ func (e *UnaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { val, err := convert.Convert(givenVal, param.Type) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid operand", Detail: fmt.Sprintf("Unsuitable value for unary operand: %s.", err), Subject: e.Val.Range().Ptr(), @@ -236,9 +236,9 @@ func (e *UnaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { args := []cty.Value{val} result, err := impl.Call(args) if err != nil { - diags = append(diags, &zcl.Diagnostic{ + diags = append(diags, &hcl.Diagnostic{ // FIXME: This diagnostic is useless. - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Operation failed", Detail: fmt.Sprintf("Error during operation: %s.", err), Subject: &e.SrcRange, @@ -249,10 +249,10 @@ func (e *UnaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { return result, diags } -func (e *UnaryOpExpr) Range() zcl.Range { +func (e *UnaryOpExpr) Range() hcl.Range { return e.SrcRange } -func (e *UnaryOpExpr) StartRange() zcl.Range { +func (e *UnaryOpExpr) StartRange() hcl.Range { return e.SymbolRange } diff --git a/zcl/zclsyntax/expression_template.go b/hcl/hclsyntax/expression_template.go similarity index 80% rename from zcl/zclsyntax/expression_template.go rename to hcl/hclsyntax/expression_template.go index ffd1397..a1c4727 100644 --- a/zcl/zclsyntax/expression_template.go +++ b/hcl/hclsyntax/expression_template.go @@ -1,10 +1,10 @@ -package zclsyntax +package hclsyntax import ( "bytes" "fmt" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/convert" ) @@ -12,7 +12,7 @@ import ( type TemplateExpr struct { Parts []Expression - SrcRange zcl.Range + SrcRange hcl.Range } func (e *TemplateExpr) walkChildNodes(w internalWalkFunc) { @@ -21,9 +21,9 @@ func (e *TemplateExpr) walkChildNodes(w internalWalkFunc) { } } -func (e *TemplateExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { buf := &bytes.Buffer{} - var diags zcl.Diagnostics + var diags hcl.Diagnostics isKnown := true for _, part := range e.Parts { @@ -31,8 +31,8 @@ func (e *TemplateExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) diags = append(diags, partDiags...) if partVal.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid template interpolation value", Detail: fmt.Sprintf( "The expression result is null. Cannot include a null value in a string template.", @@ -54,8 +54,8 @@ func (e *TemplateExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) strVal, err := convert.Convert(partVal, cty.String) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid template interpolation value", Detail: fmt.Sprintf( "Cannot include the given value in a string template: %s.", @@ -77,11 +77,11 @@ func (e *TemplateExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) return cty.StringVal(buf.String()), diags } -func (e *TemplateExpr) Range() zcl.Range { +func (e *TemplateExpr) Range() hcl.Range { return e.SrcRange } -func (e *TemplateExpr) StartRange() zcl.Range { +func (e *TemplateExpr) StartRange() hcl.Range { return e.Parts[0].StartRange() } @@ -97,7 +97,7 @@ func (e *TemplateJoinExpr) walkChildNodes(w internalWalkFunc) { e.Tuple = w(e.Tuple).(Expression) } -func (e *TemplateJoinExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *TemplateJoinExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { tuple, diags := e.Tuple.Value(ctx) if tuple.IsNull() { @@ -121,8 +121,8 @@ func (e *TemplateJoinExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti _, val := it.Element() if val.IsNull() { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid template interpolation value", Detail: fmt.Sprintf( "An iteration result is null. Cannot include a null value in a string template.", @@ -136,8 +136,8 @@ func (e *TemplateJoinExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti } strVal, err := convert.Convert(val, cty.String) if err != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid template interpolation value", Detail: fmt.Sprintf( "Cannot include one of the interpolation results into the string template: %s.", @@ -157,11 +157,11 @@ func (e *TemplateJoinExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnosti return cty.StringVal(buf.String()), diags } -func (e *TemplateJoinExpr) Range() zcl.Range { +func (e *TemplateJoinExpr) Range() hcl.Range { return e.Tuple.Range() } -func (e *TemplateJoinExpr) StartRange() zcl.Range { +func (e *TemplateJoinExpr) StartRange() hcl.Range { return e.Tuple.StartRange() } @@ -172,21 +172,21 @@ func (e *TemplateJoinExpr) StartRange() zcl.Range { type TemplateWrapExpr struct { Wrapped Expression - SrcRange zcl.Range + SrcRange hcl.Range } func (e *TemplateWrapExpr) walkChildNodes(w internalWalkFunc) { e.Wrapped = w(e.Wrapped).(Expression) } -func (e *TemplateWrapExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { +func (e *TemplateWrapExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { return e.Wrapped.Value(ctx) } -func (e *TemplateWrapExpr) Range() zcl.Range { +func (e *TemplateWrapExpr) Range() hcl.Range { return e.SrcRange } -func (e *TemplateWrapExpr) StartRange() zcl.Range { +func (e *TemplateWrapExpr) StartRange() hcl.Range { return e.SrcRange } diff --git a/zcl/zclsyntax/expression_template_test.go b/hcl/hclsyntax/expression_template_test.go similarity index 96% rename from zcl/zclsyntax/expression_template_test.go rename to hcl/hclsyntax/expression_template_test.go index bb0b7e6..e769613 100644 --- a/zcl/zclsyntax/expression_template_test.go +++ b/hcl/hclsyntax/expression_template_test.go @@ -1,9 +1,9 @@ -package zclsyntax +package hclsyntax import ( "testing" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) @@ -12,7 +12,7 @@ func TestTemplateExprParseAndValue(t *testing.T) { // method, with the focus on the latter but indirectly testing the former. tests := []struct { input string - ctx *zcl.EvalContext + ctx *hcl.EvalContext want cty.Value diagCount int }{ @@ -209,7 +209,7 @@ trim`, }, { `%{ for v in ["a", "b", "c"] }${v}${nl}%{ endfor }`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "nl": cty.StringVal("\n"), }, @@ -221,7 +221,7 @@ trim`, for _, test := range tests { t.Run(test.input, func(t *testing.T) { - expr, parseDiags := ParseTemplate([]byte(test.input), "", zcl.Pos{Line: 1, Column: 1, Byte: 0}) + expr, parseDiags := ParseTemplate([]byte(test.input), "", hcl.Pos{Line: 1, Column: 1, Byte: 0}) got, valDiags := expr.Value(test.ctx) diff --git a/zcl/zclsyntax/expression_test.go b/hcl/hclsyntax/expression_test.go similarity index 94% rename from zcl/zclsyntax/expression_test.go rename to hcl/hclsyntax/expression_test.go index 332bfa3..f290adf 100644 --- a/zcl/zclsyntax/expression_test.go +++ b/hcl/hclsyntax/expression_test.go @@ -1,9 +1,9 @@ -package zclsyntax +package hclsyntax import ( "testing" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/function/stdlib" @@ -14,7 +14,7 @@ func TestExpressionParseAndValue(t *testing.T) { // method, with the focus on the latter but indirectly testing the former. tests := []struct { input string - ctx *zcl.EvalContext + ctx *hcl.EvalContext want cty.Value diagCount int }{ @@ -38,7 +38,7 @@ func TestExpressionParseAndValue(t *testing.T) { }, { `(2+unk)`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.UnknownVal(cty.Number), }, @@ -48,7 +48,7 @@ func TestExpressionParseAndValue(t *testing.T) { }, { `(2+unk)`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.DynamicVal, }, @@ -58,7 +58,7 @@ func TestExpressionParseAndValue(t *testing.T) { }, { `(unk+unk)`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.DynamicVal, }, @@ -220,7 +220,7 @@ func TestExpressionParseAndValue(t *testing.T) { }, { `upper("foo")`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -234,7 +234,7 @@ upper( "foo" ) `, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -244,7 +244,7 @@ upper( }, { `upper(["foo"]...)`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -254,7 +254,7 @@ upper( }, { `upper("foo", []...)`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -264,7 +264,7 @@ upper( }, { `upper("foo", "bar")`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -274,7 +274,7 @@ upper( }, { `upper(["foo", "bar"]...)`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -419,7 +419,7 @@ upper( }, { `{for k, v in {hello: "world"}: upper(k) => upper(v) if k == "hello"}`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "upper": stdlib.UpperFunc, }, @@ -475,7 +475,7 @@ upper( }, { `{for k, v in {hello: "world"}: k => foo}`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.StringVal("foo"), }, @@ -578,7 +578,7 @@ upper( }, { `[for v in unk: v]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.UnknownVal(cty.List(cty.String)), }, @@ -588,7 +588,7 @@ upper( }, { `[for v in unk: v]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.DynamicVal, }, @@ -598,7 +598,7 @@ upper( }, { `[for v in unk: v]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.UnknownVal(cty.String), }, @@ -608,7 +608,7 @@ upper( }, { `[for v in ["a", "b"]: v if unkbool]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unkbool": cty.UnknownVal(cty.Bool), }, @@ -618,7 +618,7 @@ upper( }, { `[for v in ["a", "b"]: v if nullbool]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "nullbool": cty.NullVal(cty.Bool), }, @@ -628,7 +628,7 @@ upper( }, { `[for v in ["a", "b"]: v if dyn]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "dyn": cty.DynamicVal, }, @@ -638,7 +638,7 @@ upper( }, { `[for v in ["a", "b"]: v if unknum]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unknum": cty.UnknownVal(cty.List(cty.Number)), }, @@ -654,7 +654,7 @@ upper( }, { `[for v in ["a", "b"]: unkstr]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unkstr": cty.UnknownVal(cty.String), }, @@ -751,7 +751,7 @@ upper( }, { `["hello"][negate(0)]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "negate": stdlib.NegateFunc, }, @@ -761,7 +761,7 @@ upper( }, { `[][negate(0)]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: map[string]function.Function{ "negate": stdlib.NegateFunc, }, @@ -778,7 +778,7 @@ upper( { `foo`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.StringVal("hello"), }, @@ -788,13 +788,13 @@ upper( }, { `bar`, - &zcl.EvalContext{}, + &hcl.EvalContext{}, cty.DynamicVal, 1, // variables not allowed here }, { `foo.bar`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.StringVal("hello"), }, @@ -804,7 +804,7 @@ upper( }, { `foo.baz`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.ObjectVal(map[string]cty.Value{ "baz": cty.StringVal("hello"), @@ -816,7 +816,7 @@ upper( }, { `foo["baz"]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.ObjectVal(map[string]cty.Value{ "baz": cty.StringVal("hello"), @@ -828,7 +828,7 @@ upper( }, { `foo[true]`, // key is converted to string - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.ObjectVal(map[string]cty.Value{ "true": cty.StringVal("hello"), @@ -840,7 +840,7 @@ upper( }, { `foo[0].baz`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "foo": cty.ListVal([]cty.Value{ cty.ObjectVal(map[string]cty.Value{ @@ -854,7 +854,7 @@ upper( }, { `unk["baz"]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.UnknownVal(cty.String), }, @@ -864,7 +864,7 @@ upper( }, { `unk["boop"]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "unk": cty.UnknownVal(cty.Map(cty.String)), }, @@ -874,7 +874,7 @@ upper( }, { `dyn["boop"]`, - &zcl.EvalContext{ + &hcl.EvalContext{ Variables: map[string]cty.Value{ "dyn": cty.DynamicVal, }, @@ -886,7 +886,7 @@ upper( for _, test := range tests { t.Run(test.input, func(t *testing.T) { - expr, parseDiags := ParseExpression([]byte(test.input), "", zcl.Pos{Line: 1, Column: 1, Byte: 0}) + expr, parseDiags := ParseExpression([]byte(test.input), "", hcl.Pos{Line: 1, Column: 1, Byte: 0}) got, valDiags := expr.Value(test.ctx) @@ -918,7 +918,7 @@ func TestFunctionCallExprValue(t *testing.T) { tests := map[string]struct { expr *FunctionCallExpr - ctx *zcl.EvalContext + ctx *hcl.EvalContext want cty.Value diagCount int }{ @@ -931,7 +931,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.NumberIntVal(5), @@ -946,7 +946,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.NumberIntVal(4), // length of string "true" @@ -961,7 +961,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.UnknownVal(cty.Number), @@ -976,7 +976,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.UnknownVal(cty.Number), @@ -991,7 +991,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.UnknownVal(cty.Number), @@ -1006,7 +1006,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.DynamicVal, @@ -1021,7 +1021,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.StringVal("hello"), @@ -1036,7 +1036,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.DynamicVal, // type depends on arg value @@ -1051,7 +1051,7 @@ func TestFunctionCallExprValue(t *testing.T) { }, }, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.DynamicVal, @@ -1062,7 +1062,7 @@ func TestFunctionCallExprValue(t *testing.T) { Name: "lenth", Args: []Expression{}, }, - &zcl.EvalContext{ + &hcl.EvalContext{ Functions: funcs, }, cty.DynamicVal, diff --git a/hcl/hclsyntax/expression_vars.go b/hcl/hclsyntax/expression_vars.go new file mode 100755 index 0000000..c15d134 --- /dev/null +++ b/hcl/hclsyntax/expression_vars.go @@ -0,0 +1,72 @@ +package hclsyntax + +// Generated by expression_vars_get.go. DO NOT EDIT. +// Run 'go generate' on this package to update the set of functions here. + +import ( + "github.com/hashicorp/hcl2/hcl" +) + +func (e *AnonSymbolExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *BinaryOpExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *ConditionalExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *ForExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *FunctionCallExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *IndexExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *LiteralValueExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *ObjectConsExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *RelativeTraversalExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *ScopeTraversalExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *SplatExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *TemplateExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *TemplateJoinExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *TemplateWrapExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *TupleConsExpr) Variables() []hcl.Traversal { + return Variables(e) +} + +func (e *UnaryOpExpr) Variables() []hcl.Traversal { + return Variables(e) +} diff --git a/zcl/zclsyntax/expression_vars_gen.go b/hcl/hclsyntax/expression_vars_gen.go similarity index 98% rename from zcl/zclsyntax/expression_vars_gen.go rename to hcl/hclsyntax/expression_vars_gen.go index c525ce3..8c72907 100644 --- a/zcl/zclsyntax/expression_vars_gen.go +++ b/hcl/hclsyntax/expression_vars_gen.go @@ -94,6 +94,6 @@ import ( const outputMethodFmt = ` -func (e %s) Variables() []zcl.Traversal { +func (e %s) Variables() []hcl.Traversal { return Variables(e) }` diff --git a/zcl/zclsyntax/file.go b/hcl/hclsyntax/file.go similarity index 66% rename from zcl/zclsyntax/file.go rename to hcl/hclsyntax/file.go index 89ce7bd..22f66da 100644 --- a/zcl/zclsyntax/file.go +++ b/hcl/hclsyntax/file.go @@ -1,6 +1,8 @@ -package zclsyntax +package hclsyntax -import "github.com/hashicorp/hcl2/zcl" +import ( + "github.com/hashicorp/hcl2/hcl" +) // File is the top-level object resulting from parsing a configuration file. type File struct { @@ -8,8 +10,8 @@ type File struct { Bytes []byte } -func (f *File) AsZCLFile() *zcl.File { - return &zcl.File{ +func (f *File) AsZCLFile() *hcl.File { + return &hcl.File{ Body: f.Body, Bytes: f.Bytes, diff --git a/zcl/zclsyntax/generate.go b/hcl/hclsyntax/generate.go similarity index 95% rename from zcl/zclsyntax/generate.go rename to hcl/hclsyntax/generate.go index 6c160b0..ecc389f 100644 --- a/zcl/zclsyntax/generate.go +++ b/hcl/hclsyntax/generate.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax //go:generate go run expression_vars_gen.go //go:generate ruby unicode2ragel.rb --url=http://www.unicode.org/Public/9.0.0/ucd/DerivedCoreProperties.txt -m UnicodeDerived -p ID_Start,ID_Continue -o unicode_derived.rl diff --git a/zcl/zclsyntax/keywords.go b/hcl/hclsyntax/keywords.go similarity index 96% rename from zcl/zclsyntax/keywords.go rename to hcl/hclsyntax/keywords.go index 03af9f3..eef8b96 100644 --- a/zcl/zclsyntax/keywords.go +++ b/hcl/hclsyntax/keywords.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax import ( "bytes" diff --git a/zcl/zclsyntax/navigation.go b/hcl/hclsyntax/navigation.go similarity index 97% rename from zcl/zclsyntax/navigation.go rename to hcl/hclsyntax/navigation.go index 599ad0b..8a04c20 100644 --- a/zcl/zclsyntax/navigation.go +++ b/hcl/hclsyntax/navigation.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax import ( "bytes" diff --git a/zcl/zclsyntax/node.go b/hcl/hclsyntax/node.go similarity index 88% rename from zcl/zclsyntax/node.go rename to hcl/hclsyntax/node.go index af2c9ad..fd426d4 100644 --- a/zcl/zclsyntax/node.go +++ b/hcl/hclsyntax/node.go @@ -1,7 +1,7 @@ -package zclsyntax +package hclsyntax import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // Node is the abstract type that every AST node implements. @@ -16,7 +16,7 @@ type Node interface { // walks. walkChildNodes(w internalWalkFunc) - Range() zcl.Range + Range() hcl.Range } type internalWalkFunc func(Node) Node diff --git a/hcl/hclsyntax/parse_traversal_test.go b/hcl/hclsyntax/parse_traversal_test.go new file mode 100644 index 0000000..4f4962a --- /dev/null +++ b/hcl/hclsyntax/parse_traversal_test.go @@ -0,0 +1,227 @@ +package hclsyntax + +import ( + "testing" + + "reflect" + + "github.com/davecgh/go-spew/spew" + "github.com/hashicorp/hcl2/hcl" + "github.com/zclconf/go-cty/cty" +) + +func TestParseTraversalAbs(t *testing.T) { + tests := []struct { + src string + want hcl.Traversal + diagCount int + }{ + { + "", + nil, + 1, // variable name required + }, + { + "foo", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + 0, + }, + { + "foo.bar.baz", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + hcl.TraverseAttr{ + Name: "bar", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + End: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + }, + }, + hcl.TraverseAttr{ + Name: "baz", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + }, + 0, + }, + { + "foo[1]", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + hcl.TraverseIndex{ + Key: cty.NumberIntVal(1), + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + End: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + }, + }, + }, + 0, + }, + { + "foo[1][2]", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + hcl.TraverseIndex{ + Key: cty.NumberIntVal(1), + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + End: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + }, + }, + hcl.TraverseIndex{ + Key: cty.NumberIntVal(2), + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 10, Byte: 9}, + }, + }, + }, + 0, + }, + { + "foo[1].bar", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + hcl.TraverseIndex{ + Key: cty.NumberIntVal(1), + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + End: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + }, + }, + hcl.TraverseAttr{ + Name: "bar", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 11, Byte: 10}, + }, + }, + }, + 0, + }, + { + "foo.", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + 1, // attribute name required + }, + { + "foo[", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + 1, // index required + }, + { + "foo[index]", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + 1, // index must be literal + }, + { + "foo[0", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + hcl.TraverseIndex{ + Key: cty.NumberIntVal(0), + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + }, + 1, // missing close bracket + }, + { + "foo 0", + hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + 1, // extra junk after traversal + }, + } + + for _, test := range tests { + t.Run(test.src, func(t *testing.T) { + got, diags := ParseTraversalAbs([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1}) + if len(diags) != test.diagCount { + for _, diag := range diags { + t.Logf(" - %s", diag.Error()) + } + t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.diagCount) + } + + if !reflect.DeepEqual(got, test.want) { + t.Errorf("wrong result\nsrc: %s\ngot: %s\nwant: %s", test.src, spew.Sdump(got), spew.Sdump(test.want)) + } + }) + } +} diff --git a/zcl/zclsyntax/parser.go b/hcl/hclsyntax/parser.go similarity index 82% rename from zcl/zclsyntax/parser.go rename to hcl/hclsyntax/parser.go index e4b63d7..0f81ddf 100644 --- a/zcl/zclsyntax/parser.go +++ b/hcl/hclsyntax/parser.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax import ( "bufio" @@ -6,7 +6,7 @@ import ( "fmt" "github.com/apparentlymart/go-textseg/textseg" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty/convert" ) @@ -21,13 +21,13 @@ type parser struct { recovery bool } -func (p *parser) ParseBody(end TokenType) (*Body, zcl.Diagnostics) { +func (p *parser) ParseBody(end TokenType) (*Body, hcl.Diagnostics) { attrs := Attributes{} blocks := Blocks{} - var diags zcl.Diagnostics + var diags hcl.Diagnostics startRange := p.PrevRange() - var endRange zcl.Range + var endRange hcl.Range Token: for { @@ -50,8 +50,8 @@ Token: blocks = append(blocks, titem) case *Attribute: if existing, exists := attrs[titem.Name]; exists { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Attribute redefined", Detail: fmt.Sprintf( "The attribute %q was already defined at %s. Each attribute may be defined only once.", @@ -77,15 +77,15 @@ Token: bad := p.Read() if !p.recovery { if bad.Type == TokenOQuote { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid attribute name", Detail: "Attribute names must not be quoted.", Subject: &bad.Range, }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Attribute or block definition required", Detail: "An attribute or block definition is required here.", Subject: &bad.Range, @@ -103,8 +103,8 @@ Token: Attributes: attrs, Blocks: blocks, - SrcRange: zcl.RangeBetween(startRange, endRange), - EndRange: zcl.Range{ + SrcRange: hcl.RangeBetween(startRange, endRange), + EndRange: hcl.Range{ Filename: endRange.Filename, Start: endRange.End, End: endRange.End, @@ -112,13 +112,13 @@ Token: }, diags } -func (p *parser) ParseBodyItem() (Node, zcl.Diagnostics) { +func (p *parser) ParseBodyItem() (Node, hcl.Diagnostics) { ident := p.Read() if ident.Type != TokenIdent { p.recoverAfterBodyItem() - return nil, zcl.Diagnostics{ + return nil, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Attribute or block definition required", Detail: "An attribute or block definition is required here.", Subject: &ident.Range, @@ -135,9 +135,9 @@ func (p *parser) ParseBodyItem() (Node, zcl.Diagnostics) { return p.finishParsingBodyBlock(ident) default: p.recoverAfterBodyItem() - return nil, zcl.Diagnostics{ + return nil, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Attribute or block definition required", Detail: "An attribute or block definition is required here. To define an attribute, use the equals sign \"=\" to introduce the attribute value.", Subject: &ident.Range, @@ -148,14 +148,14 @@ func (p *parser) ParseBodyItem() (Node, zcl.Diagnostics) { return nil, nil } -func (p *parser) finishParsingBodyAttribute(ident Token) (Node, zcl.Diagnostics) { +func (p *parser) finishParsingBodyAttribute(ident Token) (Node, hcl.Diagnostics) { eqTok := p.Read() // eat equals token if eqTok.Type != TokenEqual { // should never happen if caller behaves panic("finishParsingBodyAttribute called with next not equals") } - var endRange zcl.Range + var endRange hcl.Range expr, diags := p.ParseExpression() if p.recovery && diags.HasErrors() { @@ -169,20 +169,20 @@ func (p *parser) finishParsingBodyAttribute(ident Token) (Node, zcl.Diagnostics) if end.Type != TokenNewline { if !p.recovery { if end.Type == TokenEOF { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing newline after attribute definition", Detail: "A newline is required after an attribute definition at the end of a file.", Subject: &end.Range, - Context: zcl.RangeBetween(ident.Range, end.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, end.Range).Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing newline after attribute definition", Detail: "An attribute definition must end with a newline.", Subject: &end.Range, - Context: zcl.RangeBetween(ident.Range, end.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, end.Range).Ptr(), }) } } @@ -198,17 +198,17 @@ func (p *parser) finishParsingBodyAttribute(ident Token) (Node, zcl.Diagnostics) Name: string(ident.Bytes), Expr: expr, - SrcRange: zcl.RangeBetween(ident.Range, endRange), + SrcRange: hcl.RangeBetween(ident.Range, endRange), NameRange: ident.Range, EqualsRange: eqTok.Range, }, diags } -func (p *parser) finishParsingBodyBlock(ident Token) (Node, zcl.Diagnostics) { +func (p *parser) finishParsingBodyBlock(ident Token) (Node, hcl.Diagnostics) { var blockType = string(ident.Bytes) - var diags zcl.Diagnostics + var diags hcl.Diagnostics var labels []string - var labelRanges []zcl.Range + var labelRanges []hcl.Range var oBrace Token @@ -244,29 +244,29 @@ Token: default: switch tok.Type { case TokenEqual: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid block definition", Detail: "The equals sign \"=\" indicates an attribute definition, and must not be used when defining a block.", Subject: &tok.Range, - Context: zcl.RangeBetween(ident.Range, tok.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, tok.Range).Ptr(), }) case TokenNewline: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid block definition", Detail: "A block definition must have block content delimited by \"{\" and \"}\", starting on the same line as the block header.", Subject: &tok.Range, - Context: zcl.RangeBetween(ident.Range, tok.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, tok.Range).Ptr(), }) default: if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid block definition", Detail: "Either a quoted string block label or an opening brace (\"{\") is expected here.", Subject: &tok.Range, - Context: zcl.RangeBetween(ident.Range, tok.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, tok.Range).Ptr(), }) } } @@ -298,20 +298,20 @@ Token: } else { if !p.recovery { if eol.Type == TokenEOF { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing newline after block definition", Detail: "A newline is required after a block definition at the end of a file.", Subject: &eol.Range, - Context: zcl.RangeBetween(ident.Range, eol.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, eol.Range).Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing newline after block definition", Detail: "A block definition must end with a newline.", Subject: &eol.Range, - Context: zcl.RangeBetween(ident.Range, eol.Range).Ptr(), + Context: hcl.RangeBetween(ident.Range, eol.Range).Ptr(), }) } } @@ -330,11 +330,11 @@ Token: }, diags } -func (p *parser) ParseExpression() (Expression, zcl.Diagnostics) { +func (p *parser) ParseExpression() (Expression, hcl.Diagnostics) { return p.parseTernaryConditional() } -func (p *parser) parseTernaryConditional() (Expression, zcl.Diagnostics) { +func (p *parser) parseTernaryConditional() (Expression, hcl.Diagnostics) { // The ternary conditional operator (.. ? .. : ..) behaves somewhat // like a binary operator except that the "symbol" is itself // an expression enclosed in two punctuation characters. @@ -345,7 +345,7 @@ func (p *parser) parseTernaryConditional() (Expression, zcl.Diagnostics) { startRange := p.NextRange() var condExpr, trueExpr, falseExpr Expression - var diags zcl.Diagnostics + var diags hcl.Diagnostics condExpr, condDiags := p.parseBinaryOps(binaryOps) diags = append(diags, condDiags...) @@ -368,12 +368,12 @@ func (p *parser) parseTernaryConditional() (Expression, zcl.Diagnostics) { colon := p.Peek() if colon.Type != TokenColon { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing false expression in conditional", Detail: "The conditional operator (...?...:...) requires a false expression, delimited by a colon.", Subject: &colon.Range, - Context: zcl.RangeBetween(startRange, colon.Range).Ptr(), + Context: hcl.RangeBetween(startRange, colon.Range).Ptr(), }) return condExpr, diags } @@ -391,14 +391,14 @@ func (p *parser) parseTernaryConditional() (Expression, zcl.Diagnostics) { TrueResult: trueExpr, FalseResult: falseExpr, - SrcRange: zcl.RangeBetween(startRange, falseExpr.Range()), + SrcRange: hcl.RangeBetween(startRange, falseExpr.Range()), }, diags } // parseBinaryOps calls itself recursively to work through all of the // operator precedence groups, and then eventually calls parseExpressionTerm // for each operand. -func (p *parser) parseBinaryOps(ops []map[TokenType]*Operation) (Expression, zcl.Diagnostics) { +func (p *parser) parseBinaryOps(ops []map[TokenType]*Operation) (Expression, hcl.Diagnostics) { if len(ops) == 0 { // We've run out of operators, so now we'll just try to parse a term. return p.parseExpressionWithTraversals() @@ -409,7 +409,7 @@ func (p *parser) parseBinaryOps(ops []map[TokenType]*Operation) (Expression, zcl var lhs, rhs Expression var operation *Operation - var diags zcl.Diagnostics + var diags hcl.Diagnostics // Parse a term that might be the first operand of a binary // operation or it might just be a standalone term. @@ -443,13 +443,13 @@ func (p *parser) parseBinaryOps(ops []map[TokenType]*Operation) (Expression, zcl Op: operation, RHS: rhs, - SrcRange: zcl.RangeBetween(lhs.Range(), rhs.Range()), + SrcRange: hcl.RangeBetween(lhs.Range(), rhs.Range()), } } operation = newOp p.Read() // eat operator token - var rhsDiags zcl.Diagnostics + var rhsDiags hcl.Diagnostics rhs, rhsDiags = p.parseBinaryOps(remaining) diags = append(diags, rhsDiags...) if p.recovery && rhsDiags.HasErrors() { @@ -466,11 +466,11 @@ func (p *parser) parseBinaryOps(ops []map[TokenType]*Operation) (Expression, zcl Op: operation, RHS: rhs, - SrcRange: zcl.RangeBetween(lhs.Range(), rhs.Range()), + SrcRange: hcl.RangeBetween(lhs.Range(), rhs.Range()), }, diags } -func (p *parser) parseExpressionWithTraversals() (Expression, zcl.Diagnostics) { +func (p *parser) parseExpressionWithTraversals() (Expression, hcl.Diagnostics) { term, diags := p.parseExpressionTerm() ret := term @@ -488,8 +488,8 @@ Traversal: case TokenIdent: attrTok = p.Read() // eat token name := string(attrTok.Bytes) - rng := zcl.RangeBetween(dot.Range, attrTok.Range) - step := zcl.TraverseAttr{ + rng := hcl.RangeBetween(dot.Range, attrTok.Range) + step := hcl.TraverseAttr{ Name: name, SrcRange: rng, } @@ -503,8 +503,8 @@ Traversal: // to do attribute traversals into each of its elements, // whereas foo[*] can support _any_ traversal. marker := p.Read() // eat star - trav := make(zcl.Traversal, 0, 1) - var firstRange, lastRange zcl.Range + trav := make(hcl.Traversal, 0, 1) + var firstRange, lastRange hcl.Range firstRange = p.NextRange() for p.Peek().Type == TokenDot { dot := p.Read() @@ -518,9 +518,9 @@ Traversal: numTok := p.Read() numVal, numDiags := p.numberLitValue(numTok) diags = append(diags, numDiags...) - trav = append(trav, zcl.TraverseIndex{ + trav = append(trav, hcl.TraverseIndex{ Key: numVal, - SrcRange: zcl.RangeBetween(dot.Range, numTok.Range), + SrcRange: hcl.RangeBetween(dot.Range, numTok.Range), }) lastRange = numTok.Range continue @@ -529,15 +529,15 @@ Traversal: if p.Peek().Type != TokenIdent { if !p.recovery { if p.Peek().Type == TokenStar { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Nested splat expression not allowed", Detail: "A splat expression (*) cannot be used inside another attribute-only splat expression.", Subject: p.Peek().Range.Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid attribute name", Detail: "An attribute name is required after a dot.", Subject: &attrTok.Range, @@ -549,15 +549,15 @@ Traversal: } attrTok := p.Read() - trav = append(trav, zcl.TraverseAttr{ + trav = append(trav, hcl.TraverseAttr{ Name: string(attrTok.Bytes), - SrcRange: zcl.RangeBetween(dot.Range, attrTok.Range), + SrcRange: hcl.RangeBetween(dot.Range, attrTok.Range), }) lastRange = attrTok.Range } itemExpr := &AnonSymbolExpr{ - SrcRange: zcl.RangeBetween(dot.Range, marker.Range), + SrcRange: hcl.RangeBetween(dot.Range, marker.Range), } var travExpr Expression if len(trav) == 0 { @@ -566,7 +566,7 @@ Traversal: travExpr = &RelativeTraversalExpr{ Source: itemExpr, Traversal: trav, - SrcRange: zcl.RangeBetween(firstRange, lastRange), + SrcRange: hcl.RangeBetween(firstRange, lastRange), } } @@ -575,13 +575,13 @@ Traversal: Each: travExpr, Item: itemExpr, - SrcRange: zcl.RangeBetween(dot.Range, lastRange), - MarkerRange: zcl.RangeBetween(dot.Range, marker.Range), + SrcRange: hcl.RangeBetween(dot.Range, lastRange), + MarkerRange: hcl.RangeBetween(dot.Range, marker.Range), } default: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid attribute name", Detail: "An attribute name is required after a dot.", Subject: &attrTok.Range, @@ -598,7 +598,7 @@ Traversal: case TokenOBrack: // Indexing of a collection. - // This may or may not be a zcl.Traverser, depending on whether + // This may or may not be a hcl.Traverser, depending on whether // the key value is something constant. open := p.Read() @@ -613,8 +613,8 @@ Traversal: } else { close = p.Read() if close.Type != TokenCBrack && !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing close bracket on index", Detail: "The index operator must end with a closing bracket (\"]\").", Subject: &close.Range, @@ -626,14 +626,14 @@ Traversal: if lit, isLit := keyExpr.(*LiteralValueExpr); isLit { litKey, _ := lit.Value(nil) - rng := zcl.RangeBetween(open.Range, close.Range) - step := &zcl.TraverseIndex{ + rng := hcl.RangeBetween(open.Range, close.Range) + step := &hcl.TraverseIndex{ Key: litKey, SrcRange: rng, } ret = makeRelativeTraversal(ret, step, rng) } else { - rng := zcl.RangeBetween(open.Range, close.Range) + rng := hcl.RangeBetween(open.Range, close.Range) ret = &IndexExpr{ Collection: ret, Key: keyExpr, @@ -655,26 +655,26 @@ Traversal: // a traversal expression that combines the two. If the given expression // is already a traversal, it is extended in place (mutating it) and // returned. If it isn't, a new RelativeTraversalExpr is created and returned. -func makeRelativeTraversal(expr Expression, next zcl.Traverser, rng zcl.Range) Expression { +func makeRelativeTraversal(expr Expression, next hcl.Traverser, rng hcl.Range) Expression { switch texpr := expr.(type) { case *ScopeTraversalExpr: texpr.Traversal = append(texpr.Traversal, next) - texpr.SrcRange = zcl.RangeBetween(texpr.SrcRange, rng) + texpr.SrcRange = hcl.RangeBetween(texpr.SrcRange, rng) return texpr case *RelativeTraversalExpr: texpr.Traversal = append(texpr.Traversal, next) - texpr.SrcRange = zcl.RangeBetween(texpr.SrcRange, rng) + texpr.SrcRange = hcl.RangeBetween(texpr.SrcRange, rng) return texpr default: return &RelativeTraversalExpr{ Source: expr, - Traversal: zcl.Traversal{next}, + Traversal: hcl.Traversal{next}, SrcRange: rng, } } } -func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { +func (p *parser) parseExpressionTerm() (Expression, hcl.Diagnostics) { start := p.Peek() switch start.Type { @@ -695,12 +695,12 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { close := p.Peek() if close.Type != TokenCParen { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unbalanced parentheses", Detail: "Expected a closing parenthesis to terminate the expression.", Subject: &close.Range, - Context: zcl.RangeBetween(start.Range, close.Range).Ptr(), + Context: hcl.RangeBetween(start.Range, close.Range).Ptr(), }) p.setRecovery() } @@ -745,8 +745,8 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { }, nil default: return &ScopeTraversalExpr{ - Traversal: zcl.Traversal{ - zcl.TraverseRoot{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ Name: name, SrcRange: tok.Range, }, @@ -768,13 +768,13 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { } return &TemplateWrapExpr{ Wrapped: exprs[0], - SrcRange: zcl.RangeBetween(open.Range, closeRange), + SrcRange: hcl.RangeBetween(open.Range, closeRange), }, diags } return &TemplateExpr{ Parts: exprs, - SrcRange: zcl.RangeBetween(open.Range, closeRange), + SrcRange: hcl.RangeBetween(open.Range, closeRange), }, diags case TokenMinus: @@ -789,7 +789,7 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { Op: OpNegate, Val: operand, - SrcRange: zcl.RangeBetween(tok.Range, operand.Range()), + SrcRange: hcl.RangeBetween(tok.Range, operand.Range()), SymbolRange: tok.Range, }, diags @@ -804,7 +804,7 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { Op: OpLogicalNot, Val: operand, - SrcRange: zcl.RangeBetween(tok.Range, operand.Range()), + SrcRange: hcl.RangeBetween(tok.Range, operand.Range()), SymbolRange: tok.Range, }, diags @@ -815,10 +815,10 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { return p.parseObjectCons() default: - var diags zcl.Diagnostics + var diags hcl.Diagnostics if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid expression", Detail: "Expected the start of an expression, but found an invalid expression token.", Subject: &start.Range, @@ -835,7 +835,7 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) { } } -func (p *parser) numberLitValue(tok Token) (cty.Value, zcl.Diagnostics) { +func (p *parser) numberLitValue(tok Token) (cty.Value, hcl.Diagnostics) { // We'll lean on the cty converter to do the conversion, to ensure that // the behavior is the same as what would happen if converting a // non-literal string to a number. @@ -843,9 +843,9 @@ func (p *parser) numberLitValue(tok Token) (cty.Value, zcl.Diagnostics) { numVal, err := convert.Convert(numStrVal, cty.Number) if err != nil { ret := cty.UnknownVal(cty.Number) - return ret, zcl.Diagnostics{ + return ret, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Invalid number literal", // FIXME: not a very good error message, but convert only // gives us "a number is required", so not much help either. @@ -860,7 +860,7 @@ func (p *parser) numberLitValue(tok Token) (cty.Value, zcl.Diagnostics) { // finishParsingFunctionCall parses a function call assuming that the function // name was already read, and so the peeker should be pointing at the opening // parenthesis after the name. -func (p *parser) finishParsingFunctionCall(name Token) (Expression, zcl.Diagnostics) { +func (p *parser) finishParsingFunctionCall(name Token) (Expression, hcl.Diagnostics) { openTok := p.Read() if openTok.Type != TokenOParen { // should never happen if callers behave @@ -868,7 +868,7 @@ func (p *parser) finishParsingFunctionCall(name Token) (Expression, zcl.Diagnost } var args []Expression - var diags zcl.Diagnostics + var diags hcl.Diagnostics var expandFinal bool var closeTok Token @@ -906,12 +906,12 @@ Token: if p.Peek().Type != TokenCParen { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing closing parenthesis", Detail: "An expanded function argument (with ...) must be immediately followed by closing parentheses.", Subject: &sep.Range, - Context: zcl.RangeBetween(name.Range, sep.Range).Ptr(), + Context: hcl.RangeBetween(name.Range, sep.Range).Ptr(), }) } closeTok = p.recover(TokenCParen) @@ -922,12 +922,12 @@ Token: } if sep.Type != TokenComma { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing argument separator", Detail: "A comma is required to separate each function argument from the next.", Subject: &sep.Range, - Context: zcl.RangeBetween(name.Range, sep.Range).Ptr(), + Context: hcl.RangeBetween(name.Range, sep.Range).Ptr(), }) closeTok = p.recover(TokenCParen) break Token @@ -955,7 +955,7 @@ Token: }, diags } -func (p *parser) parseTupleCons() (Expression, zcl.Diagnostics) { +func (p *parser) parseTupleCons() (Expression, hcl.Diagnostics) { open := p.Read() if open.Type != TokenOBrack { // Should never happen if callers are behaving @@ -971,7 +971,7 @@ func (p *parser) parseTupleCons() (Expression, zcl.Diagnostics) { var close Token - var diags zcl.Diagnostics + var diags hcl.Diagnostics var exprs []Expression for { @@ -1001,12 +1001,12 @@ func (p *parser) parseTupleCons() (Expression, zcl.Diagnostics) { if next.Type != TokenComma { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing item separator", Detail: "Expected a comma to mark the beginning of the next item.", Subject: &next.Range, - Context: zcl.RangeBetween(open.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, next.Range).Ptr(), }) } close = p.recover(TokenCBrack) @@ -1020,12 +1020,12 @@ func (p *parser) parseTupleCons() (Expression, zcl.Diagnostics) { return &TupleConsExpr{ Exprs: exprs, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), OpenRange: open.Range, }, diags } -func (p *parser) parseObjectCons() (Expression, zcl.Diagnostics) { +func (p *parser) parseObjectCons() (Expression, hcl.Diagnostics) { open := p.Read() if open.Type != TokenOBrace { // Should never happen if callers are behaving @@ -1041,7 +1041,7 @@ func (p *parser) parseObjectCons() (Expression, zcl.Diagnostics) { var close Token - var diags zcl.Diagnostics + var diags hcl.Diagnostics var items []ObjectConsItem for { @@ -1061,7 +1061,7 @@ func (p *parser) parseObjectCons() (Expression, zcl.Diagnostics) { // directly as key expression, and must instead be wrapped in some // disambiguation punctuation, like (var.a) = "b" or "${var.a}" = "b". var key Expression - var keyDiags zcl.Diagnostics + var keyDiags hcl.Diagnostics if p.Peek().Type == TokenIdent { nameTok := p.Read() key = &LiteralValueExpr{ @@ -1087,20 +1087,20 @@ func (p *parser) parseObjectCons() (Expression, zcl.Diagnostics) { if next.Type != TokenEqual && next.Type != TokenColon { if !p.recovery { if next.Type == TokenNewline || next.Type == TokenComma { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing item value", Detail: "Expected an item value, introduced by an equals sign (\"=\").", Subject: &next.Range, - Context: zcl.RangeBetween(open.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, next.Range).Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing key/value separator", Detail: "Expected an equals sign (\"=\") to mark the beginning of the item value.", Subject: &next.Range, - Context: zcl.RangeBetween(open.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, next.Range).Ptr(), }) } } @@ -1134,12 +1134,12 @@ func (p *parser) parseObjectCons() (Expression, zcl.Diagnostics) { if next.Type != TokenComma && next.Type != TokenNewline { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing item separator", Detail: "Expected a newline or comma to mark the beginning of the next item.", Subject: &next.Range, - Context: zcl.RangeBetween(open.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, next.Range).Ptr(), }) } close = p.recover(TokenCBrace) @@ -1153,12 +1153,12 @@ func (p *parser) parseObjectCons() (Expression, zcl.Diagnostics) { return &ObjectConsExpr{ Items: items, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), OpenRange: open.Range, }, diags } -func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) { +func (p *parser) finishParsingForExpr(open Token) (Expression, hcl.Diagnostics) { introducer := p.Read() if !forKeyword.TokenMatches(introducer) { // Should never happen if callers are behaving @@ -1179,23 +1179,23 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) panic("finishParsingForExpr called with invalid open token") } - var diags zcl.Diagnostics + var diags hcl.Diagnostics var keyName, valName string if p.Peek().Type != TokenIdent { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "For expression requires variable name after 'for'.", Subject: p.Peek().Range.Ptr(), - Context: zcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), + Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), }) } close := p.recover(closeType) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } @@ -1208,18 +1208,18 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) if p.Peek().Type != TokenIdent { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "For expression requires value variable name after comma.", Subject: p.Peek().Range.Ptr(), - Context: zcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), + Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), }) } close := p.recover(closeType) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } @@ -1228,18 +1228,18 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) if !inKeyword.TokenMatches(p.Peek()) { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "For expression requires 'in' keyword after names.", Subject: p.Peek().Range.Ptr(), - Context: zcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), + Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), }) } close := p.recover(closeType) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } p.Read() // eat 'in' keyword @@ -1250,30 +1250,30 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) close := p.recover(closeType) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } if p.Peek().Type != TokenColon { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "For expression requires colon after collection expression.", Subject: p.Peek().Range.Ptr(), - Context: zcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), + Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), }) } close := p.recover(closeType) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } p.Read() // eat colon var keyExpr, valExpr Expression - var keyDiags, valDiags zcl.Diagnostics + var keyDiags, valDiags hcl.Diagnostics valExpr, valDiags = p.ParseExpression() if p.Peek().Type == TokenFatArrow { // What we just parsed was actually keyExpr @@ -1288,7 +1288,7 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) close := p.recover(closeType) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } @@ -1300,7 +1300,7 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) } var condExpr Expression - var condDiags zcl.Diagnostics + var condDiags hcl.Diagnostics if ifKeyword.TokenMatches(p.Peek()) { p.Read() // eat "if" condExpr, condDiags = p.ParseExpression() @@ -1309,7 +1309,7 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) close := p.recover(p.oppositeBracket(open.Type)) return &LiteralValueExpr{ Val: cty.DynamicVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }, diags } } @@ -1319,12 +1319,12 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) close = p.Read() } else { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "Extra characters after the end of the 'for' expression.", Subject: p.Peek().Range.Ptr(), - Context: zcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), + Context: hcl.RangeBetween(open.Range, p.Peek().Range).Ptr(), }) } close = p.recover(closeType) @@ -1332,32 +1332,32 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) if !makeObj { if keyExpr != nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "Key expression is not valid when building a tuple.", Subject: keyExpr.Range().Ptr(), - Context: zcl.RangeBetween(open.Range, close.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, close.Range).Ptr(), }) } if group { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "Grouping ellipsis (...) cannot be used when building a tuple.", Subject: &ellipsis.Range, - Context: zcl.RangeBetween(open.Range, close.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, close.Range).Ptr(), }) } } else { if keyExpr == nil { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' expression", Detail: "Key expression is required when building an object.", Subject: valExpr.Range().Ptr(), - Context: zcl.RangeBetween(open.Range, close.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, close.Range).Ptr(), }) } } @@ -1371,7 +1371,7 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) CondExpr: condExpr, Group: group, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), OpenRange: open.Range, CloseRange: close.Range, }, diags @@ -1379,12 +1379,12 @@ func (p *parser) finishParsingForExpr(open Token) (Expression, zcl.Diagnostics) // parseQuotedStringLiteral is a helper for parsing quoted strings that // aren't allowed to contain any interpolations, such as block labels. -func (p *parser) parseQuotedStringLiteral() (string, zcl.Range, zcl.Diagnostics) { +func (p *parser) parseQuotedStringLiteral() (string, hcl.Range, hcl.Diagnostics) { oQuote := p.Read() if oQuote.Type != TokenOQuote { - return "", oQuote.Range, zcl.Diagnostics{ + return "", oQuote.Range, hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Invalid string literal", Detail: "A quoted string is required here.", Subject: &oQuote.Range, @@ -1392,7 +1392,7 @@ func (p *parser) parseQuotedStringLiteral() (string, zcl.Range, zcl.Diagnostics) } } - var diags zcl.Diagnostics + var diags hcl.Diagnostics ret := &bytes.Buffer{} var cQuote Token @@ -1416,36 +1416,36 @@ Token: which = "!" } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid string literal", Detail: fmt.Sprintf( "Template sequences are not allowed in this string. To include a literal %q, double it (as \"%s%s\") to escape it.", which, which, which, ), Subject: &tok.Range, - Context: zcl.RangeBetween(oQuote.Range, tok.Range).Ptr(), + Context: hcl.RangeBetween(oQuote.Range, tok.Range).Ptr(), }) p.recover(TokenTemplateSeqEnd) case TokenEOF: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unterminated string literal", Detail: "Unable to find the closing quote mark before the end of the file.", Subject: &tok.Range, - Context: zcl.RangeBetween(oQuote.Range, tok.Range).Ptr(), + Context: hcl.RangeBetween(oQuote.Range, tok.Range).Ptr(), }) break Token default: // Should never happen, as long as the scanner is behaving itself - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid string literal", Detail: "This item is not valid in a string literal.", Subject: &tok.Range, - Context: zcl.RangeBetween(oQuote.Range, tok.Range).Ptr(), + Context: hcl.RangeBetween(oQuote.Range, tok.Range).Ptr(), }) p.recover(TokenOQuote) break Token @@ -1454,7 +1454,7 @@ Token: } - return ret.String(), zcl.RangeBetween(oQuote.Range, cQuote.Range), diags + return ret.String(), hcl.RangeBetween(oQuote.Range, cQuote.Range), diags } // decodeStringLit processes the given token, which must be either a @@ -1463,7 +1463,7 @@ Token: // // If any error diagnostics are returned, the returned string may be incomplete // or otherwise invalid. -func (p *parser) decodeStringLit(tok Token) (string, zcl.Diagnostics) { +func (p *parser) decodeStringLit(tok Token) (string, hcl.Diagnostics) { var quoted bool switch tok.Type { case TokenQuotedLit: @@ -1473,7 +1473,7 @@ func (p *parser) decodeStringLit(tok Token) (string, zcl.Diagnostics) { default: panic("decodeQuotedLit can only be used with TokenStringLit and TokenQuotedLit tokens") } - var diags zcl.Diagnostics + var diags hcl.Diagnostics ret := make([]byte, 0, len(tok.Bytes)) var esc []byte @@ -1540,18 +1540,18 @@ Character: detail = fmt.Sprintf("The characters \"\\%s\" do not form a recognized escape sequence.", ch) } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid escape sequence", Detail: detail, - Subject: &zcl.Range{ + Subject: &hcl.Range{ Filename: tok.Range.Filename, - Start: zcl.Pos{ + Start: hcl.Pos{ Line: pos.Line, Column: pos.Column - 1, // safe because we know the previous character must be a backslash Byte: pos.Byte - 1, }, - End: zcl.Pos{ + End: hcl.Pos{ Line: pos.Line, Column: pos.Column + 1, // safe because we know the previous character must be a backslash Byte: pos.Byte + len(ch), @@ -1772,7 +1772,7 @@ func (p *parser) oppositeBracket(ty TokenType) TokenType { } } -func errPlaceholderExpr(rng zcl.Range) Expression { +func errPlaceholderExpr(rng hcl.Range) Expression { return &LiteralValueExpr{ Val: cty.DynamicVal, SrcRange: rng, diff --git a/zcl/zclsyntax/parser_template.go b/hcl/hclsyntax/parser_template.go similarity index 83% rename from zcl/zclsyntax/parser_template.go rename to hcl/hclsyntax/parser_template.go index 3b56192..e04c8e0 100644 --- a/zcl/zclsyntax/parser_template.go +++ b/hcl/hclsyntax/parser_template.go @@ -1,19 +1,19 @@ -package zclsyntax +package hclsyntax import ( "fmt" "strings" "unicode" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) -func (p *parser) ParseTemplate() (Expression, zcl.Diagnostics) { +func (p *parser) ParseTemplate() (Expression, hcl.Diagnostics) { return p.parseTemplate(TokenEOF) } -func (p *parser) parseTemplate(end TokenType) (Expression, zcl.Diagnostics) { +func (p *parser) parseTemplate(end TokenType) (Expression, hcl.Diagnostics) { exprs, passthru, rng, diags := p.parseTemplateInner(end) if passthru { @@ -32,7 +32,7 @@ func (p *parser) parseTemplate(end TokenType) (Expression, zcl.Diagnostics) { }, diags } -func (p *parser) parseTemplateInner(end TokenType) ([]Expression, bool, zcl.Range, zcl.Diagnostics) { +func (p *parser) parseTemplateInner(end TokenType) ([]Expression, bool, hcl.Range, hcl.Diagnostics) { parts, diags := p.parseTemplateParts(end) tp := templateParser{ Tokens: parts.Tokens, @@ -53,14 +53,14 @@ func (p *parser) parseTemplateInner(end TokenType) ([]Expression, bool, zcl.Rang type templateParser struct { Tokens []templateToken - SrcRange zcl.Range + SrcRange hcl.Range pos int } -func (p *templateParser) parseRoot() ([]Expression, zcl.Diagnostics) { +func (p *templateParser) parseRoot() ([]Expression, hcl.Diagnostics) { var exprs []Expression - var diags zcl.Diagnostics + var diags hcl.Diagnostics for { next := p.Peek() @@ -76,7 +76,7 @@ func (p *templateParser) parseRoot() ([]Expression, zcl.Diagnostics) { return exprs, diags } -func (p *templateParser) parseExpr() (Expression, zcl.Diagnostics) { +func (p *templateParser) parseExpr() (Expression, hcl.Diagnostics) { next := p.Peek() switch tok := next.(type) { @@ -99,12 +99,12 @@ func (p *templateParser) parseExpr() (Expression, zcl.Diagnostics) { case *templateEndToken: p.Read() // eat erroneous token - return errPlaceholderExpr(tok.SrcRange), zcl.Diagnostics{ + return errPlaceholderExpr(tok.SrcRange), hcl.Diagnostics{ { // This is a particularly unhelpful diagnostic, so callers // should attempt to pre-empt it and produce a more helpful // diagnostic that is context-aware. - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: "Unexpected end of template", Detail: "The control directives within this template are unbalanced.", Subject: &tok.SrcRange, @@ -113,9 +113,9 @@ func (p *templateParser) parseExpr() (Expression, zcl.Diagnostics) { case *templateEndCtrlToken: p.Read() // eat erroneous token - return errPlaceholderExpr(tok.SrcRange), zcl.Diagnostics{ + return errPlaceholderExpr(tok.SrcRange), hcl.Diagnostics{ { - Severity: zcl.DiagError, + Severity: hcl.DiagError, Summary: fmt.Sprintf("Unexpected %s directive", tok.Name()), Detail: "The control directives within this template are unbalanced.", Subject: &tok.SrcRange, @@ -128,7 +128,7 @@ func (p *templateParser) parseExpr() (Expression, zcl.Diagnostics) { } } -func (p *templateParser) parseIf() (Expression, zcl.Diagnostics) { +func (p *templateParser) parseIf() (Expression, hcl.Diagnostics) { open := p.Read() openIf, isIf := open.(*templateIfToken) if !isIf { @@ -137,16 +137,16 @@ func (p *templateParser) parseIf() (Expression, zcl.Diagnostics) { } var ifExprs, elseExprs []Expression - var diags zcl.Diagnostics - var endifRange zcl.Range + var diags hcl.Diagnostics + var endifRange hcl.Range currentExprs := &ifExprs Token: for { next := p.Peek() if end, isEnd := next.(*templateEndToken); isEnd { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unexpected end of template", Detail: fmt.Sprintf( "The if directive at %s is missing its corresponding endif directive.", @@ -167,8 +167,8 @@ Token: continue Token } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unexpected else directive", Detail: fmt.Sprintf( "Already in the else clause for the if started at %s.", @@ -182,8 +182,8 @@ Token: break Token default: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Unexpected %s directive", end.Name()), Detail: fmt.Sprintf( "Expecting an endif directive for the if started at %s.", @@ -204,7 +204,7 @@ Token: if len(ifExprs) == 0 { ifExprs = append(ifExprs, &LiteralValueExpr{ Val: cty.StringVal(""), - SrcRange: zcl.Range{ + SrcRange: hcl.Range{ Filename: openIf.SrcRange.Filename, Start: openIf.SrcRange.End, End: openIf.SrcRange.End, @@ -214,7 +214,7 @@ Token: if len(elseExprs) == 0 { elseExprs = append(elseExprs, &LiteralValueExpr{ Val: cty.StringVal(""), - SrcRange: zcl.Range{ + SrcRange: hcl.Range{ Filename: endifRange.Filename, Start: endifRange.Start, End: endifRange.Start, @@ -224,11 +224,11 @@ Token: trueExpr := &TemplateExpr{ Parts: ifExprs, - SrcRange: zcl.RangeBetween(ifExprs[0].Range(), ifExprs[len(ifExprs)-1].Range()), + SrcRange: hcl.RangeBetween(ifExprs[0].Range(), ifExprs[len(ifExprs)-1].Range()), } falseExpr := &TemplateExpr{ Parts: elseExprs, - SrcRange: zcl.RangeBetween(elseExprs[0].Range(), elseExprs[len(elseExprs)-1].Range()), + SrcRange: hcl.RangeBetween(elseExprs[0].Range(), elseExprs[len(elseExprs)-1].Range()), } return &ConditionalExpr{ @@ -236,11 +236,11 @@ Token: TrueResult: trueExpr, FalseResult: falseExpr, - SrcRange: zcl.RangeBetween(openIf.SrcRange, endifRange), + SrcRange: hcl.RangeBetween(openIf.SrcRange, endifRange), }, diags } -func (p *templateParser) parseFor() (Expression, zcl.Diagnostics) { +func (p *templateParser) parseFor() (Expression, hcl.Diagnostics) { open := p.Read() openFor, isFor := open.(*templateForToken) if !isFor { @@ -249,15 +249,15 @@ func (p *templateParser) parseFor() (Expression, zcl.Diagnostics) { } var contentExprs []Expression - var diags zcl.Diagnostics - var endforRange zcl.Range + var diags hcl.Diagnostics + var endforRange hcl.Range Token: for { next := p.Peek() if end, isEnd := next.(*templateEndToken); isEnd { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unexpected end of template", Detail: fmt.Sprintf( "The for directive at %s is missing its corresponding endfor directive.", @@ -273,8 +273,8 @@ Token: switch end.Type { case templateElse: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unexpected else directive", Detail: "An else clause is not expected for a for directive.", Subject: &end.SrcRange, @@ -285,8 +285,8 @@ Token: break Token default: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Unexpected %s directive", end.Name()), Detail: fmt.Sprintf( "Expecting an endfor directive corresponding to the for directive at %s.", @@ -307,7 +307,7 @@ Token: if len(contentExprs) == 0 { contentExprs = append(contentExprs, &LiteralValueExpr{ Val: cty.StringVal(""), - SrcRange: zcl.Range{ + SrcRange: hcl.Range{ Filename: openFor.SrcRange.Filename, Start: openFor.SrcRange.End, End: openFor.SrcRange.End, @@ -317,7 +317,7 @@ Token: contentExpr := &TemplateExpr{ Parts: contentExprs, - SrcRange: zcl.RangeBetween(contentExprs[0].Range(), contentExprs[len(contentExprs)-1].Range()), + SrcRange: hcl.RangeBetween(contentExprs[0].Range(), contentExprs[len(contentExprs)-1].Range()), } forExpr := &ForExpr{ @@ -327,7 +327,7 @@ Token: CollExpr: openFor.CollExpr, ValExpr: contentExpr, - SrcRange: zcl.RangeBetween(openFor.SrcRange, endforRange), + SrcRange: hcl.RangeBetween(openFor.SrcRange, endforRange), OpenRange: openFor.SrcRange, CloseRange: endforRange, } @@ -354,14 +354,14 @@ func (p *templateParser) Read() templateToken { // sequences, or control flow markers. // // A further pass is required on the result to turn it into an AST. -func (p *parser) parseTemplateParts(end TokenType) (*templateParts, zcl.Diagnostics) { +func (p *parser) parseTemplateParts(end TokenType) (*templateParts, hcl.Diagnostics) { var parts []templateToken - var diags zcl.Diagnostics + var diags hcl.Diagnostics startRange := p.NextRange() ltrimNext := false nextCanTrimPrev := false - var endRange zcl.Range + var endRange hcl.Range Token: for { @@ -409,12 +409,12 @@ Token: close := p.Peek() if close.Type != TokenTemplateSeqEnd { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Extra characters after interpolation expression", Detail: "Expected a closing brace to end the interpolation expression, but found extra characters.", Subject: &close.Range, - Context: zcl.RangeBetween(startRange, close.Range).Ptr(), + Context: hcl.RangeBetween(startRange, close.Range).Ptr(), }) } p.recover(TokenTemplateSeqEnd) @@ -431,7 +431,7 @@ Token: p.PopIncludeNewlines() parts = append(parts, &templateInterpToken{ Expr: expr, - SrcRange: zcl.RangeBetween(next.Range, close.Range), + SrcRange: hcl.RangeBetween(next.Range, close.Range), }) case TokenTemplateControl: @@ -449,12 +449,12 @@ Token: kw := p.Peek() if kw.Type != TokenIdent { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid template directive", Detail: "A template directive keyword (\"if\", \"for\", etc) is expected at the beginning of a !{ sequence.", Subject: &kw.Range, - Context: zcl.RangeBetween(next.Range, kw.Range).Ptr(), + Context: hcl.RangeBetween(next.Range, kw.Range).Ptr(), }) } p.recover(TokenTemplateSeqEnd) @@ -470,27 +470,27 @@ Token: diags = append(diags, exprDiags...) parts = append(parts, &templateIfToken{ CondExpr: condExpr, - SrcRange: zcl.RangeBetween(next.Range, p.NextRange()), + SrcRange: hcl.RangeBetween(next.Range, p.NextRange()), }) case elseKeyword.TokenMatches(kw): parts = append(parts, &templateEndCtrlToken{ Type: templateElse, - SrcRange: zcl.RangeBetween(next.Range, p.NextRange()), + SrcRange: hcl.RangeBetween(next.Range, p.NextRange()), }) case endifKeyword.TokenMatches(kw): parts = append(parts, &templateEndCtrlToken{ Type: templateEndIf, - SrcRange: zcl.RangeBetween(next.Range, p.NextRange()), + SrcRange: hcl.RangeBetween(next.Range, p.NextRange()), }) case forKeyword.TokenMatches(kw): var keyName, valName string if p.Peek().Type != TokenIdent { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' directive", Detail: "For directive requires variable name after 'for'.", Subject: p.Peek().Range.Ptr(), @@ -510,8 +510,8 @@ Token: if p.Peek().Type != TokenIdent { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' directive", Detail: "For directive requires value variable name after comma.", Subject: p.Peek().Range.Ptr(), @@ -527,8 +527,8 @@ Token: if !inKeyword.TokenMatches(p.Peek()) { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid 'for' directive", Detail: "For directive requires 'in' keyword after names.", Subject: p.Peek().Range.Ptr(), @@ -547,13 +547,13 @@ Token: ValVar: valName, CollExpr: collExpr, - SrcRange: zcl.RangeBetween(next.Range, p.NextRange()), + SrcRange: hcl.RangeBetween(next.Range, p.NextRange()), }) case endforKeyword.TokenMatches(kw): parts = append(parts, &templateEndCtrlToken{ Type: templateEndFor, - SrcRange: zcl.RangeBetween(next.Range, p.NextRange()), + SrcRange: hcl.RangeBetween(next.Range, p.NextRange()), }) default: @@ -565,12 +565,12 @@ Token: suggestion = fmt.Sprintf(" Did you mean %q?", suggestion) } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid template control keyword", Detail: fmt.Sprintf("%q is not a valid template control keyword.%s", given, suggestion), Subject: &kw.Range, - Context: zcl.RangeBetween(next.Range, kw.Range).Ptr(), + Context: hcl.RangeBetween(next.Range, kw.Range).Ptr(), }) } p.recover(TokenTemplateSeqEnd) @@ -582,12 +582,12 @@ Token: close := p.Peek() if close.Type != TokenTemplateSeqEnd { if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Extra characters in %s marker", kw.Bytes), Detail: "Expected a closing brace to end the sequence, but found extra characters.", Subject: &close.Range, - Context: zcl.RangeBetween(startRange, close.Range).Ptr(), + Context: hcl.RangeBetween(startRange, close.Range).Ptr(), }) } p.recover(TokenTemplateSeqEnd) @@ -605,12 +605,12 @@ Token: default: if !p.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unterminated template string", Detail: "No closing marker was found for the string.", Subject: &next.Range, - Context: zcl.RangeBetween(startRange, next.Range).Ptr(), + Context: hcl.RangeBetween(startRange, next.Range).Ptr(), }) } final := p.recover(end) @@ -625,7 +625,7 @@ Token: // if they write "" in configuration. parts = append(parts, &templateLiteralToken{ Val: "", - SrcRange: zcl.Range{ + SrcRange: hcl.Range{ // Range is the zero-character span immediately after the // opening quote. Filename: startRange.Filename, @@ -643,7 +643,7 @@ Token: ret := &templateParts{ Tokens: parts, - SrcRange: zcl.RangeBetween(startRange, endRange), + SrcRange: hcl.RangeBetween(startRange, endRange), } return ret, diags @@ -651,7 +651,7 @@ Token: type templateParts struct { Tokens []templateToken - SrcRange zcl.Range + SrcRange hcl.Range } // templateToken is a higher-level token that represents a single atom within @@ -664,19 +664,19 @@ type templateToken interface { type templateLiteralToken struct { Val string - SrcRange zcl.Range + SrcRange hcl.Range isTemplateToken } type templateInterpToken struct { Expr Expression - SrcRange zcl.Range + SrcRange hcl.Range isTemplateToken } type templateIfToken struct { CondExpr Expression - SrcRange zcl.Range + SrcRange hcl.Range isTemplateToken } @@ -684,7 +684,7 @@ type templateForToken struct { KeyVar string // empty if ignoring key ValVar string CollExpr Expression - SrcRange zcl.Range + SrcRange hcl.Range isTemplateToken } @@ -698,7 +698,7 @@ const ( type templateEndCtrlToken struct { Type templateEndCtrlType - SrcRange zcl.Range + SrcRange hcl.Range isTemplateToken } @@ -717,7 +717,7 @@ func (t *templateEndCtrlToken) Name() string { } type templateEndToken struct { - SrcRange zcl.Range + SrcRange hcl.Range isTemplateToken } diff --git a/hcl/hclsyntax/parser_test.go b/hcl/hclsyntax/parser_test.go new file mode 100644 index 0000000..df08cf7 --- /dev/null +++ b/hcl/hclsyntax/parser_test.go @@ -0,0 +1,823 @@ +package hclsyntax + +import ( + "reflect" + "testing" + + "github.com/davecgh/go-spew/spew" + "github.com/hashicorp/hcl2/hcl" + "github.com/kylelemons/godebug/pretty" + "github.com/zclconf/go-cty/cty" +) + +func TestParseConfig(t *testing.T) { + tests := []struct { + input string + diagCount int + want *Body + }{ + { + ``, + 0, + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + }, + }, + }, + + { + "block {}\n", + 0, + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: nil, + Body: &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + }, + }, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + LabelRanges: nil, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 9}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 9}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 9}, + }, + }, + }, + { + "block {}block {}\n", + 1, // missing newline after block definition + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: nil, + Body: &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + }, + }, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + LabelRanges: nil, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 9, Byte: 8}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 17}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 17}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 17}, + }, + }, + }, + { + "block \"foo\" {}\n", + 0, + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: []string{"foo"}, + Body: &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + End: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + End: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + }, + }, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + LabelRanges: []hcl.Range{ + { + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + End: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + End: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + }, + }, + }, + { + ` +block "invalid" 1.2 {} +block "valid" {} +`, + 1, + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: []string{"invalid"}, + Body: nil, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, + End: hcl.Pos{Line: 2, Column: 6, Byte: 6}, + }, + LabelRanges: []hcl.Range{ + { + Start: hcl.Pos{Line: 2, Column: 7, Byte: 7}, + End: hcl.Pos{Line: 2, Column: 16, Byte: 16}, + }, + }, + + // Since we failed parsing before we got to the + // braces, the type range is used as a placeholder + // for these. + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, + End: hcl.Pos{Line: 2, Column: 6, Byte: 6}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, + End: hcl.Pos{Line: 2, Column: 6, Byte: 6}, + }, + }, + + // Recovery behavior should allow us to still see this + // second block, even though the first was invalid. + &Block{ + Type: "block", + Labels: []string{"valid"}, + Body: &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 15, Byte: 38}, + End: hcl.Pos{Line: 3, Column: 17, Byte: 40}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 17, Byte: 40}, + End: hcl.Pos{Line: 3, Column: 17, Byte: 40}, + }, + }, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 1, Byte: 24}, + End: hcl.Pos{Line: 3, Column: 6, Byte: 29}, + }, + LabelRanges: []hcl.Range{ + { + Start: hcl.Pos{Line: 3, Column: 7, Byte: 30}, + End: hcl.Pos{Line: 3, Column: 14, Byte: 37}, + }, + }, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 15, Byte: 38}, + End: hcl.Pos{Line: 3, Column: 16, Byte: 39}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 3, Column: 16, Byte: 39}, + End: hcl.Pos{Line: 3, Column: 17, Byte: 40}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 4, Column: 1, Byte: 41}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 4, Column: 1, Byte: 41}, + End: hcl.Pos{Line: 4, Column: 1, Byte: 41}, + }, + }, + }, + { + `block "f\o" {} +`, + 1, // "\o" is not a valid escape sequence + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: []string{"fo"}, + Body: nil, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + LabelRanges: []hcl.Range{ + { + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + }, + }, + }, + { + `block "f\n" {} +`, + 0, + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{ + &Block{ + Type: "block", + Labels: []string{"f\n"}, + Body: &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + End: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + End: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + }, + }, + + TypeRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + LabelRanges: []hcl.Range{ + { + Start: hcl.Pos{Line: 1, Column: 7, Byte: 6}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + OpenBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 13, Byte: 12}, + End: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + }, + CloseBraceRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + End: hcl.Pos{Line: 1, Column: 15, Byte: 14}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 15}, + }, + }, + }, + + { + "a = 1\n", + 0, + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &LiteralValueExpr{ + Val: cty.NumberIntVal(1), + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 6}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 6}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 6}, + }, + }, + }, + { + "a = \"hello ${true}\"\n", + 0, + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &TemplateExpr{ + Parts: []Expression{ + &LiteralValueExpr{ + Val: cty.StringVal("hello "), + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + &LiteralValueExpr{ + Val: cty.True, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 14, Byte: 13}, + End: hcl.Pos{Line: 1, Column: 18, Byte: 17}, + }, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 20, Byte: 19}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 20, Byte: 19}, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 20}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 20}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 20}, + }, + }, + }, + { + "a = foo.bar\n", + 0, + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + }, + }, + hcl.TraverseAttr{ + Name: "bar", + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 8, Byte: 7}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 12, Byte: 11}, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 12}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 12}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 12}, + }, + }, + }, + { + "a = 1 # line comment\n", + 0, + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &LiteralValueExpr{ + Val: cty.NumberIntVal(1), + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 21}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 21}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 21}, + }, + }, + }, + + { + "a = [for k, v in foo: v if true]\n", + 0, + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &ForExpr{ + KeyVar: "k", + ValVar: "v", + + CollExpr: &ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 18, Byte: 17}, + End: hcl.Pos{Line: 1, Column: 21, Byte: 20}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 18, Byte: 17}, + End: hcl.Pos{Line: 1, Column: 21, Byte: 20}, + }, + }, + ValExpr: &ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ + Name: "v", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 23, Byte: 22}, + End: hcl.Pos{Line: 1, Column: 24, Byte: 23}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 23, Byte: 22}, + End: hcl.Pos{Line: 1, Column: 24, Byte: 23}, + }, + }, + CondExpr: &LiteralValueExpr{ + Val: cty.True, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 28, Byte: 27}, + End: hcl.Pos{Line: 1, Column: 32, Byte: 31}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 33, Byte: 32}, + }, + OpenRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + CloseRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 32, Byte: 31}, + End: hcl.Pos{Line: 1, Column: 33, Byte: 32}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 33, Byte: 32}, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 33}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 33}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 33}, + }, + }, + }, + { + "a = [for k, v in foo: k => v... if true]\n", + 2, // can't use => or ... in a tuple for + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &ForExpr{ + KeyVar: "k", + ValVar: "v", + + CollExpr: &ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ + Name: "foo", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 18, Byte: 17}, + End: hcl.Pos{Line: 1, Column: 21, Byte: 20}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 18, Byte: 17}, + End: hcl.Pos{Line: 1, Column: 21, Byte: 20}, + }, + }, + KeyExpr: &ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ + Name: "k", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 23, Byte: 22}, + End: hcl.Pos{Line: 1, Column: 24, Byte: 23}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 23, Byte: 22}, + End: hcl.Pos{Line: 1, Column: 24, Byte: 23}, + }, + }, + ValExpr: &ScopeTraversalExpr{ + Traversal: hcl.Traversal{ + hcl.TraverseRoot{ + Name: "v", + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 28, Byte: 27}, + End: hcl.Pos{Line: 1, Column: 29, Byte: 28}, + }, + }, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 28, Byte: 27}, + End: hcl.Pos{Line: 1, Column: 29, Byte: 28}, + }, + }, + CondExpr: &LiteralValueExpr{ + Val: cty.True, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 36, Byte: 35}, + End: hcl.Pos{Line: 1, Column: 40, Byte: 39}, + }, + }, + Group: true, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 41, Byte: 40}, + }, + OpenRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + CloseRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 40, Byte: 39}, + End: hcl.Pos{Line: 1, Column: 41, Byte: 40}, + }, + }, + + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 41, Byte: 40}, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 41}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 2, Column: 1, Byte: 41}, + End: hcl.Pos{Line: 2, Column: 1, Byte: 41}, + }, + }, + }, + + { + ` `, + 2, // tabs not allowed, and body item is required here + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + }, + }, + { + `\x81`, + 2, // invalid UTF-8, and body item is required here + &Body{ + Attributes: Attributes{}, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + }, + }, + } + + prettyConfig := &pretty.Config{ + Diffable: true, + IncludeUnexported: true, + PrintStringers: true, + } + + for _, test := range tests { + t.Run(test.input, func(t *testing.T) { + file, diags := ParseConfig([]byte(test.input), "", hcl.Pos{Byte: 0, Line: 1, Column: 1}) + if len(diags) != test.diagCount { + t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.diagCount) + for _, diag := range diags { + t.Logf(" - %s", diag.Error()) + } + } + + got := file.Body + + if !reflect.DeepEqual(got, test.want) { + diff := prettyConfig.Compare(test.want, got) + if diff != "" { + t.Errorf("wrong result\ninput: %s\ndiff: %s", test.input, diff) + } else { + t.Errorf("wrong result\ninput: %s\ngot: %s\nwant: %s", test.input, spew.Sdump(got), spew.Sdump(test.want)) + } + } + }) + } +} diff --git a/zcl/zclsyntax/parser_traversal.go b/hcl/hclsyntax/parser_traversal.go similarity index 65% rename from zcl/zclsyntax/parser_traversal.go rename to hcl/hclsyntax/parser_traversal.go index 84615df..2ff3ed6 100644 --- a/zcl/zclsyntax/parser_traversal.go +++ b/hcl/hclsyntax/parser_traversal.go @@ -1,7 +1,7 @@ -package zclsyntax +package hclsyntax import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/zclconf/go-cty/cty" ) @@ -9,15 +9,15 @@ import ( // all of the remaining tokens in the peeker. The usual parser recovery // behavior is not supported here because traversals are not expected to // be parsed as part of a larger program. -func (p *parser) ParseTraversalAbs() (zcl.Traversal, zcl.Diagnostics) { - var ret zcl.Traversal - var diags zcl.Diagnostics +func (p *parser) ParseTraversalAbs() (hcl.Traversal, hcl.Diagnostics) { + var ret hcl.Traversal + var diags hcl.Diagnostics // Absolute traversal must always begin with a variable name varTok := p.Read() if varTok.Type != TokenIdent { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Variable name required", Detail: "Must begin with a variable name.", Subject: &varTok.Range, @@ -26,7 +26,7 @@ func (p *parser) ParseTraversalAbs() (zcl.Traversal, zcl.Diagnostics) { } varName := string(varTok.Bytes) - ret = append(ret, zcl.TraverseRoot{ + ret = append(ret, hcl.TraverseRoot{ Name: varName, SrcRange: varTok.Range, }) @@ -45,29 +45,29 @@ func (p *parser) ParseTraversalAbs() (zcl.Traversal, zcl.Diagnostics) { nameTok := p.Read() if nameTok.Type != TokenIdent { if nameTok.Type == TokenStar { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Attribute name required", Detail: "Splat expressions (.*) may not be used here.", Subject: &nameTok.Range, - Context: zcl.RangeBetween(varTok.Range, nameTok.Range).Ptr(), + Context: hcl.RangeBetween(varTok.Range, nameTok.Range).Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Attribute name required", Detail: "Dot must be followed by attribute name.", Subject: &nameTok.Range, - Context: zcl.RangeBetween(varTok.Range, nameTok.Range).Ptr(), + Context: hcl.RangeBetween(varTok.Range, nameTok.Range).Ptr(), }) } return ret, diags } attrName := string(nameTok.Bytes) - ret = append(ret, zcl.TraverseAttr{ + ret = append(ret, hcl.TraverseAttr{ Name: attrName, - SrcRange: zcl.RangeBetween(dot.Range, nameTok.Range), + SrcRange: hcl.RangeBetween(dot.Range, nameTok.Range), }) case TokenOBrack: // Index @@ -82,18 +82,18 @@ func (p *parser) ParseTraversalAbs() (zcl.Traversal, zcl.Diagnostics) { close := p.Read() if close.Type != TokenCBrack { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unclosed index brackets", Detail: "Index key must be followed by a closing bracket.", Subject: &close.Range, - Context: zcl.RangeBetween(open.Range, close.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, close.Range).Ptr(), }) } - ret = append(ret, zcl.TraverseIndex{ + ret = append(ret, hcl.TraverseIndex{ Key: numVal, - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }) if diags.HasErrors() { @@ -106,18 +106,18 @@ func (p *parser) ParseTraversalAbs() (zcl.Traversal, zcl.Diagnostics) { close := p.Read() if close.Type != TokenCBrack { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unclosed index brackets", Detail: "Index key must be followed by a closing bracket.", Subject: &close.Range, - Context: zcl.RangeBetween(open.Range, close.Range).Ptr(), + Context: hcl.RangeBetween(open.Range, close.Range).Ptr(), }) } - ret = append(ret, zcl.TraverseIndex{ + ret = append(ret, hcl.TraverseIndex{ Key: cty.StringVal(str), - SrcRange: zcl.RangeBetween(open.Range, close.Range), + SrcRange: hcl.RangeBetween(open.Range, close.Range), }) if diags.HasErrors() { @@ -126,32 +126,32 @@ func (p *parser) ParseTraversalAbs() (zcl.Traversal, zcl.Diagnostics) { default: if next.Type == TokenStar { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Attribute name required", Detail: "Splat expressions ([*]) may not be used here.", Subject: &next.Range, - Context: zcl.RangeBetween(varTok.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(varTok.Range, next.Range).Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Index value required", Detail: "Index brackets must contain either a literal number or a literal string.", Subject: &next.Range, - Context: zcl.RangeBetween(varTok.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(varTok.Range, next.Range).Ptr(), }) } return ret, diags } default: - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid character", Detail: "Expected an attribute access or an index operator.", Subject: &next.Range, - Context: zcl.RangeBetween(varTok.Range, next.Range).Ptr(), + Context: hcl.RangeBetween(varTok.Range, next.Range).Ptr(), }) return ret, diags } diff --git a/zcl/zclsyntax/peeker.go b/hcl/hclsyntax/peeker.go similarity index 94% rename from zcl/zclsyntax/peeker.go rename to hcl/hclsyntax/peeker.go index cc24556..b8171ff 100644 --- a/zcl/zclsyntax/peeker.go +++ b/hcl/hclsyntax/peeker.go @@ -1,7 +1,7 @@ -package zclsyntax +package hclsyntax import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) type peeker struct { @@ -32,11 +32,11 @@ func (p *peeker) Read() Token { return ret } -func (p *peeker) NextRange() zcl.Range { +func (p *peeker) NextRange() hcl.Range { return p.Peek().Range } -func (p *peeker) PrevRange() zcl.Range { +func (p *peeker) PrevRange() hcl.Range { if p.NextIndex == 0 { return p.NextRange() } diff --git a/zcl/zclsyntax/peeker_test.go b/hcl/hclsyntax/peeker_test.go similarity index 99% rename from zcl/zclsyntax/peeker_test.go rename to hcl/hclsyntax/peeker_test.go index fbe03d2..ab5f905 100644 --- a/zcl/zclsyntax/peeker_test.go +++ b/hcl/hclsyntax/peeker_test.go @@ -1,4 +1,4 @@ -package zclsyntax +package hclsyntax import ( "reflect" diff --git a/zcl/zclsyntax/public.go b/hcl/hclsyntax/public.go similarity index 82% rename from zcl/zclsyntax/public.go rename to hcl/hclsyntax/public.go index d403d14..49d8ab1 100644 --- a/zcl/zclsyntax/public.go +++ b/hcl/hclsyntax/public.go @@ -1,26 +1,26 @@ -package zclsyntax +package hclsyntax import ( - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // ParseConfig parses the given buffer as a whole zcl config file, returning -// a *zcl.File representing its contents. If HasErrors called on the returned +// a *hcl.File representing its contents. If HasErrors called on the returned // diagnostics returns true, the returned body is likely to be incomplete // and should therefore be used with care. // // The body in the returned file has dynamic type *zclsyntax.Body, so callers // may freely type-assert this to get access to the full zclsyntax API in // situations where detailed access is required. However, most common use-cases -// should be served using the zcl.Body interface to ensure compatibility with +// should be served using the hcl.Body interface to ensure compatibility with // other configurationg syntaxes, such as JSON. -func ParseConfig(src []byte, filename string, start zcl.Pos) (*zcl.File, zcl.Diagnostics) { +func ParseConfig(src []byte, filename string, start hcl.Pos) (*hcl.File, hcl.Diagnostics) { tokens, diags := LexConfig(src, filename, start) peeker := newPeeker(tokens, false) parser := &parser{peeker: peeker} body, parseDiags := parser.ParseBody(TokenEOF) diags = append(diags, parseDiags...) - return &zcl.File{ + return &hcl.File{ Body: body, Bytes: src, @@ -32,7 +32,7 @@ func ParseConfig(src []byte, filename string, start zcl.Pos) (*zcl.File, zcl.Dia // ParseExpression parses the given buffer as a standalone zcl expression, // returning it as an instance of Expression. -func ParseExpression(src []byte, filename string, start zcl.Pos) (Expression, zcl.Diagnostics) { +func ParseExpression(src []byte, filename string, start hcl.Pos) (Expression, hcl.Diagnostics) { tokens, diags := LexExpression(src, filename, start) peeker := newPeeker(tokens, false) parser := &parser{peeker: peeker} @@ -46,8 +46,8 @@ func ParseExpression(src []byte, filename string, start zcl.Pos) (Expression, zc next := parser.Peek() if next.Type != TokenEOF && !parser.recovery { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Extra characters after expression", Detail: "An expression was successfully parsed, but extra characters were found after it.", Subject: &next.Range, @@ -59,7 +59,7 @@ func ParseExpression(src []byte, filename string, start zcl.Pos) (Expression, zc // ParseTemplate parses the given buffer as a standalone zcl template, // returning it as an instance of Expression. -func ParseTemplate(src []byte, filename string, start zcl.Pos) (Expression, zcl.Diagnostics) { +func ParseTemplate(src []byte, filename string, start hcl.Pos) (Expression, hcl.Diagnostics) { tokens, diags := LexTemplate(src, filename, start) peeker := newPeeker(tokens, false) parser := &parser{peeker: peeker} @@ -74,7 +74,7 @@ func ParseTemplate(src []byte, filename string, start zcl.Pos) (Expression, zcl. // it allows only attribute and indexing operations on variables. Traverals // are useful as a syntax for referring to objects without necessarily // evaluating them. -func ParseTraversalAbs(src []byte, filename string, start zcl.Pos) (zcl.Traversal, zcl.Diagnostics) { +func ParseTraversalAbs(src []byte, filename string, start hcl.Pos) (hcl.Traversal, hcl.Diagnostics) { tokens, diags := LexExpression(src, filename, start) peeker := newPeeker(tokens, false) parser := &parser{peeker: peeker} @@ -95,7 +95,7 @@ func ParseTraversalAbs(src []byte, filename string, start zcl.Pos) (zcl.Traversa // diagnostics may include errors about lexical issues such as bad character // encodings or unrecognized characters, but full parsing is required to // detect _all_ syntax errors. -func LexConfig(src []byte, filename string, start zcl.Pos) (Tokens, zcl.Diagnostics) { +func LexConfig(src []byte, filename string, start hcl.Pos) (Tokens, hcl.Diagnostics) { tokens := scanTokens(src, filename, start, scanNormal) diags := checkInvalidTokens(tokens) return tokens, diags @@ -108,7 +108,7 @@ func LexConfig(src []byte, filename string, start zcl.Pos) (Tokens, zcl.Diagnost // diagnostics may include errors about lexical issues such as bad character // encodings or unrecognized characters, but full parsing is required to // detect _all_ syntax errors. -func LexExpression(src []byte, filename string, start zcl.Pos) (Tokens, zcl.Diagnostics) { +func LexExpression(src []byte, filename string, start hcl.Pos) (Tokens, hcl.Diagnostics) { // This is actually just the same thing as LexConfig, since configs // and expressions lex in the same way. tokens := scanTokens(src, filename, start, scanNormal) @@ -123,7 +123,7 @@ func LexExpression(src []byte, filename string, start zcl.Pos) (Tokens, zcl.Diag // diagnostics may include errors about lexical issues such as bad character // encodings or unrecognized characters, but full parsing is required to // detect _all_ syntax errors. -func LexTemplate(src []byte, filename string, start zcl.Pos) (Tokens, zcl.Diagnostics) { +func LexTemplate(src []byte, filename string, start hcl.Pos) (Tokens, hcl.Diagnostics) { tokens := scanTokens(src, filename, start, scanTemplate) diags := checkInvalidTokens(tokens) return tokens, diags diff --git a/zcl/zclsyntax/scan_tokens.go b/hcl/hclsyntax/scan_tokens.go similarity index 99% rename from zcl/zclsyntax/scan_tokens.go rename to hcl/hclsyntax/scan_tokens.go index 058ecee..a8ab57c 100644 --- a/zcl/zclsyntax/scan_tokens.go +++ b/hcl/hclsyntax/scan_tokens.go @@ -1,10 +1,10 @@ // line 1 "scan_tokens.rl" -package zclsyntax +package hclsyntax import ( "bytes" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // This file is generated from scan_tokens.rl. DO NOT EDIT. @@ -2750,7 +2750,7 @@ const zcltok_en_main int = 949 // line 15 "scan_tokens.rl" -func scanTokens(data []byte, filename string, start zcl.Pos, mode scanMode) []Token { +func scanTokens(data []byte, filename string, start hcl.Pos, mode scanMode) []Token { f := &tokenAccum{ Filename: filename, Bytes: data, diff --git a/zcl/zclsyntax/scan_tokens.rl b/hcl/hclsyntax/scan_tokens.rl similarity index 99% rename from zcl/zclsyntax/scan_tokens.rl rename to hcl/hclsyntax/scan_tokens.rl index 9a72389..4a395c1 100644 --- a/zcl/zclsyntax/scan_tokens.rl +++ b/hcl/hclsyntax/scan_tokens.rl @@ -14,7 +14,7 @@ import ( write data; }%% -func scanTokens(data []byte, filename string, start zcl.Pos, mode scanMode) []Token { +func scanTokens(data []byte, filename string, start hcl.Pos, mode scanMode) []Token { f := &tokenAccum{ Filename: filename, Bytes: data, diff --git a/hcl/hclsyntax/scan_tokens_test.go b/hcl/hclsyntax/scan_tokens_test.go new file mode 100644 index 0000000..0fd4c64 --- /dev/null +++ b/hcl/hclsyntax/scan_tokens_test.go @@ -0,0 +1,1506 @@ +package hclsyntax + +import ( + "reflect" + "testing" + + "github.com/hashicorp/hcl2/hcl" + "github.com/kylelemons/godebug/pretty" +) + +func TestScanTokens_normal(t *testing.T) { + tests := []struct { + input string + want []Token + }{ + // Empty input + { + ``, + []Token{ + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + }, + }, + }, + }, + { + ` `, + []Token{ + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + }, + }, + { + "\n\n", + []Token{ + { + Type: TokenNewline, + Bytes: []byte("\n"), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 2, Column: 1}, + }, + }, + { + Type: TokenNewline, + Bytes: []byte("\n"), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 2, Column: 1}, + End: hcl.Pos{Byte: 2, Line: 3, Column: 1}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 2, Line: 3, Column: 1}, + End: hcl.Pos{Byte: 2, Line: 3, Column: 1}, + }, + }, + }, + }, + + // TokenNumberLit + { + `1`, + []Token{ + { + Type: TokenNumberLit, + Bytes: []byte(`1`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + }, + }, + { + `12`, + []Token{ + { + Type: TokenNumberLit, + Bytes: []byte(`12`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 2, Line: 1, Column: 3}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 2, Line: 1, Column: 3}, + End: hcl.Pos{Byte: 2, Line: 1, Column: 3}, + }, + }, + }, + }, + { + `12.3`, + []Token{ + { + Type: TokenNumberLit, + Bytes: []byte(`12.3`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + }, + }, + { + `1e2`, + []Token{ + { + Type: TokenNumberLit, + Bytes: []byte(`1e2`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + }, + }, + { + `1e+2`, + []Token{ + { + Type: TokenNumberLit, + Bytes: []byte(`1e+2`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + }, + }, + + // TokenIdent + { + `hello`, + []Token{ + { + Type: TokenIdent, + Bytes: []byte(`hello`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + }, + }, + { + `h3ll0`, + []Token{ + { + Type: TokenIdent, + Bytes: []byte(`h3ll0`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + }, + }, + { + `héllo`, // combining acute accent + []Token{ + { + Type: TokenIdent, + Bytes: []byte(`héllo`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 6}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 7, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 6}, + }, + }, + }, + }, + + // Literal-only Templates (string literals, effectively) + { + `""`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 2, Line: 1, Column: 3}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 2, Line: 1, Column: 3}, + End: hcl.Pos{Byte: 2, Line: 1, Column: 3}, + }, + }, + }, + }, + { + `"hello"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenQuotedLit, + Bytes: []byte(`hello`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + }, + }, + + // Templates with interpolations and control sequences + { + `"${1}"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenNumberLit, + Bytes: []byte(`1`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + }, + }, + { + `"%{a}"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenTemplateControl, + Bytes: []byte(`%{`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenIdent, + Bytes: []byte(`a`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + }, + }, + { + `"${{}}"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenOBrace, + Bytes: []byte(`{`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenCBrace, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + }, + }, + { + `"${""}"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 5, Line: 1, Column: 6}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + }, + }, + { + `"${"${a}"}"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenIdent, + Bytes: []byte(`a`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + End: hcl.Pos{Byte: 8, Line: 1, Column: 9}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 8, Line: 1, Column: 9}, + End: hcl.Pos{Byte: 9, Line: 1, Column: 10}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 9, Line: 1, Column: 10}, + End: hcl.Pos{Byte: 10, Line: 1, Column: 11}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 10, Line: 1, Column: 11}, + End: hcl.Pos{Byte: 11, Line: 1, Column: 12}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 11, Line: 1, Column: 12}, + End: hcl.Pos{Byte: 11, Line: 1, Column: 12}, + }, + }, + }, + }, + { + `"${"${a} foo"}"`, + []Token{ + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 0, Line: 1, Column: 1}, + End: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 1, Line: 1, Column: 2}, + End: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + }, + }, + { + Type: TokenOQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 3, Line: 1, Column: 4}, + End: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + }, + }, + { + Type: TokenTemplateInterp, + Bytes: []byte(`${`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 4, Line: 1, Column: 5}, + End: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + }, + }, + { + Type: TokenIdent, + Bytes: []byte(`a`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 6, Line: 1, Column: 7}, + End: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 7, Line: 1, Column: 8}, + End: hcl.Pos{Byte: 8, Line: 1, Column: 9}, + }, + }, + { + Type: TokenQuotedLit, + Bytes: []byte(` foo`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 8, Line: 1, Column: 9}, + End: hcl.Pos{Byte: 12, Line: 1, Column: 13}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 12, Line: 1, Column: 13}, + End: hcl.Pos{Byte: 13, Line: 1, Column: 14}, + }, + }, + { + Type: TokenTemplateSeqEnd, + Bytes: []byte(`}`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 13, Line: 1, Column: 14}, + End: hcl.Pos{Byte: 14, Line: 1, Column: 15}, + }, + }, + { + Type: TokenCQuote, + Bytes: []byte(`"`), + Range: hcl.Range{ + Start: hcl.Pos{Byte: 14, Line: 1, Column: 15}, + End: hcl.Pos{Byte: 15, Line: 1, Column: 16}, + }, + }, + { + Type: TokenEOF, + Bytes: []byte{}, + Range: hcl.Range{ + Start: hcl.Pos{Byte: 15, Line: 1, Column: 16}, + End: hcl.Pos{Byte: 15, Line: 1, Column: 16}, + }, + }, + }, + }, + + // Heredoc Templates + { + `< 0 { lastHeaderRange = b.LabelRanges[len(b.LabelRanges)-1] } - return &zcl.Block{ + return &hcl.Block{ Type: b.Type, Labels: b.Labels, Body: b.Body, - DefRange: zcl.RangeBetween(b.TypeRange, lastHeaderRange), + DefRange: hcl.RangeBetween(b.TypeRange, lastHeaderRange), TypeRange: b.TypeRange, LabelRanges: b.LabelRanges, } } -// Body is the implementation of zcl.Body for the zcl native syntax. +// Body is the implementation of hcl.Body for the zcl native syntax. type Body struct { Attributes Attributes Blocks Blocks @@ -35,23 +35,23 @@ type Body struct { hiddenAttrs map[string]struct{} hiddenBlocks map[string]struct{} - SrcRange zcl.Range - EndRange zcl.Range // Final token of the body, for reporting missing items + SrcRange hcl.Range + EndRange hcl.Range // Final token of the body, for reporting missing items } -// Assert that *Body implements zcl.Body -var assertBodyImplBody zcl.Body = &Body{} +// Assert that *Body implements hcl.Body +var assertBodyImplBody hcl.Body = &Body{} func (b *Body) walkChildNodes(w internalWalkFunc) { b.Attributes = w(b.Attributes).(Attributes) b.Blocks = w(b.Blocks).(Blocks) } -func (b *Body) Range() zcl.Range { +func (b *Body) Range() hcl.Range { return b.SrcRange } -func (b *Body) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostics) { +func (b *Body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) { content, remainZCL, diags := b.PartialContent(schema) // No we'll see if anything actually remains, to produce errors about @@ -80,8 +80,8 @@ func (b *Body) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostic } } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unsupported attribute", Detail: fmt.Sprintf("An attribute named %q is not expected here.%s", name, suggestion), Subject: &attr.NameRange, @@ -109,8 +109,8 @@ func (b *Body) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostic } } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unsupported block type", Detail: fmt.Sprintf("Blocks of type %q are not expected here.%s", blockTy, suggestion), Subject: &block.TypeRange, @@ -121,10 +121,10 @@ func (b *Body) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostic return content, diags } -func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Body, zcl.Diagnostics) { - attrs := make(zcl.Attributes) - var blocks zcl.Blocks - var diags zcl.Diagnostics +func (b *Body) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) { + attrs := make(hcl.Attributes) + var blocks hcl.Blocks + var diags hcl.Diagnostics hiddenAttrs := make(map[string]struct{}) hiddenBlocks := make(map[string]struct{}) @@ -145,8 +145,8 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod _, hidden := hiddenAttrs[name] if hidden || !exists { if attrS.Required { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Missing required attribute", Detail: fmt.Sprintf("The attribute %q is required, but no definition was found.", attrS.Name), Subject: b.MissingItemRange().Ptr(), @@ -159,7 +159,7 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod attrs[name] = attr.AsZCLAttribute() } - blocksWanted := make(map[string]zcl.BlockHeaderSchema) + blocksWanted := make(map[string]hcl.BlockHeaderSchema) for _, blockS := range schema.Blocks { blocksWanted[blockS.Type] = blockS } @@ -176,25 +176,25 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod if len(block.Labels) > len(blockS.LabelNames) { name := block.Type if len(blockS.LabelNames) == 0 { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Extraneous label for %s", name), Detail: fmt.Sprintf( "No labels are expected for %s blocks.", name, ), Subject: block.LabelRanges[0].Ptr(), - Context: zcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(), + Context: hcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(), }) } else { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Extraneous label for %s", name), Detail: fmt.Sprintf( "Only %d labels (%s) are expected for %s blocks.", len(blockS.LabelNames), strings.Join(blockS.LabelNames, ", "), name, ), Subject: block.LabelRanges[len(blockS.LabelNames)].Ptr(), - Context: zcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(), + Context: hcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(), }) } continue @@ -202,15 +202,15 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod if len(block.Labels) < len(blockS.LabelNames) { name := block.Type - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Missing %s for %s", blockS.LabelNames[len(block.Labels)], name), Detail: fmt.Sprintf( "All %s blocks must have %d labels (%s).", name, len(blockS.LabelNames), strings.Join(blockS.LabelNames, ", "), ), Subject: &block.OpenBraceRange, - Context: zcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(), + Context: hcl.RangeBetween(block.TypeRange, block.OpenBraceRange).Ptr(), }) continue } @@ -235,7 +235,7 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod EndRange: b.EndRange, } - return &zcl.BodyContent{ + return &hcl.BodyContent{ Attributes: attrs, Blocks: blocks, @@ -243,14 +243,14 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod }, remain, diags } -func (b *Body) JustAttributes() (zcl.Attributes, zcl.Diagnostics) { - attrs := make(zcl.Attributes) - var diags zcl.Diagnostics +func (b *Body) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { + attrs := make(hcl.Attributes) + var diags hcl.Diagnostics if len(b.Blocks) > 0 { example := b.Blocks[0] - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: fmt.Sprintf("Unexpected %s block", example.Type), Detail: "Blocks are not allowed here.", Context: &example.TypeRange, @@ -274,7 +274,7 @@ func (b *Body) JustAttributes() (zcl.Attributes, zcl.Diagnostics) { return attrs, diags } -func (b *Body) MissingItemRange() zcl.Range { +func (b *Body) MissingItemRange() hcl.Range { return b.EndRange } @@ -292,7 +292,7 @@ func (a Attributes) walkChildNodes(w internalWalkFunc) { // // This is provided only to complete the Node interface, but has no practical // use. -func (a Attributes) Range() zcl.Range { +func (a Attributes) Range() hcl.Range { // An attributes doesn't really have a useful range to report, since // it's just a grouping construct. So we'll arbitrarily take the // range of one of the attributes, or produce an invalid range if we have @@ -301,7 +301,7 @@ func (a Attributes) Range() zcl.Range { for _, attr := range a { return attr.Range() } - return zcl.Range{ + return hcl.Range{ Filename: "", } } @@ -311,22 +311,22 @@ type Attribute struct { Name string Expr Expression - SrcRange zcl.Range - NameRange zcl.Range - EqualsRange zcl.Range + SrcRange hcl.Range + NameRange hcl.Range + EqualsRange hcl.Range } func (a *Attribute) walkChildNodes(w internalWalkFunc) { a.Expr = w(a.Expr).(Expression) } -func (a *Attribute) Range() zcl.Range { +func (a *Attribute) Range() hcl.Range { return a.SrcRange } -// AsZCLAttribute returns the block data expressed as a *zcl.Attribute. -func (a *Attribute) AsZCLAttribute() *zcl.Attribute { - return &zcl.Attribute{ +// AsZCLAttribute returns the block data expressed as a *hcl.Attribute. +func (a *Attribute) AsZCLAttribute() *hcl.Attribute { + return &hcl.Attribute{ Name: a.Name, Expr: a.Expr, @@ -349,11 +349,11 @@ func (bs Blocks) walkChildNodes(w internalWalkFunc) { // // This is provided only to complete the Node interface, but has no practical // use. -func (bs Blocks) Range() zcl.Range { +func (bs Blocks) Range() hcl.Range { if len(bs) > 0 { return bs[0].Range() } - return zcl.Range{ + return hcl.Range{ Filename: "", } } @@ -364,16 +364,16 @@ type Block struct { Labels []string Body *Body - TypeRange zcl.Range - LabelRanges []zcl.Range - OpenBraceRange zcl.Range - CloseBraceRange zcl.Range + TypeRange hcl.Range + LabelRanges []hcl.Range + OpenBraceRange hcl.Range + CloseBraceRange hcl.Range } func (b *Block) walkChildNodes(w internalWalkFunc) { b.Body = w(b.Body).(*Body) } -func (b *Block) Range() zcl.Range { - return zcl.RangeBetween(b.TypeRange, b.CloseBraceRange) +func (b *Block) Range() hcl.Range { + return hcl.RangeBetween(b.TypeRange, b.CloseBraceRange) } diff --git a/zcl/zclsyntax/structure_test.go b/hcl/hclsyntax/structure_test.go similarity index 73% rename from zcl/zclsyntax/structure_test.go rename to hcl/hclsyntax/structure_test.go index 209371f..551f0f1 100644 --- a/zcl/zclsyntax/structure_test.go +++ b/hcl/hclsyntax/structure_test.go @@ -1,11 +1,11 @@ -package zclsyntax +package hclsyntax import ( "fmt" "reflect" "testing" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" "github.com/kylelemons/godebug/pretty" "github.com/zclconf/go-cty/cty" ) @@ -13,17 +13,17 @@ import ( func TestBodyContent(t *testing.T) { tests := []struct { body *Body - schema *zcl.BodySchema + schema *hcl.BodySchema partial bool - want *zcl.BodyContent + want *hcl.BodyContent diagCount int }{ { &Body{}, - &zcl.BodySchema{}, + &hcl.BodySchema{}, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 0, }, @@ -37,17 +37,17 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{ - "foo": &zcl.Attribute{ + &hcl.BodyContent{ + Attributes: hcl.Attributes{ + "foo": &hcl.Attribute{ Name: "foo", }, }, @@ -62,10 +62,10 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{}, + &hcl.BodySchema{}, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // attribute "foo" is not expected }, @@ -77,10 +77,10 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{}, + &hcl.BodySchema{}, true, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 0, // in partial mode, so extra "foo" is acceptable }, @@ -88,16 +88,16 @@ func TestBodyContent(t *testing.T) { &Body{ Attributes: Attributes{}, }, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 0, // "foo" not required, so no error }, @@ -105,8 +105,8 @@ func TestBodyContent(t *testing.T) { &Body{ Attributes: Attributes{}, }, - &zcl.BodySchema{ - Attributes: []zcl.AttributeSchema{ + &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ { Name: "foo", Required: true, @@ -114,8 +114,8 @@ func TestBodyContent(t *testing.T) { }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // "foo" is required }, @@ -127,16 +127,16 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // attribute "foo" not expected (it's defined as a block) }, @@ -150,17 +150,17 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, - Blocks: zcl.Blocks{ + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, + Blocks: hcl.Blocks{ { Type: "foo", Body: (*Body)(nil), @@ -180,17 +180,17 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, - Blocks: zcl.Blocks{ + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, + Blocks: hcl.Blocks{ { Type: "foo", Body: (*Body)(nil), @@ -214,17 +214,17 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, - Blocks: zcl.Blocks{ + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, + Blocks: hcl.Blocks{ { Type: "foo", Body: (*Body)(nil), @@ -244,17 +244,17 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, true, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, - Blocks: zcl.Blocks{ + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, + Blocks: hcl.Blocks{ { Type: "foo", Body: (*Body)(nil), @@ -272,8 +272,8 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", LabelNames: []string{"name"}, @@ -281,9 +281,9 @@ func TestBodyContent(t *testing.T) { }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, - Blocks: zcl.Blocks{ + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, + Blocks: hcl.Blocks{ { Type: "foo", Labels: []string{"bar"}, @@ -301,8 +301,8 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", LabelNames: []string{"name"}, @@ -310,8 +310,8 @@ func TestBodyContent(t *testing.T) { }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // missing label "name" }, @@ -322,20 +322,20 @@ func TestBodyContent(t *testing.T) { Type: "foo", Labels: []string{"bar"}, - LabelRanges: []zcl.Range{{}}, + LabelRanges: []hcl.Range{{}}, }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // no labels expected }, @@ -346,12 +346,12 @@ func TestBodyContent(t *testing.T) { Type: "foo", Labels: []string{"bar", "baz"}, - LabelRanges: []zcl.Range{{}, {}}, + LabelRanges: []hcl.Range{{}, {}}, }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", LabelNames: []string{"name"}, @@ -359,8 +359,8 @@ func TestBodyContent(t *testing.T) { }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // too many labels }, @@ -372,16 +372,16 @@ func TestBodyContent(t *testing.T) { }, }, }, - &zcl.BodySchema{ - Blocks: []zcl.BlockHeaderSchema{ + &hcl.BodySchema{ + Blocks: []hcl.BlockHeaderSchema{ { Type: "foo", }, }, }, false, - &zcl.BodyContent{ - Attributes: zcl.Attributes{}, + &hcl.BodyContent{ + Attributes: hcl.Attributes{}, }, 1, // should've been a block, not an attribute }, @@ -395,8 +395,8 @@ func TestBodyContent(t *testing.T) { for i, test := range tests { t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) { - var got *zcl.BodyContent - var diags zcl.Diagnostics + var got *hcl.BodyContent + var diags hcl.Diagnostics if test.partial { got, _, diags = test.body.PartialContent(test.schema) } else { @@ -423,19 +423,19 @@ func TestBodyContent(t *testing.T) { func TestBodyJustAttributes(t *testing.T) { tests := []struct { body *Body - want zcl.Attributes + want hcl.Attributes diagCount int }{ { &Body{}, - zcl.Attributes{}, + hcl.Attributes{}, 0, }, { &Body{ Attributes: Attributes{}, }, - zcl.Attributes{}, + hcl.Attributes{}, 0, }, { @@ -449,8 +449,8 @@ func TestBodyJustAttributes(t *testing.T) { }, }, }, - zcl.Attributes{ - "foo": &zcl.Attribute{ + hcl.Attributes{ + "foo": &hcl.Attribute{ Name: "foo", Expr: &LiteralValueExpr{ Val: cty.StringVal("bar"), @@ -475,8 +475,8 @@ func TestBodyJustAttributes(t *testing.T) { }, }, }, - zcl.Attributes{ - "foo": &zcl.Attribute{ + hcl.Attributes{ + "foo": &hcl.Attribute{ Name: "foo", Expr: &LiteralValueExpr{ Val: cty.StringVal("bar"), @@ -499,7 +499,7 @@ func TestBodyJustAttributes(t *testing.T) { "foo": struct{}{}, }, }, - zcl.Attributes{}, + hcl.Attributes{}, 0, }, } diff --git a/zcl/zclsyntax/token.go b/hcl/hclsyntax/token.go similarity index 90% rename from zcl/zclsyntax/token.go rename to hcl/hclsyntax/token.go index 4391349..f1a12a7 100644 --- a/zcl/zclsyntax/token.go +++ b/hcl/hclsyntax/token.go @@ -1,10 +1,10 @@ -package zclsyntax +package hclsyntax import ( "fmt" "github.com/apparentlymart/go-textseg/textseg" - "github.com/hashicorp/hcl2/zcl" + "github.com/hashicorp/hcl2/hcl" ) // Token represents a sequence of bytes from some zcl code that has been @@ -12,7 +12,7 @@ import ( type Token struct { Type TokenType Bytes []byte - Range zcl.Range + Range hcl.Range } // Tokens is a slice of Token. @@ -115,7 +115,7 @@ const ( type tokenAccum struct { Filename string Bytes []byte - Pos zcl.Pos + Pos hcl.Pos Tokens []Token } @@ -146,7 +146,7 @@ func (f *tokenAccum) emitToken(ty TokenType, startOfs, endOfs int) { f.Tokens = append(f.Tokens, Token{ Type: ty, Bytes: f.Bytes[startOfs:endOfs], - Range: zcl.Range{ + Range: hcl.Range{ Filename: f.Filename, Start: start, End: end, @@ -167,8 +167,8 @@ type heredocInProgress struct { // Returns a diagnostics with no errors if everything seems acceptable. // Otherwise, returns zero or more error diagnostics, though tries to limit // repetition of the same information. -func checkInvalidTokens(tokens Tokens) zcl.Diagnostics { - var diags zcl.Diagnostics +func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { + var diags hcl.Diagnostics toldBitwise := 0 toldExponent := 0 @@ -191,8 +191,8 @@ func checkInvalidTokens(tokens Tokens) zcl.Diagnostics { suggestion = " Did you mean boolean NOT (\"!\")?" } - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unsupported operator", Detail: fmt.Sprintf("Bitwise operators are not supported.%s", suggestion), Subject: &tok.Range, @@ -201,8 +201,8 @@ func checkInvalidTokens(tokens Tokens) zcl.Diagnostics { } case TokenStarStar: if toldExponent < 1 { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Unsupported operator", Detail: "\"**\" is not a supported operator. Exponentiation is not supported as an operator.", Subject: &tok.Range, @@ -214,8 +214,8 @@ func checkInvalidTokens(tokens Tokens) zcl.Diagnostics { // Only report for alternating (even) backticks, so we won't report both start and ends of the same // backtick-quoted string. if toldExponent < 4 && (toldExponent%2) == 0 { - diags = append(diags, &zcl.Diagnostic{ - Severity: zcl.DiagError, + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, Summary: "Invalid character", Detail: "The \"`\" character is not valid. To create a multi-line string, use the \"heredoc\" syntax, like \"<