lambda-list-parsing.lisp 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
;;; -*- Mode: Lisp -*-

;;; lambda-list-parsing.lisp --

;;;; See file COPYING for copyright and licensing information.

(in-package "UNIFY")


(declaim (inline type-is-t-p))

(defun type-is-t-p (type-spec)
  (nth-value 0 (and (subtypep type-spec t) (subtypep t type-spec))))


(declaim (inline type-specifier-p))

(defun type-specifier-p (x)
  (nth-value 0 (ignore-errors (subtypep x t))))

(defun normalize-lambda-list (lambda-list)
  (mapcar (lambda (lambda-element)
            (etypecase lambda-element
              (symbol lambda-element)
              (cons (first lambda-element))))
          lambda-list))


(defstruct lambda-var-info
  (name nil :read-only t)
  (type t :read-only t)
  )

(defstruct (optional-lambda-var-info (:include lambda-var-info))
  (default-value nil :read-only t)
  (name-for-supplied-indicator nil :read-only t)
  )

(defstruct (aux-lambda-var-info (:include optional-lambda-var-info)))

(defstruct (key-lambda-var-info (:include optional-lambda-var-info))
  (keyword-name nil :read-only t)
  )

(defstruct (rest-lambda-var-info (:include lambda-var-info)
                                 (:constructor make-rest-lambda-var-info (&key
                                                                          name
                                                                          (type 'list)
                                                                          (element-type t))))
  (element-type t :read-only t)
  )

;;; The next function is really implementation-dependent, give the
;;; definition of LAMBDA-LIST-KEYWORDS.


(define-condition lambda-list-parsing-error (program-error)
  ((item :reader lambda-list-parsing-error-item
         :initarg :item)
   )
  (:report (lambda (llpe stream)
             (format stream "Error while parsing an extended lambda-list (at ~S.)"
                     (lambda-list-parsing-error-item llpe))))
  )


(defun symbol-or-cons-p (x)
  (or (symbolp x) (consp x)))


(defun parse-extended-ordinary-lambda-list (lambda-list
                                            &key
                                            (ordinary-variable-test #'symbolp)
                                            (optional-variable-test #'symbol-or-cons-p)
                                            (rest-variable-test #'symbolp)
                                            (key-variable-test #'symbol-or-cons-p)
                                            (aux-variable-test #'symbol-or-cons-p)
                                            )
  (let ((vars ())
        (optionals ())
        (keywords ())
        (rest ())
        (auxiliary ())
        )
    (labels ((parse-named-arguments (lambda-list &aux (head (first lambda-list)))
               (cond ((null lambda-list) nil)
                     ((and (symbolp head)
                           (member head lambda-list-keywords))
                      (case head
                        (&optional (parse-optional-arguments (rest lambda-list)))
                        (&key (parse-keyword-arguments (rest lambda-list)))
                        (&aux (parse-auxiliary-arguments (rest lambda-list)))
                        (&rest (parse-rest-arguments (rest lambda-list)))
                        (otherwise
                         (warn "Keyword ~A is implementation dependent.~@
                                The parsing may not work properly."
                               head)
                         (skip-until-next-lambda-list-keyword (rest lambda-list))
                         ))
                      )
                     ((funcall ordinary-variable-test head)
                      (push head vars)
                      (parse-named-arguments (rest lambda-list)))
                     (t (error 'lambda-list-parsing-error :item head))
                     ))

             (parse-optional-arguments (lambda-list &aux (head (first lambda-list)))
               (cond ((null lambda-list) nil)
                     ((and (symbolp head)
                           (member head lambda-list-keywords))
                      (case head
                        (&optional (error 'lambda-list-parsing-error :item head))
                        (&key (parse-keyword-arguments (rest lambda-list)))
                        (&aux (parse-auxiliary-arguments (rest lambda-list)))
                        (&rest (parse-rest-arguments (rest lambda-list)))
                        (otherwise
                         (warn "Keyword ~A is implementation dependent.~@
                                The parsing may not work properly."
                               head)
                         (skip-until-next-lambda-list-keyword (rest lambda-list))
                         ))
                      )
                     ((funcall optional-variable-test head)
                      (push head optionals)
                      (parse-optional-arguments (rest lambda-list)))
                     (t (error 'lambda-list-parsing-error :item head))
                     ))

             (parse-keyword-arguments (lambda-list &aux (head (first lambda-list)))
               (cond ((null lambda-list) nil)
                     ((and (symbolp head)
                           (member head lambda-list-keywords))
                      (case head
                        (&optional (error 'lambda-list-parsing-error :item head))
                        (&key (error 'lambda-list-parsing-error :item head))
                        (&aux (parse-auxiliary-arguments (rest lambda-list)))
                        (&rest (parse-rest-arguments (rest lambda-list)))
                        (&allow-other-keys
                         (unless (or (null (rest lambda-list))
                                     (eql (cadr lambda-list) '&aux))
                           (error 'lambda-list-parsing-error :item head))
                         (skip-until-next-lambda-list-keyword (rest lambda-list)))
                        (otherwise
                         (warn "Keyword ~A is implementation dependent.~@
                                The parsing may not work properly."
                               head)
                         (skip-until-next-lambda-list-keyword (rest lambda-list))
                         ))
                      )
                     ((funcall key-variable-test head)
                      (push head keywords)
                      (parse-keyword-arguments (rest lambda-list)))
                     (t (error 'lambda-list-parsing-error :item head))
                     ))

             (parse-rest-arguments (lambda-list &aux (head (first lambda-list)))
               (cond ((null lambda-list) nil)
                     ((consp head)
                      (push head rest)
                      ;; Error checking here.
                      (parse-rest-arguments (rest lambda-list)))
                     ((and (symbolp head)
                           (member head lambda-list-keywords))
                      (case head
                        (&optional (error 'lambda-list-parsing-error :item head))
                        (&key (parse-keyword-arguments (rest lambda-list)))
                        (&aux (parse-auxiliary-arguments (rest lambda-list)))
                        (&rest (error 'lambda-list-parsing-error :item head))
                        (otherwise
                         (warn "Keyword ~A is implementation dependent.~@
                                The parsing may not work properly."
                               head)
                         (skip-until-next-lambda-list-keyword (rest lambda-list))
                         ))
                      )
                     ((funcall rest-variable-test head)
                      (push head rest)
                      (parse-rest-arguments (rest lambda-list)))
                     (t (error 'lambda-list-parsing-error :item head))
                     ))

             (parse-auxiliary-arguments (lambda-list &aux (head (first lambda-list)))
               (cond ((null lambda-list) nil)
                     ((and (symbolp head)
                           (member head lambda-list-keywords))
                      (case head
                        (&optional (error 'lambda-list-parsing-error :item head))
                        (&key (error 'lambda-list-parsing-error :item head))
                        (&aux (error 'lambda-list-parsing-error :item head))
                        (&rest (error 'lambda-list-parsing-error :item head))
                        (otherwise
                         (warn "Keyword ~A is implementation dependent.~@
                                The parsing may not work properly."
                               head)
                         (skip-until-next-lambda-list-keyword (rest lambda-list))
                         ))
                      )
                     ((funcall aux-variable-test head)
                      (push head auxiliary)
                      (parse-auxiliary-arguments (rest lambda-list)))
                     (t (error 'lambda-list-parsing-error :item head))
                     ))

             (skip-until-next-lambda-list-keyword (lambda-list
                                                   &aux (head (first lambda-list)))
               (cond ((null lambda-list) nil)
                     ((and (symbolp head)
                           (member head lambda-list-keywords))
                      (case head
                        (&optional (parse-optional-arguments (rest lambda-list)))
                        (&key (parse-keyword-arguments (rest lambda-list)))
                        (&aux (parse-auxiliary-arguments (rest lambda-list)))
                        (&rest (parse-rest-arguments (rest lambda-list)))
                        (otherwise
                         (warn "Keyword ~A is implementation dependent.~@
                                The parsing may not work properly."
                               head)
                         (skip-until-next-lambda-list-keyword (rest lambda-list))
                         ))
                      )
                     ((symbol-or-cons-p head)
                      (skip-until-next-lambda-list-keyword (rest lambda-list)))
                     ))
             )
      (parse-named-arguments lambda-list)
      (values (nreverse vars)
              (nreverse optionals)
              (nreverse keywords)
              (nreverse rest)
              (nreverse auxiliary))
      )))


(defun parse-var-type-info (var)
  (etypecase var
    (symbol (make-lambda-var-info :name var))
    (cons  (make-lambda-var-info :name (first var) :type (second var)))))


(defun parse-optional-var-type-info (var)
  (etypecase var
    (symbol (make-optional-lambda-var-info :name var))
    (cons (etypecase (first var)
            (symbol (if (second var) ; special case (foo nil) and (foo)
                        (make-optional-lambda-var-info
                         :name (first var)
                         :type (or (and (constantp (second var)) (type-of (second var)))
                                   t))
                        (make-optional-lambda-var-info :name (first var))))
            (cons (make-optional-lambda-var-info
                   :name (caar var)
                   :type (cadar var)
                   :default-value (cadr var)))
            ))
    ))


(defun parse-auxiliary-var-type-info (var)
  (parse-optional-var-type-info var))

(defun parse-rest-var-type-info (var) ; See the FUNCTION type ANSI spec for an explanation.
  (etypecase var
    (symbol (make-rest-lambda-var-info :name var))
    (cons   (make-rest-lambda-var-info :name (first var) :element-type (second var)))))


(defun parse-key-var-type-info (var)
  (etypecase var
    (symbol (make-key-lambda-var-info :name var))
    (cons (destructuring-bind (var &optional (init-value nil init-value-supplied-p))
              var
            (let ((init-value-type
                   (if init-value-supplied-p
                       (or (and (constantp init-value) (type-of init-value))
                           t)
                       t))
                  )
              (etypecase var
                (symbol (make-key-lambda-var-info :name var
                                                  :type init-value-type
                                                  :default-value init-value))

                (cons (destructuring-bind (kwd var)
                          var
                        (etypecase var
                          (symbol
                           (make-key-lambda-var-info :name var
                                                     :default-value init-value
                                                     :type init-value-type
                                                     :keyword-name kwd))
                          (cons
                           (make-key-lambda-var-info :name (first var)
                                                     :default-value init-value
                                                     :type (second var)
                                                     :keyword-name kwd))))
                      ))
              )))
    ))


;;; end of file -- lambda-list-parsing.lisp --