PLC-lang/rusty

Trailing comma in array elements

開放

#1,021 建立於 2023年11月21日

 (2 則留言) (0 個反應) (0 位負責人)Rust (71 個分叉)auto 404
enhancementgood first issuelow-priority

倉庫指標

星標
 (351 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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

貢獻者指南