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

Data type using one terminal behaves differently from that terminal #1358

Closed
msujew opened this issue Jan 25, 2024 Discussed in #1357 · 1 comment · Fixed by #1367
Closed

Data type using one terminal behaves differently from that terminal #1358

msujew opened this issue Jan 25, 2024 Discussed in #1357 · 1 comment · Fixed by #1367
Labels
bug Something isn't working parser Parser related issue
Milestone

Comments

@msujew
Copy link
Member

msujew commented Jan 25, 2024

Discussed in #1357

Originally posted by drhagen January 25, 2024
If I have this grammar

grammar HelloWorld

entry Model:
    ((persons+=Person) EOL)*;

Person:
    'person' name=ID;

hidden terminal WS: /[ \t]+/;
terminal ID: /[_a-zA-Z][\w_]*/;
terminal NEWLINE: '\n';
EOL returns string:
    NEWLINE;

and try to parse this file

person John
person Jane

I get an error "Expecting end of file but found 'person'".

image

If I replace EOL with NEWLINE and delete EOL entirely

grammar HelloWorld

entry Model:
    ((persons+=Person) NEWLINE)*;

Person:
    'person' name=ID;

hidden terminal WS: /[ \t]+/;
terminal ID: /[_a-zA-Z][\w_]*/;
terminal NEWLINE: '\n';

the file parses successfully. What am I missing something about Langium because I would have expected those to be equivalent?

@msujew
Copy link
Member Author

msujew commented Jan 25, 2024

Seems like we only ever assume that data type rules call other data type rules in an unassigned way, see:

// If we call a subrule without an assignment
// We either append the result of the subrule (data type rule)
// Or override the current object with the newly parsed object
const current = this.current;
if (isDataTypeNode(current)) {
current.value += result.toString();
} else {
const resultKind = result.$type;
const object = this.assignWithoutOverride(result, current);
if (resultKind) {
object.$type = resultKind;
}
const newItem = object;
this.stack.pop();
this.stack.push(newItem);
}

The assumption breaks as soon as we call the data type rule from a normal parser rule without an assignment. I assume the solution would be to just ignore the result and store the CST created in the current AST node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser Parser related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants