gproc_pt.erl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. %% ``The contents of this file are subject to the Erlang Public License,
  2. %% Version 1.1, (the "License"); you may not use this file except in
  3. %% compliance with the License. You should have received a copy of the
  4. %% Erlang Public License along with this software. If not, it can be
  5. %% retrieved via the world wide web at http://www.erlang.org/.
  6. %%
  7. %% Software distributed under the License is distributed on an "AS IS"
  8. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. %% the License for the specific language governing rights and limitations
  10. %% under the License.
  11. %%
  12. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. %% AB. All Rights Reserved.''
  15. %%
  16. %% @author Ulf Wiger <ulf@wiger.net>
  17. %% @author Dmitry Demeshchuk <demeshchuk@gmail.com>
  18. %%
  19. %% @doc Parse transform utility for gproc users.
  20. %%
  21. %% This module provides some closer syntactical integration for
  22. %% people who are enthusiastic gproc users.
  23. %%
  24. %% Specifically, this module transforms `Pid ! Msg' into
  25. %% `gproc:send(Pid, Msg)', which, apart from accepting any type for
  26. %% `Pid' that `!' understands, is also able to handle a gproc "triple",
  27. %% e.g. `{n, l, Name}' or even `{p, l, Prop}' (in the latter case, the
  28. %% message may be delivered to multiple recipients).
  29. %%
  30. %% Users should be aware that parse transforms may be confusing to
  31. %% the casual reader, since they extend the semantics of possibly
  32. %% ubiquitous constructs (as is the case with this transform). Therefore,
  33. %% you should document clearly that this is happening.
  34. %%
  35. %% Original suggestion by Dimitry Demeschuk.
  36. %% @end
  37. %%
  38. -module(gproc_pt).
  39. -export([parse_transform/2]).
  40. parse_transform(Forms, _Options) ->
  41. do_transform(Forms).
  42. do_transform([{op, L, '!', Lhs, Rhs}|Fs]) ->
  43. [NewLhs] = do_transform([Lhs]),
  44. [NewRhs] = do_transform([Rhs]),
  45. [{call, L, {remote, L, {atom, L, gproc}, {atom, L, send}},
  46. [NewLhs, NewRhs]} | do_transform(Fs)];
  47. do_transform([]) ->
  48. [];
  49. do_transform([F|Fs]) when is_tuple(F) ->
  50. [list_to_tuple(do_transform(tuple_to_list(F))) | do_transform(Fs)];
  51. do_transform([F|Fs]) ->
  52. [do_transform(F) | do_transform(Fs)];
  53. do_transform(F) ->
  54. F.