Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Macro syntax parsing doesn't work for incomplete Erlang expressions #159

Open
sergeyevstifeev opened this Issue · 10 comments

6 participants

@sergeyevstifeev

Macros in Erlang allow for some pretty ugly code in them, though it can be perfectly legal from the language perspective. However, plugin doesn't handle some tricky cases. For example, the following code is compiled correctly by erlc, but is treated as invalid by the plugin (example partially taken from http://www.erlang.org/documentation/doc-5.2/doc/extensions/macros.html ).

-module(mymod).

-export([ foo/0
        , bar/0
        , baz/0
        ]).

-record(foo,
        {baz = 0
        }).

-define(LPAREN, ().
-define(RPAREN, )).

-define(ARROW, ->).
-define(COLON, :).
-define(COMMA, ,).

-define(LBRACKET, [).
-define(RBRACKET, ]).

-define(LCURLY, {).
-define(RCURLY, }).

-define(macro2(X, Y), {a,X,b,Y).

foo ?LPAREN ?RPAREN ?ARROW
  io ?COLON format ?LPAREN "Hello" ?COMMA ?LBRACKET ?RBRACKET ?RPAREN ?COMMA
  io ?COLON format ?LPAREN " " ?COMMA ?LBRACKET ?RBRACKET ?RPAREN ?COMMA
  io ?COLON format ?LPAREN "World~n" ?COMMA ?LBRACKET ?RBRACKET ?RPAREN
  .

-define(increment_rec(Record, Field, Val), Record{Field=Record.Field + (Val)}).

bar() ->
  Foo = #foo{},
  ?increment_rec(Foo#foo, baz, 1).

baz() ->
  ?macro2(x,y)}.
@ignatov
Owner

For macros support we need improve lexer: at least correctly substitute all macros definitions. It's very time-consuming feature. Guys from AppCode have a lot of performance problems with projects with user defined macroses.

Are you really write production code like this?

foo ?LPAREN ?RPAREN ?ARROW
  io ?COLON format ?LPAREN "Hello" ?COMMA ?LBRACKET ?RBRACKET ?RPAREN ?COMMA
  io ?COLON format ?LPAREN " " ?COMMA ?LBRACKET ?RBRACKET ?RPAREN ?COMMA
  io ?COLON format ?LPAREN "World~n" ?COMMA ?LBRACKET ?RBRACKET ?RPAREN
  .
@sergeyevstifeev

@ignatov No, this is just a showcase of course, that's why I said in the description that macros allow for some quite ugly code. Though ?increment_rec macro was taken from a real Erlang project (proprietary). I am trying to use the plugin for it and simply report some issues that I found. Other examples in this case are made up.

@ignatov
Owner

@sergeyevstifeev ?increment_rec is a good candidate to improvement.
I also trained my parser on some open source projects, especially for good macros support. And I was a little bit surprised when I realized that most of macros definitions a pretty simple.
BTW, please report another issues with macros from production code. I'll fix them as well.

@sergeyevstifeev

@ignatov agree, macros should normally be simple and overusing them in a project is definitely not a good sign.
Sure I'll report issues, I'm really looking forward to using Idea for Erlang programming (mostly because of the great refactoring possibilities in intellij).

@diegollarrull

This is actual riak/riak_core production code showing that bug:
intellij record bug

I consider this bug to be of high priority if you wish to attract riak / riak_core users into using intellij-erlang

@horkhe

Joe Armstrong says "do not use marcroses" :) http://www.youtube.com/watch?v=u41GEwIq2mE

@diegollarrull

I wonder how Joe Armstrong would suggest writing the above code (which allows having different versions of a record running on the same platform transparently) without using macroses. I can only think of parse transforms but that's like shooting a mouse with an elephant gun.

There are cases where it is just impossible not to use macros for dynamic syntax generation.

@ignatov
Owner

@diegollarrull initial riak support is available in 7c7b898.

@develop7

I'm affected by this issue too, here's relevant code:
screenshot from 2014-12-01 17 38 04
inb4: there is -include("netflow_v9.hrl"). in file below. Is this the case worth handling by plugin or it is me doing it wrong?

@deadok22
Collaborator

Hi @develop7,

you're doing it right, it's just the erlang preprocessor is not supported by the plugin yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.