Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adds seresye_transform which automatically exports rules

  • Loading branch information...
commit 5e1e86e6b7906db9afb87507ccfe98fe42038e6d 1 parent 48f35b3
Yurii Rashkovskii authored February 29, 2012
1  include/seresye.hrl
@@ -3,3 +3,4 @@
3 3
 %% nicer to write them in a when clause and let the transform do its
4 4
 %% work.
5 5
 -compile({parse_transform, seresye_autoneg}).
  6
+-compile({parse_transform, seresye_transform}).
6  rebar.config
... ...
@@ -1,3 +1,5 @@
1 1
 {deps, 
2  
-  [{erlware_commons, ".*", {git, "https://github.com/erlware/erlware_commons.git", 
3  
-                            {branch, "master"}}}]}.
  2
+  [
  3
+   {erlware_commons, ".*", {git, "https://github.com/erlware/erlware_commons.git", {branch, "master"}}},
  4
+   {parse_trans, ".*", {git, "https://github.com/esl/parse_trans.git", {branch, "master"}}}
  5
+]}.
58  src/seresye_transform.erl
... ...
@@ -0,0 +1,58 @@
  1
+-module(seresye_transform).
  2
+-export([parse_transform/2]).
  3
+
  4
+-record(state,{ 
  5
+          rules = [],
  6
+          rule_functions = [],
  7
+          exports = [],
  8
+          options
  9
+         }).
  10
+
  11
+-record(context, {module,
  12
+                  function,
  13
+                  arity,
  14
+                  file,
  15
+                  options}).
  16
+
  17
+
  18
+parse_transform(Forms, Options) ->
  19
+    #context{ file = File } = parse_trans:initial_context(Forms, Options),
  20
+    case erl_lint:module(Forms, File, [nowarn_unused_function,nowarn_unused_vars,nowarn_unused_record]) of
  21
+        {error, _Errors, _Warnings} ->
  22
+            Forms;
  23
+        _ ->
  24
+            {Forms1, State} = parse_trans:transform(fun do_transform/4, 
  25
+                                             #state{ options = Options },
  26
+                                                    Forms, Options),
  27
+            Result0 = parse_trans:revert(Forms1),
  28
+            lists:foldl(fun ({Fun, Arity}, Acc) ->
  29
+                                parse_trans:export_function(Fun, Arity, Acc)
  30
+                        end, Result0, State#state.rule_functions)
  31
+    end.
  32
+
  33
+do_transform(attribute,{attribute, _, export, Exports} = Attr, _Context, #state{} = State) ->
  34
+    {Attr, true, State#state{ exports = State#state.exports ++ Exports }};
  35
+
  36
+do_transform(attribute,{attribute, _, rule, Rule} = Attr, _Context, #state{ rules = Rs } = State) ->
  37
+    {Attr, false, State#state{ rules = [rule_name(Rule)|Rs] }};
  38
+
  39
+do_transform(attribute,{attribute, _, rules, Rules0} = Attr, _Context, #state{ rules = Rs } = State) ->
  40
+    Rules = [ rule_name(R) || R <- Rules0 ],
  41
+    {Attr, false, State#state{ rules = Rules ++ Rs }};
  42
+
  43
+do_transform(function, {function, _, Fun, Arity, _Cs} = Form, _Context, #state{ rules = Rules, rule_functions = RFuns } = State) ->
  44
+    case lists:member(Fun, Rules) of
  45
+        false ->
  46
+            {Form, true, State};
  47
+        true ->
  48
+            {Form, true, State#state{ rule_functions = [{Fun, Arity}|RFuns] -- State#state.exports }}
  49
+    end;
  50
+
  51
+
  52
+do_transform(_Type, Form, _Context, State) ->
  53
+    {Form, true, State}.
  54
+
  55
+rule_name(A) when is_atom(A) ->
  56
+    A;
  57
+rule_name({A, _}) ->
  58
+    A.

0 notes on commit 5e1e86e

Please sign in to comment.
Something went wrong with that request. Please try again.