Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a Builder/Functions for creating ASTs without a CST #1755

Open
nametable opened this issue Nov 18, 2024 · 2 comments
Open

Provide a Builder/Functions for creating ASTs without a CST #1755

nametable opened this issue Nov 18, 2024 · 2 comments
Labels

Comments

@nametable
Copy link

I find that a builder to create an AST programmatically would be helpful for generating code targeting a language built using Langium. My current approach is to use the CompositeGeneratorNode syntax to manually craft text which should be compliant with my language's syntax, however this can be cumbersome and prone to error.

My thought is that if a person implements a formatter for an AST, it should be able to produce the CST and thus spit out valid syntax. The construction of the AST node types however currently requires setting the parent $container of each AST node.

Is there a recommended way to do something like this already, or would adding AST node generator functions/utilities to Langium potentially make sense?

@msujew
Copy link
Member

msujew commented Nov 19, 2024

Hey @nametable,

My thought is that if a person implements a formatter for an AST, it should be able to produce the CST and thus spit out valid syntax.

Note that the current formatter implementation operates on the existing CST, and does not produce a CST (i.e. it simply shifts the existing text nodes, and does not create any nodes).

The construction of the AST node types however currently requires setting the parent $container of each AST node.

You can just set the $container property to undefined! and then run the linkContentToContainer function on everything.

Is there a recommended way to do something like this already, or would adding AST node generator functions/utilities to Langium potentially make sense?

The recommendation is to create a manual serializer right now (as you're already doing). What you're looking for is an automatic serializer. However, building a completely generic, grammar agnostic serializer is really complicated, and while it can be done (see Xtext's serializer), we currently don't have any plans to work on that.

@spoenemann
Copy link
Contributor

See this article about developing a generic serializer: https://www.typefox.io/blog/lionweb-langium/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants