Skip to content

Commit

Permalink
Add several missing entries to table comma lookahead
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Feb 7, 2024
1 parent b51f6a6 commit 354b7bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parser/parse_errors.mlyl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rule error_message = parse error
{ Error.Use_double_equals (token, $startloc(token), $endloc(token)) }

(* Suggest a missing comma in table entries. *)
| last = [_ / table_body: table_entry . ...] @ STRING, NUMBER, TRUE, FALSE, NIL
| last = [_ / table_body: table_entry . ...] @ STRING, NUMBER, TRUE, FALSE, NIL, OSQUARE, OPAREN, IDENT
{
let token = (token, $startloc(token), $endloc(token)) in
Missing_table_comma { comma_pos = $endloc(last); token }
Expand Down
34 changes: 34 additions & 0 deletions test/parser/t_parse_errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ local function f(a
```

## Missing commas in tables
We try to detect missing commas in tables, and print an appropriate error message.

```lua
return { 1 2 }
Expand Down Expand Up @@ -123,6 +124,39 @@ return { 1, 2 3 }
1 │ return { 1, 2 3 }
│ ^ Are you missing a comma here?
```

This also works with table keys.

```lua
print({ x = 1 y = 2 })
```

```txt
=input: Unexpected identifier in table. [parse:syntax-error]
1 │ print({ x = 1 y = 2 })
│ ^
1 │ print({ x = 1 y = 2 })
│ ^ Are you missing a comma here?
```

```lua
print({ ["x"] = 1 ["y"] = 2 })
```

```txt
=input: Unexpected `[` in table. [parse:syntax-error]
1 │ print({ ["x"] = 1 ["y"] = 2 })
│ ^
1 │ print({ ["x"] = 1 ["y"] = 2 })
│ ^ Are you missing a comma here?
```

We gracefully handle the case where we are actually missing a closing brace.

```lua
print({ 1, )
```
Expand Down

0 comments on commit 354b7bd

Please sign in to comment.