Make Empty

make

my $str = q :to/EOF/;
Raku,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;

# Raku,Rust
# Rakudo,Raku
# Camelia,Camel

say '-' x 25;
# ----------------------------------------------------------

my $string = q :to/EOF/;
Raku,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;

# Raku,Rust
# Rakudo,Raku
# Camelia,Camel
# Java,Nim
# Python,PHP
ast 
comments powered by Disqus