[erlang-questions] Reading comments/annotations inside parse_transforms

Tim Watson <>
Thu Jul 5 11:10:03 CEST 2012


There doesn't appear to be any way of doing this currently. There is
support in erl_syntax for working with these and there is also an
erl_comment_scan module in the syntax-tools application which parses
and returns comments from a file (or whatever). What there doesn't
appear to be, is a means to get erl_parse to return comments as
attributes (?) in the AST that the compiler passes to a
parse_transform.

My question then, is this: what is the best (only!?) way to process
comments at the same time as source code in a parse_transform? So far,
given that this seems unlikely to work OOTB, I'm assuming I'll have to
do something like:

1. once you hit the 'file' attribute, parse the comments and stash
these away in a useful place
2. process the forms as usual, comparing each line number for each
form, against the line numbers stored for the comments
3. when you get an interleaving of code and comments, you know where
the comments reside in the source file in relation to the form you're
currently working with

I *can* do this of course, but it seems like an awful lot of hard
work. Why can't comments be preserved in the AST passed to a parse
transform, and then just dropped later on!? Is there an easier way of
doing this that I'm currently missing?

Cheers,
Tim



More information about the erlang-questions mailing list