Course of Raku / Regexes and grammars / Grammars / The parse tree, make and made

make and made

Instead of digging values out of the tree afterwards, you can attach a value to a match while parsing. Inside a token, a code block { … } runs when that token matches, and the function make stores a value on the current match:

grammar OneNum {
    token TOP    { <number> { make $<number>.Int } }
    token number { \d+ }
}

When TOP matches, the block runs and make $<number>.Int stores the integer on the match. You read it back afterwards with made (or its alias .ast):

say OneNum.parse('42').made; # 42

The value is a real Int, not text, so you can compute with it directly:

say OneNum.parse('42').made + 1; # 43

The stored value can be anything — a number, a string, an array, an object. A token can also combine the values its sub-tokens made. For example, summing two captured numbers:

grammar Sum {
    token TOP { <a> '+' <b> { make $<a>.Int + $<b>.Int } }
    token a   { \d+ }
    token b   { \d+ }
}

say Sum.parse('2+3').made; # 5

make and made are the bridge from “it matched” to “here is the meaning”. Putting the blocks inline works, but it mixes the pattern with the logic; the next section moves that logic into a separate action class.

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Quiz — The match tree   |   Quiz — make and made


💪 Or jump directly to the exercises in this section.