I'm trying to make a TOML parser but the spec rules are not clear to me. It seems that declaring existing key names allows indexing into chains of tables but the spec states this is "redefining the table" and thus contradicts itself. Please clarify what the rules are.
Does the author himself have an official parser? That would be a tremendous help in verifying our parsers follow the spec.
Examples taken from the spec:
# Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed.
[fruit]
[fruit.apple] # INVALID
[fruit.apple.taste] # INVALID
# QUESTION 1: Why is this allowed? fruit is already defined
[fruit.apple.texture]
smooth = true
[[fruit]]
name = "apple"
# QUESTION 2: why is this allowed? fruit is already defined
[fruit.physical]
color = "red"
shape = "round"
The full example you've drawn from:
[fruit]
apple.color = "red"
apple.taste.sweet = true
# [fruit.apple] # INVALID
# [fruit.apple.taste] # INVALID
[fruit.apple.texture] # you can add sub-tables
smooth = true
Note the additional keys under the first [fruit] header; _those_ are why the redefinitions are illegal. The spec's example is demonstrating that fully-qualified TOML names must be unambiguous by showing an attempt to redefine dotted key-value pairs as tables (the commented out # INVALID lines).
The modified example TOML in your snippet is legal because you've removed the lines that would provide the initial definitions, so no redefinition is occurring.
To answer the actual questions:
# QUESTION 1: Why is this allowed? fruit is already defined
[fruit.apple.texture]
smooth = true
It's allowed because you're not redefining the fruit table, you're defining one of its children (specifically, you're defining the fruit.apple.texture child table _and_ adding a value with the key smooth to it). See my clarification comment above.
# QUESTION 2: why is this allowed? fruit is already defined
[fruit.physical]
color = "red"
shape = "round"
Same answer.
Thanks @marzer. I don't think I was understanding that the tables are "defined" by the "leafs" of the dotted name chains. I would say maybe the documentation could be more clear about this but maybe I was just being dense.
I think my issue #769 points out this same problem with the spec.
Most helpful comment
To answer the actual questions:
It's allowed because you're not redefining the
fruittable, you're defining one of its children (specifically, you're defining thefruit.apple.texturechild table _and_ adding a value with the keysmoothto it). See my clarification comment above.Same answer.