Skip to content

Infix rules with explicit return type do not use that type in the AstNode #2075

@nk-coding

Description

@nk-coding

Langium version: 4.1.2
Package name: langium

Steps To Reproduce

  1. Have a infix rule which with an explicit return type with a name different from the rule name.
  2. The $type property of the AstNode is the name of the rule, not the name of the return type.

Grammar example:

grammar HelloWorld

entry Model:
    expression=OperatorExpression;

interface BaseExpression {}

interface IdExpression extends BaseExpression {
    id: string;
}

interface ComplexExpression extends BaseExpression {
    operator: string;
    left: BaseExpression;
    right: BaseExpression;
}

IdExpression returns IdExpression:
    id=ID;

infix OperatorExpression on IdExpression returns ComplexExpression:
    '%'
    > '^'
    > '*' | '/'
    > '+' | '-';

BOOLEAN returns boolean:
    'true' | 'false';

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

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

Code to parse:

a + b

The current behavior

The AstNode for the + expression has $type: "OperatorExpression"

The expected behavior

The AstNode for the + expression has $type: "ComplexExpression"

Suspected cause

computeRuleType in langium-parser.ts directly returns rule.name for the infix case, ignoring the explicit return type.

Possible solution

Use the same logic in the infix case as in the else case at the end.
Note: the type is still used for the operatorPrecedence lookup, there, the rule name should still be used (which must thus be passed through in addition to the type).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions