MCPcopy
hub / github.com/hashicorp/hcl / ParseExpression

Function ParseExpression

hclsyntax/public.go:44–74  ·  view source on GitHub ↗

ParseExpression parses the given buffer as a standalone HCL expression, returning it as an instance of Expression.

(src []byte, filename string, start hcl.Pos)

Source from the content-addressed store, hash-verified

42// ParseExpression parses the given buffer as a standalone HCL expression,
43// returning it as an instance of Expression.
44func ParseExpression(src []byte, filename string, start hcl.Pos) (Expression, hcl.Diagnostics) {
45 tokens, diags := LexExpression(src, filename, start)
46 peeker := newPeeker(tokens, false)
47 parser := &parser{peeker: peeker}
48
49 // Bare expressions are always parsed in "ignore newlines" mode, as if
50 // they were wrapped in parentheses.
51 parser.PushIncludeNewlines(false)
52
53 expr, parseDiags := parser.ParseExpression()
54 diags = append(diags, parseDiags...)
55
56 next := parser.Peek()
57 if next.Type != TokenEOF && !parser.recovery {
58 diags = append(diags, &hcl.Diagnostic{
59 Severity: hcl.DiagError,
60 Summary: "Extra characters after expression",
61 Detail: "An expression was successfully parsed, but extra characters were found after it.",
62 Subject: &next.Range,
63 })
64 }
65
66 parser.PopIncludeNewlines()
67
68 // Panic if the parser uses incorrect stack discipline with the peeker's
69 // newlines stack, since otherwise it will produce confusing downstream
70 // errors.
71 peeker.AssertEmptyIncludeNewlinesStack()
72
73 return expr, diags
74}
75
76// ParseTemplate parses the given buffer as a standalone HCL template,
77// returning it as an instance of Expression.

Callers 15

FuzzParseExpressionFunction · 0.92
TestTryFuncFunction · 0.92
TestCanFuncFunction · 0.92
TestDecodeUserFunctionsFunction · 0.92
TestGetTypeFunction · 0.92
TestGetTypeDefaultsFunction · 0.92
TestTypeConvertFuncFunction · 0.92
ExprCallMethod · 0.92
TestStaticExpressionListFunction · 0.70

Calls 7

ParseExpressionMethod · 0.95
LexExpressionFunction · 0.85
PushIncludeNewlinesMethod · 0.80
PopIncludeNewlinesMethod · 0.80
newPeekerFunction · 0.70
PeekMethod · 0.45

Tested by 15

FuzzParseExpressionFunction · 0.74
TestTryFuncFunction · 0.74
TestCanFuncFunction · 0.74
TestDecodeUserFunctionsFunction · 0.74
TestGetTypeFunction · 0.74
TestGetTypeDefaultsFunction · 0.74
TestTypeConvertFuncFunction · 0.74
TestStaticExpressionListFunction · 0.56