[erlang-questions] erl_scan, erl_parse, macros and records
Fri Jun 17 00:26:36 CEST 2011
Have you tried smerl? http://code.google.com/p/smerl/
I used it for something very similar (dynamically generating email sorting
functions from a text file containing rules)
On Thu, Jun 16, 2011 at 4:19 PM, Dan Kelley <> wrote:
> On Thu, Jun 16, 2011 at 4:12 PM, Mihai Balea <> wrote:
>> On Jun 16, 2011, at 3:48 PM, Dan Kelley wrote:
>> > Hi,
>> > I'm working on a router of sorts. I'd like to have a config file which
>> specifies how to classify messages, what channels are available, and routing
>> rules based on classifications and channels.
>> > When my application starts, I'd like to transform the configuration into
>> a module (let's call it 'router') so I can do someting like
>> 'router:route(Message)'. Message will be a record that encapsulates a bunch
>> of things about the message, and the returned value will be the channel to
>> which the message should be delivered. The Message record is reasonably
>> complicated - the top level thing contains several layers of different
>> records. I also have a large number of macros that I use to extract
>> different fields/subrecords of a top-level Message.
>> > When I started looking at how to do this, I quickly found erl_scan,
>> erl_parse, and friends. I can easily create a module on the fly with these,
>> but everything falls apart when I need to get at the Message record and
>> macro definitions. Despite the access provided to compiler-ish things by
>> erl_scan, erl_parse and friends, it looks like the only way to get at the
>> preprocessor is to use epp on files. This is a problem because the code I
>> want to dynamically create needs to use the record definitions and macros
>> from the Message header file.
>> > Trying to figure a way around this led me to aleppo, which neatly solved
>> the problem of including the header file, but aleppo doesn't process
>> records. For a brief and exciting moment, it looked like erl_expand_records
>> might save the day, but it expects forms, and I can't get anything with
>> records through erl_parse:parse_form.
>> > Am I missing an easier way to dynamically create code that references
>> macros and records?
>> > Thanks,
>> Why not generate a temporary file and apply compile:file/2 on it?
> Two reasons:
> First, I can provide better error messages, in the sense that I'll have
> smaller compile units that I'll trace back to specific parts of the config.
> Second, I'm not sure that I can make that non-ugly. I had hoped to write
> my dynamic module to disk using erl_pp so it'd be human-readable.
> But yeah, that's my fallback position.
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions