1 (in-package :alexandria
)
3 (defmacro with-gensyms
(names &body forms
)
4 "Binds each variable named by a symbol in NAMES to a unique symbol around
5 FORMS. Each of NAMES must either be either a symbol, or of the form:
7 (symbol string-designator)
9 Bare symbols appearing in NAMES are equivalent to:
13 The string-designator is used as the argument to GENSYM when constructing the
14 unique symbol the named variable will be bound to."
15 `(let ,(mapcar (lambda (name)
16 (multiple-value-bind (symbol string
)
19 (values name
(symbol-name name
)))
20 ((cons symbol
(cons string-designator null
))
21 (values (first name
) (string (second name
)))))
22 `(,symbol
(gensym ,string
))))
26 (defmacro with-unique-names
(names &body forms
)
27 "Alias for WITH-GENSYMS."
28 `(with-gensyms ,names
,@forms
))
30 (defmacro once-only
(specs &body forms
)
31 "Evaluates FORMS with symbols specified in SPECS rebound to temporary
32 variables, ensuring that each initform is evaluated only once.
34 Each of SPECS must either be a symbol naming the variable to be rebound, or of
39 Bare symbols in SPECS are equivalent to
45 (defmacro cons1 (x) (once-only (x) `(cons ,x ,x)))
46 (let ((y 0)) (cons1 (incf y))) => (1 . 1)
48 (let ((gensyms (make-gensym-list (length specs
) "ONCE-ONLY"))
49 (names-and-forms (mapcar (lambda (spec)
52 (destructuring-bind (name form
) spec
58 `(let ,(mapcar (lambda (g n
) (list g
`(gensym ,(string (car n
)))))
59 gensyms names-and-forms
)
60 ;; bind in final expansion
61 `(let (,,@(mapcar (lambda (g n
)
63 gensyms names-and-forms
))
65 ,(let ,(mapcar (lambda (n g
) (list (car n
) g
))
66 names-and-forms gensyms
)
69 (defun parse-body (body &key documentation whole
)
70 "Parses BODY into (values remaining-forms declarations doc-string).
71 Documentation strings are recognized only if DOCUMENTATION is true.
72 Syntax errors in body are signalled and WHOLE is used in the signal
73 arguments when given."
79 (setf current
(car body
))
80 (when (and documentation
(stringp current
) (cdr body
))
82 (error "Too many documentation strings in ~S." (or whole body
))
83 (setf doc
(pop body
)))
85 (when (and (listp current
) (eql (first current
) 'declare
))
86 (push (pop body
) decls
)
88 (values body
(nreverse decls
) doc
)))
90 (defun parse-ordinary-lambda-list (lambda-list &key
(normalize t
)
92 (normalize-optional normalize
)
93 (normalize-keyword normalize
)
94 (normalize-auxilary normalize
))
95 "Parses an ordinary lambda-list, returning as multiple values:
97 1. Required parameters.
99 2. Optional parameter specifications, normalized into form:
101 (name init suppliedp)
103 3. Name of the rest parameter, or NIL.
105 4. Keyword parameter specifications, normalized into form:
107 ((keyword-name name) init suppliedp)
109 5. Boolean indicating &ALLOW-OTHER-KEYS presence.
111 6. &AUX parameter specifications, normalized into form
115 Signals a PROGRAM-ERROR is the lambda-list is malformed."
116 (let ((state :required
)
117 (allow-other-keys nil
)
125 (simple-program-error "Misplaced ~S in ordinary lambda-list:~% ~S"
127 (check-variable (elt what
&optional
(allow-specializers allow-specializers
))
128 (unless (and (or (symbolp elt
)
129 (and allow-specializers
130 (consp elt
) (= 2 (length elt
)) (symbolp (first elt
))))
131 (not (constantp elt
)))
132 (simple-program-error "Invalid ~A ~S in ordinary lambda-list:~% ~S"
133 what elt lambda-list
)))
134 (check-spec (spec what
)
135 (destructuring-bind (init suppliedp
) spec
136 (declare (ignore init
))
137 (check-variable suppliedp what nil
))))
138 (dolist (elt lambda-list
)
141 (if (eq state
:required
)
145 (if (member state
'(:required
&optional
))
149 (if (member state
'(:required
&optional
:after-rest
))
154 (setf allow-other-keys t
158 (cond ((eq state
'&rest
)
161 (simple-program-error "Multiple ~S in ordinary lambda-list:~% ~S"
168 (when (member elt
'#.
(set-difference lambda-list-keywords
169 '(&optional
&rest
&key
&allow-other-keys
&aux
)))
170 (simple-program-error
171 "Bad lambda-list keyword ~S in ordinary lambda-list:~% ~S"
175 (check-variable elt
"required parameter")
179 (destructuring-bind (name &rest tail
) elt
180 (check-variable name
"optional parameter")
182 (check-spec tail
"optional-supplied-p parameter"))
184 (setf elt
(append elt
'(nil)))))))
186 (check-variable elt
"optional parameter")
187 (when normalize-optional
188 (setf elt
(cons elt
'(nil nil
))))))
189 (push (ensure-list elt
) optional
))
191 (check-variable elt
"rest parameter")
196 (destructuring-bind (var-or-kv &rest tail
) elt
197 (cond ((consp var-or-kv
)
198 (destructuring-bind (keyword var
) var-or-kv
199 (unless (symbolp keyword
)
200 (simple-program-error "Invalid keyword name ~S in ordinary ~
202 keyword lambda-list
))
203 (check-variable var
"keyword parameter")))
205 (check-variable var-or-kv
"keyword parameter")
206 (when normalize-keyword
207 (setf var-or-kv
(list (make-keyword var-or-kv
) var-or-kv
)))))
209 (check-spec tail
"keyword-supplied-p parameter")
210 (when normalize-keyword
211 (setf tail
(append tail
'(nil)))))
212 (setf elt
(cons var-or-kv tail
))))
214 (check-variable elt
"keyword parameter")
215 (setf elt
(if normalize-keyword
216 (list (list (make-keyword elt
) elt
) nil nil
)
221 (destructuring-bind (var &optional init
) elt
222 (declare (ignore init
))
223 (check-variable var
"&aux parameter"))
225 (check-variable elt
"&aux parameter")
226 (setf elt
(list* elt
(when normalize-auxilary
230 (simple-program-error "Invalid ordinary lambda-list:~% ~S" lambda-list
)))))))
231 (values (nreverse required
) (nreverse optional
) rest
(nreverse keys
)
232 allow-other-keys
(nreverse aux
))))