my $str = q :to/EOF/;
Perl6,Rust
-- this is a comment
Rakudo,Raku
-- this is another comment
Camelia,Camel
EOF
grammar MakeEmpty {
token TOP { ^ <sentence>+ % <comment> $}
token sentence { <words>+ % ',' \n }
token comment { '-- ' <words>+ % ' ' \n }
token words { \w+ }
}
class Action {
method TOP($/) { make $/.values».ast }
method sentence($/) { make ~$/.trim }
method comment($/) { make Empty }
method words($/) { make ~$/ }
}
my $parsed = MakeEmpty.parse($str, :actions(Action)).ast;
.say for @$parsed;
# Perl6,Rust
# Rakudo,Raku
# Camelia,Camel
say '-' x 25;
# ----------------------------------------------------------
my $string = q :to/EOF/;
Perl6,Rust
-- this is a comment
Rakudo,Raku
-- this is another comment
Camelia,Camel
/* this is inline comments */
Java,Nim
Python,PHP
EOF
grammar WhiteSpace {
rule TOP { ^ <sentence>+ % <.ws> $ }
token sentence { <words>+ % ',' \n }
token comment { '-- ' <words>+ % ' ' \n }
token words { \w+ }
token ws { \s* | <comment> | <slash-star-comment> }
token slash-star-comment { \s* '/*' .*? '*/' \s* }
}
class SpaceAction {
method TOP($/) { make $/.values».ast }
method sentence($/) { make ~$/.trim }
method comment($/) { make ~$/ }
method words($/) { make ~$/ }
method slash-star-comment($/) { make ~$/ }
method ws($/) { make Empty }
}
my $em = WhiteSpace.parse($string, :actions(SpaceAction)).ast;
.say for @$em;
# Perl6,Rust
# Rakudo,Raku
# Camelia,Camel
# Java,Nim
# Python,PHP
one more example
#use Grammar::Tracer;
#use Grammar::Debugger;
my $excerpt = q:to/END/;
Here's some unimportant text.
=begin code
This code block is what we're after.
We'll use 'ff' to get it.
=end code
More unimportant text.
=begin code
I want this line.
and this line as well.
HaHa
=end code
More unimport text.
=begin code
Let's to go home.
=end code
END
grammar ExtractSection {
rule TOP { ^ <line>+ % <.ws> $ }
token line { <?!before <comment>> \N+ \n }
token ws { \s* | <comment> }
token comment { ['=begin code' | '=end code' ] \n }
}
class ExtractSectionAction {
method TOP($/) { make $/.values».ast }
method line($/) { make $/.trim }
method ws($/) { make Empty }
method comment($/) { make ~$/ }
}
my $em = ExtractSection.parse($excerpt, :actions(ExtractSectionAction)).ast;
.say for @$em;
one more more thing
分段:
#use Grammar::Tracer;
#use Grammar::Debugger;
my $excerpt = q:to/END/;
Here's some unimportant text.
=begin code
This code block is what we're after.
We'll use 'ff' to get it.
=end code
More unimportant text.
=begin code
I want this line.
and this line as well.
HaHa
=end code
More unimport text.
=begin code
Let's to go home.
=end code
END
grammar ExtractSection {
rule TOP { ^ <section>+ %% <.comment> $ }
token section { <line>+ % <.ws> }
token line { <?!before <comment>> \N+ \n }
token comment { ['=begin code' | '=end code' ] \n }
}
class ExtractSectionAction {
method TOP($/) { make $/.values».ast }
method section($/) { make ~$/.trim }
method line($/) { make ~$/.trim }
method comment($/) { make ~$/ }
}
my $em = ExtractSection.parse($excerpt, :actions(ExtractSectionAction)).ast;
for @$em -> $line {
say $line;
say '-' x 15;
}