Skip to content

Commit

Permalink
Add summary to RFC 145 article
Browse files Browse the repository at this point in the history
  • Loading branch information
taboege committed Aug 1, 2020
1 parent f2d5649 commit a83fd91
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions 20th/articles/rfc145.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ for us if there had been multiple options for the opening one.

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

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

## Encore!
## Summary

In summary, brace matching is obviously useful in parsing structured data.
It was proposed by Eric Roode to make this simple in Perl 6 / Raku. Although
the feature was not implemented in the proposed form, the task has indeed
become easier to accomplish and the code much easier to read, notably due
to the new regex syntax and grammar support.

### Encore!

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

``` raku
sub unicode-reverse ($_) {
sub unicode-mirror ($_) {
join '', .comb.reverse.map: {
.uniprop('Bidi_Mirroring_Glyph')
or .self
}
}

token closing ($opening) {
"{ unicode-reverse($opening) }"
"{ unicode-mirror($opening) }"
}

regex braced {
Expand All @@ -174,7 +181,7 @@ to let the Unicode Consortium pick closing braces for us:
}
```

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

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

``` console
-- ([b - (a + 1)] * 7) ---------------------------------------------------------
Expand Down

1 comment on commit a83fd91

@JJ
Copy link
Contributor

@JJ JJ commented on a83fd91 Aug 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to upload it to the blog for you? I've sent you an invitation anyway.

Please sign in to comment.