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

Function parseBlock

hclwrite/parser.go:292–348  ·  view source on GitHub ↗
(nativeBlock *hclsyntax.Block, from, leadComments, lineComments, newline inputTokens)

Source from the content-addressed store, hash-verified

290}
291
292func parseBlock(nativeBlock *hclsyntax.Block, from, leadComments, lineComments, newline inputTokens) *node {
293 block := &Block{
294 inTree: newInTree(),
295 }
296 children := block.children
297
298 {
299 cn := newNode(newComments(leadComments.Tokens()))
300 block.leadComments = cn
301 children.AppendNode(cn)
302 }
303
304 before, typeTokens, from := from.Partition(nativeBlock.TypeRange)
305 {
306 children.AppendUnstructuredTokens(before.Tokens())
307 if typeTokens.Len() != 1 {
308 // Should never happen with valid input
309 panic("block type name is not exactly one token")
310 }
311 token := typeTokens.Tokens()[0]
312 in := newNode(newIdentifier(token))
313 block.typeName = in
314 children.AppendNode(in)
315 }
316
317 _, labelsNode, from := parseBlockLabels(nativeBlock, from)
318 block.labels = labelsNode
319 children.AppendNode(labelsNode)
320
321 before, oBrace, from := from.Partition(nativeBlock.OpenBraceRange)
322 children.AppendUnstructuredTokens(before.Tokens())
323 block.open = children.AppendUnstructuredTokens(oBrace.Tokens())
324
325 // We go a bit out of order here: we go hunting for the closing brace
326 // so that we have a delimited body, but then we'll deal with the body
327 // before we actually append the closing brace and any straggling tokens
328 // that appear after it.
329 bodyTokens, cBrace, from := from.Partition(nativeBlock.CloseBraceRange)
330 before, body, after := parseBody(nativeBlock.Body, bodyTokens)
331 children.AppendUnstructuredTokens(before.Tokens())
332 block.body = body
333 children.AppendNode(body)
334 children.AppendUnstructuredTokens(after.Tokens())
335
336 block.close = children.AppendUnstructuredTokens(cBrace.Tokens())
337
338 // stragglers
339 children.AppendUnstructuredTokens(from.Tokens())
340 if lineComments.Len() > 0 {
341 // blocks don't actually have line comments, so we'll just treat
342 // them as extra stragglers
343 children.AppendUnstructuredTokens(lineComments.Tokens())
344 }
345 children.AppendUnstructuredTokens(newline.Tokens())
346
347 return newNode(block)
348}
349

Callers 1

parseBodyItemFunction · 0.85

Calls 11

newInTreeFunction · 0.85
newNodeFunction · 0.85
newCommentsFunction · 0.85
newIdentifierFunction · 0.85
parseBlockLabelsFunction · 0.85
parseBodyFunction · 0.85
TokensMethod · 0.80
AppendNodeMethod · 0.80
PartitionMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected