Current section

Files

Jump to
cleam src internal@ast.erl
Raw

src/internal@ast.erl

-module(internal@ast).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([files_ast/1, files_paths_with_ast/2, imported_info/3]).
-export_type([file_ast/0, another_files_ast/0, module_name/0, imported_info/0, public_member/0]).
-type file_ast() :: {file_ast, glance:module_()}.
-type another_files_ast() :: {another_files_ast, list(file_ast())}.
-type module_name() :: {module_name, binary()}.
-type imported_info() :: {module_imported, module_name()} | imported_as_alias.
-type public_member() :: {public_fun, binary()} |
{public_const, binary()} |
{public_type, binary()}.
-spec files_ast(list(internal@fs:file_content())) -> list(file_ast()).
files_ast(Files_contents) ->
gleam@list:map(
Files_contents,
fun(Content) ->
{file_content, Content@1} = case Content of
{file_content, _} -> Content;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"internal/ast"/utf8>>,
function => <<"files_ast"/utf8>>,
line => 32})
end,
_assert_subject = glance:module(Content@1),
{ok, Ast} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"internal/ast"/utf8>>,
function => <<"files_ast"/utf8>>,
line => 33})
end,
{file_ast, Ast}
end
).
-spec files_paths_with_ast(
internal@fs:files_dir(),
gleam@option:option(internal@fs:files_dir())
) -> list({internal@fs:file_path(), file_ast(), another_files_ast()}).
files_paths_with_ast(Dir, Test_dir) ->
File_paths = internal@fs:files_paths(Dir),
Flle_contents = internal@fs:files_contents(File_paths),
Test_file_contents = case Test_dir of
{some, {files_dir, _} = Test_dir@1} ->
_pipe = internal@fs:files_paths(Test_dir@1),
internal@fs:files_contents(_pipe);
none ->
[]
end,
Ast_list = begin
_pipe@1 = Flle_contents,
_pipe@2 = gleam@list:append(_pipe@1, Test_file_contents),
files_ast(_pipe@2)
end,
Indexes = gleam@list:range(0, gleam@list:length(Ast_list) - 1),
gleam@list:filter_map(
Indexes,
fun(Index) -> case gleam@list:at(File_paths, Index) of
{ok, File_path} ->
_assert_subject = gleam@list:at(Ast_list, Index),
{ok, File_ast} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"internal/ast"/utf8>>,
function => <<"files_paths_with_ast"/utf8>>,
line => 55})
end,
{ok,
{File_path,
File_ast,
{another_files_ast,
gleam@list:filter_map(
Indexes,
fun(Idx) -> case Idx =:= Index of
true ->
{error, nil};
false ->
gleam@list:at(Ast_list, Idx)
end end
)}}};
{error, nil} ->
{error, nil}
end end
).
-spec module_full_name_to_module_name(internal@fs:module_full_name()) -> module_name().
module_full_name_to_module_name(Module_full_name) ->
{module_full_name, Module_full_name@1} = case Module_full_name of
{module_full_name, _} -> Module_full_name;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"internal/ast"/utf8>>,
function => <<"module_full_name_to_module_name"/utf8>>,
line => 106})
end,
_assert_subject = begin
_pipe = gleam@string:split(Module_full_name@1, <<"/"/utf8>>),
gleam@list:last(_pipe)
end,
{ok, Module_name} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"internal/ast"/utf8>>,
function => <<"module_full_name_to_module_name"/utf8>>,
line => 107})
end,
{module_name, Module_name}.
-spec imported_info(
list(glance:definition(glance:import())),
internal@fs:module_full_name(),
public_member()
) -> list(imported_info()).
imported_info(Imports, Module_full_name, Exported) ->
gleam@list:filter_map(Imports, fun(Imp) -> case Imp of
{definition,
_,
{import, Import_name, Module_alias, Type_aliases, Aliases}} when {module_full_name,
Import_name} =:= Module_full_name ->
case gleam@list:any(
Aliases,
fun(Alias) ->
{unqualified_import, Imported, _} = case Alias of
{unqualified_import, _, _} -> Alias;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"internal/ast"/utf8>>,
function => <<"imported_info"/utf8>>,
line => 81})
end,
(({public_fun, Imported} =:= Exported) orelse ({public_const,
Imported}
=:= Exported))
orelse ({public_type, Imported} =:= Exported)
end
)
orelse gleam@list:any(
Type_aliases,
fun(Type_alias) ->
{unqualified_import, Imported@1, _} = case Type_alias of
{unqualified_import, _, _} -> Type_alias;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"internal/ast"/utf8>>,
function => <<"imported_info"/utf8>>,
line => 87})
end,
{public_type, Imported@1} =:= Exported
end
) of
true ->
{ok, imported_as_alias};
false ->
{ok, {module_imported, case Module_alias of
{some, Alias@1} ->
{module_name, Alias@1};
none ->
module_full_name_to_module_name(
Module_full_name
)
end}}
end;
_ ->
{error, nil}
end end).