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

Method parseSingleAttrBody

hclsyntax/parser.go:179–238  ·  view source on GitHub ↗

parseSingleAttrBody is a weird variant of ParseBody that deals with the body of a nested block containing only one attribute value all on a single line, like foo { bar = baz } . It expects to find a single attribute item immediately followed by the end token type with no intervening newlines.

(end TokenType)

Source from the content-addressed store, hash-verified

177// line, like foo { bar = baz } . It expects to find a single attribute item
178// immediately followed by the end token type with no intervening newlines.
179func (p *parser) parseSingleAttrBody(end TokenType) (*Body, hcl.Diagnostics) {
180 ident := p.Read()
181 if ident.Type != TokenIdent {
182 p.recoverAfterBodyItem()
183 return nil, hcl.Diagnostics{
184 {
185 Severity: hcl.DiagError,
186 Summary: "Argument or block definition required",
187 Detail: "An argument or block definition is required here.",
188 Subject: &ident.Range,
189 },
190 }
191 }
192
193 var attr *Attribute
194 var diags hcl.Diagnostics
195
196 next := p.Peek()
197
198 switch next.Type {
199 case TokenEqual:
200 node, attrDiags := p.finishParsingBodyAttribute(ident, true)
201 diags = append(diags, attrDiags...)
202 attr = node.(*Attribute)
203 case TokenOQuote, TokenOBrace, TokenIdent:
204 p.recoverAfterBodyItem()
205 return nil, hcl.Diagnostics{
206 {
207 Severity: hcl.DiagError,
208 Summary: "Argument definition required",
209 Detail: fmt.Sprintf("A single-line block definition can contain only a single argument. If you meant to define argument %q, use an equals sign to assign it a value. To define a nested block, place it on a line of its own within its parent block.", ident.Bytes),
210 Subject: hcl.RangeBetween(ident.Range, next.Range).Ptr(),
211 },
212 }
213 default:
214 p.recoverAfterBodyItem()
215 return nil, hcl.Diagnostics{
216 {
217 Severity: hcl.DiagError,
218 Summary: "Argument or block definition required",
219 Detail: "An argument or block definition is required here. To set an argument, use the equals sign \"=\" to introduce the argument value.",
220 Subject: &ident.Range,
221 },
222 }
223 }
224
225 return &Body{
226 Attributes: Attributes{
227 string(ident.Bytes): attr,
228 },
229
230 SrcRange: attr.SrcRange,
231 EndRange: hcl.Range{
232 Filename: attr.SrcRange.Filename,
233 Start: attr.SrcRange.End,
234 End: attr.SrcRange.End,
235 },
236 }, diags

Callers 1

Calls 5

recoverAfterBodyItemMethod · 0.95
PtrMethod · 0.80
ReadMethod · 0.45
PeekMethod · 0.45

Tested by

no test coverage detected