Current section

Files

Jump to
deriv src deriv@parser.erl
Raw

src/deriv@parser.erl

-module(deriv@parser).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([suppress_option_warnings/0, parse_import_with_derivations/2, parse_type_with_derivations/2]).
-export_type([deriv_field_opts_acc/0]).
-type deriv_field_opts_acc() :: {deriv_field_opts_acc,
{ok, binary()} | {error, nil},
{ok, binary()} | {error, nil},
{ok, binary()} | {error, nil},
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))}.
-file("src/deriv/parser.gleam", 11).
-spec suppress_option_warnings() -> list(gleam@option:option(nil)).
suppress_option_warnings() ->
[none, {some, nil}].
-file("src/deriv/parser.gleam", 94).
-spec parse_derivations(binary()) -> {ok, list(deriv@types:derivation())} |
{error, nil}.
parse_derivations(Raw) ->
_pipe = Raw,
_pipe@1 = gleam@string:trim(_pipe),
_pipe@2 = gleam@string:split(_pipe@1, <<" "/utf8>>),
(fun(Tokens) -> case Tokens of
[<<"derive"/utf8>>, Name | Opts] ->
{ok, [{derivation, Name, Opts}]};
_ ->
{error, nil}
end end)(_pipe@2).
-file("src/deriv/parser.gleam", 57).
-spec parse_derivations_from_inside_type_def_lines(list(binary())) -> list(deriv@types:derivation()).
parse_derivations_from_inside_type_def_lines(Lines) ->
_pipe = Lines,
_pipe@2 = gleam@list:map(
_pipe,
fun(Line) -> case gleam@string:split(Line, <<"//$"/utf8>>) of
[_, Mc] ->
_pipe@1 = parse_derivations(Mc),
gleam@result:unwrap(_pipe@1, []);
_ ->
[]
end end
),
_pipe@3 = gleam@list:flatten(_pipe@2),
lists:reverse(_pipe@3).
-file("src/deriv/parser.gleam", 73).
-spec parse_import_with_derivations(glance:import(), binary()) -> {ok,
{glance:import(), list(deriv@types:derivation())}} |
{error, nil}.
parse_import_with_derivations(Import_, Src) ->
Magic_comment = begin
_pipe = Src,
_pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>),
_pipe@2 = gleam@list:find(
_pipe@1,
fun(Line) ->
gleam_stdlib:string_starts_with(Line, <<"import "/utf8>>)
andalso gleam_stdlib:contains_string(
Line,
erlang:element(2, Import_)
)
end
),
gleam@result:map(
_pipe@2,
fun(_capture) -> gleam@string:split(_capture, <<"//$"/utf8>>) end
)
end,
case Magic_comment of
{ok, [_, Mc]} ->
_pipe@3 = parse_derivations(Mc),
gleam@result:map(_pipe@3, fun(Ds) -> {Import_, Ds} end);
_ ->
{error, nil}
end.
-file("src/deriv/parser.gleam", 196).
-spec parse_deriv_field_opts(binary()) -> list(deriv@types:deriv_field_opt()).
parse_deriv_field_opts(Str) ->
_assert_subject = gleam@regexp:from_string(<<"\\s*[/][/][$]\\s*"/utf8>>),
{ok, Magic_comment_re} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_deriv_field_opts"/utf8>>,
line => 197})
end,
_assert_subject@1 = gleam@regexp:from_string(<<"\\s+"/utf8>>),
{ok, Whitespace_re} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_deriv_field_opts"/utf8>>,
line => 198})
end,
case gleam@regexp:split(Magic_comment_re, Str) of
[_, Magic_comment] ->
case gleam@regexp:split(Whitespace_re, Magic_comment) of
[] ->
[];
Strs ->
[{deriv_field_opt, Strs}]
end;
_ ->
[]
end.
-file("src/deriv/parser.gleam", 118).
-spec parse_all_deriv_field_opts(list(binary())) -> gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt())).
parse_all_deriv_field_opts(Lines) ->
_assert_subject = begin
_pipe = <<"^\\s*(pub\\s+)?type\\s+([A-Z]\\w*)\\s*[{]"/utf8>>,
gleam@regexp:from_string(_pipe)
end,
{ok, Type_re} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_all_deriv_field_opts"/utf8>>,
line => 119})
end,
_assert_subject@1 = begin
_pipe@1 = <<"^\\s*([A-Z]\\w*)\\s*[(]"/utf8>>,
gleam@regexp:from_string(_pipe@1)
end,
{ok, Variant_re} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_all_deriv_field_opts"/utf8>>,
line => 123})
end,
_assert_subject@2 = begin
_pipe@2 = <<"^\\s*([a-z]\\w*)\\s*[:]"/utf8>>,
gleam@regexp:from_string(_pipe@2)
end,
{ok, Field_re} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@2,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_all_deriv_field_opts"/utf8>>,
line => 127})
end,
_pipe@3 = Lines,
_pipe@7 = gleam@list:fold(
_pipe@3,
{deriv_field_opts_acc,
{error, nil},
{error, nil},
{error, nil},
maps:new()},
fun(Acc, Line) ->
Type_@1 = case gleam@regexp:scan(Type_re, Line) of
[{match, _, [_, {some, Type_}]}] ->
{ok, Type_};
_ ->
{error, nil}
end,
Variant@1 = case gleam@regexp:scan(Variant_re, Line) of
[{match, _, [{some, Variant}]}] ->
{ok, Variant};
_ ->
{error, nil}
end,
Field@1 = case gleam@regexp:scan(Field_re, Line) of
[{match, _, [{some, Field}]}] ->
{ok, Field};
_ ->
{error, nil}
end,
Acc@1 = case Type_@1 of
{ok, _} ->
_record = Acc,
{deriv_field_opts_acc,
Type_@1,
erlang:element(3, _record),
erlang:element(4, _record),
erlang:element(5, _record)};
_ ->
Acc
end,
Acc@2 = case Variant@1 of
{ok, _} ->
_record@1 = Acc@1,
{deriv_field_opts_acc,
erlang:element(2, _record@1),
Variant@1,
erlang:element(4, _record@1),
erlang:element(5, _record@1)};
_ ->
Acc@1
end,
Acc@3 = case Field@1 of
{ok, _} ->
_record@2 = Acc@2,
{deriv_field_opts_acc,
erlang:element(2, _record@2),
erlang:element(3, _record@2),
Field@1,
erlang:element(5, _record@2)};
_ ->
Acc@2
end,
case {erlang:element(2, Acc@3),
erlang:element(3, Acc@3),
erlang:element(4, Acc@3)} of
{{ok, Type_@2}, {ok, Variant@2}, {ok, Field@2}} ->
case parse_deriv_field_opts(Line) of
[] ->
Acc@3;
New_opts ->
Key = {deriv_field, Type_@2, Variant@2, Field@2},
Opts@1 = begin
_pipe@4 = erlang:element(5, Acc@3),
gleam@dict:upsert(
_pipe@4,
Key,
fun(Opts) -> _pipe@5 = Opts,
_pipe@6 = gleam@option:unwrap(
_pipe@5,
[]
),
lists:append(_pipe@6, New_opts) end
)
end,
_record@3 = Acc@3,
{deriv_field_opts_acc,
erlang:element(2, _record@3),
erlang:element(3, _record@3),
erlang:element(4, _record@3),
Opts@1}
end;
{_, _, _} ->
Acc@3
end
end
),
(fun(Acc@4) -> erlang:element(5, Acc@4) end)(_pipe@7).
-file("src/deriv/parser.gleam", 13).
-spec parse_type_with_derivations(glance:custom_type(), binary()) -> {ok,
{glance:custom_type(),
list(deriv@types:derivation()),
gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt()))}} |
{error, nil}.
parse_type_with_derivations(Type_, Src) ->
_assert_subject = gleam@regexp:compile(
<<<<"^(pub )?type\\s+?"/utf8, (erlang:element(2, Type_))/binary>>/binary,
"\\s*?[{]"/utf8>>,
{options, false, true}
),
{ok, Type_line_re} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_type_with_derivations"/utf8>>,
line => 14})
end,
case gleam@regexp:check(Type_line_re, Src) of
false ->
{error, nil};
true ->
Lines_from_type_start_to_eof = begin
_pipe = Src,
_pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>),
gleam@list:drop_while(
_pipe@1,
fun(Line) -> not gleam@regexp:check(Type_line_re, Line) end
)
end,
Lines_from_type_start_except_last = begin
_pipe@2 = Lines_from_type_start_to_eof,
gleam@list:take_while(
_pipe@2,
fun(Line@1) ->
not gleam_stdlib:string_starts_with(
Line@1,
<<"}"/utf8>>
)
end
)
end,
_assert_subject@1 = begin
_pipe@3 = Lines_from_type_start_to_eof,
_pipe@4 = gleam@list:drop(
_pipe@3,
erlang:length(Lines_from_type_start_except_last)
),
_pipe@5 = gleam@list:take(_pipe@4, 1),
gleam@list:first(_pipe@5)
end,
{ok, Line_last_for_type} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"deriv/parser"/utf8>>,
function => <<"parse_type_with_derivations"/utf8>>,
line => 32})
end,
Lines = begin
_pipe@6 = [Lines_from_type_start_except_last,
[Line_last_for_type]],
gleam@list:flatten(_pipe@6)
end,
case parse_derivations_from_inside_type_def_lines(Lines) of
[] ->
{error, nil};
Derivs ->
Deriv_field_opts = parse_all_deriv_field_opts(Lines),
{ok, {Type_, Derivs, Deriv_field_opts}}
end
end.