Current section
Files
Jump to
Current section
Files
src/glerd.erl
-module(glerd).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glerd.gleam").
-export([generate/1, main/0]).
-export_type([file_path/0, file_content/0, module_name/0, record_name/0, meta/0, meta_dict/0]).
-type file_path() :: {file_path, binary()}.
-type file_content() :: {file_content, binary()}.
-type module_name() :: {module_name, binary()}.
-type record_name() :: {record_name, binary()}.
-type meta() :: {meta, binary()}.
-type meta_dict() :: {meta_dict, gleam@dict:dict(record_name(), meta())}.
-file("src/glerd.gleam", 223).
-spec stringify_queue(gleam@deque:deque(binary()), binary()) -> binary().
stringify_queue(Que, Str) ->
case begin
_pipe = Que,
gleam@deque:pop_front(_pipe)
end of
{ok, {El, Que@1}} ->
stringify_queue(
Que@1,
<<<<Str/binary, " "/utf8>>/binary, El/binary>>
);
{error, nil} ->
Str
end.
-file("src/glerd.gleam", 216).
-spec type_args(list(glance:type())) -> binary().
type_args(Types) ->
_pipe = Types,
_pipe@1 = gleam@list:map(_pipe, fun field_type/1),
_pipe@2 = gleam@list:intersperse(_pipe@1, <<","/utf8>>),
gleam@string:join(_pipe@2, <<""/utf8>>).
-file("src/glerd.gleam", 164).
-spec field_type(glance:type()) -> binary().
field_type(Typ) ->
case Typ of
{named_type, _, Type_name, _, _} when Type_name =:= <<"String"/utf8>> ->
<<"types.IsString"/utf8>>;
{named_type, _, Type_name@1, _, _} when Type_name@1 =:= <<"Int"/utf8>> ->
<<"types.IsInt"/utf8>>;
{named_type, _, Type_name@2, _, _} when Type_name@2 =:= <<"Float"/utf8>> ->
<<"types.IsFloat"/utf8>>;
{named_type, _, Type_name@3, _, _} when Type_name@3 =:= <<"Bool"/utf8>> ->
<<"types.IsBool"/utf8>>;
{named_type, _, Type_name@4, _, _} when Type_name@4 =:= <<"Nil"/utf8>> ->
<<"types.IsNil"/utf8>>;
{named_type, _, Type_name@5, _, [Typ@1]} when Type_name@5 =:= <<"List"/utf8>> ->
<<<<"types.IsList("/utf8, (field_type(Typ@1))/binary>>/binary,
")"/utf8>>;
{named_type, _, Type_name@6, _, [Key_type, Val_type]} when Type_name@6 =:= <<"Dict"/utf8>> ->
<<<<"types.IsDict("/utf8, (type_args([Key_type, Val_type]))/binary>>/binary,
")"/utf8>>;
{named_type, _, Type_name@7, _, [Typ@2]} when Type_name@7 =:= <<"Option"/utf8>> ->
<<<<"types.IsOption("/utf8, (field_type(Typ@2))/binary>>/binary,
")"/utf8>>;
{named_type, _, Type_name@8, _, [Typ1, Typ2]} when Type_name@8 =:= <<"Result"/utf8>> ->
<<<<"types.IsResult("/utf8, (type_args([Typ1, Typ2]))/binary>>/binary,
")"/utf8>>;
{tuple_type, _, [Typ1@1, Typ2@1]} ->
<<<<"types.IsTuple2("/utf8, (type_args([Typ1@1, Typ2@1]))/binary>>/binary,
")"/utf8>>;
{tuple_type, _, [Typ1@2, Typ2@2, Typ3]} ->
<<<<"types.IsTuple3("/utf8,
(type_args([Typ1@2, Typ2@2, Typ3]))/binary>>/binary,
")"/utf8>>;
{tuple_type, _, [Typ1@3, Typ2@3, Typ3@1, Typ4]} ->
<<<<"types.IsTuple4("/utf8,
(type_args([Typ1@3, Typ2@3, Typ3@1, Typ4]))/binary>>/binary,
")"/utf8>>;
{tuple_type, _, [Typ1@4, Typ2@4, Typ3@2, Typ4@1, Typ5]} ->
<<<<"types.IsTuple5("/utf8,
(type_args([Typ1@4, Typ2@4, Typ3@2, Typ4@1, Typ5]))/binary>>/binary,
")"/utf8>>;
{tuple_type, _, [Typ1@5, Typ2@5, Typ3@3, Typ4@2, Typ5@1, Typ6]} ->
<<<<"types.IsTuple6("/utf8,
(type_args([Typ1@5, Typ2@5, Typ3@3, Typ4@2, Typ5@1, Typ6]))/binary>>/binary,
")"/utf8>>;
{named_type, _, Record_name, _, _} ->
<<<<"types.IsRecord(\""/utf8, Record_name/binary>>/binary,
"\")"/utf8>>;
{function_type, _, [], Return} ->
<<<<"types.IsFunction0("/utf8, (type_args([Return]))/binary>>/binary,
")"/utf8>>;
{function_type, _, [Typ1@6], Return@1} ->
<<<<"types.IsFunction1("/utf8,
(type_args([Typ1@6, Return@1]))/binary>>/binary,
")"/utf8>>;
{function_type, _, [Typ1@7, Typ2@6], Return@2} ->
<<<<"types.IsFunction2("/utf8,
(type_args([Typ1@7, Typ2@6, Return@2]))/binary>>/binary,
")"/utf8>>;
{function_type, _, [Typ1@8, Typ2@7, Typ3@4], Return@3} ->
<<<<"types.IsFunction3("/utf8,
(type_args([Typ1@8, Typ2@7, Typ3@4, Return@3]))/binary>>/binary,
")"/utf8>>;
{function_type, _, [Typ1@9, Typ2@8, Typ3@5, Typ4@3], Return@4} ->
<<<<"types.IsFunction4("/utf8,
(type_args([Typ1@9, Typ2@8, Typ3@5, Typ4@3, Return@4]))/binary>>/binary,
")"/utf8>>;
{function_type, _, [Typ1@10, Typ2@9, Typ3@6, Typ4@4, Typ5@2], Return@5} ->
<<<<"types.IsFunction5("/utf8,
(type_args(
[Typ1@10, Typ2@9, Typ3@6, Typ4@4, Typ5@2, Return@5]
))/binary>>/binary,
")"/utf8>>;
{function_type,
_,
[Typ1@11, Typ2@10, Typ3@7, Typ4@5, Typ5@3, Typ6@1],
Return@6} ->
<<<<"types.IsFunction6("/utf8,
(type_args(
[Typ1@11,
Typ2@10,
Typ3@7,
Typ4@5,
Typ5@3,
Typ6@1,
Return@6]
))/binary>>/binary,
")"/utf8>>;
_ ->
<<"types.Unknown"/utf8>>
end.
-file("src/glerd.gleam", 48).
-spec generate(binary()) -> nil.
generate(Root) ->
Records_info = begin
_pipe = fswalk:builder(),
_pipe@1 = fswalk:with_path(_pipe, Root),
_pipe@2 = fswalk:walk(_pipe@1),
_pipe@3 = gleam@yielder:filter(
_pipe@2,
fun(Entry_result) -> case Entry_result of
{ok, {entry, Path, {stat, Is_dir}}} ->
((Is_dir =:= false) andalso gleam_stdlib:string_ends_with(
Path,
<<".gleam"/utf8>>
))
andalso not gleam_stdlib:contains_string(
Path,
<<"glerd_gen.gleam"/utf8>>
);
_ ->
false
end end
),
_pipe@4 = gleam@yielder:map(
_pipe@3,
fun(Entry_result@1) ->
Path@2 = case Entry_result@1 of
{ok, {entry, Path@1, _}} -> Path@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glerd"/utf8>>,
function => <<"generate"/utf8>>,
line => 63,
value => _assert_fail,
start => 1230,
'end' => 1274,
pattern_start => 1241,
pattern_end => 1259})
end,
{file_path, Path@2}
end
),
_pipe@7 = gleam@yielder:map(
_pipe@4,
fun(File_path) ->
Content@1 = case begin
_pipe@5 = erlang:element(2, File_path),
simplifile:read(_pipe@5)
end of
{ok, Content} -> Content;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glerd"/utf8>>,
function => <<"generate"/utf8>>,
line => 67,
value => _assert_fail@1,
start => 1344,
'end' => 1401,
pattern_start => 1355,
pattern_end => 1366})
end,
{File_path,
begin
_pipe@6 = Content@1,
{file_content, _pipe@6}
end}
end
),
_pipe@11 = gleam@yielder:map(
_pipe@7,
fun(Ctx) ->
gluple@addition:with_append2(
Ctx,
fun(File_path@1, _) ->
_pipe@8 = erlang:element(2, File_path@1),
_pipe@9 = gleam@string:replace(
_pipe@8,
<<Root/binary, "/"/utf8>>,
<<""/utf8>>
),
_pipe@10 = gleam@string:replace(
_pipe@9,
<<".gleam"/utf8>>,
<<""/utf8>>
),
{module_name, _pipe@10}
end
)
end
),
_pipe@13 = gleam@yielder:map(_pipe@11, fun(Ctx@1) -> _pipe@12 = Ctx@1,
gluple@removal:remove_first3(_pipe@12) end),
_pipe@25 = gleam@yielder:map(
_pipe@13,
fun(Ctx@2) ->
gluple@addition:with_append2(
Ctx@2,
fun(File_content, _) ->
_pipe@14 = erlang:element(2, File_content),
_pipe@15 = glexer:new(_pipe@14),
_pipe@16 = glexer:lex(_pipe@15),
_pipe@23 = gleam@list:fold(
_pipe@16,
{gleam@deque:new(), maps:new()},
fun(Acc, Lexem) ->
{Meta_batch, Meta_final} = Acc,
Meta_batch_is_empty = gleam@deque:is_empty(
Meta_batch
),
case Lexem of
{{comment_doc, Meta}, {position, _}} ->
{begin
_pipe@17 = Meta_batch,
gleam@deque:push_back(
_pipe@17,
begin
_pipe@18 = Meta,
gleam@string:trim(
_pipe@18
)
end
)
end,
Meta_final};
{{upper_name, Rec_name}, {position, _}} when not Meta_batch_is_empty ->
{gleam@deque:new(),
gleam@dict:insert(
Meta_final,
begin
_pipe@19 = Rec_name,
{record_name, _pipe@19}
end,
begin
_pipe@20 = Meta_batch,
_pipe@21 = stringify_queue(
_pipe@20,
<<""/utf8>>
),
_pipe@22 = gleam@string:trim(
_pipe@21
),
{meta, _pipe@22}
end
)};
_ ->
{Meta_batch, Meta_final}
end
end
),
_pipe@24 = gleam@pair:second(_pipe@23),
{meta_dict, _pipe@24}
end
)
end
),
_pipe@27 = gleam@yielder:map(
_pipe@25,
fun(Ctx@3) ->
gluple@addition:with_append3(
Ctx@3,
fun(File_content@1, _, _) ->
Module@1 = case begin
_pipe@26 = erlang:element(2, File_content@1),
glance:module(_pipe@26)
end of
{ok, Module} -> Module;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glerd"/utf8>>,
function => <<"generate"/utf8>>,
line => 108,
value => _assert_fail@2,
start => 2703,
'end' => 2760,
pattern_start => 2714,
pattern_end => 2724})
end,
{module, _, Custom_types_definitions, _, _, _} = Module@1,
gleam@list:flat_map(
Custom_types_definitions,
fun(Custom_type_definition) ->
{definition, _, Custom_type} = Custom_type_definition,
{custom_type, _, _, _, _, _, Variants} = Custom_type,
Variants
end
)
end
)
end
),
_pipe@29 = gleam@yielder:map(_pipe@27, fun(Ctx@4) -> _pipe@28 = Ctx@4,
gluple@removal:remove_first4(_pipe@28) end),
_pipe@34 = gleam@yielder:map(
_pipe@29,
fun(Ctx@5) ->
{Module_name, Meta_dict, Variants@1} = Ctx@5,
gleam@list:map(
Variants@1,
fun(_use0) ->
{variant, Record_name, Fields, _} = _use0,
Fields@1 = begin
_pipe@30 = Fields,
_pipe@31 = gleam@list:map(
_pipe@30,
fun(Field) ->
{Field_name@1, Typ@2} = case Field of
{labelled_variant_field,
Typ,
Field_name} ->
{Field_name, Typ};
{unlabelled_variant_field, Typ@1} ->
{<<"__none__"/utf8>>, Typ@1}
end,
<<<<<<<<"#(\""/utf8, Field_name@1/binary>>/binary,
"\", "/utf8>>/binary,
(field_type(Typ@2))/binary>>/binary,
")"/utf8>>
end
),
gleam@string:join(_pipe@31, <<","/utf8>>)
end,
Meta@2 = case begin
_pipe@33 = gleam_stdlib:map_get(
erlang:element(2, Meta_dict),
begin
_pipe@32 = Record_name,
{record_name, _pipe@32}
end
),
gleam@result:'or'(
_pipe@33,
{ok, {meta, <<""/utf8>>}}
)
end of
{ok, {meta, Meta@1}} -> Meta@1;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glerd"/utf8>>,
function => <<"generate"/utf8>>,
line => 129,
value => _assert_fail@3,
start => 3618,
'end' => 3739,
pattern_start => 3629,
pattern_end => 3643})
end,
<<<<<<<<<<<<<<<<<<"#(\""/utf8, Record_name/binary>>/binary,
"\",\""/utf8>>/binary,
(erlang:element(
2,
Module_name
))/binary>>/binary,
"\","/utf8>>/binary,
"["/utf8>>/binary,
Fields@1/binary>>/binary,
"],\""/utf8>>/binary,
Meta@2/binary>>/binary,
"\")"/utf8>>
end
)
end
),
_pipe@36 = gleam@yielder:flat_map(
_pipe@34,
fun(Record_code) -> _pipe@35 = Record_code,
gleam@yielder:from_list(_pipe@35) end
),
gleam@yielder:fold(
_pipe@36,
<<""/utf8>>,
fun(Acc@1, Record_code@1) ->
<<<<Acc@1/binary, Record_code@1/binary>>/binary, ",\n"/utf8>>
end
)
end,
Gen_file_path = <<<<"./"/utf8, Root/binary>>/binary,
"/glerd_gen.gleam"/utf8>>,
case simplifile:write(
Gen_file_path,
<<<<"// this file was generated via \"gleam run -m glerd\"
import glerd/types
pub const record_info = ["/utf8,
Records_info/binary>>/binary,
"]"/utf8>>
) of
{ok, _} -> nil;
_assert_fail@4 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glerd"/utf8>>,
function => <<"generate"/utf8>>,
line => 148,
value => _assert_fail@4,
start => 4129,
'end' => 4341,
pattern_start => 4140,
pattern_end => 4145})
end,
case gleamyshell_ffi:execute(
<<"gleam"/utf8>>,
<<"."/utf8>>,
[<<"format"/utf8>>, Gen_file_path]
) of
{ok, _} -> nil;
_assert_fail@5 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"glerd"/utf8>>,
function => <<"generate"/utf8>>,
line => 158,
value => _assert_fail@5,
start => 4345,
'end' => 4428,
pattern_start => 4356,
pattern_end => 4361})
end,
nil.
-file("src/glerd.gleam", 44).
-spec main() -> nil.
main() ->
generate(<<"src"/utf8>>).