Created
December 13, 2017 19:18
-
-
Save moritz/7406bb757d01269345cc9eb4d6bd3dbc to your computer and use it in GitHub Desktop.
A version of https://github.com/JJ/my-perl6-examples/blob/master/Enhanced-Paragraph.p6 that preserve whitespace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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