Named captures give every matched part a label — no positional indices to break.
grammar DateParser {
token TOP { <year> '-' <month> '-' <day> }
token year { \d ** 4 }
token month { \d ** 2 }
token day { \d ** 2 }
}
Each featured card links to a working playground session. Edit, run, and experiment in your browser. See the Ecosystem page for many more real-world examples.
Full worked examples — click a title to open in the playground.
Named captures give every matched part a label — no positional indices to break.
grammar DateParser {
token TOP { <year> '-' <month> '-' <day> }
token year { \d ** 4 }
token month { \d ** 2 }
token day { \d ** 2 }
}
Separate structure from semantics — grammar defines shape, actions class defines meaning.
grammar Calculator {
token TOP { <calc-op> }
proto rule calc-op {*}
rule calc-op:sym<add> { <num> '+' <num> }
rule calc-op:sym<sub> { <num> '-' <num> }
rule calc-op:sym<mul> { <num> '*' <num> }
rule calc-op:sym<div> { <num> '/' <num> }
token num { \d+ }
}
An ecosystem module - parse contact records accurately - names, addresses, etc.
grammar Contact {
rule TOP { <name> <email> <phone>? }
rule name { <first> <last> }
token first { <[A..Z]> <[a..z]>+ }
token last { <[A..Z]> <[a..z]>+ }
token email { \S+ '@' \S+ '.' \S+ }
token phone { [\d+]+ % <[-.\s]> }
}
Parse structured URLs into named components — verb, subject, and data in one grammar.
grammar REST {
token TOP { <verb> '/' <subject> ['/' <data>]? }
token verb { get | post | put | delete }
token subject { \w+ }
token data { \w+ }
}
An ecosystem module — parse HTTP requests into method, target, version, and headers.
use Grammar::HTTP;
my $req = "GET / HTTP/1.1\r\nHost: www.raku.org\r\n\r\n";
my $m = Grammar::HTTP.parse($req);
say $m<request-line><method>; # 「GET」
say $m<request-line><target>; # 「/」
say $m<header>[0]<field-name>; # 「Host」
A full domain-specific language in one grammar — structured fields, items, and totals.
grammar Invoice {
token TOP { <head> \n <line>+ }
rule head { invoice <id> }
rule line { | date <date>
| client <quoted>
| item <quoted> <amount> }
token id { <[A..Za..z0..9_-]>+ }
token date { \d**4 '-' \d**2 '-' \d**2 }
token quoted { '"' <( <-["]>+ )> '"' }
token amount { \d+ ['.' \d+]? }
}
Shorter examples — each one isolates a specific grammar feature.
Grammar is a first-class language feature — no imports, no external library.
grammar WordParser {
token TOP { <word>+ % \s+ }
token word { <letter>+ }
token letter { <[a..zA..Z]> }
}
Dispatch on token variants — each sym candidate matches longest-token first.
grammar Expr {
token TOP { <expr>+ % \s+ }
proto token expr {*}
token expr:sym<number> { \d+ }
token expr:sym<ident> { <[a..z]>+ }
token expr:sym<operator> { <[+\-*\/]> }
}
Grammars are classes — extend and override tokens without touching the original.
grammar Base {
token TOP { <word>+ }
token word { <[a..z]>+ }
}
grammar Extended is Base {
token word { <[a..z]>+ | <[0..9]>+ }
}
The % operator matches a list with a delimiter — no manual glue or recursion needed.
grammar CSV {
token TOP { <row>+ % \n }
token row { <cell>+ % ',' }
token cell { <-[,\n]>* }
}
token ignores whitespace; rule inserts implicit \s* between atoms — choose deliberately.
grammar Decl {
rule TOP { let <ident> '=' <value> }
token ident { <[a..z]>+ }
token value { \d+ }
}
Match any Unicode script natively — no flags, no extra packages, no encoding surprises.
grammar NaturalText {
token TOP { <word>+ % \s+ }
token word { <:Letter>+ }
}