support printing erl_syntax meta calls in the same way as erl source

Signed-off-by: Jordan Wilberding <diginux@gmail.com>
This commit is contained in:
Eric Merritt 2012-12-03 11:16:55 -05:00 committed by Jordan Wilberding
parent 95f723e1e0
commit 407ccf886f

View file

@ -13,9 +13,11 @@
erl_source_to_core_ast/1, erl_source_to_core_ast/1,
erl_source_to_erl_ast/1, erl_source_to_erl_ast/1,
erl_source_to_asm/1, erl_source_to_asm/1,
erl_source_to_erl_syntax/1,
erl_string_to_core_ast/1, erl_string_to_core_ast/1,
erl_string_to_erl_ast/1, erl_string_to_erl_ast/1,
erl_string_to_asm/1]). erl_string_to_asm/1,
erl_string_to_erl_syntax/1]).
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -33,13 +35,13 @@
beam_to_erl_source(BeamFName, ErlFName) -> beam_to_erl_source(BeamFName, ErlFName) ->
case beam_lib:chunks(BeamFName, [abstract_code]) of case beam_lib:chunks(BeamFName, [abstract_code]) of
{ok, {_, [{abstract_code, {raw_abstract_v1,Forms}}]}} -> {ok, {_, [{abstract_code, {raw_abstract_v1,Forms}}]}} ->
Src = Src =
erl_prettypr:format(erl_syntax:form_list(tl(Forms))), erl_prettypr:format(erl_syntax:form_list(tl(Forms))),
{ok, Fd} = file:open(ErlFName, [write]), {ok, Fd} = file:open(ErlFName, [write]),
io:fwrite(Fd, "~s~n", [Src]), io:fwrite(Fd, "~s~n", [Src]),
file:close(Fd); file:close(Fd);
Error -> Error ->
Error Error
end. end.
%% @doc compile an erlang source file into a Core Erlang AST %% @doc compile an erlang source file into a Core Erlang AST
@ -67,6 +69,15 @@ erl_source_to_asm(Path) ->
{ok, Contents} = file:read_file(Path), {ok, Contents} = file:read_file(Path),
erl_string_to_asm(binary_to_list(Contents)). erl_string_to_asm(binary_to_list(Contents)).
%% @doc compile an erlang source file to a string that displays the
%% 'erl_syntax1 calls needed to reproduce those terms.
%%
%% @param Path - The path to the erlang source file
-spec erl_source_to_erl_syntax(file:filename()) -> string().
erl_source_to_erl_syntax(Path) ->
{ok, Contents} = file:read_file(Path),
erl_string_to_erl_syntax(Contents).
%% @doc compile a string representing an erlang expression into an %% @doc compile a string representing an erlang expression into an
%% Erlang AST %% Erlang AST
%% %%
@ -105,3 +116,16 @@ erl_string_to_core_ast(StringExpr) ->
-spec erl_string_to_asm(string()) -> ErlangAsm::term(). -spec erl_string_to_asm(string()) -> ErlangAsm::term().
erl_string_to_asm(StringExpr) -> erl_string_to_asm(StringExpr) ->
compile:forms(erl_string_to_erl_ast(StringExpr), ['S']). compile:forms(erl_string_to_erl_ast(StringExpr), ['S']).
%% @doc compile an erlang source file to a string that displays the
%% 'erl_syntax1 calls needed to reproduce those terms.
%%
%% @param StringExpr - The string representing the erlang code.
-spec erl_string_to_erl_syntax(string() | binary()) -> string().
erl_string_to_erl_syntax(BinaryExpr)
when erlang:is_binary(BinaryExpr) ->
erlang:binary_to_list(BinaryExpr);
erl_string_to_erl_syntax(StringExpr) ->
{ok, Tokens, _} = erl_scan:string(StringExpr),
{ok, ErlAST} = erl_parse:parse_form(Tokens),
io:format(erl_prettypr:format(erl_syntax:meta(ErlAST))).