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 isType function for primitive datatype rules #1003

Merged
merged 7 commits into from
Apr 5, 2023

Conversation

gfontorbe
Copy link
Contributor

@gfontorbe gfontorbe commented Mar 28, 2023

closes #990

The idea is to generate checker functions for datatype rules through langium-cli.
This only applies on datatype rules that are only composed of strings or terminals. I also had to slightly touch the type inference to provide the regex for terminals.

Example:

A returns string:
    'a';

AB returns string:
    A | 'b';

ABC returns string:
    AB | ID;

will generate the following functions in the generated ast.ts file:

export function isA(item: string): item is A {
    return item === 'a';
}

export function isAB(item: string): item is AB {
    return isA(item) || item === 'b';
}

export function isABC(item: string): item is ABC {
    return isAB(item) || /[_a-zA-Z][\w_]*/.test(item);
}

@spoenemann
Copy link
Contributor

Could you run npm run langium:generate in the root and check in the resulting changes?

@msujew msujew changed the title add checker for datatype rules of type string Generate isType function for primitive datatype rules Mar 28, 2023
Copy link
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, thanks!

Can we also include generated checks for number, bigint and Date types? I think that would complete the feature on this part.

@msujew msujew added this to the v1.2.0 milestone Mar 29, 2023
@msujew msujew added ast AST structure related issue types Types related issue labels Mar 30, 2023
Copy link
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking fairly good. I have a few minor improvements.

Copy link
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the generator tests a bit further. Looks good to me now 👍

@msujew msujew merged commit 82613e6 into main Apr 5, 2023
@msujew msujew deleted the gfontorbe/990-checker-datatypes branch April 5, 2023 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ast AST structure related issue types Types related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate checker functions for data type rules
4 participants