Skip to content

Commit

Permalink
Loading files with syntax errors returns an error (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 authored Mar 14, 2024
1 parent 01e4010 commit 0803ba3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
12 changes: 8 additions & 4 deletions lib/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ defmodule Lua do
{:lua_error, _e, _state} = error ->
raise Lua.RuntimeException, error

{:error, reason, _stuff} ->
raise Lua.CompilerException, reason: reason
{:error, [error | _], _} ->
raise Lua.CompilerException, error
end
rescue
e in [UndefinedFunctionError] ->
Expand Down Expand Up @@ -204,15 +204,19 @@ defmodule Lua do
end

def load_lua_file!(%__MODULE__{state: state} = lua, path) when is_binary(path) do
case Luerl.dofile(state, String.to_charlist(path)) do
case Luerl.dofile(state, String.to_charlist(path), [:return]) do
{:ok, [], state} ->
%__MODULE__{lua | state: state}

{:lua_error, _error, _lua} = error ->
raise Lua.CompilerException, error

:error ->
{:error, [{:none, :file, :enoent} | _], _} ->
raise "Cannot load lua file, #{inspect(path <> ".lua")} does not exist"

{:error, [error | _], _} ->
# We just take the first error
raise Lua.CompilerException, error
end
end

Expand Down
28 changes: 10 additions & 18 deletions lib/lua/compiler_exception.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,23 @@ defmodule Lua.CompilerException do
stacktrace = Luerl.New.get_stacktrace(state)

message = """
Failed to compile Lua script: #{Util.format_error(error)}
Failed to compile Lua script!
#{Util.format_error(error)}
#{Util.format_stacktrace(stacktrace, state)}
"""

%__MODULE__{message: message}
end

def exception(data) when is_list(data) do
message =
data
|> Keyword.fetch!(:reason)
|> Enum.map_join("\n", fn {line, error, failure} ->
"Line #{line}: #{format_error(error)} due to #{format_error(failure)}"
end)

%__MODULE__{message: "Failed to compile Lua script\n\n#{message}\n"}
end

defp format_error(:luerl_parse), do: "failed to parse"
defp format_error(:luerl_scan), do: "failed to tokenize"
def exception({_line, _type, _failure} = error) do
message = """
Failed to compile Lua script!
defp format_error({:illegal, value}) when is_list(value),
do: "illegal token: #{to_string(value)}"
#{Util.format_error(error)}
"""

defp format_error({:illegal, value}), do: "illegal token: #{inspect(value)}}"
defp format_error(value), do: inspect(value)
%__MODULE__{message: message}
end
end
9 changes: 9 additions & 0 deletions lib/lua/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ defmodule Lua.Util do
{:illegal_index, _, name} ->
"invalid index #{inspect(name)}"

{line, type, {:illegal, value}} ->
type =
case type do
:luerl_parse -> "parse"
:luerl_scan -> "tokenize"
end

"Failed to #{type} illegal token on line #{line}: #{value}"

{:badarith, operator, values} ->
expression = values |> Enum.map(&to_string/1) |> Enum.join(" #{operator} ")

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Lua.MixProject do
use Mix.Project

@url "https://github.com/tv-labs/lua"
@version "0.0.5"
@version "0.0.6"

def project do
[
Expand Down
29 changes: 19 additions & 10 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,32 @@ defmodule LuaTest do
""")
end

@tag :skip
test "loading files with syntax errors returns an error" do
path = test_file("syntax_error")

assert_raise Lua.CompilerException, fn ->
error = """
Failed to compile Lua script!
Failed to tokenize illegal token on line 1: '
"""

assert_raise Lua.CompilerException, error, fn ->
Lua.load_lua_file!(Lua.new(), path)
end
end

test "loading files with undefined functions returns an error" do
path = test_file("undefined_function")

error = """
Failed to compile Lua script: undefined function
error =
"""
Failed to compile Lua script!
script line 1: <unknown function>()
"""
undefined function
script line 1: <unknown function>()
"""

assert_raise Lua.CompilerException, error, fn ->
Lua.load_lua_file!(Lua.new(), path)
Expand Down Expand Up @@ -139,9 +148,9 @@ defmodule LuaTest do
lua = Lua.new()

error = """
Failed to compile Lua script
Failed to compile Lua script!
Line 1: failed to tokenize due to illegal token: ")
Failed to tokenize illegal token on line 1: ")
"""

Expand All @@ -152,9 +161,9 @@ defmodule LuaTest do
end

error = """
Failed to compile Lua script
Failed to compile Lua script!
Line 1: failed to tokenize due to illegal token: "yuup)
Failed to tokenize illegal token on line 1: "yuup)
"""

Expand Down

0 comments on commit 0803ba3

Please sign in to comment.