[erlang-questions] Parse transforms: signalling errors and warnings.

Alex Arnon alex.arnon@REDACTED
Sun Oct 30 16:01:23 CET 2016


Thanks Slava and Peti, this works!

Specifically, adding the form:

    {error, {Line, ModuleWithFormatError, Reason}}

Where:
- "Line" is the line number,
- "ModuleWithFormatError" is a module exporting a format_error/1 function,
which will be used to format the Reason.
- "Reason" is some kind of info, given to the format_error/1 above.

I've built a minimal parse transform module to test this:

-module(fail_on_function_trans).


-export([parse_transform/2]).

-export([format_error/1]).


parse_transform(Forms, _Options) ->
  Mod = extract_module(Forms),
  io:format("processing module: ~w~n", [Mod]),

  OutForms = Forms ++ [error_form(15, "bad thing has happened")],

  io:format("output will be: ~p~n", [OutForms]),
  OutForms.


extract_module([{attribute, _, module, Mod} | _]) ->
  Mod;
extract_module([_ | H]) ->
  extract_module(H);
extract_module([]) ->
  unknown.


error_form(Line, ReasonString) ->
  {error, {Line, ?MODULE, ReasonString}}.

format_error(Arg) ->
  io_lib:format("~s", [Arg]).


The output then contains, when compiling module "xxx_check_fail":

    xxx_check_fail.erl:15: bad thing has happened


Cheers,
Alex.

On Sun, Oct 30, 2016 at 8:08 AM, Slava Yurin <yurinvv@REDACTED> wrote:

> Try this:
> https://github.com/uwiger/parse_trans/blob/master/src/parse_trans.erl#L127
>
> 29.10.2016, 15:47, "Alex Arnon" <alex.arnon@REDACTED>:
>
> Hi All!
>
> I am trying to build a simple parse transform, that makes a small
> modification when a called function's name is of a certain form. I would
> like to signal that an error has occurred (e.g. the name is malformed, or
> arguments of wrong kind). How can I do this?
> I have tried to insert {error, X}, with various types of X (including the
> {Line, Module, Reason} form, which pacifies erl_lint at least), but
> compilation with erlc keeps failing with:
>
>     Compiler function compile:compile/3 returned:
>     {error,undef}
>
> Thanks in advance,
> Alex.
>
> ,
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161030/598ab1eb/attachment.htm>


More information about the erlang-questions mailing list