How to allow one-time matching of optional groups #1191
-
Hi there, in yaml, it's only possible for each key1: key2: key1:
key2: key2:
key1: It would be possible to match unordered optional rules doing this, but not only one-time:
It's possible to match one-time by doing this, but it would be more complex if there were more possible rules:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
@Yokozuna59 This kind of semantic validation is out of scope for parsers. Parsers perform lexical analysis of text, while the semantic analysis should be performed on the parser result. That's what our validation engine/phase does in Langium. I would recommend something like this instead:
Since you're parsing yaml, it might make sense to make the parser even more generic, with
Generally, parsers should be able to parse more than what is semantically valid. The advantage is mainly that you don't get cryptic parser errors but reasonable validation messages instead. It also reduces the risk of the parser just exiting the file completely, which results in incomplete AST results. If you really want to go down that route, it should be enough to just write (key1=KeyOne & key2=KeyTwo & ...) though. |
Beta Was this translation helpful? Give feedback.
In theory unordered groups (i.e. elements split via
&
should be used for this. However, our go-to approach is to just use normal repeated alternatives (i.e.(a | b | ...)*
and validate that no duplicates appear. Everything else only results in cryptic parser error messages.