#
Chapter 32
`plt-match.ss`: Pattern Matching

To load: `(require (lib "plt-match.ss"))`

This library provide a pattern matcher just like Chapter 25, but with an improved syntax for patterns. This pattern syntax uses keywords for each of the different pattern matches, making the syntax both extensible and more clear. It also provides extensions that are unavailable in `match.ss`.

The only difference between `plt-match.ss` and `match.ss` is the syntax of the patterns and the set of available patterns. The forms where the patterns may appear are identical.

Figure 2 gives the full syntax for patterns.

`

`pat``::=` `identifier` `[`not `ooo``]``` Match anything, bind `identifier` as a variable``
`|` _`` Match anything``
`|` `literal``` Match `literal```
`|` '`datum``` Match `equal?` `datum```
`|` '`symbol``` Match `equal?` `symbol` (special case of `datum`)``
`|` `(``list` `lvp` ``.``.```.``)``` Match sequence of `lvp`s``
`|` `(``list-rest` `lvp` ``.``.```.` `pat``)``` Match sequence of `lvp`s `cons`ed onto a `pat```
`|` `(``list-no-order` `pat` ``.``.```.` `lvp``)``` Match arguments in a list in any order``
`|` `(``vector` `lvp` ``.``.```.` `lvp``)``` Match vector of `pat`s``
`|` `(``hash-table` `(``pat` `pat``)` ``.``.```.``)``` Match hash table mapping `pat`s to `pat`s``
`|` `(``hash-table` `(``pat` `pat``)` ``.``.```.` `ooo``)``` Match hash table mapping `pat`s to `pat`s``
`|` `(``box` `pat``)``` Match boxed `pat```
`|` `(``struct` `struct-name` `(``pat` ``.``.```.``)``)``` Match `struct-name` instance with matching fields``
`|` `(``regexp` `rx-expr``)``` Match string using (`regexp-match` `rx-expr` `..`.)``
`|` `(``regexp` `rx-expr` `pat``)``` Match string to `rx-expr`, `pat` matches regexp result``
`|` `(``pregexp` `prx-expr``)``` Match string using (`pregexp-match` `prx-expr` `..`.)``
`|` `(``pregexp` `prx-expr` `pat``)``` Match string to `prx-expr`, `pat` matches pregexp result``
`|` `(``and` `pat` ``.``.```.``)``` Match when all `pat`s match``
`|` `(``or` `pat` ``.``.```.``)``` Match when any `pat` match``
`|` `(``not` `pat` ``.``.```.``)``` Match when no `pat` match``
`|` `(``app` `expr` `pat``)``` Match when result of applying `expr` matches `pat```
`|` `(``?` `expr` `pat` ``.``.```.``)``` Match if `expr` is true and all `pat`s match``
`|` `(``set!` `identifier``)``` Match anything, bind `identifier` as a setter``
`|` `(``get!` `identifier``)``` Match anything, bind `identifier` as a getter``
`|` ````qp``` Match a quasi`pat`tern``
`literal``::=` `(``)``` Match the empty list``
`|` #t`` Match true``
`|` #f`` Match false``
`|` `string``` Match `equal?` `string```
`|` `number``` Match `equal?` `number```
`|` `character``` Match `equal?` `character```
`lvp``::=` `pat` `ooo``` Greedily match `pat` instances``
`|` `pat``` Match `pat```
`ooo``::=` ``.``.``.```` Zero or more (where `...` is a keyword)``
`|` `___``` Zero or more``
`|` ``.``.```k``` `k` or more, where `k` is a non-negative integer``
`|` `__``k``` `k` or more, where `k` is a non-negative integer``
`qp``::=` `literal``` Match `literal```
`|` `identifier``` Match `equal?` `symbol```
`|` `(``qp` ``.``.```.``)``` Match sequences of `qp`s``
`|` `(``qp` ``.``.```.` `.` `qp``)``` Match sequence of `qp`s `cons`ed onto a `qp```
`|` `(``qp` ``.``.```.` `ooo``)``` Match `qp`s `cons`ed onto a repeated `qp```
`|` #`(``qp` ``.``.```.``)``` Match vector of `qp`s``
`|` #&`qp``` Match boxed `qp```
`|` ,`pat``` Match `pat```
`|` ,@`(``list` `lvp` ``.``.```.``)``` Match `lvp` sequence, spliced``
`|` ,@`(``list-rest` `lvp` ``.``.```.` `pat``)``` Match `lvp` sequence plus `pat`, spliced``
`|` ,@'`qp``` Match list-matching `qp`, spliced``

`

Figure 2: Pattern Syntax