Skip to content

Instantly share code, notes, and snippets.

@ferd
Created February 19, 2013 17:09
Show Gist options
  • Select an option

  • Save ferd/4987814 to your computer and use it in GitHub Desktop.

Select an option

Save ferd/4987814 to your computer and use it in GitHub Desktop.
Parse transform to support -spec and behaviour_info in Erlang
-module(behaviour_parse).
-export([parse_transform/2]).
-define(CUTOFF, "2.15"). % R15B
parse_transform(AST, _Opts) ->
[Vsn] = [X || {kernel,_,X} <- application:which_applications()],
walk_ast(if Vsn >= ?CUTOFF -> spec;
Vsn < ?CUTOFF -> func
end,
AST).
walk_ast(_, []) ->
[];
walk_ast(func, [{attribute, _Ln, callback, _}|T]) ->
walk_ast(func, T);
walk_ast(spec, [{attribute, Ln, export, List}|T]) ->
[{attribute, Ln, export, List -- [{behaviour_info,1},{behavior_info,1}]}
| walk_ast(spec, T)];
walk_ast(spec, [{function, _Ln, behaviour_info,1,_Clauses}|T]) ->
walk_ast(spec, T);
walk_ast(spec, [{function, _Ln, behavior_info,1,_Clauses}|T]) ->
walk_ast(spec, T);
walk_ast(Type, [H|T]) ->
[H|walk_ast(Type, T)].
@ferd

ferd commented Feb 19, 2013

Copy link
Copy Markdown
Author

This will obviously break with versions of the kernel < 2.10, but that should be RB09 and prior, which I figure is fairly rare in the wild.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment