Function: parseclj--reduce-coll

parseclj--reduce-coll is a byte-compiled function defined in parseclj-parser.el.

Signature

(parseclj--reduce-coll STACK CLOSING-TOKEN REDUCE-BRANCH OPTIONS)

Documentation

Reduce collection based on the top of the STACK and a CLOSING-TOKEN.

REDUCE-BRANCH is a function to be applied to the collection of tokens found from the top of the stack until an opening token that matches CLOSING-TOKEN. This function should return an AST token representing such collection.

OPTIONS is an association list. This list is also passed down to the REDUCE-BRANCH function. See parseclj-parser for more information on available options.

Source Code

;; Defined in ~/.emacs.d/elpa/parseclj-20261002.1230/parseclj-parser.el
(defun parseclj--reduce-coll (stack closing-token reduce-branch options)
  "Reduce collection based on the top of the STACK and a CLOSING-TOKEN.

REDUCE-BRANCH is a function to be applied to the collection of tokens found
from the top of the stack until an opening token that matches
CLOSING-TOKEN.  This function should return an AST token representing such
collection.

OPTIONS is an association list.  This list is also passed down to the
REDUCE-BRANCH function.  See `parseclj-parser' for more information on
available options."
  (let ((opening-token-type (parseclj--find-opening-token stack closing-token))
        (fail-fast (map-elt options :fail-fast t))
        (collection nil))
    (if (not opening-token-type)
        (if fail-fast
            (parseclj--error "At position %s, unmatched %S"
                             (map-elt closing-token :pos)
                             (parseclj-lex-token-type closing-token))

          stack)

      (progn
        ;; unwind the stack until opening-token-type is found, adding to collection
        (while (and stack (not (eq (parseclj-lex-token-type (car stack)) opening-token-type)))
          (push (pop stack) collection))

        ;; did we find the right token?
        (if (eq (parseclj-lex-token-type (car stack)) opening-token-type)
            (progn
              (when fail-fast
                ;; any unreduced tokens left: bail early
                (when-let* ((token (seq-find #'parseclj-lex-token-p collection)))
                  (parseclj--error "At position %s, unmatched %S"
                                   (map-elt token :pos)
                                   (parseclj-lex-token-type token))))

              ;; all good, call the reducer so it can return an updated stack with a
              ;; new node at the top.
              (let ((opening-token (pop stack)))
                (funcall reduce-branch stack opening-token collection options)))

          ;; Unwound the stack without finding a matching paren: either bail early
          ;; or return the original stack and continue parsing
          (if fail-fast
              (parseclj--error "At position %s, unmatched %S"
                               (map-elt closing-token :pos)
                               (parseclj-lex-token-type closing-token))

            (reverse collection)))))))