-
Notifications
You must be signed in to change notification settings - Fork 857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added grouping to ambiguous alternatives, allow leading newlines inside arrays and spaces after newlines before array elements and newlines and spaces before comments in arrays #378
Conversation
RFC 4234 Section 3.5 advises the use of grouping notation rather than "bare" alternation when alternatives consist of multiple rules or literals, e.g. "( int float ) / ( bool char )" instead of "int float / bool char". I might've gotten the grouping wrong, which just serves to illustrate the importance of using grouping notation.
The README.md has this at the end of the first TOML example: ```toml hosts = [ "alpha", "omega" ] ``` And later says: >...arrays also ignore newlines.... But the ABNF doesn't allow for a newline to precede the first element. I wasn't sure if the grammar should allow more than one newline preceding the first element, but given that an arbitrary number of whitespaces are allowed and the readme says that newlines are ignored in general I figured allowing an arbitrary number of newlines best matches your intent. It also won't break anyone that happens to have multiple leading newlines in their arrays.
The array-values production already allows for an arbitrary amount of newlines before the closing brace. It doesn't, however, allow any whitespace after the newlines that don't come just before the closing brace so: ```toml [5, 6 \t\t] ``` is valid, but ```toml [5, 6 \t\t ] ``` is invalid. Probably not a big deal.
I guess this automatically added my new commits to this pull request. The documentation states that newlines are ignored in arrays and gives examples of arrays with newlines before the first element, however the ABNF doesn't allow for newlines before the first element. I've added a production for multiple whitespaces or newlines, "ws-newline", and changed the "ws" after the array opening brace to "ws-newline". |
Back to the README example: ```toml hosts = [ "alpha", "omega" ] ``` The ABNF allows spaces after an element, but before any newlines, the examples in the README have spaces before elements after a newline which isn't allowed in the grammar. I've added a new production "ws-newlines" which is just like "ws-newline" except it requires at least one newline like the production it replaces("newlines"). With "ws-newlines" it makes it legal to to put whitespace before an element after a newline.
Added another new production to allow whitespace before an array element after a newline. The examples in the README do this, but it isn't allowed by the grammar. |
Going over your hard example I found I couldn't parse this: ```toml multi_line_array = [ "]", # ] Oh yes I did ] ``` It turns out that the grammar doesn't allow newlines and whitespaces after the array-sep. I've modified the production again to accommodate newlines and whitespace after the array separator.
Going over your hard example I found I couldn't parse this: multi_line_array = [
"]",
# ] Oh yes I did
] It turns out that the grammar doesn't allow newlines and whitespaces after the array-sep before the comment. I've modified the production again to accommodate newlines and whitespace after the array separator before the comment. |
after the last element without a leading newline.
RFC 4234 Section 3.5 advises the use of grouping notation rather than "bare" alternation when alternatives consist of multiple rules or literals, e.g. "( int float ) / ( bool char )" instead of "int float / bool char".
I might've gotten the grouping wrong, which just serves to illustrate the importance of using grouping notation.