make empty

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;
}

参考 https://stackoverflow.com/questions/42254754/how-to-skip-unrelated-lines-when-using-perl-6-grammar-to-parse-structurized-text

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容