Current section
Files
Jump to
Current section
Files
src/derived.erl
-module(derived).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/derived.gleam").
-export([parse/1, generate/2]).
-file("src/derived.gleam", 7).
-spec parse(binary()) -> list(derived@ast:derived_type()).
parse(Input) ->
derived@ast:parse(Input).
-file("src/derived.gleam", 78).
-spec insert_after_type(binary(), derived@ast:derived_type(), binary()) -> binary().
insert_after_type(Source, Derived_type, Content) ->
{_, End} = erlang:element(2, Derived_type),
Before = gleam@string:slice(Source, 0, End + 1),
After = gleam@string:slice(
Source,
End + 1,
(string:length(Source) - End) - 1
),
<<<<<<Before/binary, "\n\n"/utf8>>/binary, Content/binary>>/binary,
After/binary>>.
-file("src/derived.gleam", 90).
-spec start_marker(binary()) -> binary().
start_marker(Derived_name) ->
<<<<"// ---- BEGIN DERIVED "/utf8, Derived_name/binary>>/binary,
" DO NOT MODIFY ---- //"/utf8>>.
-file("src/derived.gleam", 94).
-spec end_marker(binary()) -> binary().
end_marker(Derived_name) ->
<<<<"// ---- END DERIVED "/utf8, Derived_name/binary>>/binary, " //"/utf8>>.
-file("src/derived.gleam", 54).
-spec find_and_replace_markers(binary(), binary(), binary()) -> {ok, binary()} |
{error, nil}.
find_and_replace_markers(Source, Derived_name, New_content) ->
Pattern = <<<<(start_marker(Derived_name))/binary, "[\\s\\S]*?"/utf8>>/binary,
(end_marker(Derived_name))/binary>>,
case gleam@regexp:compile(Pattern, {options, false, true}) of
{ok, Regex} ->
case gleam_regexp_ffi:replace(Regex, Source, New_content) of
Result when Result =/= Source ->
{ok, Result};
_ ->
{error, nil}
end;
{error, _} ->
{error, nil}
end.
-file("src/derived.gleam", 35).
-spec create_derived_content(
binary(),
binary(),
binary(),
derived@ast:derived_type()
) -> binary().
create_derived_content(Generated_code, Derived_name, Source, Derived_type) ->
New_content = <<<<<<<<(start_marker(Derived_name))/binary, "\n"/utf8>>/binary,
Generated_code/binary>>/binary,
"\n"/utf8>>/binary,
(end_marker(Derived_name))/binary>>,
_pipe = find_and_replace_markers(Source, Derived_name, New_content),
gleam@result:lazy_unwrap(
_pipe,
fun() -> insert_after_type(Source, Derived_type, New_content) end
).
-file("src/derived.gleam", 11).
-spec generate(
binary(),
fun((derived@ast:derived_type()) -> {ok, binary()} | {error, nil})
) -> binary().
generate(Input, Callback) ->
Derived_types = begin
_pipe = Input,
_pipe@1 = parse(_pipe),
lists:reverse(_pipe@1)
end,
gleam@list:fold(
Derived_types,
Input,
fun(Source_after_types, Derived_type) ->
gleam@list:fold(
begin
_pipe@2 = erlang:element(8, Derived_type),
lists:reverse(_pipe@2)
end,
Source_after_types,
fun(Source_after_derivations, Derived_name) ->
_pipe@3 = Derived_type,
_pipe@4 = Callback(_pipe@3),
_pipe@5 = gleam@result:map(
_pipe@4,
fun(_capture) ->
create_derived_content(
_capture,
Derived_name,
Source_after_derivations,
Derived_type
)
end
),
gleam@result:unwrap(_pipe@5, Source_after_derivations)
end
)
end
).