diff options
| author | Thien-Thi Nguyen | 2018-05-21 18:16:35 +0200 |
|---|---|---|
| committer | Thien-Thi Nguyen | 2018-05-27 17:14:27 +0200 |
| commit | 567cb9046d098b617c76541a75516ac6ef563be7 (patch) | |
| tree | cac40d9b8ea3162c5e4762d0a35baaf0d598f983 /lisp | |
| parent | 4d7e54acff0869d42bfb5b95014f7e6b988666d5 (diff) | |
| download | emacs-567cb9046d098b617c76541a75516ac6ef563be7.tar.gz emacs-567cb9046d098b617c76541a75516ac6ef563be7.zip | |
Overhaul pcase documentation
Suggested by Drew Adams (Bug#31311).
* doc/lispref/control.texi (Control Structures):
Add "Pattern-Matching Conditional" to menu, before "Iteration".
(Conditionals): Delete menu.
(Pattern matching case statement): Delete node/subsection,
by actually moving, renaming, and overhauling it to...
(Pattern-Matching Conditional): ...new node/section.
(pcase Macro): New node/subsection.
(Extending pcase): Likewise.
(Backquote Patterns): Likewise.
* doc/lispref/elisp.texi (Top) In @detailmenu, add
"Pattern-Matching Conditional" under "Control Structures"
section and delete "Conditionals" section.
* lisp/emacs-lisp/pcase.el (pcase): Rewrite docstring.
(pcase-defmacro \` (qpat) ...): Likewise.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/emacs-lisp/pcase.el | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 38e434de375..fa7b1de8b4d 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el | |||
| @@ -110,56 +110,41 @@ | |||
| 110 | (defmacro pcase (exp &rest cases) | 110 | (defmacro pcase (exp &rest cases) |
| 111 | "Evaluate EXP to get EXPVAL; try passing control to one of CASES. | 111 | "Evaluate EXP to get EXPVAL; try passing control to one of CASES. |
| 112 | CASES is a list of elements of the form (PATTERN CODE...). | 112 | CASES is a list of elements of the form (PATTERN CODE...). |
| 113 | 113 | For the first CASE whose PATTERN \"matches\" EXPVAL, | |
| 114 | A structural PATTERN describes a template that identifies a class | 114 | evaluate its CODE..., and return the value of the last form. |
| 115 | of values. For example, the pattern \\=`(,foo ,bar) matches any | 115 | If no CASE has a PATTERN that matches, return nil. |
| 116 | two element list, binding its elements to symbols named `foo' and | 116 | |
| 117 | `bar' -- in much the same way that `cl-destructuring-bind' would. | 117 | Each PATTERN expands, in essence, to a predicate to call |
| 118 | 118 | on EXPVAL. When the return value of that call is non-nil, | |
| 119 | A significant difference from `cl-destructuring-bind' is that, if | 119 | PATTERN matches. PATTERN can take one of the forms: |
| 120 | a pattern match fails, the next case is tried until either a | 120 | |
| 121 | successful match is found or there are no more cases. The CODE | 121 | _ matches anything. |
| 122 | expression corresponding to the matching pattern determines the | 122 | \\='VAL matches if EXPVAL is `equal' to VAL. |
| 123 | return value. If there is no match the returned value is nil. | 123 | KEYWORD shorthand for \\='KEYWORD |
| 124 | 124 | INTEGER shorthand for \\='INTEGER | |
| 125 | Another difference is that pattern elements may be quoted, | 125 | STRING shorthand for \\='STRING |
| 126 | meaning they must match exactly: The pattern \\='(foo bar) | 126 | SYMBOL matches anything and binds it to SYMBOL. |
| 127 | matches only against two element lists containing the symbols | 127 | If a SYMBOL is used twice in the same pattern |
| 128 | `foo' and `bar' in that order. (As a short-hand, atoms always | 128 | the second occurrence becomes an `eq'uality test. |
| 129 | match themselves, such as numbers or strings, and need not be | 129 | (pred FUN) matches if FUN called on EXPVAL returns non-nil. |
| 130 | quoted.) | 130 | (app FUN PAT) matches if FUN called on EXPVAL matches PAT. |
| 131 | 131 | (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. | |
| 132 | Lastly, a pattern can be logical, such as (pred numberp), that | 132 | (let PAT EXPR) matches if EXPR matches PAT. |
| 133 | matches any number-like element; or the symbol `_', that matches | 133 | (and PAT...) matches if all the patterns match. |
| 134 | anything. Also, when patterns are backquoted, a comma may be | 134 | (or PAT...) matches if any of the patterns matches. |
| 135 | used to introduce logical patterns inside backquoted patterns. | 135 | |
| 136 | 136 | FUN in `pred' and `app' can take one of the forms: | |
| 137 | The complete list of standard patterns is as follows: | 137 | SYMBOL or (lambda ARGS BODY) |
| 138 | 138 | call it with one argument | |
| 139 | _ matches anything. | 139 | (F ARG1 .. ARGn) |
| 140 | SYMBOL matches anything and binds it to SYMBOL. | 140 | call F with ARG1..ARGn and EXPVAL as n+1'th argument |
| 141 | If a SYMBOL is used twice in the same pattern | 141 | |
| 142 | the second occurrence becomes an `eq'uality test. | 142 | FUN, BOOLEXP, EXPR, and subsequent PAT can refer to variables |
| 143 | (or PAT...) matches if any of the patterns matches. | 143 | bound earlier in the pattern by a SYMBOL pattern. |
| 144 | (and PAT...) matches if all the patterns match. | ||
| 145 | \\='VAL matches if the object is `equal' to VAL. | ||
| 146 | ATOM is a shorthand for \\='ATOM. | ||
| 147 | ATOM can be a keyword, an integer, or a string. | ||
| 148 | (pred FUN) matches if FUN applied to the object returns non-nil. | ||
| 149 | (guard BOOLEXP) matches if BOOLEXP evaluates to non-nil. | ||
| 150 | (let PAT EXP) matches if EXP matches PAT. | ||
| 151 | (app FUN PAT) matches if FUN applied to the object matches PAT. | ||
| 152 | 144 | ||
| 153 | Additional patterns can be defined using `pcase-defmacro'. | 145 | Additional patterns can be defined using `pcase-defmacro'. |
| 154 | 146 | ||
| 155 | The FUN argument in the `app' pattern may have the following forms: | 147 | See Info node `(elisp) Pattern-Matching Conditional' in the |
| 156 | SYMBOL or (lambda ARGS BODY) in which case it's called with one argument. | ||
| 157 | (F ARG1 .. ARGn) in which case F gets called with an n+1'th argument | ||
| 158 | which is the value being matched. | ||
| 159 | So a FUN of the form SYMBOL is equivalent to (FUN). | ||
| 160 | FUN can refer to variables bound earlier in the pattern. | ||
| 161 | |||
| 162 | See Info node `(elisp) Pattern matching case statement' in the | ||
| 163 | Emacs Lisp manual for more information and examples." | 148 | Emacs Lisp manual for more information and examples." |
| 164 | (declare (indent 1) (debug (form &rest (pcase-PAT body)))) | 149 | (declare (indent 1) (debug (form &rest (pcase-PAT body)))) |
| 165 | ;; We want to use a weak hash table as a cache, but the key will unavoidably | 150 | ;; We want to use a weak hash table as a cache, but the key will unavoidably |
| @@ -926,14 +911,29 @@ Otherwise, it defers to REST which is a list of branches of the form | |||
| 926 | sexp)) | 911 | sexp)) |
| 927 | 912 | ||
| 928 | (pcase-defmacro \` (qpat) | 913 | (pcase-defmacro \` (qpat) |
| 929 | "Backquote-style pcase patterns. | 914 | "Backquote-style pcase patterns: \\=`QPAT |
| 930 | QPAT can take the following forms: | 915 | QPAT can take the following forms: |
| 931 | (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. | 916 | (QPAT1 . QPAT2) matches if QPAT1 matches the car and QPAT2 the cdr. |
| 932 | [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match | 917 | [QPAT1 QPAT2..QPATn] matches a vector of length n and QPAT1..QPATn match |
| 933 | its 0..(n-1)th elements, respectively. | 918 | its 0..(n-1)th elements, respectively. |
| 934 | ,PAT matches if the pcase pattern PAT matches. | 919 | ,PAT matches if the `pcase' pattern PAT matches. |
| 935 | ATOM matches if the object is `equal' to ATOM. | 920 | SYMBOL matches if EXPVAL is `equal' to SYMBOL. |
| 936 | ATOM can be a symbol, an integer, or a string." | 921 | KEYWORD likewise for KEYWORD. |
| 922 | INTEGER likewise for INTEGER. | ||
| 923 | STRING likewise for STRING. | ||
| 924 | |||
| 925 | The list or vector QPAT is a template. The predicate formed | ||
| 926 | by a backquote-style pattern is a combination of those | ||
| 927 | formed by any sub-patterns, wrapped in a top-level condition: | ||
| 928 | EXPVAL must be \"congruent\" with the template. For example: | ||
| 929 | |||
| 930 | \\=`(technical ,forum) | ||
| 931 | |||
| 932 | The predicate is the logical-AND of: | ||
| 933 | - Is EXPVAL a list of two elements? | ||
| 934 | - Is the first element the symbol `technical'? | ||
| 935 | - True! (The second element can be anything, and for the sake | ||
| 936 | of the body forms, its value is bound to the symbol `forum'.)" | ||
| 937 | (declare (debug (pcase-QPAT))) | 937 | (declare (debug (pcase-QPAT))) |
| 938 | (cond | 938 | (cond |
| 939 | ((eq (car-safe qpat) '\,) (cadr qpat)) | 939 | ((eq (car-safe qpat) '\,) (cadr qpat)) |