How to use user defined macros in Erlang preprocessor?

erlang

Solution

The epp:parse_file/3 function preprocesses and parses the Erlang source file. For preprocessing it needs all the macro definition. There can be 3 possibilities.

- The macro can be defined in the same file or the hrl file where it is defined is included with the complete path. In this case it automatically resolves the same.

- The macro can be defined in a hrl file. Then the directories of the include files can be specified in the 2nd argument. Ex: `epp:parse_file("test.erl", ["../include"], []).` It searches all the files in the directory and resolves it.

- The macro may not be defined (or you may not want it to search in the include directories). This will result in an error in the form. For example

`{error,{21,epp,{undefined,'YOURSERVER',none}}},`

In this case you can specify it in the parse_file function itself. For example

epp:parse_file("yaws.erl", [], [{'YOURSERVER',yourserver}]).

This will resolve the macro.

So if you have the macro in the source file, you do not have to send it. Only if it is not there in the source or in include (or dont want to specify the directory) then you can specify it in the function

Note: You can send even if you have it in the source file. But there will be a tuple `{error, redefine, 'YOURSERVER'}` in the abstract form. But it will override in all the places with the value sent in the function.

Edit:

From code analysis of epp I found that currently it is not possible to give arguments.The epp module cannot handle the complex macro types. Passing Function structure is not possible in the current way.

I have changed the epp file to handle this case. You can check this link if you are ok to have the changed epp file (only 3 lines added).

Problem

What format does `epp:parse_file/3` take predefined macros in? The docs are a bit lacking on this: ``` PredefMacros = macros() macros() = [{atom(), term()}] ``` I understand for a simple macro I can do this: ``` epp:parse_file("code.erl", [], [{DEBUG, 0}]). ``` But what about complex macros that take arguments? Say I have this macro: ``` -define(DEBUG(Arg1, Arg2), ((fun () -> io:format("~p ~p~n", [Arg1, Arg2]) end)())). ``` What do I need to pass as the third argument to `epp:parse_file`?

Original source