diff options
| author | Liam Cervante <liam.cervante@hashicorp.com> | 2024-04-22 14:20:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-22 14:20:01 +0200 |
| commit | f7cd61ac04cc66dcbb42ba84dfe640c976762021 (patch) | |
| tree | a064f9857b6674890ae00b0389aa7e88054e6c6e /hclsyntax/public.go | |
| parent | 303be6113391cd39e05f26423d724e9bc4fb07f5 (diff) | |
Add additional function for parsing traversals with [*] keys (#673)
* Add additional function for parsing traversals with [*] keys
* add more context around skipped test cases
Diffstat (limited to 'hclsyntax/public.go')
| -rw-r--r-- | hclsyntax/public.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/hclsyntax/public.go b/hclsyntax/public.go index d56f8e5..17dc1ed 100644 --- a/hclsyntax/public.go +++ b/hclsyntax/public.go @@ -118,6 +118,37 @@ func ParseTraversalAbs(src []byte, filename string, start hcl.Pos) (hcl.Traversa return expr, diags } +// ParseTraversalPartial matches the behavior of ParseTraversalAbs except +// that it allows splat expressions ([*]) to appear in the traversal. +// +// The returned traversals are "partial" in that the splat expression indicates +// an unknown value for the index. +// +// Traversals that include splats cannot be automatically traversed by HCL using +// the TraversalAbs or TraversalRel methods. Instead, the caller must handle +// the traversals manually. +func ParseTraversalPartial(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} + + // Bare traverals are always parsed in "ignore newlines" mode, as if + // they were wrapped in parentheses. + parser.PushIncludeNewlines(false) + + expr, parseDiags := parser.ParseTraversalPartial() + diags = append(diags, parseDiags...) + + parser.PopIncludeNewlines() + + // Panic if the parser uses incorrect stack discipline with the peeker's + // newlines stack, since otherwise it will produce confusing downstream + // errors. + peeker.AssertEmptyIncludeNewlinesStack() + + return expr, diags +} + // LexConfig performs lexical analysis on the given buffer, treating it as a // whole HCL config file, and returns the resulting tokens. // |
