Is there a way to define a list of Sorts separated by a newline? I tried using the following syntax:
syntax Entity = entity: "entity" EntityName entityName
{Attribute "\n"}* attribs
"end"
;
but it returns a parse error each time, but when I try doing the same thing with a comma for example. It works.
syntax Entity = entity: "entity" EntityName entityName
{Attribute ","}* attribs
"end"
;
What you wrote is fine, but there is probably a rule for layout
in the same scope with a follow restriction (longest match) declared on it:
layout WS = [\ \t\n]* !>> [\ \t\n];
To get rid of the parse errors you can remove the \n
from the follow restriction.
But it is likely you could get ambiguity in return. There could be several ways to solve this. I would start with making sure the separator is recognized as the first newline, so that consecutive newlines can go to the layout:
{Attribute [\n] !<< "\n"}+
It all depends on the other rules in your grammar if this will work or not. In particular nullable nonterminals will generate ambiguity. All occurrences of them will require additional follow or precede restrictions to make sure the newlines all go before or all go after their empty instances:
A = /* empty */ !>> [\n]
| "NonemptyA"
;
Hope this helps!
PS thinking about it a little more, we probably have to reconsider the follow restriction on all whitespace characters. Consider sequences like space, newline, space, newline, between attributes. The question will always be which of the two newlines is the separator, and which is the layout.
Another direction you could go is to use a follow requirement, such as "" >> [\n]
; this won't eat the newline but every empty separator will have to be followed by one. With this solution direction you can keep all the original layout definitions. Never tried it myself though, so no guarantees 😇