Skip to content

Commit

Permalink
fix: struct types (#16)
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
mhanberg authored Feb 15, 2024
1 parent 7af1625 commit 1000574
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- nerves-hub/nerves_hub_web
- nerves-hub/nerves_hub_link
- mishka-group/mishka_developer_tools
# - absinthe-graphql/absinthe
- absinthe-graphql/absinthe
- elixir-wallaby/wallaby
- livebook-dev/livebook
- livebook-dev/kino
Expand Down
45 changes: 44 additions & 1 deletion lib/spitfire.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,9 @@ defmodule Spitfire do
defp parse_struct_type(parser) do
# structs can only have certain expressions to denote the type,
# so we special case them here rather than parse an arbitrary expression

{associativity, precedence} = @lowest

prefix =
case current_token_type(parser) do
:identifier -> &parse_lone_identifier/1
Expand Down Expand Up @@ -1379,7 +1382,43 @@ defmodule Spitfire do

{{:__error__, meta, ["unknown token: #{ctype}"]}, parser}
else
prefix.(parser)
{left, parser} = prefix.(parser)

calc_prec = fn parser ->
{_associativity, power} = peek_precedence(parser)

precedence =
case associativity do
:left -> precedence
:right -> precedence - 1
end

precedence < power
end

terminals = [:eol, :eof, :"}", :")", :"]", :">>"]

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

if is_valid do
while peek_token(parser) not in terminals && calc_prec.(parser) <- {left, parser} do
infix =
case peek_token_type(parser) do
:. -> &parse_dot_expression/2
_ -> nil
end

case infix do
nil ->
{left, parser}

_ ->
infix.(next_token(parser), left)
end
end
else
{left, parser}
end
end
end

Expand Down Expand Up @@ -2304,6 +2343,10 @@ defmodule Spitfire do
true
end

defp valid_peek?(:")", :"{") do
true
end

defp valid_peek?(:"}", ptype) do
ptype in (@operators ++ [:"[", :";", :eol, :eof, :",", :")", :do, :., :"}", :"]", :">>", :end])
end
Expand Down
12 changes: 10 additions & 2 deletions test/spitfire_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,14 @@ defmodule SpitfireTest do
b: b,
}
''',
"%^resource{}"
"%^resource{}",
~S'%__MODULE__.Foo{bar: "foo"}',
~S'%Bar.__MODULE__.Foo{bar: "foo"}',
~S'''
%Bar.__MODULE__.Foo{
bar: "foo"
}
'''
]

for code <- codes do
Expand Down Expand Up @@ -1760,7 +1767,8 @@ defmodule SpitfireTest do
codes = [
"__MODULE__",
"__MODULE__.foo()",
"__MODULE__.Foo"
"__MODULE__.Foo",
"Foo.__MODULE__.Bar"
]

for code <- codes do
Expand Down

0 comments on commit 1000574

Please sign in to comment.