bang !some text! at @more words@ percent %good stuff% star !not in stars! #### #!/usr/bin/env perl6 use Test; our grammar StatGrammar { rule TOP { [ | | | | | | | ] } regex bang-line { ^ 'bang' \s+ '!' (.*?) '!' \n? $ } regex at-line { ^ 'at' \s+ '@' (.*?) '@' \n? $ } regex hash-line { ^ 'hash' \s+ '#' (.*?) '#' \n? $ } regex dollar-line { ^ 'dollar' \s+ '$' (.*?) '$' \n? $ } regex percent-line { ^ 'percent' \s+ '%' (.*?) '%' \n? $ } regex caret-line { ^ 'caret' \s+ '^' (.*?) '^' \n? $ } regex and-line { ^ 'and' \s+ '&' (.*?) '&' \n? $ } regex star-line { ^ 'star' \s+ '*' (.*?) '*' \n? $ } } our class StatActions { method TOP($/) { make $[0] if $; make $[0] if $; make $[0] if $; make $[0] if $; make $[0] if $; make $[0] if $; make $[0] if $; make $[0] if $; } } sub static(Str $str) { StatGrammar.parse($str, :actions(StatActions)).made } is static('bang !one!' ), 'one', 'parse bang!one ok'; isnt static('bang @one@' ), 'one', 'isnt bang@one ok'; nok StatGrammar.parse('bang @one@', :actions(StatActions)), 'nok'; #### #!/usr/bin/env perl6 use Test; our %sigils = ( bang => '!', at => '@', hash => '#', dollar => '$', percent => '%', caret => '^', and => '&', star => '*', ); our grammar DynGrammar { regex TOP { ^ (\w+) \s+ (.*?) \n? $ } } our class DynActions { method TOP($/) { if $1.match( rx/ ^ "{%sigils{$0}}" (.*?) "{%sigils{$0}}" $ / ) -> $match { make $match[0]; } } } sub dyn(Str $str) { DynGrammar.parse($str, :actions(DynActions)).made } is dyn('bang !one!' ), 'one', 'parse bang!one ok'; isnt dyn('bang @one@' ), 'one', 'isnt bang@one ok'; nok DynGrammar.parse('bang @one@', :actions(DynActions)), 'nok';