rusty icon indicating copy to clipboard operation
rusty copied to clipboard

Trailing comma in array elements

Open volsa opened this issue 2 years ago • 2 comments

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

volsa avatar Nov 21 '23 09:11 volsa

maybe we just allow it in general. I like the rust style where the last comma can be ignored

ghaith avatar Nov 21 '23 15:11 ghaith

What about these situations

int_array : ARRAY[0..2, 0..2, 0..2,] OF INT := [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,];
bArray : ARRAY[0..7,] OF BOOL := [8(FALSE),];
a : ARRAY[0..3,] OF INT := (1, 2, 3, 4,)
bracket : ARRAY[0..1,] OF myStruct := [(x := 2, y := 4,), (x := 6, y := 8,),];

a0, b0, c0, d0, : INT;

Should last comma still be ignored?

teksturi avatar Nov 23 '23 10:11 teksturi