PLC-lang/rusty

Trailing comma in array elements

Offen

#1.021 geöffnet am 21.11.2023

 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Rust (71 Forks)auto 404
enhancementgood first issuelow-priority

Repository-Metriken

Stars
 (351 Sterne)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

Is your feature request related to a problem? Please describe. A trailing comma in array elements will generate "weird" error messages. For example arr : [...] := [1, 2, 3, 4, 5,]; currently returns

error: Unexpected token: expected Literal but found ]
  ┌─ target/demo.st:3:53
  │
3 │         arr : ARRAY[1..5] OF DINT := [1, 2, 3, 4, 5,];
  │                                                     ^ Unexpected token: expected Literal but found ]

error: Unresolved constant 'arr' variable: Cannot resolve constant: EmptyStatement
  ┌─ target/demo.st:3:38
  │
3 │         arr : ARRAY[1..5] OF DINT := [1, 2, 3, 4, 5,];
  │                                      ^^^^^^^^^^^^^^^^ Unresolved constant 'arr' variable: Cannot resolve constant: EmptyStatement

Describe the solution you'd like Either allow trailing commas returning a warning about it or make it a hard error. In either case a better error message would be great. Technically the error message is already clear if not for the second error.

Edit, additional context: We get the second error because the generated AST will look like the following due to the trailing comma

LiteralArray {
    elements: ExpressionList {
        expressions: [
            LiteralInteger { value: 1 },
            ...
            LiteralInteger { value: 5 },
            EmptyStatement,
        ],
    },
}

Contributor Guide