Builtin Library as document prelude #853
-
Hello everyone, I'm really enjoying using Langium as my first ever language engineering tool (I've briefly sketched the grammar in Xtext but then migrated to Langium). I'm developing a DSL where each file is independent from one another, so I'm really not using the workspace as a manager for multiple files feature. So each file can be checked, validated, scoped, linked and so on independently. Then I have an option at the top of a file to include or not a prelude, which means a list of prebuilt definitions of values and references I want available in the file without redeclaring them (nor importing, since there's no import stuff going on between files). This prelude is known and it's the same for everyone, if needed, so I thought about adding it directly to the language. My idea was to somehow prepend some text/nodes to the file/document I'm working on in a said moment, so that every check/validation/scope/linking process can then be executed as if the prelude was written originally in the file. My questions/doubts are:
For context: since I don't have imports/exports between files I reimplemented the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Hey @kristiannotari, Glad to hear you're enjoying your time with Langium! Leave the GitHub repo a star ;) I believe that modifying the text to-be-parsed in place is not a good idea. At the very least it will massively mess with all language services as you already mentioned, due to all offsets being incorrect compared to the original file that's displayed by the user. I think a best-practice solution involves the builtin libraries as well as some minor modification to the scoping mechanism.
That's a good start! First off, define your different preludes as separate builtin library documents. The solution that I have in mind also does something similar as your current solution, but performs the normal Next, you will need to override the I believe that solves everything:
Yes, you can just include it into the |
Beta Was this translation helpful? Give feedback.
Hey @kristiannotari,
Glad to hear you're enjoying your time with Langium! Leave the GitHub repo a star ;)
I believe that modifying the text to-be-parsed in place is not a good idea. At the very least it will massively mess with all language services as you already mentioned, due to all offsets being incorrect compared to the original file that's displayed by the user. I think a best-practice solution involves the builtin libraries as well as some minor modification to the scoping mechanism.