Current section

Files

Jump to
eyg_parser src eyg@parser.erl
Raw

src/eyg@parser.erl

-module(eyg@parser).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/eyg/parser.gleam").
-export([from_string/1, all_from_string/1, block_from_string/1, render_error/4, format_error/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/eyg/parser.gleam", 12).
-spec from_string(binary()) -> {ok,
{{eyg@ir@tree:expression({integer(), integer()}),
{integer(), integer()}},
list({eyg@parser@token:token(), integer()})}} |
{error, eyg@parser@parser:reason()}.
from_string(Src) ->
_pipe = Src,
_pipe@1 = eyg@parser@lexer:lex(_pipe),
_pipe@2 = eyg@parser@token:drop_whitespace(_pipe@1),
_pipe@3 = eyg@parser@token:drop_comments(_pipe@2),
eyg@parser@parser:expression(_pipe@3).
-file("src/eyg/parser.gleam", 20).
-spec all_from_string(binary()) -> {ok,
{eyg@ir@tree:expression({integer(), integer()}), {integer(), integer()}}} |
{error, eyg@parser@parser:reason()}.
all_from_string(Src) ->
gleam@result:'try'(
from_string(Src),
fun(_use0) ->
{Source, Remaining} = _use0,
case Remaining of
[] ->
{ok, Source};
[{Tok, At} | _] ->
{error, {trailing_tokens, Tok, At}}
end
end
).
-file("src/eyg/parser.gleam", 41).
-spec do_gather(
{eyg@ir@tree:expression(GIW), GIW},
list({binary(), {eyg@ir@tree:expression(GIW), GIW}, GIW})
) -> {list({binary(), {eyg@ir@tree:expression(GIW), GIW}, GIW}),
gleam@option:option({eyg@ir@tree:expression(GIW), GIW})}.
do_gather(Exp, Acc) ->
{Exp@1, Span} = Exp,
case Exp@1 of
{'let', Label, Value, Then} ->
do_gather(Then, [{Label, Value, Span} | Acc]);
vacant ->
{Acc, none};
_ ->
{Acc, {some, {Exp@1, Span}}}
end.
-file("src/eyg/parser.gleam", 28).
-spec block_from_string(binary()) -> {ok,
{{list({binary(),
{eyg@ir@tree:expression({integer(), integer()}),
{integer(), integer()}},
{integer(), integer()}}),
gleam@option:option({eyg@ir@tree:expression({integer(),
integer()}),
{integer(), integer()}})},
list({eyg@parser@token:token(), integer()})}} |
{error, eyg@parser@parser:reason()}.
block_from_string(Src) ->
Parsed = begin
_pipe = Src,
_pipe@1 = eyg@parser@lexer:lex(_pipe),
_pipe@2 = eyg@parser@token:drop_whitespace(_pipe@1),
_pipe@3 = eyg@parser@token:drop_comments(_pipe@2),
eyg@parser@parser:block(_pipe@3)
end,
case Parsed of
{ok, {Exp, Left}} ->
{ok, {do_gather(Exp, []), Left}};
{error, Reason} ->
{error, Reason}
end.
-file("src/eyg/parser.gleam", 63).
-spec render_error(binary(), binary(), binary(), {integer(), integer()}) -> binary().
render_error(Description, Hint, Code, Span) ->
Lines = [<<"error: "/utf8, Description/binary>>,
<<"hint: "/utf8, Hint/binary>>],
Context = case Span of
{0, 0} ->
[];
_ ->
[<<""/utf8>> | eyg@parser@location:source_context(Code, Span)]
end,
gleam@string:join(lists:append(Lines, Context), <<"\n"/utf8>>).
-file("src/eyg/parser.gleam", 72).
-spec reason_position(eyg@parser@parser:reason()) -> gleam@option:option(integer()).
reason_position(Reason) ->
case Reason of
{unexpected_token, _, Pos} ->
{some, Pos};
unexpect_end ->
none;
{missing_equals, Pos@1} ->
{some, Pos@1};
{missing_arrow, Pos@2} ->
{some, Pos@2};
{unclosed_function_body, Open_at} ->
{some, Open_at};
{expected_effect_name, _, Pos@3} ->
{some, Pos@3};
{expected_builtin_name, Pos@4} ->
{some, Pos@4};
{invalid_cid_reference, Pos@5} ->
{some, Pos@5};
{invalid_release_version, Pos@6} ->
{some, Pos@6};
{invalid_import_path, Pos@7} ->
{some, Pos@7};
{trailing_tokens, _, Pos@8} ->
{some, Pos@8};
{invalid_character, _, Pos@9} ->
{some, Pos@9};
{unterminated_string_literal, Pos@10} ->
{some, Pos@10};
{invalid_escape_sequence, _, Pos@11} ->
{some, Pos@11};
{integer_literal_out_of_range, _, Pos@12} ->
{some, Pos@12}
end.
-file("src/eyg/parser.gleam", 53).
?DOC(
" Format a parse error as a human-readable string, showing the relevant\n"
" source line with a pointer to the error location.\n"
).
-spec format_error(eyg@parser@parser:reason(), binary()) -> binary().
format_error(Reason, Source) ->
Description = eyg@parser@debug:describe(Reason),
Hint = eyg@parser@debug:hint(Reason),
Span = case reason_position(Reason) of
{some, Start} ->
{Start, Start};
none ->
{0, 0}
end,
render_error(Description, Hint, Source, Span).