xcomment:
<skip: qr/[ \t]*/> newline(0..) '<!--'
{
($text,$return) = main::parse_delimited($text,'<!--','-+->');
$return = ['xcomment',$return];
}
####
comment:
newline(0..) '/*'
{
($text,$return) = main::parse_delimited($text,'/*','*/');
$return = ['comment',$return];
}
##
##
#!/usr/bin/perl -w
use strict;
use Parse::RecDescent;
use Text::DelimMatch;
$::RD_ERRORS = 1;
$::RD_WARN = 1;
$::RD_HINT = 1;
$::RD_TRACE = 1;
my $grammar = q{
{
sub parse_delimited {
my $text = shift;
my $startdelim = shift;
my $enddelim = shift;
my $mc = new Text::DelimMatch( $startdelim, $enddelim );
my ( $p, $m, $r ) = $mc->match( $text );
if ($p) {
$text = $p;
}
else {
$text = "";
}
$text .= $r if ($r);
$m =~ s/^$startdelim//;
$m =~ s/$enddelim$//;
return $text, $m;
}
}
file: line(s) eofile { use Data::Dumper 'Dumper'; print Dumper @item}
line:
comment
|
eofile: /^\Z/
comment:
'/*'
{
($text,$return) = $thisparser->parse_delimited($text, '/*', '*/');
$return = ['comment',$return];
}
};
my $parser = new Parse::RecDescent($grammar) or die "Bad grammar!\n";
while ()
{
chomp;
print "$_...\n";
defined($parser->file($_)) or print "Bad text!\n";
}
__DATA__
/*Hello
World
*/
##
##
Argument "/*" isn't numeric in addition (+) at C:/Perl/site/lib/Parse/RecDescent.pm line 2783, line 1.
Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl/site/lib/Parse/RecDescent.pm:2618] line 22, line 1.
Use of uninitialized value in concatenation (.) or string at (eval 15)[C:/Perl/site/lib/Parse/RecDescent.pm:2618] line 23, line 1.
Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl/site/lib/Parse/RecDescent.pm:2618] line 23, line 1.
Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl/site/lib/Parse/RecDescent.pm:2618] line 23, line 1.