Extracting constants from a header file

Torben Hoffmann <>
Thu Jul 9 00:28:34 CEST 2009


Hi,

I would like to get a hold of all the
-define(CONSTANT,42).
constant definitions so I can output them for use in an external tool.

I have played with epp_dodger, erl_syntax_lib et al, but I have not found an
easy way of doing it.

Right now the following approach seems to be what I must do:

   1. Get the AST using epp:dodger:parse_file/1
   2. Filter all the elements from the AST where
   erl_syntax_lib:analyze_attribute/1 returns preprocessor
   3. Take the subtrees of the each element from the step above (using
   erl_syntaxt_lib:subtrees/1) and then use the fact that
   1. the first subtree is a list that contains {atom,_,define} and
      2.  the second subtree is a list consisting of {var,_,'PDU_TYPE'} and
      {integer,_,0} (i.e., the constant's name and type+value)

Is this the one and only way of doing it?
Not that I would mind terribly to code the above, but it seems like
something that ought to be easier and/or have been solved by someone else
before me.

Cheers,
Torben
-- 
http://www.linkedin.com/in/torbenhoffmann


More information about the erlang-questions mailing list