Skip to content

Instantly share code, notes, and snippets.

@moritz
Created Dec 13, 2017
Embed
What would you like to do?
grammar Enhanced-Paragraph {
token TOP { <superword>[ (\s+) <superword>]+ }
token superword { <word> | <enhanced-word> }
token word { \w+ }
token enhanced-word { \* <word> \* }
}
class Enhanced-Paragraph-actions {
method TOP ($/) { make [~] $/.caps».value.map({ .made // .Str }) }
method superword($/) { make $<enhanced-word>?? $<enhanced-word>.made !! $<word>.made }
method enhanced-word($/) { make "<em>"~$<word>.made~"</em>";}
method word($/) { make ~$/ }
}
my $paragraph = "This includes *two* *enhanced* words";
my $parsed = Enhanced-Paragraph.parse($paragraph, actions => Enhanced-Paragraph-actions.new);
say "\nParsed→\n", $parsed;
say "Actions: ", $parsed.made;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment