Alternative to Enums #521
-
I'm creating a grammar with a concrete syntax that is html-like for attribute assignments, e.g.,:
To be syntactically correct, the attribute's value should always be enclosed in quotes. There are cases where I want to limit value to a range of pre-defined values. In Xtext, I'd have created an enum and assigned the enum's value to the matching string value, making the parser recognize the string value as the enum value. Since Langium requires me to create a data type rule for this, I'm not really sure how to accomplish it. I have defined the following data type rule as an example:
and a rule that uses it like this:
But I get a parser error if I enclose a value for Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In this case you could write it like this:
But I'm not sure how this affects the lexer (tokenizer). Chevrotain has a simple lexing approach that takes the first match in the defined order, so it might be that this shadows the general
|
Beta Was this translation helpful? Give feedback.
In this case you could write it like this:
But I'm not sure how this affects the lexer (tokenizer). Chevrotain has a simple lexing approach that takes the first match in the defined order, so it might be that this shadows the general
STRING
terminal rule. An alternative is to hard-code the quotes into the enum values: