how to handle comments alongside regular code

45 views
Skip to first unread message

haroldo...@gmail.com

unread,
Apr 17, 2016, 1:12:06 AM4/17/16
to marpa parser
hi, i'd like to write a parser to a DSL in which I have a lot of code written. There are comments, as usual. I believe the comments are a constituent part of the operational body. Should I put the comments nonterminal in each G1 rule , so that I can do things with them as I do things with the DSL lines ? Or should I parse twice , one with the comments, and one with the code, and then link them back somehow?

thanks!
Haroldo

Durand Jean-Damien

unread,
Jul 19, 2016, 4:05:08 PM7/19/16
to marpa parser, haroldo...@gmail.com
Usually comments are managed by a :discard rule, for example:

:discard ~ <Cplusplus style comment>
:discard ~ <C style comment>

##########################
# Discard of a C++ comment
##########################
<Cplusplus style comment> ~ '//' <Cplusplus comment interior>
<Cplusplus comment interior> ~ [^\n]*

############################################################################
# Discard of a C comment, c.f. https://gist.github.com/jeffreykegler/5015057
############################################################################
<C style comment> ~ '/*' <comment interior> '*/'
<comment interior> ~
    <optional non stars>
    <optional star prefixed segments>
    <optional pre final stars>
<optional non stars> ~ [^*]*
<optional star prefixed segments> ~ <star prefixed segment>*
<star prefixed segment> ~ <stars> [^/*] <optional star free text>
<stars> ~ [*]+
<optional star free text> ~ [^*]*
<optional pre final stars> ~ [*]*

Regards, Jean-Damien
Reply all
Reply to author
Forward
0 new messages