From c55c8861cd7a2486c7c674093022e36dd859fd87 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Wed, 19 Dec 2012 17:29:53 +0100 Subject: [PATCH] Added revert_form/1; deal with revert bugs --- doc/parse_trans.md | 27 +++++++++++++++++++++++++-- src/parse_trans.erl | 22 ++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/doc/parse_trans.md b/doc/parse_trans.md index d68e351..cd99992 100644 --- a/doc/parse_trans.md +++ b/doc/parse_trans.md @@ -96,7 +96,7 @@ Performs a transform of Forms using the fun Fun(Form). Reads debug_info from the beam file Beam and returns a string containing the pretty-printed corresponding erlang source code.pp_beam/2 Reads debug_info from the beam file Beam and pretty-prints it as -Erlang source code, storing it in the file Out.pp_src/2Pretty-prints the erlang source code corresponding to Forms into Out.replace_function/4return/2Checks the transformed result for errors and warnings.revert/1Reverts back from Syntax Tools format to Erlang forms.top/3transform/4 +Erlang source code, storing it in the file Out.pp_src/2Pretty-prints the erlang source code corresponding to Forms into Out.replace_function/4return/2Checks the transformed result for errors and warnings.revert/1Reverts back from Syntax Tools format to Erlang forms.revert_form/1Reverts a single form back from Syntax Tools format to Erlang forms.top/3transform/4 Makes one pass. @@ -524,7 +524,30 @@ Reverts back from Syntax Tools format to Erlang forms. Note that the Erlang forms are a subset of the Syntax Tools syntax tree, so this function is safe to call even on a list of -regular Erlang forms. +regular Erlang forms. + +###revert_form/1## + + + + +
revert_form(F::Tree) -> Form
+

+ + + + +Reverts a single form back from Syntax Tools format to Erlang forms. + + +`erl_syntax:revert/1` has had a long-standing bug where it doesn't +completely revert attribute forms. This function deals properly with those +cases. + + +Note that the Erlang forms are a subset of the Syntax Tools +syntax tree, so this function is safe to call even on a regular Erlang +form. ###top/3## diff --git a/src/parse_trans.erl b/src/parse_trans.erl index 3685f83..f2703ef 100644 --- a/src/parse_trans.erl +++ b/src/parse_trans.erl @@ -40,6 +40,7 @@ transform/4, depth_first/4, revert/1, + revert_form/1, format_exception/2, format_exception/3, return/2 ]). @@ -513,8 +514,25 @@ get_orig_syntax_tree(File) -> %%% -spec revert(forms()) -> forms(). -revert(Tree) -> - [erl_syntax:revert(T) || T <- lists:flatten(Tree)]. +revert(Tree) when is_list(Tree) -> + [revert_form(T) || T <- lists:flatten(Tree)]. + +%%% @spec (Tree) -> Form +%%% +%%% @doc Reverts a single form back from Syntax Tools format to Erlang forms. +%%%

`erl_syntax:revert/1' has had a long-standing bug where it doesn't +%%% completely revert attribute forms. This function deals properly with those +%%% cases.

+%%%

Note that the Erlang forms are a subset of the Syntax Tools +%%% syntax tree, so this function is safe to call even on a regular Erlang +%%% form.

+revert_form(F) -> + case erl_syntax:revert(F) of + {attribute,L,A,Tree} when element(1,Tree) == tree -> + {attribute,L,A,erl_syntax:revert(Tree)}; + Result -> + Result + end. %%% @spec (Forms, Context) -> Forms | {error,Es,Ws} | {warnings,Forms,Ws} %%%