@@ -86,8 +86,7 @@ for us if there had been multiple options for the opening one.
86
86
87
87
To compute the closing brace, it would suffice to have a way to capture
88
88
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] :
91
90
92
91
``` raku
93
92
grammar Formula {
@@ -142,11 +141,19 @@ Braces: ( * ) ||| Subexpr: [b - (a + 1)] * 7
142
141
[ the tilde ] : https://docs.raku.org/language/regexes#index-entry-regex__tilde-_regex__~-Tilde_for_nesting_structures
143
142
[ response ] : https://raku.org/archive/doc/design/apo/A05.html#RFC%20145:%20Brace-matching%20for%20Perl%20Regular%20Expressions
144
143
[ regexes ] : https://docs.raku.org/language/regexes
144
+ [ grammars ] : https://docs.raku.org/language/grammars
145
145
[ longest-token matching ] : https://docs.raku.org/language/regexes#Quoted_lists_are_LTM_matches
146
146
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!
148
156
149
- That would be it for the main dish, but here is the dessert.
150
157
If, like me, you are slightly bothered by the static brace table but are fine
151
158
with heuristics, then the Unicode Consortium may be an unexpected ally. The
152
159
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
156
163
to let the Unicode Consortium pick closing braces for us:
157
164
158
165
``` raku
159
- sub unicode-reverse ($ _ ) {
166
+ sub unicode-mirror ($ _ ) {
160
167
join ' ' , . comb . reverse . map : {
161
168
. uniprop (' Bidi_Mirroring_Glyph' )
162
169
or . self
163
170
}
164
171
}
165
172
166
173
token closing ($ opening ) {
167
- "{ unicode-reverse ($opening) }"
174
+ "{ unicode-mirror ($opening) }"
168
175
}
169
176
170
177
regex braced {
@@ -174,7 +181,7 @@ to let the Unicode Consortium pick closing braces for us:
174
181
}
175
182
```
176
183
177
- The ` &unicode-reverse ` heuristic splits the argument into characters,
184
+ The ` &unicode-mirror ` heuristic splits the argument into characters,
178
185
reverses their order and then either picks its mirroring glyph, if one
179
186
is defined, or leaves the character as-is, then reassembles them into
180
187
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
184
191
` regex ` for full backtracking power when it is too greedy in consuming
185
192
opening braces.
186
193
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" :
189
196
190
197
``` console
191
198
-- ([b - (a + 1)] * 7) ---------------------------------------------------------
0 commit comments