Course of Raku / Regexes and grammars / Grammars / Action classes / Exercises / An upper-case action
Solution: An upper-case action
Here is a possible solution to the task.
Code
grammar WordG {
token TOP { <word> }
token word { \w+ }
}
class UpcaseAction {
method TOP($/) { make $<word>.Str.uc }
}
say WordG.parse('hello', actions => UpcaseAction.new).made;🦋 You can find the source code in the file upcase-action.raku.
Output
HELLOComments
The grammar captures the word; the action class decides what to do with it.
$<word>.Strgives the matched text and.ucupper-cases it, and that is whatmakestores on the match.