Current section

Files

Jump to
parser_gleam src examples@custom_type.erl
Raw

src/examples@custom_type.erl

-module(examples@custom_type).
-compile(no_auto_import).
-export([record_constructor_argument_parser/0, record_constructor_parser/0, ast_parser/0, filter_custom_types/1]).
-export_type([type_ast/0, record_constructor_arg/0, record_constructor/0, x_custom_type/0, ignored_code/0, ast_node/0]).
-type type_ast() :: {constructor,
gleam@option:option(binary()),
binary(),
list(type_ast())} |
{fn, list(type_ast()), type_ast()} |
{var, binary()} |
{tuple, list(type_ast())} |
{hole, binary()}.
-type record_constructor_arg() :: {record_constructor_arg, binary(), type_ast()}.
-type record_constructor() :: {record_constructor,
binary(),
list(record_constructor_arg())}.
-type x_custom_type() :: {x_custom_type,
binary(),
list(record_constructor()),
list(binary())}.
-type ignored_code() :: {ignored_code, binary()}.
-type ast_node() :: {node_ignored_code, ignored_code()} |
{node_x_custom_type, x_custom_type()}.
-spec string_eof() -> fun((parser_gleam@stream:stream(LSH)) -> {ok,
parser_gleam@parse_result:parse_success(LSH, binary())} |
{error, parser_gleam@parse_result:parse_error(LSH)}).
string_eof() ->
_pipe = parser_gleam@parser:eof(),
parser_gleam@parser:map(_pipe, fun(_) -> <<""/utf8>> end).
-spec type_opener() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), binary())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_opener() ->
parser_gleam@string:string(<<"pub type "/utf8>>).
-spec ignored_code_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), ignored_code())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
ignored_code_parser() ->
_pipe@3 = parser_gleam@parser:many_till(
parser_gleam@parser:item(),
begin
_pipe@1 = parser_gleam@parser:look_ahead(
begin
_pipe = type_opener(),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
parser_gleam@parser:many1_till(
parser_gleam@parser:item(),
parser_gleam@char:char(<<"{"/utf8>>)
)
end
)
end
),
_pipe@2 = parser_gleam@parser:map(
_pipe@1,
fun(_) -> <<""/utf8>> end
),
parser_gleam@parser:alt(_pipe@2, fun() -> string_eof() end)
end
),
_pipe@5 = parser_gleam@parser:map(
_pipe@3,
fun(Chars) ->
_pipe@4 = Chars,
gleam@string:join(_pipe@4, <<""/utf8>>)
end
),
parser_gleam@parser:map(_pipe@5, fun(A) -> {ignored_code, A} end).
-spec type_ast_end() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), binary())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_end() ->
_pipe = parser_gleam@string:string(<<","/utf8>>),
_pipe@1 = parser_gleam@parser:alt(
_pipe,
fun() -> parser_gleam@char:char(<<")"/utf8>>) end
),
parser_gleam@parser:alt(_pipe@1, fun() -> string_eof() end).
-spec type_ast_end_look_ahead() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), binary())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_end_look_ahead() ->
parser_gleam@parser:look_ahead(type_ast_end()).
-spec type_ast_constructor_arguments_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), list(type_ast()))} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_constructor_arguments_parser() ->
_pipe = parser_gleam@char:char(<<"("/utf8>>),
_pipe@1 = parser_gleam@parser:chain(
_pipe,
fun(_) ->
parser_gleam@parser:many1_till(
type_ast_parser(),
type_ast_end_look_ahead()
)
end
),
parser_gleam@parser:chain(
_pipe@1,
fun(It) ->
_pipe@2 = parser_gleam@char:char(<<")"/utf8>>),
parser_gleam@parser:map(
_pipe@2,
fun(_) ->
_pipe@3 = It,
fp_gl@non_empty_list:to_list(_pipe@3)
end
)
end
).
-spec type_ast_constructor_no_module_parser(gleam@option:option(binary())) -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_constructor_no_module_parser(Module) ->
_pipe = parser_gleam@char:upper(),
parser_gleam@parser:chain(
_pipe,
fun(Head) ->
_pipe@4 = parser_gleam@parser:many_till(
parser_gleam@parser:item(),
begin
_pipe@1 = parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<","/utf8>>)
),
_pipe@2 = parser_gleam@parser:alt(
_pipe@1,
fun() ->
parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<")"/utf8>>)
)
end
),
_pipe@3 = parser_gleam@parser:alt(
_pipe@2,
fun() ->
parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<"("/utf8>>)
)
end
),
parser_gleam@parser:alt(_pipe@3, fun string_eof/0)
end
),
parser_gleam@parser:chain(
_pipe@4,
fun(Tail) ->
Name = begin
_pipe@5 = [Head | Tail],
gleam@string:join(_pipe@5, <<""/utf8>>)
end,
_pipe@6 = type_ast_constructor_arguments_parser(),
_pipe@8 = parser_gleam@parser:alt(
_pipe@6,
fun() ->
_pipe@7 = type_ast_end_look_ahead(),
parser_gleam@parser:map(_pipe@7, fun(_) -> [] end)
end
),
parser_gleam@parser:map(
_pipe@8,
fun(Arguments) ->
{constructor, Module, Name, Arguments}
end
)
end
)
end
).
-spec type_ast_constructor_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_constructor_parser() ->
_pipe = type_ast_constructor_no_module_parser(none),
parser_gleam@parser:alt(
_pipe,
fun() ->
_pipe@1 = parser_gleam@parser:many1_till(
parser_gleam@char:lower(),
parser_gleam@char:char(<<"."/utf8>>)
),
parser_gleam@parser:chain(
_pipe@1,
fun(Module) ->
type_ast_constructor_no_module_parser(
begin
_pipe@2 = Module,
_pipe@3 = fp_gl@non_empty_list:to_list(_pipe@2),
_pipe@4 = gleam@string:join(_pipe@3, <<""/utf8>>),
{some, _pipe@4}
end
)
end
)
end
).
-spec type_ast_fn_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_fn_parser() ->
_pipe = parser_gleam@string:string(<<"fn"/utf8>>),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
_pipe@1 = parser_gleam@string:spaces(),
parser_gleam@parser:chain(
_pipe@1,
fun(_) ->
_pipe@2 = parser_gleam@char:char(<<"("/utf8>>),
_pipe@3 = parser_gleam@parser:chain(
_pipe@2,
fun(_) ->
parser_gleam@parser:many_till(
type_ast_parser(),
type_ast_end_look_ahead()
)
end
),
_pipe@5 = parser_gleam@parser:chain(
_pipe@3,
fun(It) ->
_pipe@4 = parser_gleam@char:char(<<")"/utf8>>),
parser_gleam@parser:map(_pipe@4, fun(_) -> It end)
end
),
parser_gleam@parser:chain(
_pipe@5,
fun(Arguments) ->
_pipe@6 = parser_gleam@string:spaces(),
parser_gleam@parser:chain(
_pipe@6,
fun(_) ->
_pipe@7 = parser_gleam@string:string(
<<"->"/utf8>>
),
parser_gleam@parser:chain(
_pipe@7,
fun(_) ->
_pipe@8 = parser_gleam@string:spaces(
),
parser_gleam@parser:chain(
_pipe@8,
fun(_) ->
_pipe@9 = type_ast_parser(),
parser_gleam@parser:map(
_pipe@9,
fun(Return_) ->
{fn,
Arguments,
Return_}
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-spec type_ast_var_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_var_parser() ->
_pipe = parser_gleam@parser:many1_till(
parser_gleam@char:lower(),
type_ast_end_look_ahead()
),
parser_gleam@parser:map(_pipe, fun(Chars) -> {var, to_name(Chars)} end).
-spec type_ast_tuple_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_tuple_parser() ->
_pipe = parser_gleam@char:char(<<"#"/utf8>>),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
_pipe@1 = parser_gleam@char:char(<<"("/utf8>>),
parser_gleam@parser:chain(
_pipe@1,
fun(_) ->
_pipe@2 = parser_gleam@parser:many1_till(
type_ast_parser(),
type_ast_end()
),
parser_gleam@parser:map(
_pipe@2,
fun(Elems) ->
{tuple, fp_gl@non_empty_list:to_list(Elems)}
end
)
end
)
end
).
-spec type_ast_hole_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_hole_parser() ->
_pipe = parser_gleam@char:char(<<"_"/utf8>>),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
_pipe@1 = parser_gleam@parser:many_till(
parser_gleam@parser:item(),
type_ast_end_look_ahead()
),
parser_gleam@parser:map(
_pipe@1,
fun(Chars) ->
{hole,
begin
_pipe@2 = [<<"_"/utf8>> | Chars],
gleam@string:join(_pipe@2, <<""/utf8>>)
end}
end
)
end
).
-spec type_ast_parser_no_comma_end() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_parser_no_comma_end() ->
_pipe = type_ast_hole_parser(),
_pipe@1 = parser_gleam@parser:alt(_pipe, fun type_ast_tuple_parser/0),
_pipe@2 = parser_gleam@parser:alt(_pipe@1, fun type_ast_fn_parser/0),
_pipe@3 = parser_gleam@parser:alt(
_pipe@2,
fun type_ast_constructor_parser/0
),
parser_gleam@parser:alt(_pipe@3, fun type_ast_var_parser/0).
-spec type_ast_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), type_ast())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
type_ast_parser() ->
_pipe = type_ast_parser_no_comma_end(),
parser_gleam@parser:chain_first(
_pipe,
fun(_) ->
_pipe@1 = parser_gleam@parser:optional(
parser_gleam@string:string(<<","/utf8>>)
),
parser_gleam@parser:chain(
_pipe@1,
fun(_) -> parser_gleam@string:spaces() end
)
end
).
-spec record_constructor_argment_end() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), binary())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
record_constructor_argment_end() ->
_pipe = parser_gleam@char:char(<<","/utf8>>),
_pipe@1 = parser_gleam@parser:chain(
_pipe,
fun(_) -> parser_gleam@string:spaces() end
),
_pipe@2 = parser_gleam@parser:alt(
_pipe@1,
fun() ->
parser_gleam@parser:look_ahead(parser_gleam@char:char(<<")"/utf8>>))
end
),
parser_gleam@parser:alt(_pipe@2, fun() -> string_eof() end).
-spec record_constructor_argument_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), record_constructor_arg())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
record_constructor_argument_parser() ->
_pipe = parser_gleam@parser:many1_till(
parser_gleam@parser:item(),
parser_gleam@string:string(<<": "/utf8>>)
),
parser_gleam@parser:chain(
_pipe,
fun(Name) ->
_pipe@1 = parser_gleam@parser:many1_till(
type_ast_parser_no_comma_end(),
record_constructor_argment_end()
),
parser_gleam@parser:map(
_pipe@1,
fun(Value) ->
{record_constructor_arg,
to_name(Name),
erlang:element(2, Value)}
end
)
end
).
-spec record_constructor_name() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), fp_gl@non_empty_list:non_empty_list(binary()))} |
{error, parser_gleam@parse_result:parse_error(binary())}).
record_constructor_name() ->
parser_gleam@parser:many1_till(
parser_gleam@parser:item(),
begin
_pipe = parser_gleam@string:spaces1(),
_pipe@1 = parser_gleam@parser:alt(
_pipe,
fun() ->
parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<"}"/utf8>>)
)
end
),
_pipe@2 = parser_gleam@parser:alt(
_pipe@1,
fun() ->
parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<"("/utf8>>)
)
end
),
parser_gleam@parser:alt(_pipe@2, fun() -> string_eof() end)
end
).
-spec record_constructor_arguments() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), list(record_constructor_arg()))} |
{error, parser_gleam@parse_result:parse_error(binary())}).
record_constructor_arguments() ->
parser_gleam@parser:either(
begin
_pipe = parser_gleam@char:char(<<"("/utf8>>),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
_pipe@2 = parser_gleam@parser:many1_till(
record_constructor_argument_parser(),
begin
_pipe@1 = parser_gleam@char:char(<<")"/utf8>>),
parser_gleam@parser:chain(
_pipe@1,
fun(_) -> parser_gleam@string:spaces() end
)
end
),
parser_gleam@parser:map(
_pipe@2,
fun fp_gl@non_empty_list:to_list/1
)
end
)
end,
fun() -> parser_gleam@parser:'of'([]) end
).
-spec record_constructor_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), record_constructor())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
record_constructor_parser() ->
_pipe = record_constructor_name(),
parser_gleam@parser:chain(
_pipe,
fun(Name) ->
_pipe@1 = record_constructor_arguments(),
parser_gleam@parser:map(
_pipe@1,
fun(Args) -> {record_constructor, to_name(Name), Args} end
)
end
).
-spec custom_type_parameters_name_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), binary())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
custom_type_parameters_name_parser() ->
_pipe@2 = parser_gleam@parser:many1_till(
parser_gleam@parser:item(),
begin
_pipe = parser_gleam@char:char(<<","/utf8>>),
_pipe@1 = parser_gleam@parser:chain(
_pipe,
fun(_) -> parser_gleam@string:spaces() end
),
parser_gleam@parser:alt(
_pipe@1,
fun() ->
parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<")"/utf8>>)
)
end
)
end
),
_pipe@3 = parser_gleam@parser:map(
_pipe@2,
fun fp_gl@non_empty_list:to_list/1
),
parser_gleam@parser:map(
_pipe@3,
fun(It) ->
_pipe@4 = It,
gleam@string:join(_pipe@4, <<""/utf8>>)
end
).
-spec custom_type_parameters_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), list(binary()))} |
{error, parser_gleam@parse_result:parse_error(binary())}).
custom_type_parameters_parser() ->
parser_gleam@parser:either(
begin
_pipe = parser_gleam@char:char(<<"("/utf8>>),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
_pipe@1 = parser_gleam@parser:many1_till(
custom_type_parameters_name_parser(),
parser_gleam@char:char(<<")"/utf8>>)
),
_pipe@2 = parser_gleam@parser:chain_first(
_pipe@1,
fun(_) -> parser_gleam@string:spaces() end
),
parser_gleam@parser:map(
_pipe@2,
fun fp_gl@non_empty_list:to_list/1
)
end
)
end,
fun() -> parser_gleam@parser:'of'([]) end
).
-spec custom_type_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), x_custom_type())} |
{error, parser_gleam@parse_result:parse_error(binary())}).
custom_type_parser() ->
_pipe = type_opener(),
parser_gleam@parser:chain(
_pipe,
fun(_) ->
_pipe@2 = parser_gleam@parser:many1_till(
parser_gleam@parser:item(),
begin
_pipe@1 = parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<"{"/utf8>>)
),
parser_gleam@parser:alt(
_pipe@1,
fun() ->
parser_gleam@parser:look_ahead(
parser_gleam@char:char(<<"("/utf8>>)
)
end
)
end
),
parser_gleam@parser:chain(
_pipe@2,
fun(Name) ->
_pipe@3 = custom_type_parameters_parser(),
parser_gleam@parser:chain(
_pipe@3,
fun(Parameters) ->
_pipe@4 = parser_gleam@char:char(<<"{"/utf8>>),
_pipe@5 = parser_gleam@parser:chain(
_pipe@4,
fun(_) -> parser_gleam@string:spaces() end
),
parser_gleam@parser:chain(
_pipe@5,
fun(_) ->
_pipe@6 = parser_gleam@parser:many_till(
record_constructor_parser(),
parser_gleam@char:char(<<"}"/utf8>>)
),
parser_gleam@parser:map(
_pipe@6,
fun(Constructors) ->
{x_custom_type,
begin
_pipe@7 = Name,
_pipe@8 = to_name(_pipe@7),
gleam@string:trim(_pipe@8)
end,
Constructors,
Parameters}
end
)
end
)
end
)
end
)
end
).
-spec block_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), {ignored_code(),
gleam@option:option(x_custom_type())})} |
{error, parser_gleam@parse_result:parse_error(binary())}).
block_parser() ->
_pipe = ignored_code_parser(),
parser_gleam@parser:chain(
_pipe,
fun(Code) ->
_pipe@1 = parser_gleam@parser:optional(custom_type_parser()),
parser_gleam@parser:map(
_pipe@1,
fun(Custom_type) -> {Code, Custom_type} end
)
end
).
-spec block_to_ast_nodes({ignored_code(), gleam@option:option(x_custom_type())}) -> list(ast_node()).
block_to_ast_nodes(Block) ->
{Code, Typ} = Block,
case Typ of
none ->
[{node_ignored_code, Code}];
{some, Typ@1} ->
[{node_ignored_code, Code}, {node_x_custom_type, Typ@1}]
end.
-spec ast_parser() -> fun((parser_gleam@stream:stream(binary())) -> {ok,
parser_gleam@parse_result:parse_success(binary(), list(ast_node()))} |
{error, parser_gleam@parse_result:parse_error(binary())}).
ast_parser() ->
_pipe = parser_gleam@parser:many1_till(
block_parser(),
parser_gleam@parser:eof()
),
_pipe@1 = parser_gleam@parser:map(_pipe, fun fp_gl@non_empty_list:to_list/1),
parser_gleam@parser:map(
_pipe@1,
fun(Blocks) -> gleam@list:flat_map(Blocks, fun block_to_ast_nodes/1) end
).
-spec filter_custom_types(list(ast_node())) -> list(x_custom_type()).
filter_custom_types(Ast) ->
_pipe = Ast,
gleam@list:fold(_pipe, [], fun(P, C) -> case C of
{node_ignored_code, _@1} ->
P;
{node_x_custom_type, It} ->
gleam@list:append(P, [It])
end end).
-spec to_name(fp_gl@non_empty_list:non_empty_list(binary())) -> binary().
to_name(It) ->
_pipe = It,
_pipe@1 = fp_gl@non_empty_list:to_list(_pipe),
gleam@string:join(_pipe@1, <<""/utf8>>).