Skip to content

Commit

Permalink
Standard error format for parser error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
msaraiva committed Dec 7, 2019
1 parent 75d7c0e commit 33d392a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/surface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Surface do
defmacro sigil_H({:<<>>, _, [string]}, _) do
line_offset = __CALLER__.line + 1
string
|> Translator.run(line_offset, __CALLER__)
|> Translator.run(line_offset, __CALLER__, __CALLER__.file)
|> EEx.compile_string(engine: Phoenix.LiveView.Engine, line: line_offset)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/surface/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule Surface.Translator do

@component_directives [":for", ":if", ":bindings", ":debug"]

def run(string, line_offset, caller) do
def run(string, line_offset, caller, file \\ "nofile") do
string
|> Parser.parse(line_offset)
|> Parser.parse(line_offset, file)
|> build_metadata(caller)
|> prepend_context()
|> translate(caller)
Expand Down
6 changes: 3 additions & 3 deletions lib/surface/translator/component_translator_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ defmodule Surface.Translator.ComponentTranslatorHelper do
end
end

# TODO: Is there a better way to check if the code is blank?
@blanks ' \n\r\t\v\b\f\e\d\a'

defp blank?([]), do: true

defp blank?([h|t]), do: blank?(h) && blank?(t)

defp blank?(""), do: true

defp blank?(char) when char in [32, 10, 13, 9, 11, 8, 12, 27, 127, 7], do: true
defp blank?(char) when char in @blanks, do: true

defp blank?(<<h, t::binary>>) when h in ' \n\r\t\v\b\f\e\d\a', do: blank?(t)
defp blank?(<<h, t::binary>>) when h in @blanks, do: blank?(t)

defp blank?(_), do: false
end
23 changes: 8 additions & 15 deletions lib/surface/translator/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ defmodule Surface.Translator.Parser do
import NimbleParsec

defmodule ParseError do
defexception string: "", line: 0, col: 0, message: "error parsing HTML"
defexception file: "", line: 0, message: "error parsing HTML/Surface"

def message(e) do
"""
Failed to parse HTML: #{e.message}
Check your syntax near line #{e.line}:
#{e.string}
"""
@impl true
def message(exception) do
"#{exception.file}:#{exception.line}: #{exception.message}"
end
end

Expand Down Expand Up @@ -217,16 +211,15 @@ defmodule Surface.Translator.Parser do
end
end

def parse(string, line_offset) do
def parse(string, line_offset, file \\ "nofile") do
case parse_root(string, context: [macro: nil]) do
{:ok, nodes, _, _, _, _} ->
nodes

{:error, reason, rest, _, {line, col}, _} ->
{:error, reason, _rest, _, {line, _col}, _} ->
raise %ParseError{
string: String.split(rest, "\n") |> Enum.take(2) |> Enum.join("\n"),
line: line + line_offset,
col: col,
line: line + line_offset - 1,
file: file,
message: reason
}
end
Expand Down

0 comments on commit 33d392a

Please sign in to comment.