Skip to content

Instantly share code, notes, and snippets.

@moritz
Created December 13, 2017 19:18
Show Gist options
  • Save moritz/7406bb757d01269345cc9eb4d6bd3dbc to your computer and use it in GitHub Desktop.
Save moritz/7406bb757d01269345cc9eb4d6bd3dbc to your computer and use it in GitHub Desktop.
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