anchor.diff:      end.
   
   
  +
  +
   %% @doc Writes a generated tree as a standard erlang file
   -spec write(atom(), [erl_syntax:syntaxTree()], string()) ->
       ok | {error, term()}.
   
   write(Mod, Tree, BasePath) ->
  -    Path = filename:join(BasePath, atom_to_list(Mod)++".erl"),
  +    Path = filename:join(BasePath, nklib_util:to_list(Mod)++".erl"),
       Content = list_to_binary(
           [io_lib:format("~s\n\n", [erl_prettypr:format(S)]) || S <-Tree]),
       file:write_file(Path, Content).
  
positive.code: 
  call_expr(Mod, Fun, Arity, Vers) ->
      erl_syntax:application(
          erl_syntax:atom(Mod),
          erl_syntax:atom(Fun),
          var_list(Arity, Vers)).
  
  
negative.code:     end.
  
  
  
  
  
  %% ===================================================================
  %% Forms utilities
  %% ===================================================================
  
  
  %% @doc Finds the module attribute
  forms_find_attribute(_Key, []) ->
      undefined;
  
  forms_find_attribute(Key, [{attribute, _, Key, Value}|_]) ->
      Value;
  
  forms_find_attribute(Key, [_|Rest]) ->
      forms_find_attribute(Key, Rest).
  
  
  %% @private Adds an attribute at the end of the attribute list
  forms_add_attributes(Attrs, Forms) ->
      forms_add_attributes(Forms, Attrs, []).
  
  
  %% @private
  forms_add_attributes([{attribute, _, _, _}=Attr|Rest], Attrs, Acc) ->
      forms_add_attributes(Rest, Attrs, [Attr|Acc]);
  
  forms_add_attributes(Rest, Attrs, Acc) ->
      lists:reverse(Acc) ++ Attrs ++ Rest.
  
  
  %% @private Gets all attributes
  forms_get_attributes(Forms) ->
      forms_get_attributes(Forms, []).
  
  
  %% @private
  forms_get_attributes([], Acc) ->
      lists:reverse(Acc);
  
  forms_get_attributes([{attribute, _, _, _}=Attr|Rest], Acc) ->
      forms_get_attributes(Rest, [Attr|Acc]);
  
  forms_get_attributes([_|Rest], Acc) ->
      forms_get_attributes(Rest, Acc).
  
  
  %% @private Finds all exported functions
  forms_find_exported(Forms) ->
      forms_find_exported(Forms, []).
  
  
  forms_find_exported([], Acc) ->
      Acc;
  
  forms_find_exported([{attribute, _, export, List}|Rest], Acc) ->
      forms_find_exported(Rest, Acc++List);
  
  forms_find_exported([_|Rest], Acc) ->
      forms_find_exported(Rest, Acc).
  
  
  
  %% @private Gets all defined funs
  forms_get_funs(Forms) ->
      forms_get_funs(Forms, #{}).
  
  
  %% @private
  forms_get_funs([], Acc) ->
      Acc;
  
  forms_get_funs([{function, _Line, Name, Arity, Spec}|Rest], Acc) ->
      forms_get_funs(Rest, Acc#{{Name, Arity}=>Spec});
  
  forms_get_funs([_|Rest], Acc) ->
      forms_get_funs(Rest, Acc).
  
  
  %% @private Adds a number of funs at the end of the module
  %% Funs must be [{function, ...}]
  forms_add_funs(Funs, Forms) ->
      forms_add_funs(Forms, Funs, []).
  
  
  %% @private
  forms_add_funs([{eof, _}|_]=End, Funs, Acc) ->
      lists:reverse(Acc) ++ Funs ++ End;
  
  forms_add_funs([Term|Rest], Funs, Acc) ->
      forms_add_funs(Rest, Funs, [Term|Acc]).
  
  
  %% @private
  forms_print(Forms) ->
      Content = list_to_binary([io_lib:format("~s\n\n", [erl_prettypr:format(S)]) || S <- Forms]),
      io:format("Forms: \n~s\n", [Content]).
  
  
  
  %% ===================================================================
  %% Internal
  %% ===================================================================
  