Skip to content

Commit a83fd91

Browse files
committed
Add summary to RFC 145 article
1 parent f2d5649 commit a83fd91

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

20th/articles/rfc145.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ for us if there had been multiple options for the opening one.
8686

8787
To compute the closing brace, it would suffice to have a way to capture
8888
the opening brace and pass it to a function whose return value is dynamically
89-
interpolated into the regex. This is now easy in Raku [regexes] and
90-
in particular `Grammar`s:
89+
interpolated into the regex. This is now easy in Raku [regexes] and [grammars]:
9190

9291
``` raku
9392
grammar Formula {
@@ -142,11 +141,19 @@ Braces: ( * ) ||| Subexpr: [b - (a + 1)] * 7
142141
[the tilde]: https://docs.raku.org/language/regexes#index-entry-regex__tilde-_regex__~-Tilde_for_nesting_structures
143142
[response]: https://raku.org/archive/doc/design/apo/A05.html#RFC%20145:%20Brace-matching%20for%20Perl%20Regular%20Expressions
144143
[regexes]: https://docs.raku.org/language/regexes
144+
[grammars]: https://docs.raku.org/language/grammars
145145
[longest-token matching]: https://docs.raku.org/language/regexes#Quoted_lists_are_LTM_matches
146146

147-
## Encore!
147+
## Summary
148+
149+
In summary, brace matching is obviously useful in parsing structured data.
150+
It was proposed by Eric Roode to make this simple in Perl 6 / Raku. Although
151+
the feature was not implemented in the proposed form, the task has indeed
152+
become easier to accomplish and the code much easier to read, notably due
153+
to the new regex syntax and grammar support.
154+
155+
### Encore!
148156

149-
That would be it for the main dish, but here is the dessert.
150157
If, like me, you are slightly bothered by the static brace table but are fine
151158
with heuristics, then the Unicode Consortium may be an unexpected ally. The
152159
Unicode `Bidi_Mirroring_Glyph` property gives hints about bidirectional
@@ -156,15 +163,15 @@ Raku has built-in support for Unicode properties and we can use this one
156163
to let the Unicode Consortium pick closing braces for us:
157164

158165
``` raku
159-
sub unicode-reverse ($_) {
166+
sub unicode-mirror ($_) {
160167
join '', .comb.reverse.map: {
161168
.uniprop('Bidi_Mirroring_Glyph')
162169
or .self
163170
}
164171
}
165172

166173
token closing ($opening) {
167-
"{ unicode-reverse($opening) }"
174+
"{ unicode-mirror($opening) }"
168175
}
169176

170177
regex braced {
@@ -174,7 +181,7 @@ to let the Unicode Consortium pick closing braces for us:
174181
}
175182
```
176183

177-
The `&unicode-reverse` heuristic splits the argument into characters,
184+
The `&unicode-mirror` heuristic splits the argument into characters,
178185
reverses their order and then either picks its mirroring glyph, if one
179186
is defined, or leaves the character as-is, then reassembles them into
180187
a string. This function successfully turns `<{` into `}>`, for example.
@@ -184,8 +191,8 @@ and punctuation as opening braces now and it has been turned into a
184191
`regex` for full backtracking power when it is too greedy in consuming
185192
opening braces.
186193

187-
With these tweaks, we can go nuts and have the grammar do the brace
188-
matching analogue of free association:
194+
With these tweaks, we can go nuts and have the grammar do free association
195+
and match everything that "looks like a brace pair":
189196

190197
``` console
191198
-- ([b - (a + 1)] * 7) ---------------------------------------------------------

0 commit comments

Comments
 (0)