build-a-toml-parser / lesson-28.md
Lesson 28 · Arrays and inline tables

Arrays of tables

Config often has a list of like-shaped sections, such as several products. Today you parse an array of tables, a double-bracket header that appends a new table element each time it appears.

The goal

Parse [[name]] headers that append a table element per occurrence.

Start here - the target
TO DO
Scenario: Repeated double-bracket headers
Giventhe document open-bracket-bracket products close-bracket-bracket then name = "A", then open-bracket-bracket products close-bracket-bracket then name = "B"
WhenParse is called
Thenproducts is an array of two tables: the first has name "A", the second has name "B"
Andpairs after each double-bracket header go into that occurrence, and the current table becomes the newly appended element
Background

When a document has several sections of the same shape - a list of products, servers, or fruits - TOML uses an array of tables, written with double brackets: [[products]]. Each time that header appears, it appends a new table to the array named products, and the pairs that follow fill in that newest element. So two [[products]] headers produce a two-element array, each element the table its following pairs describe.

This is the one header form that is not a redefinition when repeated - that is its entire purpose. The first [[products]] creates the array and its first table; the second finds the existing array and appends a second table, rather than erroring. The current-table pointer moves to the freshly appended element, so name = "A" after the first header and name = "B" after the second land in different tables. This gives TOML its way of expressing ordered, repeated records without any outer list syntax, and the next lesson lets those records hold subtables of their own.

Make it work
// a line [[ name ]] is an array-of-tables header:
// find or create an array value at `name`
// append a NEW empty table to it
// set current = that new table
// a second [[name]] appends another element (does not redefine)
CheckpointDONE
Double-bracket headers append a table per occurrence. Commit and stop here.