6
votes

I found a tilde ~ in this Config::INI Perl 6 Grammar:

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

There are no tildes ~ in the text I am processing. I know that '[' ~ ']' is important because omitting any or all of '[', ~, and ']' makes the grammar no longer match my text.

Since I knew what the pattern was that I was matching, I changed it so that the square brackets were around the text expression, thus:

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

So it seems to me that '[' ~ ']' is really saying match a square bracket here and expect the closing bracket afterwards.

Anyway, I know that in normal Perl 6 syntax, the tilde ~ is used for concatenating strings. But this obviously means something different inside this Grammar. (In Perl 6, you can use Grammars to extract complicated data structures from text. They are like regular expressions taken to the next level.).

Anyway, I searched the documentation for Grammars and for Regular Expressions for a single ~, but I didn't find any inside a grammar nor inside a regular expression.


cross posted on StackOverflow en español

1
I keep forgetting that feature.Brad Gilbert

1 Answers

7
votes

You can find an explanation in the design documents here: https://github.com/perl6/roast/blob/master/S05-metachars/tilde.t#L6-L81

It mostly does what you discovered: replaces the tilde with the expression that follows the right bracket, and searches for it between the bracket characters. However, it adds some extra magic to help the expression recognize the terminating bracket and to provide a more useful error message if the final bracket isn't found. So you'll usually get the same results doing it either way, but not always.