()
| 136 | } |
| 137 | |
| 138 | func (d *astDecoder) decode() (*ast.Node, error) { |
| 139 | if d.nodeCount < 2 { |
| 140 | return nil, errors.New("no nodes to decode") |
| 141 | } |
| 142 | |
| 143 | d.nodes = make([]*ast.Node, d.nodeCount) |
| 144 | d.nodeLists = make([]*ast.NodeList, d.nodeCount) |
| 145 | // Pre-allocate arena for NodeList child slices. Each node can appear as a |
| 146 | // child at most once, so nodeCount is an upper bound on total child pointers. |
| 147 | d.nodeArena = make([]*ast.Node, 0, d.nodeCount) |
| 148 | |
| 149 | // Process bottom-up so children exist before parents. |
| 150 | for i := d.nodeCount - 1; i >= 1; i-- { |
| 151 | kind := d.nodeField(i, NodeOffsetKind) |
| 152 | pos := d.nodeField(i, NodeOffsetPos) |
| 153 | end := d.nodeField(i, NodeOffsetEnd) |
| 154 | data := d.nodeField(i, NodeOffsetData) |
| 155 | childIndices := d.collectChildren(i) |
| 156 | |
| 157 | if kind == SyntaxKindNodeList { |
| 158 | childNodes := d.allocNodeSlice(len(childIndices)) |
| 159 | for _, ci := range childIndices { |
| 160 | if d.nodes[ci] != nil { |
| 161 | childNodes = append(childNodes, d.nodes[ci]) |
| 162 | } |
| 163 | } |
| 164 | nl := d.factory.NewNodeList(childNodes) |
| 165 | nl.Loc = core.NewTextRange(int(pos), int(end)) |
| 166 | d.nodeLists[i] = nl |
| 167 | continue |
| 168 | } |
| 169 | |
| 170 | node, err := d.createNode(ast.Kind(kind), data, childIndices) |
| 171 | if err != nil { |
| 172 | return nil, fmt.Errorf("at node %d (kind %v): %w", i, ast.Kind(kind), err) |
| 173 | } |
| 174 | node.Loc = core.NewTextRange(int(pos), int(end)) |
| 175 | node.Flags = ast.NodeFlags(d.nodeField(i, NodeOffsetFlags)) |
| 176 | d.nodes[i] = node |
| 177 | } |
| 178 | |
| 179 | return d.nodes[1], nil |
| 180 | } |
| 181 | |
| 182 | // getModifierList creates a *ast.ModifierList from a child index that is a NodeList. |
| 183 | func (d *astDecoder) getModifierList(ci int) *ast.ModifierList { |
nothing calls this directly
no test coverage detected