Skip to content

Commit

Permalink
Tiny refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
palm86 committed Nov 12, 2020
1 parent 573b660 commit 28a61cb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ erl_crash.dump
joy-*.tar

.elixir_ls/

src/joy_lexer.erl
src/joy_parser.erl
41 changes: 25 additions & 16 deletions lib/joy/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@ defmodule Joy.Parser do
@type func :: atom
@type quot :: [program]

@spec parse(binary) :: {:ok, program} | {:error, any}
def parse(str) when is_binary(str) do
with {:ok, tokens, _} <- str |> to_charlist() |> :joy_lexer.string(),
{:ok, joy} <- :joy_parser.parse(tokens) do
{:ok, joy}
else
{:error, {_, :joy_lexer, {_, _}}, _} ->
{:error, "invalid token"}
@spec parse(binary, keyword) :: {:ok, program} | {:error, any}
def parse(str, opts \\ []) when is_binary(str) do
do_parse(str, Keyword.put(opts, :bang, false))
end

{:error, _} ->
{:error, "parser error"}
end
@spec parse!(binary, keyword) :: program | no_return
def parse!(str, opts \\ []) when is_binary(str) do
do_parse(str, Keyword.put(opts, :bang, true))
end

@spec parse!(binary) :: program | no_return
def parse!(str) when is_binary(str) do
defp do_parse(str, opts) when is_binary(str) do
bang = Keyword.get(opts, :bang, false)

with {:ok, tokens, _} <- str |> to_charlist() |> :joy_lexer.string(),
{:ok, joy} <- :joy_parser.parse(tokens) do
joy
if bang do
joy
else
{:ok, joy}
end
else
{:error, {_, :joy_lexer, {_, _}}, _} ->
raise "invalid token"
if bang do
raise "invalid token"
else
{:error, "invalid token"}
end

{:error, _} ->
raise "parser error"
if bang do
raise "parser error"
else
{:error, "parser error"}
end
end
end
end
6 changes: 3 additions & 3 deletions src/joy_lexer.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-file("/usr/lib/erlang/lib/parsetools-2.1.8/include/leexinc.hrl", 0).
-file("/usr/local/Cellar/erlang/22.3.4/lib/erlang/lib/parsetools-2.1.8/include/leexinc.hrl", 0).
%% The source of this file is part of leex distribution, as such it
%% has the same Copyright as the other files in the leex
%% distribution. The Copyright is defined in the accompanying file
Expand All @@ -17,7 +17,7 @@
to_atom(Chars) ->
list_to_atom(Chars).

-file("/usr/lib/erlang/lib/parsetools-2.1.8/include/leexinc.hrl", 14).
-file("/usr/local/Cellar/erlang/22.3.4/lib/erlang/lib/parsetools-2.1.8/include/leexinc.hrl", 14).

format_error({illegal,S}) -> ["illegal characters ",io_lib:write_string(S)];
format_error({user,S}) -> S.
Expand Down Expand Up @@ -395,4 +395,4 @@ yyaction_2(TokenLine) ->
yyaction_3() ->
skip_token .

-file("/usr/lib/erlang/lib/parsetools-2.1.8/include/leexinc.hrl", 313).
-file("/usr/local/Cellar/erlang/22.3.4/lib/erlang/lib/parsetools-2.1.8/include/leexinc.hrl", 313).
2 changes: 1 addition & 1 deletion src/joy_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

extract_value({_Token, _Line, Value}) -> Value.

-file("/usr/lib/erlang/lib/parsetools-2.1.8/include/yeccpre.hrl", 0).
-file("/usr/local/Cellar/erlang/22.3.4/lib/erlang/lib/parsetools-2.1.8/include/yeccpre.hrl", 0).
%%
%% %CopyrightBegin%
%%
Expand Down
1 change: 0 additions & 1 deletion test/joy/formatter_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Joy.FormatterTest do
import Joy, only: [sigil_J: 2]
alias Joy.Formatter
alias Joy.Parser
use ExUnit.Case

test "format program" do
Expand Down

0 comments on commit 28a61cb

Please sign in to comment.