WhatsApp / erlfmt

An automated code formatter for Erlang
Apache License 2.0
412 stars 52 forks source link

Syntax error when parsing macros #314

Closed dziaineka closed 2 years ago

dziaineka commented 3 years ago

Hello! I've made simple example:

-module(example_module).

-export([example_function/2]).

-define(CATCH_SPECIAL_ERROR(HandleFunction),
        error:(E = some_special_error) -> HandleFunction(E)).

example_function(Data, Function) ->
    try Function(Data)
    catch
        ?CATCH_SPECIAL_ERROR(
            fun(SpecialError) ->
                some_action(),
                {error, SpecialError}
            end
        );

        Error:_Reason:_Stacktrace ->
            {error, Error}
    end.

some_action() ->
    some_action.

Where erlfmt shows me errors:

example_module.erl:6:14: syntax error before: ':'
example_module.erl:12:13: syntax error before: 'fun'

But the code seems to be valid.

Thank you!

michalmuskala commented 2 years ago

I'm sorry for a late reply.

Unfortunately supporting this sort of syntax with the current parser is extremely challenging - I've tried couple times to make this work, but I was not able to - primarily because it's "hiding" the structure of the program with the standalone -> token unrelated to surrounding code.

As such I'm going to close this issue.