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

Function parseBody

hclwrite/parser.go:184–221  ·  view source on GitHub ↗

parseBody locates the given body within the given input tokens and returns the resulting *Body object as well as the tokens that appeared before and after it.

(nativeBody *hclsyntax.Body, from inputTokens)

Source from the content-addressed store, hash-verified

182// the resulting *Body object as well as the tokens that appeared before and
183// after it.
184func parseBody(nativeBody *hclsyntax.Body, from inputTokens) (inputTokens, *node, inputTokens) {
185 before, within, after := from.PartitionIncludingComments(nativeBody.SrcRange)
186
187 // The main AST doesn't retain the original source ordering of the
188 // body items, so we need to reconstruct that ordering by inspecting
189 // their source ranges.
190 nativeItems := make([]hclsyntax.Node, 0, len(nativeBody.Attributes)+len(nativeBody.Blocks))
191 for _, nativeAttr := range nativeBody.Attributes {
192 nativeItems = append(nativeItems, nativeAttr)
193 }
194 for _, nativeBlock := range nativeBody.Blocks {
195 nativeItems = append(nativeItems, nativeBlock)
196 }
197 sort.Sort(nativeNodeSorter{nativeItems})
198
199 body := &Body{
200 inTree: newInTree(),
201 items: newNodeSet(),
202 }
203
204 remain := within
205 for _, nativeItem := range nativeItems {
206 beforeItem, item, afterItem := parseBodyItem(nativeItem, remain)
207
208 if beforeItem.Len() > 0 {
209 body.AppendUnstructuredTokens(beforeItem.Tokens())
210 }
211 body.appendItemNode(item)
212
213 remain = afterItem
214 }
215
216 if remain.Len() > 0 {
217 body.AppendUnstructuredTokens(remain.Tokens())
218 }
219
220 return before, newNode(body), after
221}
222
223func parseBodyItem(nativeItem hclsyntax.Node, from inputTokens) (inputTokens, *node, inputTokens) {
224 before, leadComments, within, lineComments, newline, after := from.PartitionBlockItem(nativeItem.Range())

Callers 2

parseFunction · 0.85
parseBlockFunction · 0.85

Calls 9

appendItemNodeMethod · 0.95
newInTreeFunction · 0.85
newNodeSetFunction · 0.85
parseBodyItemFunction · 0.85
newNodeFunction · 0.85
TokensMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected