[erlang-questions] Parse Transform with Macros

Tristan Sloughter <>
Sun Mar 6 23:14:32 CET 2011


Now I see from the code that there is an include option, which from what I
can tell should work like :

 aleppo:process_tokens(Tokens, [{include, [Dirs]}])

correct?

On Sun, Mar 6, 2011 at 1:44 PM, Tristan Sloughter <
> wrote:

> Is the file option so it is able to expand macros by providing a include
> file as an option?
>
> This isn't working and neither is if they are included in the module that
> I'm running the process tokens from.
>
> So this is only working if i just use the macro ?MACHINE which I guess
> always exists... Any other and it is unable to find it:
>
> {function_clause,
>                                                 [{lists,reverse,
>                                                   [{badarg,
>                                                     [{dict,fetch,
>                                                       ['BASE_MODULE',
>
> {dict,1,16,16,8,80,48,
>
>  {[],[],[],[],[],[],[],
>
> [],[],[],[],[],[],[],
>                                                          [],[]},
>                                                         {{[],[],[],[],[],
>                                                           [['MACHINE',
>
>  {atom,1,'BEAM'}]],
>
>  [],[],[],[],[],[],
>                                                           [],[],[],[]}}}]},
>
> {aleppo,process_tree,3},
>
>
>
> Tristan
>
>
> On Sun, Mar 6, 2011 at 1:24 PM, Tristan Sloughter <
> > wrote:
>
>> Sweet, that worked, thanks!
>>
>> Tristan
>>
>>
>> On Sun, Mar 6, 2011 at 12:51 PM, Evan Miller <> wrote:
>>
>>> Aleppo adds an "eof" token at the end if one is not present. Perhaps
>>> try removing it before passing to erl_parse? It's a new "feature" and
>>> perhaps a cause of bugs.
>>>
>>> On Sun, Mar 6, 2011 at 12:46 PM, Tristan Sloughter
>>> <> wrote:
>>> > This was working for me with erl_scan and erl_parse. The error I'm
>>> getting
>>> > is:
>>> > {{badmatch,
>>> >                                                  {error,
>>> >                                                   {0,erl_parse,
>>> >                                                    ["syntax error
>>> before: ",
>>> >                                                     "eof"]}}},
>>> > I got the code for what I'm doing by
>>> > reading:
>>> http://chlorophil.blogspot.com/2007/04/erlang-macro-processor-v2-part-v.html
>>> > But then I wanted to use a macro in what I am added to the module. So
>>> I've
>>> > modified 'ast_reversed_results' to:
>>> > ....
>>> >         String ->
>>> >             {done,{ok,Tokens,LineEnd},StringRest} =
>>> >                 erl_scan:tokens([], String, LineStart),
>>> >             {ok, NewTokens} = aleppo:process_tokens(Tokens),
>>> >             {ok, AST} = erl_parse:parse_form(NewTokens),
>>> > ....
>>> > But this fails with the above error.
>>> > Maybe I'm doing something else wrong, I'll keep working on it.
>>> > Thanks,
>>> > Tristan
>>> > On Sun, Mar 6, 2011 at 12:41 PM, Evan Miller <>
>>> wrote:
>>> >>
>>> >> IIRC erl_parse needs a full module as input, i.e. -module() at the top
>>> >> and all Erlang expressions inside of named functions.
>>> >>
>>> >> On Sun, Mar 6, 2011 at 11:46 AM, Tristan Sloughter
>>> >> <> wrote:
>>> >> > OK, so maybe it helps if I wrote the fun correctly :). I fixed it to
>>> >> > fun()
>>> >> > -> ?MACHINE end. and I get:
>>> >> > {error,{1,erl_parse,["syntax error before: ","'fun'"]}}
>>> >> >
>>> >> > On Sun, Mar 6, 2011 at 11:42 AM, Tristan Sloughter
>>> >> > <> wrote:
>>> >> >>
>>> >> >> Hey, this is great! But the result it gives me fails when sent to
>>> >> >> erl_parse:
>>> >> >> > {done,{ok,Tokens2,LineEnd2},StringRest2} = erl_scan:tokens([],
>>> "fun()
>>> >> >> > ->
>>> >> >> > ?MACHINE. ", 1).
>>> >> >> {done,{ok,[{'fun',1},
>>> >> >>            {'(',1},
>>> >> >>            {')',1},
>>> >> >>            {'->',1},
>>> >> >>            {'?',1},
>>> >> >>            {var,1,'MACHINE'},
>>> >> >>            {dot,1}],
>>> >> >>           1},
>>> >> >>       []}
>>> >> >> >  {ok, NewTokens2} = aleppo:process_tokens(Tokens2).
>>> >> >> >
>>> >> >> {ok,[{'fun',1},
>>> >> >>      {'(',1},
>>> >> >>      {')',1},
>>> >> >>      {'->',1},
>>> >> >>      {atom,1,'BEAM'},
>>> >> >>      {dot,1},
>>> >> >>      {eof,0}]}
>>> >> >> > erl_parse:parse_form(NewTokens2).
>>> >> >> {error,{1,erl_parse,["syntax error before: ","'fun'"]}}
>>> >> >> Any ideas?
>>> >> >> Thanks.
>>> >> >> On Sun, Mar 6, 2011 at 11:27 AM, Evan Miller <>
>>> >> >> wrote:
>>> >> >>>
>>> >> >>> Hi Tristan,
>>> >> >>>
>>> >> >>> Aleppo will apply preprocessor macros on tokens returned by
>>> erl_scan:
>>> >> >>>
>>> >> >>> https://github.com/evanmiller/aleppo
>>> >> >>>
>>> >> >>> I think it will fit your needs. Aleppo also lets you use -ifdef
>>> and
>>> >> >>> the like inside of functions since it uses a proper grammar for
>>> the
>>> >> >>> macro syntax.
>>> >> >>>
>>> >> >>> On Sun, Mar 6, 2011 at 9:41 AM, Tristan Sloughter
>>> >> >>> <> wrote:
>>> >> >>> > I'd like to construct a parse transform that has a macro in the
>>> >> >>> > inserted
>>> >> >>> > code. I've had no problem using erl_scan:tokens and
>>> >> >>> > erl_parse:parse_form to
>>> >> >>> > construct the AST as long as no macros exist.
>>> >> >>> >
>>> >> >>> > Since macros are expanded before getting to this point it fails
>>> if
>>> >> >>> > in
>>> >> >>> > the
>>> >> >>> > string scanned has something like ?MODULE. I looked at
>>> epp_dodger
>>> >> >>> > and
>>> >> >>> > elsewhere but I can't seem to find something I could use to send
>>> in
>>> >> >>> > a
>>> >> >>> > string
>>> >> >>> > like:
>>> >> >>> >
>>> >> >>> > "init() -> ?MODULE."
>>> >> >>> >
>>> >> >>> > And have tokens I could use to then generate the AST.
>>> >> >>> >
>>> >> >>> > Is this possible?
>>> >> >>> >
>>> >> >>> > Thanks,
>>> >> >>> > Tristan
>>> >> >>> >
>>> >> >>>
>>> >> >>>
>>> >> >>>
>>> >> >>> --
>>> >> >>> Evan Miller
>>> >> >>> http://www.evanmiller.org/
>>> >> >>
>>> >> >
>>> >> >
>>> >>
>>> >>
>>> >>
>>> >> --
>>> >> Evan Miller
>>> >> http://www.evanmiller.org/
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Evan Miller
>>> http://www.evanmiller.org/
>>>
>>
>>
>


More information about the erlang-questions mailing list