make empty

作者: 焉知非鱼 | 来源:发表于2018-11-15 22:34 被阅读0次
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

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

相关文章

  • make empty

    参考 https://stackoverflow.com/questions/42254754/how-to-sk...

  • Swift Strings and Characters

    Initializing an Empty String To create an empty Stringval...

  • Empty

    我拿了一支笔,问小佛陀,这是什么?答,笔。 又问,如果一个小狗进来,小狗认为是什么?答,小狗玩具。 ...

  • empty

    我终于选择去做这份兼职 在那 没有人知道我是谁 我照样有很多东西要学 很多东西不懂 被人说 在那刹那 我发现其实我...

  • Empty

    Emacs

  • With empty

    关于双性恋最近开始意识到,这种现象普遍存在却被隐藏,你是否也对同性动心,是偏于幻想还是胜负心作怪

  • Empty

    明天就是女神节了。 昨天去图书馆拿书,发现箱子里还留着他去年女神节送我礼物时用的礼袋,上面写着“给囡囡的小确幸,祝...

  • empty

    empty

  • 年轻就该有年轻的姿态

    “An empty street,An empty house,A hole inside my heart,I’...

  • ArrayList

    构造方法 EMPTY_ELEMENTDATA 与 DEFAULTCAPACITY_EMPTY_ELEMENTDAT...

网友评论

    本文标题:make empty

    本文链接:https://www.haomeiwen.com/subject/aawufqtx.html