eclipse-langium / langium

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

Fragments that assign to arrays only assign the last parsed element #896

Closed daumantas-kavolis-sensmetry closed 1 year ago

daumantas-kavolis-sensmetry commented 1 year ago

Langium version: 1.0.1 Package name: langium

Fragments that assign to an array only assign the last parsed element when used with + or * cardinalities. This is a serious bug that prevents using fragment composition in more complex grammars for parsing declarations and bodies without resorting to copy-pasting .

Steps To Reproduce

  1. Replace
entry Model:
    (persons+=Person | greetings+=Greeting)*;

with

entry Model:
    ModelItem*;

fragment ModelItem:
    persons+=Person | greetings+=Greeting;

Link to code example:

Link to playground

The current behavior

{
  $type: "Model",
  greetings: [
    {
      $type: "Greeting",
      person: Reference("/persons@0"),
    },
  ],
  persons: [
    {
      $type: "Person",
      name: "Jane",
    },
  ],
}

The expected behavior

{
  $type: "Model",
  persons: [
    {
      $type: "Person",
      name: "John",
    },
    {
      $type: "Person",
      name: "Jane",
    },
  ],
  greetings: [
    {
      $type: "Greeting",
      person: Reference("/persons@0"),
    },
    {
      $type: "Greeting",
      person: Reference("/persons@1"),
    },
  ],
}
msujew commented 1 year ago

Hey @daumantas-kavolis-sensmetry, thanks for the report. I've created #898 to fix this issue.