Tolerate parse_transforming non-top-level forms.

A few changes were made to provide default values for context attributes so that parse_transform:transform/4 could be called for non-top-level forms. I don't know if this is generally useful or if it just fit my weird use case.
This commit is contained in:
Joe Osborn 2012-12-12 06:55:54 -08:00
parent ae7163f3db
commit 9b594f639b

View File

@ -65,6 +65,7 @@
get_file/1, get_file/1,
get_module/1, get_module/1,
get_attribute/2, get_attribute/2,
get_attribute/3,
get_orig_syntax_tree/1, get_orig_syntax_tree/1,
function_exists/3, function_exists/3,
optionally_pretty_print/3, optionally_pretty_print/3,
@ -212,7 +213,7 @@ get_pos(I) when is_list(I) ->
-spec get_file(forms()) -> -spec get_file(forms()) ->
string(). string().
get_file(Forms) -> get_file(Forms) ->
string_value(hd(get_attribute(file, Forms))). string_value(hd(get_attribute(file, Forms, [erl_syntax:string("undefined")]))).
@ -224,7 +225,7 @@ get_file(Forms) ->
-spec get_module([any()]) -> -spec get_module([any()]) ->
atom(). atom().
get_module(Forms) -> get_module(Forms) ->
atom_value(hd(get_attribute(module, Forms))). atom_value(hd(get_attribute(module, Forms, [erl_syntax:atom(undefined)]))).
@ -238,10 +239,11 @@ get_module(Forms) ->
-spec get_attribute(atom(), [any()]) -> -spec get_attribute(atom(), [any()]) ->
'none' | [erl_syntax:syntaxTree()]. 'none' | [erl_syntax:syntaxTree()].
%% %%
get_attribute(A, Forms) -> get_attribute(A, Forms) -> get_attribute(A,Forms,[erl_syntax:atom(undefined)]).
get_attribute(A, Forms, Undef) ->
case find_attribute(A, Forms) of case find_attribute(A, Forms) of
false -> false ->
throw({error, ?DUMMY_LINE, {missing_attribute, A}}); Undef;
Other -> Other ->
Other Other
end. end.
@ -633,8 +635,7 @@ apply_F(F, Type, Form, Context, Acc) ->
{context, Context}, {context, Context},
{acc, Acc}, {acc, Acc},
{apply_f, F}, {apply_f, F},
{form, Form}] {form, Form}] ++ [{stack, erlang:get_stacktrace()}])
++ [{stack, erlang:get_stacktrace()}])
end. end.