Function: rx--human-readable

rx--human-readable is a byte-compiled function defined in rx.el.gz.

Signature

(rx--human-readable FORM)

Documentation

Turn FORM into something that is more human-readable, for error messages.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/rx.el.gz
(defun rx--human-readable (form)
  "Turn FORM into something that is more human-readable, for error messages."
  ;; FIXME: Should we produce a string instead?
  ;; That way we wouldn't have problems with ? and ??, and we could escape
  ;; single chars.
  ;; We could steal `xr--rx-to-string' and just file off the serials.
  (let ((recurse (lambda (op skip)
                   (cons op (append (take skip (cdr form))
                                    (mapcar #'rx--human-readable
                                            (nthcdr skip (cdr form))))))))
  (pcase form
    ;; strings are more readable than numbers for single chars
    ((pred characterp) (char-to-string form))
    ;; resugar `rx--char-alt'
    (`(rx--char-alt ((,c . ,c)) . nil)
     (char-to-string form))
    (`(rx--char-alt nil . (,class))
     class)
    ;; TODO: render in complemented form if more readable that way?
    (`(rx--char-alt ,ivs . ,classes)
     (let ((strings (mapcan (lambda (iv)
                              (let ((beg (car iv))
                                    (end (cdr iv)))
                                (cond
                                 ;; single char
                                 ((eq beg end)
                                  (list (string beg)))
                                 ;; two chars
                                 ((eq end (1+ beg))
                                  (list (string beg) (string end)))
                                 ;; first char is hyphen
                                 ((eq beg ?-)
                                  (cons (string "-")
                                        (if (eq end (+ ?- 2))
                                            (list (string (1+ ?-) end))
                                          (list (string (1+ ?-) ?- end)))))
                                 ;; other range
                                 (t (list (string beg ?- end))))))
                            ivs)))
       `(any ,@strings ,@classes)))
    ;; avoid numbers as ops
    (`(?  . ,_) (funcall recurse '\? 0))
    (`(??  . ,_) (funcall recurse '\?? 0))
    ;; recurse on arguments
    (`(repeat ,_ ,_) (funcall recurse (car form) 1))
    (`(,(or '** 'repeat) . ,_) (funcall recurse (car form) 2))
    (`(,(or '= '>= 'group-n 'submatch-n) . ,_) (funcall recurse (car form) 1))
    (`(,(or 'backref 'syntax 'not-syntax 'category
            'eval 'regex 'regexp 'literal)
       . ,_)
     form)
    (`(,_ . ,_) (funcall recurse (car form) 0))
    (_ form))))