-module(cog). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([run/2, main/0]). -export_type([cog_error/0]). -type cog_error() :: {invalid_path_error, binary()} | {unexpected_token, glexer@token:token()} | {file_error, simplifile:file_error()}. -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 77). -spec collect_project_files(binary(), binary()) -> {ok, list(binary())} | {error, cog_error()}. collect_project_files(Root, Path) -> Path@1 = filepath:join(Root, Path), gleam@result:'try'( begin _pipe = simplifile_erl:is_directory(Path@1), gleam@result:map_error( _pipe, fun(Field@0) -> {file_error, Field@0} end ) end, fun(Is_directory) -> gleam@bool:guard( not Is_directory, {ok, []}, fun() -> _pipe@1 = simplifile:get_files(Path@1), gleam@result:map_error( _pipe@1, fun(Field@0) -> {file_error, Field@0} end ) end ) end ). -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 103). -spec do_find_project_root(binary()) -> {ok, binary()} | {error, cog_error()}. do_find_project_root(Dir) -> gleam@result:'try'( begin _pipe = filepath:join(Dir, <<"gleam.toml"/utf8>>), _pipe@1 = simplifile_erl:is_file(_pipe), gleam@result:map_error( _pipe@1, fun(Field@0) -> {file_error, Field@0} end ) end, fun(Is_file) -> case Is_file of true -> {ok, Dir}; false -> do_find_project_root(filepath:directory_name(Dir)) end end ). -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 94). -spec find_project_root() -> {ok, binary()} | {error, cog_error()}. find_project_root() -> gleam@result:'try'( begin _pipe = simplifile:current_directory(), gleam@result:map_error( _pipe, fun(Field@0) -> {file_error, Field@0} end ) end, fun(Cwd) -> do_find_project_root(Cwd) end ). -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 203). -spec cog_embed(list(glexer@token:token()), binary(), binary()) -> {ok, list(glexer@token:token())} | {error, cog_error()}. cog_embed(Tokens, Path, Dir) -> gleam@result:'try'( begin _pipe = filepath:expand(Path), gleam@result:map_error( _pipe, fun(_) -> {invalid_path_error, Path} end ) end, fun(Path@1) -> gleam@result:'try'( begin _pipe@1 = simplifile:read(filepath:join(Dir, Path@1)), gleam@result:map_error( _pipe@1, fun(Field@0) -> {file_error, Field@0} end ) end, fun(Data) -> Codepoints = begin _pipe@2 = Data, _pipe@3 = gleam@string:to_utf_codepoints(_pipe@2), _pipe@4 = gleam@list:map( _pipe@3, fun(Codepoint) -> <<<<"\\u{"/utf8, (gleam@int:to_base16( gleam_stdlib:identity(Codepoint) ))/binary>>/binary, "}"/utf8>> end ), gleam@string:join(_pipe@4, <<""/utf8>>) end, {ok, [{string, Codepoints} | lists:reverse(Tokens)]} end ) end ). -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 124). -spec do_perform_actions( list(glexer@token:token()), binary(), list(glexer@token:token()) ) -> {ok, list(glexer@token:token())} | {error, cog_error()}. do_perform_actions(Tokens, Dir, Acc) -> case Tokens of [] -> {ok, Acc}; [{comment_normal, <<"cog:embed "/utf8, Path/binary>>} = T1 | Tokens@1] -> {Comments, Tokens@2} = gleam@list:split_while( Tokens@1, fun(Token) -> case Token of {space, _} -> true; {comment_doc, _} -> true; {comment_normal, _} -> true; {comment_module, _} -> true; _ -> false end end ), case Tokens@2 of [const = T2, {space, _} = T3, {name, _} = T4, {space, _} = T5, equal = T6, {space, _} = T7, {string, _} | Tokens@3] -> gleam@result:'try'( (cog_embed( gleam@list:flatten( [[T1], Comments, [T2, T3, T4, T5, T6, T7]] ), Path, Dir )), fun(Generated) -> do_perform_actions( Tokens@3, Dir, lists:append(Generated, Acc) ) end ); [pub = T2@1, {space, _} = T3@1, const = T4@1, {space, _} = T5@1, {name, _} = T6@1, {space, _} = T7@1, equal = T8, {space, _} = T9, {string, _} | Tokens@4] -> gleam@result:'try'( (cog_embed( gleam@list:flatten( [[T1], Comments, [T2@1, T3@1, T4@1, T5@1, T6@1, T7@1, T8, T9]] ), Path, Dir )), fun(Generated@1) -> do_perform_actions( Tokens@4, Dir, lists:append(Generated@1, Acc) ) end ); [] -> {error, {unexpected_token, end_of_file}}; [Token@1 | _] -> {error, {unexpected_token, Token@1}} end; [Token@2 | Tokens@5] -> do_perform_actions(Tokens@5, Dir, [Token@2 | Acc]) end. -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 116). -spec perform_actions(list(glexer@token:token()), binary()) -> {ok, list(glexer@token:token())} | {error, cog_error()}. perform_actions(Tokens, Dir) -> _pipe = do_perform_actions(Tokens, Dir, []), gleam@result:map(_pipe, fun lists:reverse/1). -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 67). -spec run(binary(), binary()) -> {ok, binary()} | {error, cog_error()}. run(Content, Dir) -> Tokens = begin _pipe = glexer:new(Content), glexer:lex(_pipe) end, Tokens@1 = gleam@list:map( Tokens, fun(Token) -> erlang:element(1, Token) end ), gleam@result:'try'( perform_actions(Tokens@1, Dir), fun(Tokens@2) -> {ok, cog@glexer_printer:print(Tokens@2)} end ). -file("/Users/danielle/Repositories/Projects/cog/src/cog.gleam", 19). -spec main() -> nil. main() -> Result = (gleam@result:'try'( find_project_root(), fun(Root) -> gleam@result:'try'( collect_project_files(Root, <<"src"/utf8>>), fun(Source_files) -> gleam@result:'try'( collect_project_files(Root, <<"test"/utf8>>), fun(Test_files) -> Files = lists:append(Source_files, Test_files), Gleam_files = gleam@list:filter( Files, fun(_capture) -> gleam_stdlib:string_ends_with( _capture, <<".gleam"/utf8>> ) end ), gleam@result:'try'( begin _pipe = Gleam_files, _pipe@3 = gleam@list:map( _pipe, fun(File) -> gleam@result:'try'( begin _pipe@1 = simplifile:read( File ), gleam@result:map_error( _pipe@1, fun(Field@0) -> {file_error, Field@0} end ) end, fun(Content) -> gleam@result:'try'( run(Content, Root), fun(Source) -> _pipe@2 = simplifile:write( File, Source ), gleam@result:map_error( _pipe@2, fun(Field@0) -> {file_error, Field@0} end ) end ) end ) end ), gleam@result:all(_pipe@3) end, fun(_) -> {ok, nil} end ) end ) end ) end )), case Result of {ok, nil} -> nil; {error, {unexpected_token, Token}} -> gleam_stdlib:println_error( <<"unexpected token: "/utf8, (gleam@string:inspect(Token))/binary>> ); {error, {invalid_path_error, Path}} -> gleam_stdlib:println_error( <<"invalid path found: "/utf8, Path/binary>> ); {error, {file_error, Error}} -> gleam_stdlib:println_error( <<"file error: "/utf8, (gleam@string:inspect(Error))/binary>> ) end.