Current section

Files

Jump to
gleamgen src gleamgen@module.erl
Raw

src/gleamgen@module.erl

-module(gleamgen@module).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([with_constant/3, with_import/2, with_function/3, with_custom_type2/3, with_custom_type_unchecked/3, eof/0, render_function/3, render/2, render_imported_module/1]).
-export_type([definition/0, decorator/0, definition_attributes/0, definable/0, module_/0]).
-type definition() :: {definition, definition_attributes(), definable()}.
-type decorator() :: external_erlang |
external_javascript |
{deprecated, binary()} |
internal.
-type definition_attributes() :: {definition_attributes,
binary(),
boolean(),
list(decorator())}.
-type definable() :: {function,
gleamgen@function:function_(gleamgen@types:unchecked(), gleamgen@types:unchecked())} |
{custom_type,
gleamgen@types@custom:custom_type(gleamgen@types:unchecked(), nil, nil)} |
{constant, gleamgen@expression:expression(gleamgen@types:unchecked())}.
-type module_() :: {module,
list(definition()),
list(gleamgen@import_:imported_module())}.
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 42).
-spec with_constant(
definition_attributes(),
gleamgen@expression:expression(KWP),
fun((gleamgen@expression:expression(KWP)) -> module_())
) -> module_().
with_constant(Attributes, Value, Handler) ->
Rest = Handler(
gleamgen@expression:unchecked_ident(erlang:element(2, Attributes))
),
erlang:setelement(
2,
Rest,
[{definition,
Attributes,
{constant,
begin
_pipe = Value,
gleamgen_ffi:identity(_pipe)
end}} |
erlang:element(2, Rest)]
).
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 60).
-spec with_import(
gleamgen@import_:imported_module(),
fun((gleamgen@import_:imported_module()) -> module_())
) -> module_().
with_import(Module, Handler) ->
Rest = Handler(Module),
erlang:setelement(3, Rest, [Module | erlang:element(3, Rest)]).
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 68).
-spec with_function(
definition_attributes(),
gleamgen@function:function_(KWS, any()),
fun((gleamgen@expression:expression(KWS)) -> module_())
) -> module_().
with_function(Attributes, Func, Handler) ->
Rest = Handler(
gleamgen@expression:unchecked_ident(erlang:element(2, Attributes))
),
erlang:setelement(
2,
Rest,
[{definition,
Attributes,
{function,
begin
_pipe = Func,
gleamgen_ffi:identity(_pipe)
end}} |
erlang:element(2, Rest)]
).
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 83).
-spec with_custom_type2(
definition_attributes(),
gleamgen@types@custom:custom_type(KWX, {{{}, KWY}, KWZ}, any()),
fun((gleamgen@types:generated_type(KWX), gleamgen@expression@constructor:construtor(KWX, KWY), gleamgen@expression@constructor:construtor(KWX, KWZ)) -> module_())
) -> module_().
with_custom_type2(Attributes, Type_, Handler) ->
_assert_subject = erlang:element(2, Type_),
[Variant2, Variant1] = case _assert_subject of
[_, _] -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleamgen/module"/utf8>>,
function => <<"with_custom_type2"/utf8>>,
line => 93})
end,
Rest = Handler(
gleamgen@types:unchecked_ident(erlang:element(2, Attributes)),
gleamgen@expression@constructor:new(Variant1),
gleamgen@expression@constructor:new(Variant2)
),
erlang:setelement(
2,
Rest,
[{definition,
Attributes,
{custom_type,
begin
_pipe = Type_,
gleamgen@types@custom:to_unchecked(_pipe)
end}} |
erlang:element(2, Rest)]
).
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 109).
-spec with_custom_type_unchecked(
definition_attributes(),
gleamgen@types@custom:custom_type(KXJ, gleamgen@types:unchecked(), any()),
fun((gleamgen@types:generated_type(KXJ), list(gleamgen@expression@constructor:construtor(KXJ, gleamgen@types:unchecked()))) -> module_())
) -> module_().
with_custom_type_unchecked(Attributes, Type_, Handler) ->
Rest = Handler(
gleamgen@types:unchecked_ident(erlang:element(2, Attributes)),
begin
_pipe = erlang:element(2, Type_),
_pipe@1 = lists:reverse(_pipe),
gleam@list:map(_pipe@1, fun gleamgen@expression@constructor:new/1)
end
),
erlang:setelement(
2,
Rest,
[{definition,
Attributes,
{custom_type,
begin
_pipe@2 = Type_,
gleamgen@types@custom:to_unchecked(_pipe@2)
end}} |
erlang:element(2, Rest)]
).
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 132).
-spec eof() -> module_().
eof() ->
{module, [], []}.
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 185).
-spec render_decorator(decorator()) -> glam@doc:document().
render_decorator(Decorator) ->
case Decorator of
external_erlang ->
glam@doc:from_string(<<"@external(erlang)"/utf8>>);
external_javascript ->
glam@doc:from_string(<<"@external(javascript)"/utf8>>);
{deprecated, Reason} ->
glam@doc:concat(
[glam@doc:from_string(<<"@deprecated(\""/utf8>>),
glam@doc:from_string(Reason),
glam@doc:from_string(<<"\")"/utf8>>)]
);
internal ->
glam@doc:from_string(<<"@internal"/utf8>>)
end.
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 199).
-spec render_function(
gleamgen@function:function_(gleamgen@types:unchecked(), gleamgen@types:unchecked()),
gleamgen@render:context(),
binary()
) -> gleamgen@render:rendered().
render_function(Func, Context, Name) ->
Rendered_args = begin
_pipe = erlang:element(2, Func),
_pipe@1 = gleam@list:map(
_pipe,
fun(_capture) ->
gleamgen@expression:render_attribute(_capture, Context)
end
),
gleamgen@render:pretty_list(_pipe@1)
end,
_pipe@2 = glam@doc:concat(
[glam@doc:from_string(<<"fn "/utf8>>),
glam@doc:from_string(Name),
Rendered_args,
{break, <<" "/utf8>>, <<""/utf8>>},
case gleamgen@types:render_type(erlang:element(3, Func)) of
{ok, Returned} ->
glam@doc:concat(
[glam@doc:from_string(<<"->"/utf8>>),
{break, <<" "/utf8>>, <<""/utf8>>},
erlang:element(2, Returned),
{break, <<" "/utf8>>, <<""/utf8>>}]
);
{error, _} ->
{concat, []}
end,
gleamgen@render:body(
erlang:element(
2,
gleamgen@expression:render(
erlang:element(4, Func),
erlang:setelement(3, Context, false)
)
),
true
)]
),
{render, _pipe@2}.
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 136).
-spec render(module_(), gleamgen@render:context()) -> gleamgen@render:rendered().
render(Module, Context) ->
Rendered_defs = gleam@list:map(
erlang:element(2, Module),
fun(Def) ->
_pipe = glam@doc:join(
gleam@list:map(
erlang:element(4, erlang:element(2, Def)),
fun render_decorator/1
),
{line, 1}
),
_pipe@1 = glam@doc:append(
_pipe,
case gleam@list:is_empty(
erlang:element(4, erlang:element(2, Def))
) of
true ->
{concat, []};
false ->
{line, 1}
end
),
_pipe@2 = glam@doc:append(
_pipe@1,
case erlang:element(3, erlang:element(2, Def)) of
true ->
glam@doc:concat(
[glam@doc:from_string(<<"pub"/utf8>>),
{break, <<" "/utf8>>, <<""/utf8>>}]
);
false ->
{concat, []}
end
),
glam@doc:append(_pipe@2, case erlang:element(3, Def) of
{constant, Value} ->
glam@doc:concat(
[glam@doc:from_string(<<"const "/utf8>>),
glam@doc:from_string(
erlang:element(2, erlang:element(2, Def))
),
{break, <<" "/utf8>>, <<""/utf8>>},
glam@doc:from_string(<<"="/utf8>>),
{break, <<" "/utf8>>, <<""/utf8>>},
erlang:element(
2,
gleamgen@expression:render(Value, Context)
)]
);
{custom_type, Type_} ->
glam@doc:concat(
[glam@doc:from_string(<<"type "/utf8>>),
glam@doc:from_string(
erlang:element(2, erlang:element(2, Def))
),
erlang:element(
2,
gleamgen@types@custom:render(Type_)
)]
);
{function, Func} ->
erlang:element(
2,
render_function(
Func,
Context,
erlang:element(2, erlang:element(2, Def))
)
)
end)
end
),
Rendered_imports = begin
_pipe@3 = erlang:element(3, Module),
_pipe@6 = gleam@list:map(_pipe@3, fun(X) -> _pipe@4 = X,
_pipe@5 = render_imported_module(_pipe@4),
gleamgen@render:to_string(_pipe@5) end),
_pipe@7 = gleam@list:sort(_pipe@6, fun gleam@string:compare/2),
_pipe@8 = gleam@list:map(_pipe@7, fun glam@doc:from_string/1),
_pipe@9 = glam@doc:join(_pipe@8, {line, 1}),
glam@doc:append(
_pipe@9,
case gleam@list:is_empty(erlang:element(3, Module)) of
true ->
{concat, []};
false ->
glam@doc:concat([{line, 1}, {line, 1}])
end
)
end,
_pipe@10 = Rendered_imports,
_pipe@11 = glam@doc:append(
_pipe@10,
glam@doc:concat_join(Rendered_defs, [{line, 1}, {line, 1}])
),
{render, _pipe@11}.
-file("/home/weasel/gleam/testing_gleamgen/gleamgen/src/gleamgen/module.gleam", 229).
-spec render_imported_module(gleamgen@import_:imported_module()) -> gleamgen@render:rendered().
render_imported_module(Module) ->
_pipe = glam@doc:concat(
[glam@doc:from_string(<<"import "/utf8>>),
glam@doc:from_string(
gleam@string:join(erlang:element(2, Module), <<"/"/utf8>>)
),
case erlang:element(3, Module) of
{some, Alias} ->
glam@doc:concat(
[{break, <<" "/utf8>>, <<""/utf8>>},
glam@doc:from_string(<<"as"/utf8>>),
{break, <<" "/utf8>>, <<""/utf8>>},
glam@doc:from_string(Alias)]
);
none ->
{concat, []}
end]
),
{render, _pipe}.