langium icon indicating copy to clipboard operation
langium copied to clipboard

Declared Types cannot include Keywords as Properties

Open montymxb opened this issue 2 years ago • 4 comments

Present in Langium version: 0.4.0

We can currently generate AST types via inference that include various Langium keywords as properties, like so:

A: interface=ID type=ID grammar=ID;

infers...

export interface A extends AstNode {
    grammar: string
    interface: string
    type: string
}

However, if cannot do the same via declared types. The following is an example demonstrating how this fails, but also shows that Langium is highlighting the properties incorrectly as keywords.

Unless we intend to remove these properties from being used via inference, we should also support them in their declared forms.

To solve this:

  • we should be able to declare the same properties via inference as we can explicitly
  • we should have a test verifying this behavior
  • if we choose to block certain keywords, we'll need extra tests guarding against those keywords (should we choose this route instead)

montymxb avatar Jun 22 '22 09:06 montymxb

You can always escape keywords using the ^ token at the start of the ID. But anway, we should use the appropriate data type rule for the names of properties.

msujew avatar Jun 22 '22 09:06 msujew

Yep, missed that the first time around with the ^ token, that does correct the issue above. Highlighting is still the same as before, but it generates ok 👍 . I think in this case, with just the highlighting thing, this can probably be closed as is. Maybe a separate issue to resolve the highlighting can be opened separately.

Lastly, @msujew could you elaborate on what you mean about using the appropriate data type rule for the names of properties in this context? Are you talking about #559 by chance?

montymxb avatar Jun 22 '22 11:06 montymxb

@montymxb There's a special data type rule that can be used to work around the ^ token:

https://github.com/langium/langium/blob/207b6e59f063d04825ab77185cda1baa96b7ee25/packages/langium/src/grammar/langium-grammar.langium#L198-L199

msujew avatar Jun 22 '22 11:06 msujew

Got it, makes more sense now how this can be rectified 👍

montymxb avatar Jun 23 '22 08:06 montymxb

I have a probably similar problem: When creating the hello-world-language (langium 0.5.0) and changing the grammar from 'person' name=ID to 'ID' name=ID I get a problem:

Person:
    'ID' name=ID;

with the model

ID Pierre
Hello Pierre!

--> Error "Expecting token of type 'EOF' but found ID.hello-world(parsing-error)".

Renaming the rule ID to Id fixes the issue...

goto40 avatar Nov 29 '22 15:11 goto40

@goto40 This seems to be a less relevant issue. After a few tests, I find that this behavior occurs if a terminal rule uses a keyword (like 'ID' in this example) as its name. Also, this error does not occur at v0.5.0 (but occurs after that).

luan-xiaokun avatar Dec 08 '22 09:12 luan-xiaokun

Fixed with #811.

spoenemann avatar Dec 09 '22 08:12 spoenemann