The example in random tip #16 does not explicitly spell out that you need to :
use Text::DelimMatch;
and in the grammar definition you need a rule :
newline: "\n"
With those two things in place the code works fine for parsing multi line HTML comments.
Parsing multi line C style comments is complicated by the fact that * is a regexp character so it needs escaping. I managed to get the following code to work OK based on technique outlined in the tip. I'm sure it could be done better but I was struggling with the escaping
# Function to cope with multiline comments
# Must be placed in main section of program
sub parse_multilinecomment
{
my $text = shift;
my $mc = new Text::DelimMatch( '\\/\\*', '\\*\\/' );
my ( $p, $m, $r ) = $mc->match( '/*' . $text );
if ($p) {
$text = $p;
}
else {
$text = "";
}
$text .= $r if ($r);
$m =~ s/^\/\*//;
$m =~ s/\*\/$//;
return $text, $m;
}
and the grammar rules :
newline: "\n"
multilinecomment:
<skip: qr/[ \t]*/> newline(0..) '/*'
{
($text,$return) = main::parse_multilinecomment($text);
print $return . "\n";
$return = ['xcomment',$return];
}
Successfully matches the following example :
/*
A multiple line
/*
with nested
*/
comment
*/
Hope this may help someone
Adrian
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|