How to implement a line breaks and indentations aware language? Any example? #1446
-
Hi team, I am going to implement a DSL that recognizes statements via line breaks and indentations (similar to yaml or python), is there any example for reference? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hey @jindong-zhannng, I don't think there's a public example for this available right now. Though you might want to take a look at how these languages work in ANTLR4 to see how one would implement such a thing in Langium. See also this example from Chevrotain to see how one implements indentation based lexing. Generally, white space aware languages work well from a parser perspective, but break pretty spectacularly in the presence of parser errors due to the amount of tokens the parser needs to keep track of. Whenever we had to deal with a whitespace aware language, we tried to make it as non-whitespace aware as possible (not possible with some languages) and only add some whitespace awareness to special terminal tokens. |
Beta Was this translation helpful? Give feedback.
Ok, I see, that makes sense. Well, I guess you won't be able to get around using some whitespace sensitivity. Something like:
Note that this has the major drawback of explicitly using the
NEWLINE
everywhere you expect a newline character to appear, as it is now no longer hidden.