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

Generate a new type corresponding to the model root type #1811

Open
theogiraudet opened this issue Feb 15, 2025 · 3 comments
Open

Generate a new type corresponding to the model root type #1811

theogiraudet opened this issue Feb 15, 2025 · 3 comments

Comments

@theogiraudet
Copy link

theogiraudet commented Feb 15, 2025

A really simple suggestion: add in the ast.ts a new type corresponding to the type being returned by the entry rule.

For instance, with the Hello World example:

grammar HelloWorld

entry Model:
    (persons+=Person | greetings+=Greeting)*;

Person:
    'person' name=ID;

Greeting:
    'Hello' person=[Person:ID] '!';

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;

hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/;

The generated ast.ts file will contain a new type:

export type Root = Model;

"Root" here is just an example, but I suppose this will be better to use a name that cannot be used as a type name in the grammar.
This is useful for reflection since, as far as I know, there is currently no way to know statically which interface is the root one.

@Lotes
Copy link
Contributor

Lotes commented Feb 15, 2025

You could have one Langium project with multiple grammars contributing a different root node each.

What would you like to solve? Having explicit entry points? Why is this so important? As the author of your language you know the root.
Just some thoughts. You might have a good argument…

@msujew
Copy link
Member

msujew commented Feb 15, 2025

@Lotes I think there are ways around that. I don't think that Root is a good idea either, but having something like <GrammarName>Root could work.

@theogiraudet
Copy link
Author

Thanks for your feedback!

You could have one Langium project with multiple grammars contributing a different root node each.

Indeed, I didn't think about this point.
After consideration, a type for that is not a good idea.
However, having somewhere this information can be useful. Not necessary for the language designer but for reflection purposes and external libraries. To iterate over the abstract syntax definition at a higher level of abstraction.
We already have the AstReflection providing some useful information in this direction, adding information about the root type can be more relevant in this class, for instance if the getTypeMetaData function.

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

No branches or pull requests

3 participants