PLC-lang/rusty

Trailing comma in array elements

Aperta

#1021 aperta il 21 nov 2023

 (2 commenti) (0 reazioni) (0 assegnatari)Rust (71 fork)auto 404
enhancementgood first issuelow-priority

Metriche repository

Star
 (351 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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,
        ],
    },
}

Guida contributor