eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
725 stars 65 forks source link

Parser rule used as type for cross-reference is marked as "unused" #1309

Open JohannesMeierSE opened 10 months ago

JohannesMeierSE commented 10 months ago

The following grammar uses the (unassigned) parser rule Person as type in the cross-reference of Greeting in order to link to Neighbors or Friends:

grammar HelloWorld

entry Model:
    (persons+=Neighbor | friends+=Friend | greetings+=Greeting)*;

Neighbor:
    'neighbor' name=ID;
Friend:
    'friend' name=ID;

Person: Neighbor | Friend;

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

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

The current behavior

Nevertheless, the parser rule Person is marked as "unused", e.g. in the Playground:

Screenshot 2023-11-30 at 10 54 04

The expected behavior

I would expect, that Person is not marked as "unused", since it is used as type in the cross-reference.

JohannesMeierSE commented 10 months ago

I am willing to work on this issue.

msujew commented 10 months ago

This is working as intended. The parser rule is indeed unused. You're supposed to use a type union instead.

JohannesMeierSE commented 10 months ago

This is working as intended. The parser rule is indeed unused. You're supposed to use a type union instead.

I see, that makes sense.

What do you think about showing a corresponding hint with a quick-fix to replace the parser rule by the following union type?

grammar HelloWorld

entry Model:
    (persons+=Neighbor | friends+=Friend | greetings+=Greeting)*;

Neighbor:
    'neighbor' name=ID;
Friend:
    'friend' name=ID;

type Person = Neighbor | Friend;

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

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;
msujew commented 10 months ago

Sure, I think that's good 👍