Skip to content

Commit

Permalink
Refactor to rely on move_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella committed Oct 25, 2024
1 parent 828708d commit 4099bdf
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions lib/live_view_native/template/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse(<<"<!--", document::binary>>, cursor, nodes, args) do
cursor = incr_column(cursor, 4)
cursor = move_cursor(cursor, ~c"<!--")

parse_comment_node(document, cursor, [], args)
|> case do
Expand All @@ -42,7 +42,7 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse(<<"<", document::binary>>, cursor, nodes, args) do
cursor = incr_column(cursor)
cursor = move_cursor(cursor, ?<)

parse_node(document, cursor, args)
|> case do
Expand Down Expand Up @@ -90,7 +90,7 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse_comment_node(<<"-->", document::binary>>, cursor, buffer, _args) do
cursor = incr_column(cursor, 3)
cursor = move_cursor(cursor, ~c"-->")

comment =
buffer
Expand Down Expand Up @@ -121,12 +121,12 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse_tag_name(<<char, document::binary>>, cursor, [], args) when char in @first_chars do
cursor = incr_column(cursor)
cursor = move_cursor(cursor, char)
parse_tag_name(document, cursor, [char], args)
end

defp parse_tag_name(<<char, document::binary>>, cursor, buffer, args) when char in @chars do
cursor = incr_column(cursor)
cursor = move_cursor(cursor, char)
parse_tag_name(document, cursor, [char | buffer], args)
end

Expand Down Expand Up @@ -195,11 +195,11 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse_attribute_key(<<char, document::binary>>, cursor, [], args) when char in @first_chars do
parse_attribute_key(document, incr_column(cursor), [char], args)
parse_attribute_key(document, move_cursor(cursor, char), [char], args)
end

defp parse_attribute_key(<<char, document::binary>>, cursor, key_buffer, args) when char in @chars do
parse_attribute_key(document, incr_column(cursor), [char | key_buffer], args)
parse_attribute_key(document, move_cursor(cursor, char), [char | key_buffer], args)
end

defp parse_attribute_key(<<"=", document::binary>>, cursor, key_buffer, _args) do
Expand All @@ -208,7 +208,7 @@ defmodule LiveViewNative.Template.Parser do
|> Enum.reverse()
|> List.to_string()

{document, cursor} = drain_whitespace(document, incr_column(cursor))
{document, cursor} = drain_whitespace(document, move_cursor(cursor, ?=))

{:ok, {document, key, cursor}}
end
Expand All @@ -226,16 +226,12 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse_attribute_value(<<"\"\"", document::binary>>, cursor, [], _args) do
cursor = incr_column(cursor, 2)
cursor = move_cursor(cursor, ~c'""')
{:ok, {document, "", cursor}}
end

defp parse_attribute_value(<<"\"", char, document::binary>>, cursor, [], args) do
cursor =
cursor
|> incr_column()
|> move_cursor(char)

cursor = move_cursor(cursor, [?", char])
parse_attribute_value(document, cursor, [char], args)
end

Expand All @@ -245,7 +241,7 @@ defmodule LiveViewNative.Template.Parser do
|> Enum.reverse()
|> List.to_string()

{:ok, {document, value, incr_column(cursor)}}
{:ok, {document, value, move_cursor(cursor, ?")}}
end

defp parse_attribute_value(_document, cursor, [], _args) do
Expand All @@ -258,7 +254,7 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse_tag_close(<<">", document::binary>>, cursor, _start_cursor, _args) do
{:ok, {document, incr_column(cursor)}}
{:ok, {document, move_cursor(cursor, ?>)}}
end

defp parse_tag_close(<<"/>", _document::binary>> = document, cursor, _start_cursor, _args) do
Expand All @@ -277,25 +273,25 @@ defmodule LiveViewNative.Template.Parser do
end

defp parse_end_tag(<<"</", document::binary>>, cursor, buffer, tag_name, start_cursor, args) do
cursor = incr_column(cursor, 2)
cursor = move_cursor(cursor, ~c"</")
{document, cursor} = drain_whitespace(document, cursor)
parse_end_tag(document, cursor, buffer, tag_name, start_cursor, args)
end

defp parse_end_tag(<<char, document::binary>>, cursor, [], tag_name, start_cursor, args) when char in @first_chars do
cursor = incr_column(cursor)
cursor = move_cursor(cursor, char)
parse_end_tag(document, cursor, [char], tag_name, start_cursor, args)
end

defp parse_end_tag(<<char, document::binary>>, cursor, buffer, tag_name, start_cursor, args) when char in @chars do
cursor = incr_column(cursor)
cursor = move_cursor(cursor, char)
parse_end_tag(document, cursor, [char | buffer], tag_name, start_cursor, args)
end

defp parse_end_tag(document, cursor, [], _tag_name, _start_cursor, _args) do
case document do
<<">", document::binary>> -> {:ok, {document, incr_column(cursor)}}
<<"/>", document::binary>> -> {:ok, {document, incr_column(cursor, 2)}}
<<">", document::binary>> -> {:ok, {document, move_cursor(cursor, ?>)}}
<<"/>", document::binary>> -> {:ok, {document, move_cursor(cursor, ~c"/>")}}
_document -> {:error, "invalid character for end tag", [start: cursor, end: cursor]}
end
end
Expand All @@ -322,16 +318,12 @@ defmodule LiveViewNative.Template.Parser do
defp drain_whitespace(document, cursor),
do: {document, cursor}

defp move_cursor(cursor, char) when char in [?\n] do
incr_line(cursor)
end
defp move_cursor(cursor, _char),
do: incr_column(cursor)
defp move_cursor(cursor, chars) when is_list(chars),
do: Enum.reduce(chars, cursor, &move_cursor(&2, &1))

defp incr_column([line: line, column: column], count \\ 1),
do: [line: line, column: column + count]
defp move_cursor(cursor, char) when char in [?\n],
do: [line: cursor[:line] + 1, column: 1]

defp incr_line([line: line, column: _column], count \\ 1) do
[line: line + count, column: 1]
end
defp move_cursor(cursor, _char),
do: [line: cursor[:line], column: cursor[:column] + 1]
end

0 comments on commit 4099bdf

Please sign in to comment.