diff options
| author | Stefan Monnier | 2015-06-16 12:37:33 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2015-06-16 12:37:33 -0400 |
| commit | 3ef311678b1467d6e6852cbc509fb92917f6f449 (patch) | |
| tree | 62140db7d6da1fec51ebdf1cc1777628e01bddc5 | |
| parent | 34a43ba26a049bb966426022ffb2c41ab07841b8 (diff) | |
| download | emacs-3ef311678b1467d6e6852cbc509fb92917f6f449.tar.gz emacs-3ef311678b1467d6e6852cbc509fb92917f6f449.zip | |
* lisp/emacs-lisp/pcase.el: Improve docs and error handling
(pcase--self-quoting-p): Floats aren't self-quoting.
(pcase): Tweak docstring.
(pcase--u1): Deprecate the t pattern. Improve error detection for
the nil pattern.
(\`): Tweak docstring. Signal an error for unrecognized cases.
(bug#20784)
| -rw-r--r-- | lisp/emacs-lisp/pcase.el | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 0d3b21b8330..50a25072128 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el | |||
| @@ -112,11 +112,12 @@ CASES is a list of elements of the form (PATTERN CODE...). | |||
| 112 | 112 | ||
| 113 | Patterns can take the following forms: | 113 | Patterns can take the following forms: |
| 114 | _ matches anything. | 114 | _ matches anything. |
| 115 | SELFQUOTING matches itself. This includes keywords, numbers, and strings. | ||
| 116 | SYMBOL matches anything and binds it to SYMBOL. | 115 | SYMBOL matches anything and binds it to SYMBOL. |
| 117 | (or PAT...) matches if any of the patterns matches. | 116 | (or PAT...) matches if any of the patterns matches. |
| 118 | (and PAT...) matches if all the patterns match. | 117 | (and PAT...) matches if all the patterns match. |
| 119 | 'VAL matches if the object is `equal' to VAL | 118 | 'VAL matches if the object is `equal' to VAL |
| 119 | ATOM is a shorthand for 'ATOM. | ||
| 120 | ATOM can be a keyword, an integer, or a string. | ||
| 120 | (pred FUN) matches if FUN applied to the object returns non-nil. | 121 | (pred FUN) matches if FUN applied to the object returns non-nil. |
| 121 | (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. | 122 | (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. |
| 122 | (let PAT EXP) matches if EXP matches PAT. | 123 | (let PAT EXP) matches if EXP matches PAT. |
| @@ -638,7 +639,7 @@ MATCH is the pattern that needs to be matched, of the form: | |||
| 638 | res)) | 639 | res)) |
| 639 | 640 | ||
| 640 | (defun pcase--self-quoting-p (upat) | 641 | (defun pcase--self-quoting-p (upat) |
| 641 | (or (keywordp upat) (numberp upat) (stringp upat))) | 642 | (or (keywordp upat) (integerp upat) (stringp upat))) |
| 642 | 643 | ||
| 643 | (defun pcase--app-subst-match (match sym fun nsym) | 644 | (defun pcase--app-subst-match (match sym fun nsym) |
| 644 | (cond | 645 | (cond |
| @@ -770,7 +771,12 @@ Otherwise, it defers to REST which is a list of branches of the form | |||
| 770 | (sym (car cdrpopmatches)) | 771 | (sym (car cdrpopmatches)) |
| 771 | (upat (cdr cdrpopmatches))) | 772 | (upat (cdr cdrpopmatches))) |
| 772 | (cond | 773 | (cond |
| 773 | ((memq upat '(t _)) (pcase--u1 matches code vars rest)) | 774 | ((memq upat '(t _)) |
| 775 | (let ((code (pcase--u1 matches code vars rest))) | ||
| 776 | (if (eq upat '_) code | ||
| 777 | (macroexp--warn-and-return | ||
| 778 | "Pattern t is deprecated. Use `_' instead" | ||
| 779 | code)))) | ||
| 774 | ((eq upat 'pcase--dontcare) :pcase--dontcare) | 780 | ((eq upat 'pcase--dontcare) :pcase--dontcare) |
| 775 | ((memq (car-safe upat) '(guard pred)) | 781 | ((memq (car-safe upat) '(guard pred)) |
| 776 | (if (eq (car upat) 'pred) (pcase--mark-used sym)) | 782 | (if (eq (car upat) 'pred) (pcase--mark-used sym)) |
| @@ -784,7 +790,7 @@ Otherwise, it defers to REST which is a list of branches of the form | |||
| 784 | (pcase--eval (cadr upat) vars)) | 790 | (pcase--eval (cadr upat) vars)) |
| 785 | (pcase--u1 matches code vars then-rest) | 791 | (pcase--u1 matches code vars then-rest) |
| 786 | (pcase--u else-rest)))) | 792 | (pcase--u else-rest)))) |
| 787 | ((symbolp upat) | 793 | ((and (symbolp upat) upat) |
| 788 | (pcase--mark-used sym) | 794 | (pcase--mark-used sym) |
| 789 | (if (not (assq upat vars)) | 795 | (if (not (assq upat vars)) |
| 790 | (pcase--u1 matches code (cons (cons upat sym) vars) rest) | 796 | (pcase--u1 matches code (cons (cons upat sym) vars) rest) |
| @@ -854,7 +860,7 @@ Otherwise, it defers to REST which is a list of branches of the form | |||
| 854 | (pcase--u rest)) | 860 | (pcase--u rest)) |
| 855 | vars | 861 | vars |
| 856 | (list `((and . ,matches) ,code . ,vars)))) | 862 | (list `((and . ,matches) ,code . ,vars)))) |
| 857 | (t (error "Unknown internal pattern `%S'" upat))))) | 863 | (t (error "Unknown pattern `%S'" upat))))) |
| 858 | (t (error "Incorrect MATCH %S" (car matches))))) | 864 | (t (error "Incorrect MATCH %S" (car matches))))) |
| 859 | 865 | ||
| 860 | (def-edebug-spec | 866 | (def-edebug-spec |
| @@ -870,9 +876,9 @@ QPAT can take the following forms: | |||
| 870 | (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. | 876 | (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. |
| 871 | [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match | 877 | [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match |
| 872 | its 0..(n-1)th elements, respectively. | 878 | its 0..(n-1)th elements, respectively. |
| 873 | ,PAT matches if the pattern PAT matches. | 879 | ,PAT matches if the pcase pattern PAT matches. |
| 874 | STRING matches if the object is `equal' to STRING. | 880 | ATOM matches if the object is `equal' to ATOM. |
| 875 | ATOM matches if the object is `eq' to ATOM." | 881 | ATOM can be a symbol, an integer, or a string." |
| 876 | (declare (debug (pcase-QPAT))) | 882 | (declare (debug (pcase-QPAT))) |
| 877 | (cond | 883 | (cond |
| 878 | ((eq (car-safe qpat) '\,) (cadr qpat)) | 884 | ((eq (car-safe qpat) '\,) (cadr qpat)) |
| @@ -888,7 +894,8 @@ QPAT can take the following forms: | |||
| 888 | `(and (pred consp) | 894 | `(and (pred consp) |
| 889 | (app car ,(list '\` (car qpat))) | 895 | (app car ,(list '\` (car qpat))) |
| 890 | (app cdr ,(list '\` (cdr qpat))))) | 896 | (app cdr ,(list '\` (cdr qpat))))) |
| 891 | ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat))) | 897 | ((or (stringp qpat) (integerp qpat) (symbolp qpat)) `',qpat) |
| 898 | (t (error "Unknown QPAT: %S" qpat)))) | ||
| 892 | 899 | ||
| 893 | 900 | ||
| 894 | (provide 'pcase) | 901 | (provide 'pcase) |