What does a tilde [~] mean within a Raku grammar [Grammar] (Perl 6)?


I found a tilde in this grammar (Grammar) from Raku's Config :: INI module :

token header { ^^ \h* '[' ~ ']' $<text>=<-[ \] \n ]>+ \h* <.eol>+ }

I don't have any tilde in the text that this pattern matches. I know that '[' ~ ']' is important because if I remove any (or two or all) of these three symbols, the pattern stops matching the text.

I changed the expression like this and it works:

token header   { ^^ \h* '[' $<text>=<-[ \] \n ]>+ ']' \h* <.eol>+ }

It seems to me that '[' ~ ']' means that what follows will be within a pair of [ and ] .

I know that in normal Raku syntax, the tilde ~ is used for concatenation, but this example is inside a grammar ('Grammar'). (In Raku, you can use grammar to extract structured data from text.) I looked up the documentation of grammars and regular expressions for a single ~ , but found none within a grammar or within a regular expression either.

"cross-posted" on Stack Overflow


Taken (and translated) from the Apocalypse 5 documentation (syntax for regexes) in the "New Metacharacters" section:

The ~ operator is an aid to matching nested sub-rules to a specific terminator. It is designed to be placed between opening and closing parentheses, like this:

 '(' ~ ')' <expression>

In that explanation there is also the link to the corresponding tests in the repository of the Perl6 test suite (roast).

Scroll to Top