Fare-quasiquote — Quasiquote Patterns for Trivia
Table of Contents
Definition
fare-quasiquote is a Common Lisp library by François-René Rideau that extends
the standard quasiquote syntax to act as a structural pattern in trivia (and
other pattern-matching libraries). It enables patterns of the form `(operator
,var1 ,var2) to match list structure and bind sub-expressions.
Online documentation:
How It Is Used in Tolk
Without fare-quasiquote, trivia cannot use quasiquote patterns. With it,
the (in-readtable :fare-quasiquote) declaration enables the extended reader,
and the patterns `(+ ,l ,r) become valid:
(named-readtables:in-readtable :fare-quasiquote)
(ematch sexp
(`(+ ,l ,r) (make-instance 'plus :l (parse l) :r (parse r)))
(`(* ,l ,r) (make-instance 'mult :l (parse l) :r (parse r))))
The pattern `(+ ,l ,r) matches a three-element proper list whose:
- first element is the symbol
+ - second element is bound to
l - third element is bound to
r
Nested patterns work: `(+ (* ,a ,b) ,c) matches (+ (* 1 2) 3) and
binds a, b, c.
Activation
fare-quasiquote works through named-readtables: the package header
declares the readtable, and every file that uses quasiquote patterns needs:
(named-readtables:in-readtable :fare-quasiquote)
tolk.asd lists :trivia.quasiquote as a dependency, which loads
fare-quasiquote automatically.
Related
- Trivia — the pattern-matching library that fare-quasiquote extends
- Named-readtables — the readtable mechanism that activates fare-quasiquote
- Tolk — uses fare-quasiquote for parsing