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)
| 182 | // the resulting *Body object as well as the tokens that appeared before and |
| 183 | // after it. |
| 184 | func 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 | |
| 223 | func parseBodyItem(nativeItem hclsyntax.Node, from inputTokens) (inputTokens, *node, inputTokens) { |
| 224 | before, leadComments, within, lineComments, newline, after := from.PartitionBlockItem(nativeItem.Range()) |
no test coverage detected