diff options
| author | Stefan Monnier | 2015-05-24 22:38:05 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2015-05-24 22:38:05 -0400 |
| commit | dde09cdbce0239bba0248a8ed5c1f4d85c5e8476 (patch) | |
| tree | 635c0ca6eff77f70229d6d9c4ed7332d9e9b267b | |
| parent | c205098b6a8bd3b13256803f86f6863558a7a34e (diff) | |
| download | emacs-dde09cdbce0239bba0248a8ed5c1f4d85c5e8476.tar.gz emacs-dde09cdbce0239bba0248a8ed5c1f4d85c5e8476.zip | |
* lisp/emacs-lisp/pcase.el: Use PAT rather than UPAT in docstring
(pcase-let): Document the behavior in case the pattern doesn't match.
| -rw-r--r-- | lisp/emacs-lisp/pcase.el | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 5a81bb20e57..8c4f4bcbc7d 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el | |||
| @@ -47,7 +47,7 @@ | |||
| 47 | ;; to be performed anyway, so better do it first so it's shared). | 47 | ;; to be performed anyway, so better do it first so it's shared). |
| 48 | ;; - then choose the test that discriminates more (?). | 48 | ;; - then choose the test that discriminates more (?). |
| 49 | ;; - provide Agda's `with' (along with its `...' companion). | 49 | ;; - provide Agda's `with' (along with its `...' companion). |
| 50 | ;; - implement (not UPAT). This might require a significant redesign. | 50 | ;; - implement (not PAT). This might require a significant redesign. |
| 51 | ;; - ideally we'd want (pcase s ((re RE1) E1) ((re RE2) E2)) to be able to | 51 | ;; - ideally we'd want (pcase s ((re RE1) E1) ((re RE2) E2)) to be able to |
| 52 | ;; generate a lex-style DFA to decide whether to run E1 or E2. | 52 | ;; generate a lex-style DFA to decide whether to run E1 or E2. |
| 53 | 53 | ||
| @@ -71,14 +71,14 @@ | |||
| 71 | (defvar pcase--dontwarn-upats '(pcase--dontcare)) | 71 | (defvar pcase--dontwarn-upats '(pcase--dontcare)) |
| 72 | 72 | ||
| 73 | (def-edebug-spec | 73 | (def-edebug-spec |
| 74 | pcase-UPAT | 74 | pcase-PAT |
| 75 | (&or symbolp | 75 | (&or symbolp |
| 76 | ("or" &rest pcase-UPAT) | 76 | ("or" &rest pcase-PAT) |
| 77 | ("and" &rest pcase-UPAT) | 77 | ("and" &rest pcase-PAT) |
| 78 | ("guard" form) | 78 | ("guard" form) |
| 79 | ("let" pcase-UPAT form) | 79 | ("let" pcase-PAT form) |
| 80 | ("pred" pcase-FUN) | 80 | ("pred" pcase-FUN) |
| 81 | ("app" pcase-FUN pcase-UPAT) | 81 | ("app" pcase-FUN pcase-PAT) |
| 82 | pcase-MACRO | 82 | pcase-MACRO |
| 83 | sexp)) | 83 | sexp)) |
| 84 | 84 | ||
| @@ -108,19 +108,19 @@ | |||
| 108 | ;;;###autoload | 108 | ;;;###autoload |
| 109 | (defmacro pcase (exp &rest cases) | 109 | (defmacro pcase (exp &rest cases) |
| 110 | "Perform ML-style pattern matching on EXP. | 110 | "Perform ML-style pattern matching on EXP. |
| 111 | CASES is a list of elements of the form (UPATTERN CODE...). | 111 | CASES is a list of elements of the form (PATTERN CODE...). |
| 112 | 112 | ||
| 113 | UPatterns 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. | 115 | SELFQUOTING matches itself. This includes keywords, numbers, and strings. |
| 116 | SYMBOL matches anything and binds it to SYMBOL. | 116 | SYMBOL matches anything and binds it to SYMBOL. |
| 117 | (or UPAT...) matches if any of the patterns matches. | 117 | (or PAT...) matches if any of the patterns matches. |
| 118 | (and UPAT...) matches if all the patterns match. | 118 | (and PAT...) matches if all the patterns match. |
| 119 | 'VAL matches if the object is `equal' to VAL | 119 | 'VAL matches if the object is `equal' to VAL |
| 120 | (pred FUN) matches if FUN applied to the object returns non-nil. | 120 | (pred FUN) matches if FUN applied to the object returns non-nil. |
| 121 | (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. | 121 | (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. |
| 122 | (let UPAT EXP) matches if EXP matches UPAT. | 122 | (let PAT EXP) matches if EXP matches PAT. |
| 123 | (app FUN UPAT) matches if FUN applied to the object matches UPAT. | 123 | (app FUN PAT) matches if FUN applied to the object matches PAT. |
| 124 | If a SYMBOL is used twice in the same pattern (i.e. the pattern is | 124 | If a SYMBOL is used twice in the same pattern (i.e. the pattern is |
| 125 | \"non-linear\"), then the second occurrence is turned into an `eq'uality test. | 125 | \"non-linear\"), then the second occurrence is turned into an `eq'uality test. |
| 126 | 126 | ||
| @@ -138,7 +138,7 @@ like `(,a . ,(pred (< a))) or, with more checks: | |||
| 138 | 138 | ||
| 139 | Additional patterns can be defined via `pcase-defmacro'. | 139 | Additional patterns can be defined via `pcase-defmacro'. |
| 140 | Currently, the following patterns are provided this way:" | 140 | Currently, the following patterns are provided this way:" |
| 141 | (declare (indent 1) (debug (form &rest (pcase-UPAT body)))) | 141 | (declare (indent 1) (debug (form &rest (pcase-PAT body)))) |
| 142 | ;; We want to use a weak hash table as a cache, but the key will unavoidably | 142 | ;; We want to use a weak hash table as a cache, but the key will unavoidably |
| 143 | ;; be based on `exp' and `cases', yet `cases' is a fresh new list each time | 143 | ;; be based on `exp' and `cases', yet `cases' is a fresh new list each time |
| 144 | ;; we're called so it'll be immediately GC'd. So we use (car cases) as key | 144 | ;; we're called so it'll be immediately GC'd. So we use (car cases) as key |
| @@ -200,12 +200,12 @@ Currently, the following patterns are provided this way:" | |||
| 200 | 200 | ||
| 201 | ;;;###autoload | 201 | ;;;###autoload |
| 202 | (defmacro pcase-lambda (lambda-list &rest body) | 202 | (defmacro pcase-lambda (lambda-list &rest body) |
| 203 | "Like `lambda' but allow each argument to be a UPattern. | 203 | "Like `lambda' but allow each argument to be a pattern. |
| 204 | I.e. accepts the usual &optional and &rest keywords, but every | 204 | I.e. accepts the usual &optional and &rest keywords, but every |
| 205 | formal argument can be any pattern accepted by `pcase' (a mere | 205 | formal argument can be any pattern accepted by `pcase' (a mere |
| 206 | variable name being but a special case of it)." | 206 | variable name being but a special case of it)." |
| 207 | (declare (doc-string 2) (indent defun) | 207 | (declare (doc-string 2) (indent defun) |
| 208 | (debug ((&rest pcase-UPAT) body))) | 208 | (debug ((&rest pcase-PAT) body))) |
| 209 | (let* ((bindings ()) | 209 | (let* ((bindings ()) |
| 210 | (parsed-body (macroexp-parse-body body)) | 210 | (parsed-body (macroexp-parse-body body)) |
| 211 | (args (mapcar (lambda (pat) | 211 | (args (mapcar (lambda (pat) |
| @@ -242,9 +242,9 @@ variable name being but a special case of it)." | |||
| 242 | (defmacro pcase-let* (bindings &rest body) | 242 | (defmacro pcase-let* (bindings &rest body) |
| 243 | "Like `let*' but where you can use `pcase' patterns for bindings. | 243 | "Like `let*' but where you can use `pcase' patterns for bindings. |
| 244 | BODY should be an expression, and BINDINGS should be a list of bindings | 244 | BODY should be an expression, and BINDINGS should be a list of bindings |
| 245 | of the form (UPAT EXP)." | 245 | of the form (PAT EXP)." |
| 246 | (declare (indent 1) | 246 | (declare (indent 1) |
| 247 | (debug ((&rest (pcase-UPAT &optional form)) body))) | 247 | (debug ((&rest (pcase-PAT &optional form)) body))) |
| 248 | (let ((cached (gethash bindings pcase--memoize))) | 248 | (let ((cached (gethash bindings pcase--memoize))) |
| 249 | ;; cached = (BODY . EXPANSION) | 249 | ;; cached = (BODY . EXPANSION) |
| 250 | (if (equal (car cached) body) | 250 | (if (equal (car cached) body) |
| @@ -257,7 +257,10 @@ of the form (UPAT EXP)." | |||
| 257 | (defmacro pcase-let (bindings &rest body) | 257 | (defmacro pcase-let (bindings &rest body) |
| 258 | "Like `let' but where you can use `pcase' patterns for bindings. | 258 | "Like `let' but where you can use `pcase' patterns for bindings. |
| 259 | BODY should be a list of expressions, and BINDINGS should be a list of bindings | 259 | BODY should be a list of expressions, and BINDINGS should be a list of bindings |
| 260 | of the form (UPAT EXP)." | 260 | of the form (PAT EXP). |
| 261 | The macro is expanded and optimized under the assumption that those | ||
| 262 | patterns *will* match, so a mismatch may go undetected or may cause | ||
| 263 | any kind of error." | ||
| 261 | (declare (indent 1) (debug pcase-let*)) | 264 | (declare (indent 1) (debug pcase-let*)) |
| 262 | (if (null (cdr bindings)) | 265 | (if (null (cdr bindings)) |
| 263 | `(pcase-let* ,bindings ,@body) | 266 | `(pcase-let* ,bindings ,@body) |
| @@ -275,7 +278,7 @@ of the form (UPAT EXP)." | |||
| 275 | 278 | ||
| 276 | ;;;###autoload | 279 | ;;;###autoload |
| 277 | (defmacro pcase-dolist (spec &rest body) | 280 | (defmacro pcase-dolist (spec &rest body) |
| 278 | (declare (indent 1) (debug ((pcase-UPAT form) body))) | 281 | (declare (indent 1) (debug ((pcase-PAT form) body))) |
| 279 | (if (pcase--trivial-upat-p (car spec)) | 282 | (if (pcase--trivial-upat-p (car spec)) |
| 280 | `(dolist ,spec ,@body) | 283 | `(dolist ,spec ,@body) |
| 281 | (let ((tmpvar (make-symbol "x"))) | 284 | (let ((tmpvar (make-symbol "x"))) |
| @@ -381,7 +384,9 @@ of the form (UPAT EXP)." | |||
| 381 | 384 | ||
| 382 | ;;;###autoload | 385 | ;;;###autoload |
| 383 | (defmacro pcase-defmacro (name args &rest body) | 386 | (defmacro pcase-defmacro (name args &rest body) |
| 384 | "Define a pcase UPattern macro." | 387 | "Define a new kind of pcase PATTERN, by macro expansion. |
| 388 | Patterns of the form (NAME ...) will be expanded according | ||
| 389 | to this macro." | ||
| 385 | (declare (indent 2) (debug defun) (doc-string 3)) | 390 | (declare (indent 2) (debug defun) (doc-string 3)) |
| 386 | ;; Add the function via `fsym', so that an autoload cookie placed | 391 | ;; Add the function via `fsym', so that an autoload cookie placed |
| 387 | ;; on a pcase-defmacro will cause the macro to be loaded on demand. | 392 | ;; on a pcase-defmacro will cause the macro to be loaded on demand. |
| @@ -452,7 +457,7 @@ Each BRANCH has the form (MATCH CODE . VARS) where | |||
| 452 | CODE is the code generator for that branch. | 457 | CODE is the code generator for that branch. |
| 453 | VARS is the set of vars already bound by earlier matches. | 458 | VARS is the set of vars already bound by earlier matches. |
| 454 | MATCH is the pattern that needs to be matched, of the form: | 459 | MATCH is the pattern that needs to be matched, of the form: |
| 455 | (match VAR . UPAT) | 460 | (match VAR . PAT) |
| 456 | (and MATCH ...) | 461 | (and MATCH ...) |
| 457 | (or MATCH ...)" | 462 | (or MATCH ...)" |
| 458 | (when (setq branches (delq nil branches)) | 463 | (when (setq branches (delq nil branches)) |
| @@ -797,7 +802,7 @@ Otherwise, it defers to REST which is a list of branches of the form | |||
| 797 | (pcase--u1 (cons (pcase--match sym (nth 1 upat)) matches) | 802 | (pcase--u1 (cons (pcase--match sym (nth 1 upat)) matches) |
| 798 | code vars rest))) | 803 | code vars rest))) |
| 799 | ((eq (car-safe upat) 'app) | 804 | ((eq (car-safe upat) 'app) |
| 800 | ;; A upat of the form (app FUN UPAT) | 805 | ;; A upat of the form (app FUN PAT) |
| 801 | (pcase--mark-used sym) | 806 | (pcase--mark-used sym) |
| 802 | (let* ((fun (nth 1 upat)) | 807 | (let* ((fun (nth 1 upat)) |
| 803 | (nsym (make-symbol "x")) | 808 | (nsym (make-symbol "x")) |
| @@ -854,7 +859,7 @@ Otherwise, it defers to REST which is a list of branches of the form | |||
| 854 | 859 | ||
| 855 | (def-edebug-spec | 860 | (def-edebug-spec |
| 856 | pcase-QPAT | 861 | pcase-QPAT |
| 857 | (&or ("," pcase-UPAT) | 862 | (&or ("," pcase-PAT) |
| 858 | (pcase-QPAT . pcase-QPAT) | 863 | (pcase-QPAT . pcase-QPAT) |
| 859 | (vector &rest pcase-QPAT) | 864 | (vector &rest pcase-QPAT) |
| 860 | sexp)) | 865 | sexp)) |
| @@ -865,7 +870,7 @@ QPAT can take the following forms: | |||
| 865 | (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. | 870 | (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. |
| 866 | [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match | 871 | [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match |
| 867 | its 0..(n-1)th elements, respectively. | 872 | its 0..(n-1)th elements, respectively. |
| 868 | ,UPAT matches if the UPattern UPAT matches. | 873 | ,PAT matches if the pattern PAT matches. |
| 869 | STRING matches if the object is `equal' to STRING. | 874 | STRING matches if the object is `equal' to STRING. |
| 870 | ATOM matches if the object is `eq' to ATOM." | 875 | ATOM matches if the object is `eq' to ATOM." |
| 871 | (declare (debug (pcase-QPAT))) | 876 | (declare (debug (pcase-QPAT))) |