1 (in-package parenscript
)
2 ;;;; This software was taken from the SBCL system. there are very few
3 ;;;; changes, and one SBCL-specific thing left (sb-c::collect
5 ;;; if you have found this on google, THIS IS NOT AN SBCL SOURCE FILE
7 ;;; Break something like a lambda list (but not necessarily actually a
8 ;;; lambda list, e.g. the representation of argument types which is
9 ;;; used within an FTYPE specification) into its component parts. We
10 ;;; return twelve values:
11 ;;; 1. a list of the required args;
12 ;;; 2. a list of the &OPTIONAL arg specs;
13 ;;; 3. true if a &REST arg was specified;
15 ;;; 5. true if &KEY args are present;
16 ;;; 6. a list of the &KEY arg specs;
17 ;;; 7. true if &ALLOW-OTHER-KEYS was specified.;
18 ;;; 8. true if any &AUX is present (new in SBCL vs. CMU CL);
19 ;;; 9. a list of the &AUX specifiers;
20 ;;; 10. true if a &MORE arg was specified;
21 ;;; 11. the &MORE context var;
22 ;;; 12. the &MORE count var;
23 ;;; 13. true if any lambda list keyword is present (only for
24 ;;; PARSE-LAMBDA-LIST-LIKE-THING).
26 ;;; The top level lambda list syntax is checked for validity, but the
27 ;;; arg specifiers are just passed through untouched. If something is
28 ;;; wrong, we use COMPILER-ERROR, aborting compilation to the last
30 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
31 (defun collect-list-expander (n-value n-tail forms
)
32 (let ((n-res (gensym)))
34 ,@(mapcar (lambda (form)
35 `(let ((,n-res
(cons ,form nil
)))
37 (setf (cdr ,n-tail
) ,n-res
)
38 (setq ,n-tail
,n-res
))
40 (setq ,n-tail
,n-res
,n-value
,n-res
)))))
44 (defmacro collect
(collections &body body
)
47 (dolist (spec collections
)
48 ; (unless (proper-list-of-length-p spec 1 3)
49 ; (error "malformed collection specifier: ~S" spec))
50 (let* ((name (first spec
))
51 (default (second spec
))
52 (kind (or (third spec
) 'collect
))
53 (n-value (gensym (concatenate 'string
56 (push `(,n-value
,default
) binds
)
57 (if (eq kind
'collect
)
58 (let ((n-tail (gensym (concatenate 'string
62 (push `(,n-tail
(last ,n-value
)) binds
)
64 (push `(,name
(&rest args
)
65 (collect-list-expander ',n-value
',n-tail args
))
67 (push `(,name
(&rest args
)
68 (collect-normal-expander ',n-value
',kind args
))
70 `(macrolet ,macros
(let* ,(nreverse binds
) ,@body
))))
72 (defparameter *lambda-list-keywords
*
73 '(&allow-other-keys
&aux
&body
&environment
&key
&optional
&rest
&whole
))
75 (defun style-warn (&rest args
) (apply #'format t args
))
78 (defun parse-lambda-list-like-thing (list)
92 (declare (type (member :allow-other-keys
:aux
94 :more-context
:more-count
100 (if (member arg
*lambda-list-keywords
*)
103 (unless (eq state
:required
)
104 (format t
"misplaced &OPTIONAL in lambda list: ~S"
106 (setq state
:optional
))
108 (unless (member state
'(:required
:optional
))
109 (format t
"misplaced &REST in lambda list: ~S" list
))
112 (unless (member state
'(:required
:optional
))
113 (format t
"misplaced &MORE in lambda list: ~S" list
))
115 state
:more-context
))
117 (unless (member state
118 '(:required
:optional
:post-rest
:post-more
))
119 (format t
"misplaced &KEY in lambda list: ~S" list
))
123 "&OPTIONAL and &KEY found in the same lambda list: ~S" list
))
127 (unless (eq state
':key
)
128 (format t
"misplaced &ALLOW-OTHER-KEYS in ~
132 state
:allow-other-keys
))
134 (when (member state
'(:rest
:more-context
:more-count
))
135 (format t
"misplaced &AUX in lambda list: ~S" list
))
137 (format t
"multiple &AUX in lambda list: ~S" list
))
140 (t (format t
"unknown LAMBDA-LIST-KEYWORD in lambda list: ~S." arg
)))
143 (let ((name (symbol-name arg
)))
144 (when (and (plusp (length name
))
145 (char= (char name
0) #\
&))
147 "suspicious variable in lambda list: ~S." arg
))))
149 (:required
(required arg
))
150 (:optional
(optional arg
))
156 (setq more-context arg
164 (format t
"found garbage in lambda list when expecting ~
167 (when (eq state
:rest
)
168 (format t
"&REST without rest variable"))
170 (values (required) (optional) restp rest keyp
(keys) allowp auxp
(aux)
171 morep more-context more-count
172 (not (eq state
:required
))))))
174 ;;; like PARSE-LAMBDA-LIST-LIKE-THING, except our LAMBDA-LIST argument
175 ;;; really *is* a lambda list, not just a "lambda-list-like thing", so
176 ;;; can barf on things which're illegal as arguments in lambda lists
177 ;;; even if they could conceivably be legal in not-quite-a-lambda-list
179 (defun parse-lambda-list (lambda-list)
181 ;; Classify parameters without checking their validity individually.
182 (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
183 morep more-context more-count
)
184 (parse-lambda-list-like-thing lambda-list
)
186 ;; Check validity of parameters.
187 (flet ((need-symbol (x why
)
188 (unless (or (symbolp x
) t
)
189 (format t
"~A is not a symbol: ~S" why x
))))
191 (need-symbol i
"Required argument"))
196 (destructuring-bind (var &optional init-form supplied-p
) i
197 (declare (ignore init-form supplied-p
))
198 (need-symbol var
"&OPTIONAL parameter name")))
200 (format t
"&OPTIONAL parameter is not a symbol or cons: ~S"
203 (need-symbol rest
"&REST argument"))
209 (destructuring-bind (var-or-kv &optional init-form supplied-p
) i
210 (declare (ignore init-form supplied-p
))
211 (if (consp var-or-kv
)
212 (destructuring-bind (keyword-name var
) var-or-kv
213 (declare (ignore keyword-name
))
214 (need-symbol var
"&KEY parameter name"))
215 (need-symbol var-or-kv
"&KEY parameter name"))))
217 (format t
"&KEY parameter is not a symbol or cons: ~S"
221 (values required optional restp rest keyp keys allowp auxp aux
222 morep more-context more-count
)))