Function: clojure--parse-pattern

clojure--parse-pattern is a byte-compiled function defined in clojure-mode.el.

Signature

(clojure--parse-pattern FORM)

Documentation

Return the destructuring pattern FORM as a list, or nil.

That's (sym FORM), (vec FORM PATTERN...) or (map FORM UNIT...), each UNIT being a key of the map and its value:

  (keys KEY VEC ((NAME . FORM)...)) for :keys, :strs and :syms, NAME
    being nil for what doesn't bind a name
  (as KEY SYM NAME)
  (or KEY MAP ((NAME KEY DEFAULT)...))
  (pair KEY VAL NAME) for a symbol bound to the value of a key
  (nested KEY VAL PATTERN) for a map or a vector bound to it
  (other KEY VAL) for anything else.

Source Code

;; Defined in ~/.emacs.d/elpa/clojure-mode-20261103.1226/clojure-mode.el
(defun clojure--parse-pattern (form)
  "Return the destructuring pattern FORM as a list, or nil.
That's (sym FORM), (vec FORM PATTERN...) or (map FORM UNIT...), each
UNIT being a key of the map and its value:

  (keys KEY VEC ((NAME . FORM)...)) for :keys, :strs and :syms, NAME
    being nil for what doesn't bind a name
  (as KEY SYM NAME)
  (or KEY MAP ((NAME KEY DEFAULT)...))
  (pair KEY VAL NAME) for a symbol bound to the value of a key
  (nested KEY VAL PATTERN) for a map or a vector bound to it
  (other KEY VAL) for anything else."
  (pcase (clojure--form-type form)
    ('symbol (list 'sym form))
    ('vector
     (cons 'vec (cons form (delq nil (mapcar #'clojure--parse-pattern
                                             (clojure--form-values form))))))
    ((and (or 'map 'ns-map)
          (let pairs (clojure--form-pairs form))
          (guard pairs))
     (cons 'map
           (cons form
                 (mapcar
                  (pcase-lambda (`(,key ,val))
                    (let* ((type (clojure--form-type key))
                           (name (and (eq type 'keyword)
                                      (not (clojure--form-namespace key))
                                      (clojure--form-name key))))
                      (cond
                       ((null val) (list 'other key val))
                       ((and (eq type 'keyword)
                             (member (clojure--form-name key) '("keys" "strs" "syms"))
                             (eq (clojure--form-type val) 'vector))
                        (list 'keys key val
                              (mapcar (lambda (elem)
                                        (cons (and (memq (clojure--form-type elem)
                                                         '(symbol keyword))
                                                   (clojure--form-name elem))
                                              elem))
                                      (clojure--form-values val))))
                       ((and (equal name "as") (eq (clojure--form-type val) 'symbol))
                        (list 'as key val (clojure--form-name val)))
                       ((and (equal name "or") (eq (clojure--form-type val) 'map))
                        (list 'or key val
                              (delq nil
                                    (mapcar (pcase-lambda (`(,entry-key ,default))
                                              (and default
                                                   (eq (clojure--form-type entry-key) 'symbol)
                                                   (list (clojure--form-name entry-key)
                                                         entry-key default)))
                                            (clojure--form-pairs val)))))
                       ((eq type 'symbol)
                        (list 'pair key val (clojure--form-name key)))
                       ((memq type '(map ns-map vector))
                        (list 'nested key val (clojure--parse-pattern key)))
                       (t (list 'other key val)))))
                  pairs))))))