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

HELLO

Comments

  1. The grammar captures the word; the action class decides what to do with it.

  2. $<word>.Str gives the matched text and .uc upper-cases it, and that is what make stores on the match.

Course navigation

An upper-case action   |   A multiply action