Skip to content

Commit 17d4b8f

Browse files
committed
fix: Handle semicolon in parse_grouped_expression
Add comment
1 parent 485c503 commit 17d4b8f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

lib/spitfire.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ defmodule Spitfire do
310310
else
311311
@terminals_with_comma
312312
end
313-
313+
314314
{parser, is_valid} = validate_peek(parser, current_token_type(parser))
315315
# TODO should we handle ; here?
316316
# TODO removing peek_token(parser) != :eol does not break any tests
@@ -399,7 +399,7 @@ defmodule Spitfire do
399399

400400
cond do
401401
# if the next token is the closing paren or if the next token is a newline and the next next token is the closing paren
402-
peek_token(parser) == :")" || (peek_token(parser) == :eol && peek_token(next_token(parser)) == :")") ->
402+
peek_token(parser) == :")" || (peek_token(parser) in [:eol, :";"] && peek_token(next_token(parser)) == :")") ->
403403
parser =
404404
parser
405405
|> Map.put(:nesting, old_nesting)
@@ -431,11 +431,11 @@ defmodule Spitfire do
431431
{ast, parser}
432432

433433
# if the next token is a new line, but the next next token is not the closing paren (implied from previous clause)
434-
peek_token(parser) == :eol or current_token(parser) == :-> ->
434+
peek_token(parser) in [:eol, :";"] or current_token(parser) == :-> ->
435435
# second conditon checks of the next next token is a closing paren or another expression
436436
{exprs, parser} =
437437
while2 current_token(parser) == :-> ||
438-
(peek_token(parser) == :eol && parser |> next_token() |> peek_token() != :")") <- parser do
438+
(peek_token(parser) in [:eol, :";"] && parser |> next_token() |> peek_token() != :")") <- parser do
439439
{ast, parser} =
440440
case Map.get(parser, :stab_state) do
441441
%{ast: lhs} ->
@@ -482,7 +482,7 @@ defmodule Spitfire do
482482

483483
# handles if the closing paren is on a new line or the same line
484484
parser =
485-
if peek_token(parser) == :eol do
485+
if peek_token(parser) in [:eol, :";"] do
486486
next_token(parser)
487487
else
488488
parser
@@ -1673,7 +1673,6 @@ defmodule Spitfire do
16731673
# TODO should we add ; here?
16741674
# TODO is anything needed? Removing all tokens does not break any tests
16751675
terminals = [:eol, :eof, :"}", :")", :"]", :">>"]
1676-
terminals = [:eol, :eof, :"}", :")", :"]", :">>"]
16771676

16781677
{parser, is_valid} = validate_peek(parser, current_token_type(parser))
16791678

@@ -2331,6 +2330,7 @@ defmodule Spitfire do
23312330
parser
23322331
end
23332332

2333+
# TODO this may be too greedy. Probably the better option would be to have distinct eat_eol/1 and eat_eol_or_semicolon/1
23342334
defp eat_eol(parser) do
23352335
case eat(%{:eol => true, :";" => true}, parser) do
23362336
%{current_token: {token, _}} = parser when token in [:eol, :";"] -> eat_eol(parser)

test/spitfire_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ defmodule SpitfireTest do
427427
!false
428428
)
429429
''',
430+
~S'''
431+
(; !false;)
432+
''',
430433
"(not false)",
431434
~S'''
432435
a do
@@ -1007,6 +1010,21 @@ defmodule SpitfireTest do
10071010

10081011
assert Spitfire.parse(code) == s2q(code)
10091012

1013+
code = ~S'''
1014+
(
1015+
min_line = line(meta); max_line = closing_line(meta); Enum.any?(comments, fn %{line: line} -> line > min_line and line < max_line end)
1016+
)
1017+
'''
1018+
1019+
assert Spitfire.parse(code) == s2q(code)
1020+
1021+
code = ~S'''
1022+
(
1023+
min_line = line(meta); max_line = closing_line(meta); Enum.any?(comments, fn %{line: line} -> line > min_line and line < max_line end); )
1024+
'''
1025+
1026+
assert Spitfire.parse(code) == s2q(code)
1027+
10101028
code = ~S'''
10111029
(min_line = line(meta)
10121030
max_line = closing_line(meta)

0 commit comments

Comments
 (0)