pnathan / pp-toml

Paul's Parser for Tom's Own Minimal Language
https://github.com/mojombo/toml
Other
19 stars 3 forks source link

Implement array of tables #8

Open pnathan opened 9 years ago

pnathan commented 9 years ago

copypasta of the spec


Array of Tables

The last type that has not yet been expressed is an array of tables. These can be expressed by using a table name in double brackets. Each table with the same double bracketed name will be an element in the array. The tables are inserted in the order encountered. A double bracketed table without any key/value pairs will be considered an empty table.

[[products]] name = "Hammer" sku = 738594937

[[products]]

[[products]] name = "Nail" sku = 284758393 color = "gray"

In JSON land, that would give you the following structure.

{ "products": [ { "name": "Hammer", "sku": 738594937 }, { }, { "name": "Nail", "sku": 284758393, "color": "gray" } ] }

You can create nested arrays of tables as well. Just use the same double bracket syntax on sub-tables. Each double-bracketed sub-table will belong to the most recently defined table element above it.

[[fruit]] name = "apple"

[fruit.physical] color = "red" shape = "round"

[[fruit.variety]] name = "red delicious"

[[fruit.variety]] name = "granny smith"

[[fruit]] name = "banana"

[[fruit.variety]] name = "plantain"

The above TOML maps to the following JSON.

{ "fruit": [ { "name": "apple", "physical": { "color": "red", "shape": "round" }, "variety": [ { "name": "red delicious" }, { "name": "granny smith" } ] }, { "name": "banana", "variety": [ { "name": "plantain" } ] } ] }

Attempting to define a normal table with the same name as an already established array must produce an error at parse time.

INVALID TOML DOC

[[fruit]] name = "apple"

[[fruit.variety]] name = "red delicious"

This table conflicts with the previous table

[fruit.variety] name = "granny smith"

pnathan commented 2 years ago

review for v1.0