Mercurial > emacs
annotate lisp/emacs-lisp/rx.el @ 99978:bfd3ac81ee0f
(enum charset_method): Delete
CHARSET_METHOD_MAP_DEFERRED.
(DECODE_CHAR): Check if the decoder vector is ready.
(ENCODE_CHAR): Check if the encoder char-table is ready.
(maybe_unify_char): Extern it.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Thu, 27 Nov 2008 08:00:43 +0000 |
parents | 73eaaf9adee6 |
children | bb1d2d686d04 |
rev | line source |
---|---|
39516 | 1 ;;; rx.el --- sexp notation for regular expressions |
2 | |
68648
067115a6e738
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64751
diff
changeset
|
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, |
79704 | 4 ;; 2006, 2007, 2008 Free Software Foundation, Inc. |
39516 | 5 |
6 ;; Author: Gerd Moellmann <gerd@gnu.org> | |
7 ;; Maintainer: FSF | |
8 ;; Keywords: strings, regexps, extensions | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
39516 | 13 ;; it under the terms of the GNU General Public License as published by |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
15 ;; (at your option) any later version. |
39516 | 16 |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
39516 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; This is another implementation of sexp-form regular expressions. | |
28 ;; It was unfortunately written without being aware of the Sregex | |
29 ;; package coming with Emacs, but as things stand, Rx completely | |
30 ;; covers all regexp features, which Sregex doesn't, doesn't suffer | |
31 ;; from the bugs mentioned in the commentary section of Sregex, and | |
32 ;; uses a nicer syntax (IMHO, of course :-). | |
33 | |
55102 | 34 ;; This significantly extended version of the original, is almost |
35 ;; compatible with Sregex. The only incompatibility I (fx) know of is | |
36 ;; that the `repeat' form can't have multiple regexp args. | |
37 | |
38 ;; Now alternative forms are provided for a degree of compatibility | |
39 ;; with Shivers' attempted definitive SRE notation | |
40 ;; <URL:http://www.ai.mit.edu/~/shivers/sre.txt>. SRE forms not | |
41 ;; catered for include: dsm, uncase, w/case, w/nocase, ,@<exp>, | |
42 ;; ,<exp>, (word ...), word+, posix-string, and character class forms. | |
43 ;; Some forms are inconsistent with SRE, either for historical reasons | |
44 ;; or because of the implementation -- simple translation into Emacs | |
45 ;; regexp strings. These include: any, word. Also, case-sensitivity | |
46 ;; and greediness are controlled by variables external to the regexp, | |
47 ;; and you need to feed the forms to the `posix-' functions to get | |
48 ;; SRE's POSIX semantics. There are probably more difficulties. | |
49 | |
39516 | 50 ;; Rx translates a sexp notation for regular expressions into the |
51 ;; usual string notation. The translation can be done at compile-time | |
52 ;; by using the `rx' macro. It can be done at run-time by calling | |
53 ;; function `rx-to-string'. See the documentation of `rx' for a | |
54 ;; complete description of the sexp notation. | |
55 ;; | |
56 ;; Some examples of string regexps and their sexp counterparts: | |
57 ;; | |
58 ;; "^[a-z]*" | |
59 ;; (rx (and line-start (0+ (in "a-z")))) | |
60 ;; | |
61 ;; "\n[^ \t]" | |
62 ;; (rx (and "\n" (not blank))), or | |
63 ;; (rx (and "\n" (not (any " \t")))) | |
64 ;; | |
65 ;; "\\*\\*\\* EOOH \\*\\*\\*\n" | |
66 ;; (rx "*** EOOH ***\n") | |
67 ;; | |
68 ;; "\\<\\(catch\\|finally\\)\\>[^_]" | |
69 ;; (rx (and word-start (submatch (or "catch" "finally")) word-end | |
70 ;; (not (any ?_)))) | |
71 ;; | |
72 ;; "[ \t\n]*:\\([^:]+\\|$\\)" | |
73 ;; (rx (and (zero-or-more (in " \t\n")) ":" | |
74 ;; (submatch (or line-end (one-or-more (not (any ?:))))))) | |
75 ;; | |
76 ;; "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*" | |
77 ;; (rx (and line-start | |
78 ;; "content-transfer-encoding:" | |
48938
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
79 ;; (+ (? ?\n)) blank |
39516 | 80 ;; "quoted-printable" |
48938
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
81 ;; (+ (? ?\n)) blank)) |
39516 | 82 ;; |
83 ;; (concat "^\\(?:" something-else "\\)") | |
84 ;; (rx (and line-start (eval something-else))), statically or | |
85 ;; (rx-to-string '(and line-start ,something-else)), dynamically. | |
86 ;; | |
87 ;; (regexp-opt '(STRING1 STRING2 ...)) | |
88 ;; (rx (or STRING1 STRING2 ...)), or in other words, `or' automatically | |
89 ;; calls `regexp-opt' as needed. | |
90 ;; | |
91 ;; "^;;\\s-*\n\\|^\n" | |
92 ;; (rx (or (and line-start ";;" (0+ space) ?\n) | |
93 ;; (and line-start ?\n))) | |
94 ;; | |
95 ;; "\\$[I]d: [^ ]+ \\([^ ]+\\) " | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
96 ;; (rx (and "$Id: " |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
97 ;; (1+ (not (in " "))) |
39516 | 98 ;; " " |
99 ;; (submatch (1+ (not (in " ")))) | |
48938
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
100 ;; " ")) |
39516 | 101 ;; |
102 ;; "\\\\\\\\\\[\\w+" | |
103 ;; (rx (and ?\\ ?\\ ?\[ (1+ word))) | |
104 ;; | |
105 ;; etc. | |
106 | |
107 ;;; History: | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
108 ;; |
39516 | 109 |
110 ;;; Code: | |
111 | |
112 (defconst rx-constituents | |
113 '((and . (rx-and 1 nil)) | |
55102 | 114 (seq . and) ; SRE |
115 (: . and) ; SRE | |
116 (sequence . and) ; sregex | |
39516 | 117 (or . (rx-or 1 nil)) |
55102 | 118 (| . or) ; SRE |
39516 | 119 (not-newline . ".") |
55102 | 120 (nonl . not-newline) ; SRE |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
121 (anything . (rx-anything 0 nil)) |
55102 | 122 (any . (rx-any 1 nil rx-check-any)) ; inconsistent with SRE |
39516 | 123 (in . any) |
55102 | 124 (char . any) ; sregex |
125 (not-char . (rx-not-char 1 nil rx-check-any)) ; sregex | |
39516 | 126 (not . (rx-not 1 1 rx-check-not)) |
55102 | 127 ;; Partially consistent with sregex, whose `repeat' is like our |
128 ;; `**'. (`repeat' with optional max arg and multiple sexp forms | |
129 ;; is ambiguous.) | |
39516 | 130 (repeat . (rx-repeat 2 3)) |
55102 | 131 (= . (rx-= 2 nil)) ; SRE |
132 (>= . (rx->= 2 nil)) ; SRE | |
133 (** . (rx-** 2 nil)) ; SRE | |
134 (submatch . (rx-submatch 1 nil)) ; SRE | |
39516 | 135 (group . submatch) |
55102 | 136 (zero-or-more . (rx-kleene 1 nil)) |
137 (one-or-more . (rx-kleene 1 nil)) | |
138 (zero-or-one . (rx-kleene 1 nil)) | |
139 (\? . zero-or-one) ; SRE | |
39516 | 140 (\?? . zero-or-one) |
55102 | 141 (* . zero-or-more) ; SRE |
39516 | 142 (*? . zero-or-more) |
143 (0+ . zero-or-more) | |
55102 | 144 (+ . one-or-more) ; SRE |
39516 | 145 (+? . one-or-more) |
146 (1+ . one-or-more) | |
147 (optional . zero-or-one) | |
55102 | 148 (opt . zero-or-one) ; sregex |
39516 | 149 (minimal-match . (rx-greedy 1 1)) |
150 (maximal-match . (rx-greedy 1 1)) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
151 (backref . (rx-backref 1 1 rx-check-backref)) |
39516 | 152 (line-start . "^") |
55102 | 153 (bol . line-start) ; SRE |
39516 | 154 (line-end . "$") |
55102 | 155 (eol . line-end) ; SRE |
39516 | 156 (string-start . "\\`") |
55102 | 157 (bos . string-start) ; SRE |
158 (bot . string-start) ; sregex | |
39516 | 159 (string-end . "\\'") |
55102 | 160 (eos . string-end) ; SRE |
161 (eot . string-end) ; sregex | |
39516 | 162 (buffer-start . "\\`") |
163 (buffer-end . "\\'") | |
164 (point . "\\=") | |
165 (word-start . "\\<") | |
55102 | 166 (bow . word-start) ; SRE |
39516 | 167 (word-end . "\\>") |
55102 | 168 (eow . word-end) ; SRE |
39516 | 169 (word-boundary . "\\b") |
55102 | 170 (not-word-boundary . "\\B") ; sregex |
60930
a6ae354aa8ef
(rx-constituents): Add symbol-start and symbol-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55103
diff
changeset
|
171 (symbol-start . "\\_<") |
a6ae354aa8ef
(rx-constituents): Add symbol-start and symbol-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55103
diff
changeset
|
172 (symbol-end . "\\_>") |
39516 | 173 (syntax . (rx-syntax 1 1)) |
55102 | 174 (not-syntax . (rx-not-syntax 1 1)) ; sregex |
39516 | 175 (category . (rx-category 1 1 rx-check-category)) |
176 (eval . (rx-eval 1 1)) | |
177 (regexp . (rx-regexp 1 1 stringp)) | |
178 (digit . "[[:digit:]]") | |
55102 | 179 (numeric . digit) ; SRE |
180 (num . digit) ; SRE | |
181 (control . "[[:cntrl:]]") ; SRE | |
182 (cntrl . control) ; SRE | |
183 (hex-digit . "[[:xdigit:]]") ; SRE | |
184 (hex . hex-digit) ; SRE | |
185 (xdigit . hex-digit) ; SRE | |
186 (blank . "[[:blank:]]") ; SRE | |
187 (graphic . "[[:graph:]]") ; SRE | |
188 (graph . graphic) ; SRE | |
189 (printing . "[[:print:]]") ; SRE | |
190 (print . printing) ; SRE | |
191 (alphanumeric . "[[:alnum:]]") ; SRE | |
192 (alnum . alphanumeric) ; SRE | |
39516 | 193 (letter . "[[:alpha:]]") |
55102 | 194 (alphabetic . letter) ; SRE |
195 (alpha . letter) ; SRE | |
196 (ascii . "[[:ascii:]]") ; SRE | |
39516 | 197 (nonascii . "[[:nonascii:]]") |
55102 | 198 (lower . "[[:lower:]]") ; SRE |
199 (lower-case . lower) ; SRE | |
200 (punctuation . "[[:punct:]]") ; SRE | |
201 (punct . punctuation) ; SRE | |
202 (space . "[[:space:]]") ; SRE | |
203 (whitespace . space) ; SRE | |
204 (white . space) ; SRE | |
205 (upper . "[[:upper:]]") ; SRE | |
206 (upper-case . upper) ; SRE | |
207 (word . "[[:word:]]") ; inconsistent with SRE | |
208 (wordchar . word) ; sregex | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
209 (not-wordchar . "\\W")) |
39516 | 210 "Alist of sexp form regexp constituents. |
211 Each element of the alist has the form (SYMBOL . DEFN). | |
212 SYMBOL is a valid constituent of sexp regular expressions. | |
213 If DEFN is a string, SYMBOL is translated into DEFN. | |
214 If DEFN is a symbol, use the definition of DEFN, recursively. | |
215 Otherwise, DEFN must be a list (FUNCTION MIN-ARGS MAX-ARGS PREDICATE). | |
216 FUNCTION is used to produce code for SYMBOL. MIN-ARGS and MAX-ARGS | |
217 are the minimum and maximum number of arguments the function-form | |
218 sexp constituent SYMBOL may have in sexp regular expressions. | |
219 MAX-ARGS nil means no limit. PREDICATE, if specified, means that | |
220 all arguments must satisfy PREDICATE.") | |
221 | |
222 | |
223 (defconst rx-syntax | |
224 '((whitespace . ?-) | |
225 (punctuation . ?.) | |
226 (word . ?w) | |
227 (symbol . ?_) | |
228 (open-parenthesis . ?\() | |
229 (close-parenthesis . ?\)) | |
230 (expression-prefix . ?\') | |
231 (string-quote . ?\") | |
232 (paired-delimiter . ?$) | |
233 (escape . ?\\) | |
234 (character-quote . ?/) | |
235 (comment-start . ?<) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
236 (comment-end . ?>) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
237 (string-delimiter . ?|) |
55103
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
238 (comment-delimiter . ?!)) |
39516 | 239 "Alist mapping Rx syntax symbols to syntax characters. |
240 Each entry has the form (SYMBOL . CHAR), where SYMBOL is a valid | |
241 symbol in `(syntax SYMBOL)', and CHAR is the syntax character | |
242 corresponding to SYMBOL, as it would be used with \\s or \\S in | |
243 regular expressions.") | |
244 | |
245 | |
246 (defconst rx-categories | |
247 '((consonant . ?0) | |
248 (base-vowel . ?1) | |
249 (upper-diacritical-mark . ?2) | |
250 (lower-diacritical-mark . ?3) | |
251 (tone-mark . ?4) | |
252 (symbol . ?5) | |
253 (digit . ?6) | |
254 (vowel-modifying-diacritical-mark . ?7) | |
255 (vowel-sign . ?8) | |
256 (semivowel-lower . ?9) | |
257 (not-at-end-of-line . ?<) | |
258 (not-at-beginning-of-line . ?>) | |
259 (alpha-numeric-two-byte . ?A) | |
260 (chinse-two-byte . ?C) | |
261 (greek-two-byte . ?G) | |
262 (japanese-hiragana-two-byte . ?H) | |
263 (indian-two-byte . ?I) | |
264 (japanese-katakana-two-byte . ?K) | |
265 (korean-hangul-two-byte . ?N) | |
266 (cyrillic-two-byte . ?Y) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
267 (combining-diacritic . ?^) |
39516 | 268 (ascii . ?a) |
269 (arabic . ?b) | |
270 (chinese . ?c) | |
271 (ethiopic . ?e) | |
272 (greek . ?g) | |
273 (korean . ?h) | |
274 (indian . ?i) | |
275 (japanese . ?j) | |
276 (japanese-katakana . ?k) | |
277 (latin . ?l) | |
278 (lao . ?o) | |
279 (tibetan . ?q) | |
280 (japanese-roman . ?r) | |
281 (thai . ?t) | |
282 (vietnamese . ?v) | |
283 (hebrew . ?w) | |
284 (cyrillic . ?y) | |
285 (can-break . ?|)) | |
286 "Alist mapping symbols to category characters. | |
287 Each entry has the form (SYMBOL . CHAR), where SYMBOL is a valid | |
288 symbol in `(category SYMBOL)', and CHAR is the category character | |
289 corresponding to SYMBOL, as it would be used with `\\c' or `\\C' in | |
290 regular expression strings.") | |
291 | |
292 | |
293 (defvar rx-greedy-flag t | |
294 "Non-nil means produce greedy regular expressions for `zero-or-one', | |
295 `zero-or-more', and `one-or-more'. Dynamically bound.") | |
296 | |
297 | |
298 (defun rx-info (op) | |
299 "Return parsing/code generation info for OP. | |
300 If OP is the space character ASCII 32, return info for the symbol `?'. | |
301 If OP is the character `?', return info for the symbol `??'. | |
302 See also `rx-constituents'." | |
303 (cond ((eq op ? ) (setq op '\?)) | |
304 ((eq op ??) (setq op '\??))) | |
305 (while (and (not (null op)) (symbolp op)) | |
306 (setq op (cdr (assq op rx-constituents)))) | |
307 op) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
308 |
39516 | 309 |
310 (defun rx-check (form) | |
311 "Check FORM according to its car's parsing info." | |
55102 | 312 (unless (listp form) |
313 (error "rx `%s' needs argument(s)" form)) | |
39516 | 314 (let* ((rx (rx-info (car form))) |
315 (nargs (1- (length form))) | |
316 (min-args (nth 1 rx)) | |
317 (max-args (nth 2 rx)) | |
318 (type-pred (nth 3 rx))) | |
319 (when (and (not (null min-args)) | |
320 (< nargs min-args)) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
321 (error "rx form `%s' requires at least %d args" |
39516 | 322 (car form) min-args)) |
323 (when (and (not (null max-args)) | |
324 (> nargs max-args)) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
325 (error "rx form `%s' accepts at most %d args" |
39516 | 326 (car form) max-args)) |
327 (when (not (null type-pred)) | |
328 (dolist (sub-form (cdr form)) | |
329 (unless (funcall type-pred sub-form) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
330 (error "rx form `%s' requires args satisfying `%s'" |
39516 | 331 (car form) type-pred)))))) |
332 | |
333 | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
334 (defun rx-group-if (regexp group) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
335 "Put shy groups around REGEXP if seemingly necessary when GROUP |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
336 is non-nil." |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
337 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
338 ;; for some repetition |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
339 ((eq group '*) (if (rx-atomic-p regexp) (setq group nil))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
340 ;; for concatenation |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
341 ((eq group ':) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
342 (if (rx-atomic-p |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
343 (if (string-match |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
344 "\\(?:[?*+]\\??\\|\\\\{[0-9]*,?[0-9]*\\\\}\\)\\'" regexp) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
345 (substring regexp 0 (match-beginning 0)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
346 regexp)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
347 (setq group nil))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
348 ;; for OR |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
349 ((eq group '|) (setq group nil)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
350 ;; do anyway |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
351 ((eq group t)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
352 ((rx-atomic-p regexp t) (setq group nil))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
353 (if group |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
354 (concat "\\(?:" regexp "\\)") |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
355 regexp)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
356 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
357 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
358 (defvar rx-parent) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
359 ;; dynamically bound in some functions. |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
360 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
361 |
39516 | 362 (defun rx-and (form) |
363 "Parse and produce code from FORM. | |
364 FORM is of the form `(and FORM1 ...)'." | |
365 (rx-check form) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
366 (rx-group-if |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
367 (mapconcat (lambda (x) (rx-form x ':)) (cdr form) nil) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
368 (and (memq rx-parent '(* t)) rx-parent))) |
39516 | 369 |
370 | |
371 (defun rx-or (form) | |
372 "Parse and produce code from FORM, which is `(or FORM1 ...)'." | |
373 (rx-check form) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
374 (rx-group-if |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
375 (if (memq nil (mapcar 'stringp (cdr form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
376 (mapconcat (lambda (x) (rx-form x '|)) (cdr form) "\\|") |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
377 (regexp-opt (cdr form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
378 (and (memq rx-parent '(: * t)) rx-parent))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
379 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
380 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
381 (defun rx-anything (form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
382 "Match any character." |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
383 (if (consp form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
384 (error "rx `anythng' syntax error: %s" form)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
385 (rx-or (list 'or 'not-newline ?\n))) |
39516 | 386 |
387 | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
388 (defun rx-any-delete-from-range (char ranges) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
389 "Delete by side effect character CHAR from RANGES. |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
390 Only both edges of each range is checked." |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
391 (let (m) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
392 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
393 ((memq char ranges) (setq ranges (delq char ranges))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
394 ((setq m (assq char ranges)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
395 (if (eq (1+ char) (cdr m)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
396 (setcar (memq m ranges) (1+ char)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
397 (setcar m (1+ char)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
398 ((setq m (rassq char ranges)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
399 (if (eq (1- char) (car m)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
400 (setcar (memq m ranges) (1- char)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
401 (setcdr m (1- char))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
402 ranges)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
403 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
404 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
405 (defun rx-any-condense-range (args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
406 "Condense by side effect ARGS as range for Rx `any'." |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
407 (let (str |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
408 l) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
409 ;; set STR list of all strings |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
410 ;; set L list of all ranges |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
411 (mapc (lambda (e) (cond ((stringp e) (push e str)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
412 ((numberp e) (push (cons e e) l)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
413 (t (push e l)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
414 args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
415 ;; condense overlapped ranges in L |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
416 (let ((tail (setq l (sort l #'car-less-than-car))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
417 d) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
418 (while (setq d (cdr tail)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
419 (if (>= (cdar tail) (1- (caar d))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
420 (progn |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
421 (setcdr (car tail) (max (cdar tail) (cdar d))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
422 (setcdr tail (cdr d))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
423 (setq tail d)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
424 ;; Separate small ranges to single number, and delete dups. |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
425 (nconc |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
426 (apply #'nconc |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
427 (mapcar (lambda (e) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
428 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
429 ((= (car e) (cdr e)) (list (car e))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
430 ;; ((= (1+ (car e)) (cdr e)) (list (car e) (cdr e))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
431 ((list e)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
432 l)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
433 (delete-dups str)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
434 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
435 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
436 (defun rx-check-any-string (str) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
437 "Check string argument STR for Rx `any'." |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
438 (let ((i 0) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
439 c1 c2 l) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
440 (if (= 0 (length str)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
441 (error "String arg for Rx `any' must not be empty")) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
442 (while (string-match ".-." str i) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
443 ;; string before range: convert it to characters |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
444 (if (< i (match-beginning 0)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
445 (setq l (nconc |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
446 l |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
447 (append (substring str i (match-beginning 0)) nil)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
448 ;; range |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
449 (setq i (match-end 0) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
450 c1 (aref str (match-beginning 0)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
451 c2 (aref str (1- i))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
452 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
453 ((< c1 c2) (setq l (nconc l (list (cons c1 c2))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
454 ((= c1 c2) (setq l (nconc l (list c1)))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
455 ;; rest? |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
456 (if (< i (length str)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
457 (setq l (nconc l (append (substring str i) nil)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
458 l)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
459 |
39516 | 460 |
461 (defun rx-check-any (arg) | |
462 "Check arg ARG for Rx `any'." | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
463 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
464 ((integerp arg) (list arg)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
465 ((symbolp arg) |
55102 | 466 (let ((translation (condition-case nil |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
467 (rx-form arg) |
55102 | 468 (error nil)))) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
469 (if (or (null translation) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
470 (null (string-match "\\`\\[\\[:[-a-z]+:\\]\\]\\'" translation))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
471 (error "Invalid char class `%s' in Rx `any'" arg)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
472 (list (substring translation 1 -1)))) ; strip outer brackets |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
473 ((and (integerp (car-safe arg)) (integerp (cdr-safe arg))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
474 (list arg)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
475 ((stringp arg) (rx-check-any-string arg)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
476 ((error |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
477 "rx `any' requires string, character, char pair or char class args")))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
478 |
39516 | 479 |
480 (defun rx-any (form) | |
55102 | 481 "Parse and produce code from FORM, which is `(any ARG ...)'. |
482 ARG is optional." | |
39516 | 483 (rx-check form) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
484 (let* ((args (rx-any-condense-range |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
485 (apply |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
486 #'nconc |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
487 (mapcar #'rx-check-any (cdr form))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
488 m |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
489 s) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
490 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
491 ;; single close bracket |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
492 ;; => "[]...-]" or "[]...--.]" |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
493 ((memq ?\] args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
494 ;; set ] at the beginning |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
495 (setq args (cons ?\] (delq ?\] args))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
496 ;; set - at the end |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
497 (if (or (memq ?- args) (assq ?- args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
498 (setq args (nconc (rx-any-delete-from-range ?- args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
499 (list ?-))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
500 ;; close bracket starts a range |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
501 ;; => "[]-....-]" or "[]-.--....]" |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
502 ((setq m (assq ?\] args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
503 ;; bring it to the beginning |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
504 (setq args (cons m (delq m args))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
505 (cond ((memq ?- args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
506 ;; to the end |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
507 (setq args (nconc (delq ?- args) (list ?-)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
508 ((setq m (assq ?- args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
509 ;; next to the bracket's range, make the second range |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
510 (setcdr args (cons m (delq m args)))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
511 ;; bracket in the end range |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
512 ;; => "[]...-]" |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
513 ((setq m (rassq ?\] args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
514 ;; set ] at the beginning |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
515 (setq args (cons ?\] (rx-any-delete-from-range ?\] args))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
516 ;; set - at the end |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
517 (if (or (memq ?- args) (assq ?- args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
518 (setq args (nconc (rx-any-delete-from-range ?- args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
519 (list ?-))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
520 ;; {no close bracket appears} |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
521 ;; |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
522 ;; bring single bar to the beginning |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
523 ((memq ?- args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
524 (setq args (cons ?- (delq ?- args)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
525 ;; bar start a range, bring it to the beginning |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
526 ((setq m (assq ?- args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
527 (setq args (cons m (delq m args)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
528 ;; |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
529 ;; hat at the beginning? |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
530 ((or (eq (car args) ?^) (eq (car-safe (car args)) ?^)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
531 (setq args (if (cdr args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
532 `(,(cadr args) ,(car args) ,@(cddr args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
533 (nconc (rx-any-delete-from-range ?^ args) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
534 (list ?^)))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
535 ;; some 1-char? |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
536 (if (and (null (cdr args)) (numberp (car args)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
537 (or (= 1 (length |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
538 (setq s (regexp-quote (string (car args)))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
539 (and (equal (car args) ?^) ;; unnecessary predicate? |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
540 (null (eq rx-parent '!))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
541 s |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
542 (concat "[" |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
543 (mapconcat |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
544 (lambda (e) (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
545 ((numberp e) (string e)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
546 ((consp e) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
547 (if (and (= (1+ (car e)) (cdr e)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
548 (null (memq (car e) '(?\] ?-)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
549 (string (car e) (cdr e)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
550 (string (car e) ?- (cdr e)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
551 (e))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
552 args |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
553 nil) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
554 "]")))) |
39516 | 555 |
556 | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
557 (defun rx-check-not (arg) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
558 "Check arg ARG for Rx `not'." |
55102 | 559 (unless (or (and (symbolp arg) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
560 (string-match "\\`\\[\\[:[-a-z]+:\\]\\]\\'" |
55102 | 561 (condition-case nil |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
562 (rx-form arg) |
55102 | 563 (error "")))) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
564 (eq arg 'word-boundary) |
55102 | 565 (and (consp arg) |
566 (memq (car arg) '(not any in syntax category)))) | |
567 (error "rx `not' syntax error: %s" arg)) | |
568 t) | |
39516 | 569 |
570 | |
571 (defun rx-not (form) | |
572 "Parse and produce code from FORM. FORM is `(not ...)'." | |
573 (rx-check form) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
574 (let ((result (rx-form (cadr form) '!)) |
53974
818e19ae4c5a
(rx-not): Bind case-fold-search to nil.
Eli Zaretskii <eliz@is.elta.co.il>
parents:
52971
diff
changeset
|
575 case-fold-search) |
39516 | 576 (cond ((string-match "\\`\\[^" result) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
577 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
578 ((equal result "[^]") "[^^]") |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
579 ((and (= (length result) 4) (null (eq rx-parent '!))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
580 (regexp-quote (substring result 2 3))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
581 ((concat "[" (substring result 2))))) |
55102 | 582 ((eq ?\[ (aref result 0)) |
39516 | 583 (concat "[^" (substring result 1))) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
584 ((string-match "\\`\\\\[scbw]" result) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
585 (concat (upcase (substring result 0 2)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
586 (substring result 2))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
587 ((string-match "\\`\\\\[SCBW]" result) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
588 (concat (downcase (substring result 0 2)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
589 (substring result 2))) |
39516 | 590 (t |
591 (concat "[^" result "]"))))) | |
592 | |
593 | |
55102 | 594 (defun rx-not-char (form) |
595 "Parse and produce code from FORM. FORM is `(not-char ...)'." | |
596 (rx-check form) | |
597 (rx-not `(not (in ,@(cdr form))))) | |
598 | |
599 | |
600 (defun rx-not-syntax (form) | |
601 "Parse and produce code from FORM. FORM is `(not-syntax SYNTAX)'." | |
602 (rx-check form) | |
603 (rx-not `(not (syntax ,@(cdr form))))) | |
604 | |
605 | |
606 (defun rx-trans-forms (form &optional skip) | |
607 "If FORM's length is greater than two, transform it to length two. | |
608 A form (HEAD REST ...) becomes (HEAD (and REST ...)). | |
609 If SKIP is non-nil, allow that number of items after the head, i.e. | |
610 `(= N REST ...)' becomes `(= N (and REST ...))' if SKIP is 1." | |
611 (unless skip (setq skip 0)) | |
612 (let ((tail (nthcdr (1+ skip) form))) | |
613 (if (= (length tail) 1) | |
614 form | |
615 (let ((form (copy-sequence form))) | |
616 (setcdr (nthcdr skip form) (list (cons 'and tail))) | |
617 form)))) | |
618 | |
619 | |
620 (defun rx-= (form) | |
621 "Parse and produce code from FORM `(= N ...)'." | |
622 (rx-check form) | |
623 (setq form (rx-trans-forms form 1)) | |
624 (unless (and (integerp (nth 1 form)) | |
625 (> (nth 1 form) 0)) | |
626 (error "rx `=' requires positive integer first arg")) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
627 (format "%s\\{%d\\}" (rx-form (nth 2 form) '*) (nth 1 form))) |
55102 | 628 |
629 | |
630 (defun rx->= (form) | |
631 "Parse and produce code from FORM `(>= N ...)'." | |
632 (rx-check form) | |
633 (setq form (rx-trans-forms form 1)) | |
634 (unless (and (integerp (nth 1 form)) | |
635 (> (nth 1 form) 0)) | |
636 (error "rx `>=' requires positive integer first arg")) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
637 (format "%s\\{%d,\\}" (rx-form (nth 2 form) '*) (nth 1 form))) |
55102 | 638 |
639 | |
640 (defun rx-** (form) | |
641 "Parse and produce code from FORM `(** N M ...)'." | |
642 (rx-check form) | |
643 (setq form (cons 'repeat (cdr (rx-trans-forms form 2)))) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
644 (rx-form form '*)) |
55102 | 645 |
646 | |
39516 | 647 (defun rx-repeat (form) |
648 "Parse and produce code from FORM. | |
649 FORM is either `(repeat N FORM1)' or `(repeat N M FORM1)'." | |
650 (rx-check form) | |
651 (cond ((= (length form) 3) | |
652 (unless (and (integerp (nth 1 form)) | |
653 (> (nth 1 form) 0)) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
654 (error "rx `repeat' requires positive integer first arg")) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
655 (format "%s\\{%d\\}" (rx-form (nth 2 form) '*) (nth 1 form))) |
39516 | 656 ((or (not (integerp (nth 2 form))) |
657 (< (nth 2 form) 0) | |
658 (not (integerp (nth 1 form))) | |
659 (< (nth 1 form) 0) | |
660 (< (nth 2 form) (nth 1 form))) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
661 (error "rx `repeat' range error")) |
39516 | 662 (t |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
663 (format "%s\\{%d,%d\\}" (rx-form (nth 3 form) '*) |
39516 | 664 (nth 1 form) (nth 2 form))))) |
665 | |
666 | |
667 (defun rx-submatch (form) | |
668 "Parse and produce code from FORM, which is `(submatch ...)'." | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
669 (concat "\\(" (mapconcat #'rx-form (cdr form) nil) "\\)")) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
670 |
39516 | 671 |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
672 (defun rx-backref (form) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
673 "Parse and produce code from FORM, which is `(backref N)'." |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
674 (rx-check form) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
675 (format "\\%d" (nth 1 form))) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
676 |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
677 (defun rx-check-backref (arg) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
678 "Check arg ARG for Rx `backref'." |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
679 (or (and (integerp arg) (>= arg 1) (<= arg 9)) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
680 (error "rx `backref' requires numeric 1<=arg<=9: %s" arg))) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
681 |
39516 | 682 (defun rx-kleene (form) |
683 "Parse and produce code from FORM. | |
684 FORM is `(OP FORM1)', where OP is one of the `zero-or-one', | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
685 `zero-or-more' etc. operators. |
39516 | 686 If OP is one of `*', `+', `?', produce a greedy regexp. |
687 If OP is one of `*?', `+?', `??', produce a non-greedy regexp. | |
688 If OP is anything else, produce a greedy regexp if `rx-greedy-flag' | |
689 is non-nil." | |
690 (rx-check form) | |
55102 | 691 (setq form (rx-trans-forms form)) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
692 (let ((suffix (cond ((memq (car form) '(* + ?\s)) "") |
39516 | 693 ((memq (car form) '(*? +? ??)) "?") |
694 (rx-greedy-flag "") | |
695 (t "?"))) | |
696 (op (cond ((memq (car form) '(* *? 0+ zero-or-more)) "*") | |
697 ((memq (car form) '(+ +? 1+ one-or-more)) "+") | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
698 (t "?")))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
699 (rx-group-if |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
700 (concat (rx-form (cadr form) '*) op suffix) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
701 (and (memq rx-parent '(t *)) rx-parent)))) |
48938
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
702 |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
703 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
704 (defun rx-atomic-p (r &optional lax) |
48938
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
705 "Return non-nil if regexp string R is atomic. |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
706 An atomic regexp R is one such that a suffix operator |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
707 appended to R will apply to all of R. For example, \"a\" |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
708 \"[abc]\" and \"\\(ab\\|ab*c\\)\" are atomic and \"ab\", |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
709 \"[ab]c\", and \"ab\\|ab*c\" are not atomic. |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
710 |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
711 This function may return false negatives, but it will not |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
712 return false positives. It is nevertheless useful in |
78474
88c9f4e4160e
Replace `iff' in doc-strings and comments.
Glenn Morris <rgm@gnu.org>
parents:
78217
diff
changeset
|
713 situations where an efficiency shortcut can be taken only if a |
48938
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
714 regexp is atomic. The function can be improved to detect |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
715 more cases of atomic regexps. Presently, this function |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
716 detects the following categories of atomic regexp; |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
717 |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
718 a group or shy group: \\(...\\) |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
719 a character class: [...] |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
720 a single character: a |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
721 |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
722 On the other hand, false negatives will be returned for |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
723 regexps that are atomic but end in operators, such as |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
724 \"a+\". I think these are rare. Probably such cases could |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
725 be detected without much effort. A guarantee of no false |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
726 negatives would require a theoretic specification of the set |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
727 of all atomic regexps." |
05f00479612c
(rx-and): Generate a shy group.
Richard M. Stallman <rms@gnu.org>
parents:
47257
diff
changeset
|
728 (let ((l (length r))) |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
729 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
730 ((<= l 1)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
731 ((= l 2) (= (aref r 0) ?\\)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
732 ((= l 3) (string-match "\\`\\(?:\\\\[cCsS_]\\|\\[[^^]\\]\\)" r)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
733 ((null lax) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
734 (cond |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
735 ((string-match "\\`\\[^?\]?\\(?:\\[:[a-z]+:]\\|[^\]]\\)*\\]\\'" r)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
736 ((string-match "\\`\\\\(\\(?:[^\\]\\|\\\\[^\)]\\)*\\\\)\\'" r))))))) |
39516 | 737 |
738 | |
739 (defun rx-syntax (form) | |
740 "Parse and produce code from FORM, which is `(syntax SYMBOL)'." | |
741 (rx-check form) | |
55103
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
742 (let* ((sym (cadr form)) |
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
743 (syntax (assq sym rx-syntax))) |
39516 | 744 (unless syntax |
55103
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
745 ;; Try sregex compatibility. |
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
746 (let ((name (symbol-name sym))) |
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
747 (if (= 1 (length name)) |
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
748 (setq syntax (rassq (aref name 0) rx-syntax)))) |
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
749 (unless syntax |
93f6ab2a0eb5
(rx-syntax): Move sregex style syntax to code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55102
diff
changeset
|
750 (error "Unknown rx syntax `%s'" (cadr form)))) |
39516 | 751 (format "\\s%c" (cdr syntax)))) |
752 | |
753 | |
754 (defun rx-check-category (form) | |
755 "Check the argument FORM of a `(category FORM)'." | |
756 (unless (or (integerp form) | |
757 (cdr (assq form rx-categories))) | |
758 (error "Unknown category `%s'" form)) | |
759 t) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
760 |
39516 | 761 |
762 (defun rx-category (form) | |
55102 | 763 "Parse and produce code from FORM, which is `(category SYMBOL)'." |
39516 | 764 (rx-check form) |
765 (let ((char (if (integerp (cadr form)) | |
766 (cadr form) | |
767 (cdr (assq (cadr form) rx-categories))))) | |
768 (format "\\c%c" char))) | |
769 | |
770 | |
771 (defun rx-eval (form) | |
772 "Parse and produce code from FORM, which is `(eval FORM)'." | |
773 (rx-check form) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
774 (rx-form (eval (cadr form)) rx-parent)) |
39516 | 775 |
776 | |
777 (defun rx-greedy (form) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
778 "Parse and produce code from FORM. |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
779 If FORM is '(minimal-match FORM1)', non-greedy versions of `*', |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
780 `+', and `?' operators will be used in FORM1. If FORM is |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
781 '(maximal-match FORM1)', greedy operators will be used." |
39516 | 782 (rx-check form) |
783 (let ((rx-greedy-flag (eq (car form) 'maximal-match))) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
784 (rx-form (cadr form) rx-parent))) |
39516 | 785 |
786 | |
787 (defun rx-regexp (form) | |
788 "Parse and produce code from FORM, which is `(regexp STRING)'." | |
789 (rx-check form) | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
790 (rx-group-if (cadr form) rx-parent)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
791 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
792 |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
793 (defun rx-form (form &optional rx-parent) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
794 "Parse and produce code for regular expression FORM. |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
795 FORM is a regular expression in sexp form. |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
796 RX-PARENT shows which type of expression calls and controls putting of |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
797 shy groups around the result and some more in other functions." |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
798 (if (stringp form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
799 (rx-group-if (regexp-quote form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
800 (if (and (eq rx-parent '*) (< 1 (length form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
801 rx-parent)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
802 (cond ((integerp form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
803 (regexp-quote (char-to-string form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
804 ((symbolp form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
805 (let ((info (rx-info form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
806 (cond ((stringp info) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
807 info) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
808 ((null info) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
809 (error "Unknown rx form `%s'" form)) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
810 (t |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
811 (funcall (nth 0 info) form))))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
812 ((consp form) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
813 (let ((info (rx-info (car form)))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
814 (unless (consp info) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
815 (error "Unknown rx form `%s'" (car form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
816 (funcall (nth 0 info) form))) |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
817 (t |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
818 (error "rx syntax error at `%s'" form))))) |
39516 | 819 |
820 | |
821 ;;;###autoload | |
822 (defun rx-to-string (form &optional no-group) | |
823 "Parse and produce code for regular expression FORM. | |
824 FORM is a regular expression in sexp form. | |
825 NO-GROUP non-nil means don't put shy groups around the result." | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
826 (rx-group-if (rx-form form) (null no-group))) |
39516 | 827 |
828 | |
829 ;;;###autoload | |
55102 | 830 (defmacro rx (&rest regexps) |
831 "Translate regular expressions REGEXPS in sexp form to a regexp string. | |
832 REGEXPS is a non-empty sequence of forms of the sort listed below. | |
39516 | 833 See also `rx-to-string' for how to do such a translation at run-time. |
834 | |
835 The following are valid subforms of regular expressions in sexp | |
836 notation. | |
837 | |
838 STRING | |
839 matches string STRING literally. | |
840 | |
841 CHAR | |
842 matches character CHAR literally. | |
843 | |
55102 | 844 `not-newline', `nonl' |
39516 | 845 matches any character except a newline. |
83899
bdeef0472e21
(rx): Fix typo in docstring.
Michaël Cadilhac <michael.cadilhac@lrde.org>
parents:
82365
diff
changeset
|
846 |
39516 | 847 `anything' |
848 matches any character | |
849 | |
55102 | 850 `(any SET ...)' |
851 `(in SET ...)' | |
852 `(char SET ...)' | |
853 matches any character in SET .... SET may be a character or string. | |
39516 | 854 Ranges of characters can be specified as `A-Z' in strings. |
55102 | 855 Ranges may also be specified as conses like `(?A . ?Z)'. |
39516 | 856 |
55102 | 857 SET may also be the name of a character class: `digit', |
858 `control', `hex-digit', `blank', `graph', `print', `alnum', | |
859 `alpha', `ascii', `nonascii', `lower', `punct', `space', `upper', | |
860 `word', or one of their synonyms. | |
39516 | 861 |
55102 | 862 `(not (any SET ...))' |
863 matches any character not in SET ... | |
39516 | 864 |
55102 | 865 `line-start', `bol' |
39516 | 866 matches the empty string, but only at the beginning of a line |
867 in the text being matched | |
868 | |
55102 | 869 `line-end', `eol' |
39516 | 870 is similar to `line-start' but matches only at the end of a line |
871 | |
55102 | 872 `string-start', `bos', `bot' |
39516 | 873 matches the empty string, but only at the beginning of the |
874 string being matched against. | |
875 | |
55102 | 876 `string-end', `eos', `eot' |
39516 | 877 matches the empty string, but only at the end of the |
878 string being matched against. | |
879 | |
880 `buffer-start' | |
881 matches the empty string, but only at the beginning of the | |
55102 | 882 buffer being matched against. Actually equivalent to `string-start'. |
39516 | 883 |
884 `buffer-end' | |
885 matches the empty string, but only at the end of the | |
55102 | 886 buffer being matched against. Actually equivalent to `string-end'. |
39516 | 887 |
888 `point' | |
889 matches the empty string, but only at point. | |
890 | |
55102 | 891 `word-start', `bow' |
77829
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
892 matches the empty string, but only at the beginning of a word. |
39516 | 893 |
55102 | 894 `word-end', `eow' |
39516 | 895 matches the empty string, but only at the end of a word. |
896 | |
897 `word-boundary' | |
898 matches the empty string, but only at the beginning or end of a | |
899 word. | |
900 | |
901 `(not word-boundary)' | |
55102 | 902 `not-word-boundary' |
39516 | 903 matches the empty string, but not at the beginning or end of a |
904 word. | |
905 | |
77829
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
906 `symbol-start' |
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
907 matches the empty string, but only at the beginning of a symbol. |
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
908 |
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
909 `symbol-end' |
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
910 matches the empty string, but only at the end of a symbol. |
d858d80ae609
Nikolaj Schumacher <n_schumacher at web.de> (tiny change)
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
911 |
55102 | 912 `digit', `numeric', `num' |
39516 | 913 matches 0 through 9. |
914 | |
55102 | 915 `control', `cntrl' |
39516 | 916 matches ASCII control characters. |
917 | |
55102 | 918 `hex-digit', `hex', `xdigit' |
39516 | 919 matches 0 through 9, a through f and A through F. |
920 | |
921 `blank' | |
922 matches space and tab only. | |
923 | |
55102 | 924 `graphic', `graph' |
39516 | 925 matches graphic characters--everything except ASCII control chars, |
926 space, and DEL. | |
927 | |
55102 | 928 `printing', `print' |
39516 | 929 matches printing characters--everything except ASCII control chars |
930 and DEL. | |
931 | |
55102 | 932 `alphanumeric', `alnum' |
39516 | 933 matches letters and digits. (But at present, for multibyte characters, |
934 it matches anything that has word syntax.) | |
935 | |
55102 | 936 `letter', `alphabetic', `alpha' |
39516 | 937 matches letters. (But at present, for multibyte characters, |
938 it matches anything that has word syntax.) | |
939 | |
940 `ascii' | |
941 matches ASCII (unibyte) characters. | |
942 | |
943 `nonascii' | |
944 matches non-ASCII (multibyte) characters. | |
945 | |
55102 | 946 `lower', `lower-case' |
39516 | 947 matches anything lower-case. |
948 | |
55102 | 949 `upper', `upper-case' |
39516 | 950 matches anything upper-case. |
951 | |
55102 | 952 `punctuation', `punct' |
39516 | 953 matches punctuation. (But at present, for multibyte characters, |
954 it matches anything that has non-word syntax.) | |
955 | |
55102 | 956 `space', `whitespace', `white' |
39516 | 957 matches anything that has whitespace syntax. |
958 | |
55102 | 959 `word', `wordchar' |
39516 | 960 matches anything that has word syntax. |
961 | |
55102 | 962 `not-wordchar' |
963 matches anything that has non-word syntax. | |
964 | |
39516 | 965 `(syntax SYNTAX)' |
966 matches a character with syntax SYNTAX. SYNTAX must be one | |
55102 | 967 of the following symbols, or a symbol corresponding to the syntax |
968 character, e.g. `\\.' for `\\s.'. | |
39516 | 969 |
970 `whitespace' (\\s- in string notation) | |
971 `punctuation' (\\s.) | |
972 `word' (\\sw) | |
973 `symbol' (\\s_) | |
974 `open-parenthesis' (\\s() | |
975 `close-parenthesis' (\\s)) | |
976 `expression-prefix' (\\s') | |
977 `string-quote' (\\s\") | |
978 `paired-delimiter' (\\s$) | |
979 `escape' (\\s\\) | |
980 `character-quote' (\\s/) | |
981 `comment-start' (\\s<) | |
982 `comment-end' (\\s>) | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
983 `string-delimiter' (\\s|) |
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
984 `comment-delimiter' (\\s!) |
39516 | 985 |
986 `(not (syntax SYNTAX))' | |
55102 | 987 matches a character that doesn't have syntax SYNTAX. |
39516 | 988 |
989 `(category CATEGORY)' | |
990 matches a character with category CATEGORY. CATEGORY must be | |
991 either a character to use for C, or one of the following symbols. | |
992 | |
993 `consonant' (\\c0 in string notation) | |
994 `base-vowel' (\\c1) | |
995 `upper-diacritical-mark' (\\c2) | |
996 `lower-diacritical-mark' (\\c3) | |
997 `tone-mark' (\\c4) | |
998 `symbol' (\\c5) | |
999 `digit' (\\c6) | |
1000 `vowel-modifying-diacritical-mark' (\\c7) | |
1001 `vowel-sign' (\\c8) | |
1002 `semivowel-lower' (\\c9) | |
1003 `not-at-end-of-line' (\\c<) | |
1004 `not-at-beginning-of-line' (\\c>) | |
1005 `alpha-numeric-two-byte' (\\cA) | |
1006 `chinse-two-byte' (\\cC) | |
1007 `greek-two-byte' (\\cG) | |
1008 `japanese-hiragana-two-byte' (\\cH) | |
1009 `indian-tow-byte' (\\cI) | |
1010 `japanese-katakana-two-byte' (\\cK) | |
1011 `korean-hangul-two-byte' (\\cN) | |
1012 `cyrillic-two-byte' (\\cY) | |
55102 | 1013 `combining-diacritic' (\\c^) |
39516 | 1014 `ascii' (\\ca) |
1015 `arabic' (\\cb) | |
1016 `chinese' (\\cc) | |
1017 `ethiopic' (\\ce) | |
1018 `greek' (\\cg) | |
1019 `korean' (\\ch) | |
1020 `indian' (\\ci) | |
1021 `japanese' (\\cj) | |
1022 `japanese-katakana' (\\ck) | |
1023 `latin' (\\cl) | |
1024 `lao' (\\co) | |
1025 `tibetan' (\\cq) | |
1026 `japanese-roman' (\\cr) | |
1027 `thai' (\\ct) | |
1028 `vietnamese' (\\cv) | |
1029 `hebrew' (\\cw) | |
1030 `cyrillic' (\\cy) | |
1031 `can-break' (\\c|) | |
1032 | |
1033 `(not (category CATEGORY))' | |
55102 | 1034 matches a character that doesn't have category CATEGORY. |
39516 | 1035 |
1036 `(and SEXP1 SEXP2 ...)' | |
55102 | 1037 `(: SEXP1 SEXP2 ...)' |
1038 `(seq SEXP1 SEXP2 ...)' | |
1039 `(sequence SEXP1 SEXP2 ...)' | |
39516 | 1040 matches what SEXP1 matches, followed by what SEXP2 matches, etc. |
1041 | |
1042 `(submatch SEXP1 SEXP2 ...)' | |
55102 | 1043 `(group SEXP1 SEXP2 ...)' |
39516 | 1044 like `and', but makes the match accessible with `match-end', |
1045 `match-beginning', and `match-string'. | |
1046 | |
98557
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
1047 `(group SEXP1 SEXP2 ...)' |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
1048 another name for `submatch'. |
73eaaf9adee6
(rx-constituents): Change `anything' to call
Chong Yidong <cyd@stupidchicken.com>
parents:
98489
diff
changeset
|
1049 |
39516 | 1050 `(or SEXP1 SEXP2 ...)' |
55102 | 1051 `(| SEXP1 SEXP2 ...)' |
39516 | 1052 matches anything that matches SEXP1 or SEXP2, etc. If all |
1053 args are strings, use `regexp-opt' to optimize the resulting | |
1054 regular expression. | |
1055 | |
1056 `(minimal-match SEXP)' | |
1057 produce a non-greedy regexp for SEXP. Normally, regexps matching | |
53992
c5c237251824
(rx-check, rx-check-any, rx-check-not)
Eli Zaretskii <eliz@is.elta.co.il>
parents:
53974
diff
changeset
|
1058 zero or more occurrences of something are \"greedy\" in that they |
39516 | 1059 match as much as they can, as long as the overall regexp can |
1060 still match. A non-greedy regexp matches as little as possible. | |
1061 | |
1062 `(maximal-match SEXP)' | |
47257 | 1063 produce a greedy regexp for SEXP. This is the default. |
39516 | 1064 |
55102 | 1065 Below, `SEXP ...' represents a sequence of regexp forms, treated as if |
1066 enclosed in `(and ...)'. | |
39516 | 1067 |
55102 | 1068 `(zero-or-more SEXP ...)' |
1069 `(0+ SEXP ...)' | |
1070 matches zero or more occurrences of what SEXP ... matches. | |
39516 | 1071 |
55102 | 1072 `(* SEXP ...)' |
1073 like `zero-or-more', but always produces a greedy regexp, independent | |
1074 of `rx-greedy-flag'. | |
39516 | 1075 |
55102 | 1076 `(*? SEXP ...)' |
1077 like `zero-or-more', but always produces a non-greedy regexp, | |
1078 independent of `rx-greedy-flag'. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
1079 |
55102 | 1080 `(one-or-more SEXP ...)' |
1081 `(1+ SEXP ...)' | |
1082 matches one or more occurrences of SEXP ... | |
39516 | 1083 |
55102 | 1084 `(+ SEXP ...)' |
39516 | 1085 like `one-or-more', but always produces a greedy regexp. |
1086 | |
55102 | 1087 `(+? SEXP ...)' |
39516 | 1088 like `one-or-more', but always produces a non-greedy regexp. |
1089 | |
55102 | 1090 `(zero-or-one SEXP ...)' |
1091 `(optional SEXP ...)' | |
1092 `(opt SEXP ...)' | |
39516 | 1093 matches zero or one occurrences of A. |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48938
diff
changeset
|
1094 |
55102 | 1095 `(? SEXP ...)' |
39516 | 1096 like `zero-or-one', but always produces a greedy regexp. |
1097 | |
55102 | 1098 `(?? SEXP ...)' |
39516 | 1099 like `zero-or-one', but always produces a non-greedy regexp. |
1100 | |
1101 `(repeat N SEXP)' | |
55102 | 1102 `(= N SEXP ...)' |
1103 matches N occurrences. | |
1104 | |
1105 `(>= N SEXP ...)' | |
1106 matches N or more occurrences. | |
39516 | 1107 |
1108 `(repeat N M SEXP)' | |
55102 | 1109 `(** N M SEXP ...)' |
1110 matches N to M occurrences. | |
1111 | |
1112 `(backref N)' | |
54461
5c8be4779a36
(rx): Work at compile time, not run time.
Juanma Barranquero <lekktu@gmail.com>
parents:
53992
diff
changeset
|
1113 matches what was matched previously by submatch N. |
5c8be4779a36
(rx): Work at compile time, not run time.
Juanma Barranquero <lekktu@gmail.com>
parents:
53992
diff
changeset
|
1114 |
39516 | 1115 `(eval FORM)' |
54461
5c8be4779a36
(rx): Work at compile time, not run time.
Juanma Barranquero <lekktu@gmail.com>
parents:
53992
diff
changeset
|
1116 evaluate FORM and insert result. If result is a string, |
5c8be4779a36
(rx): Work at compile time, not run time.
Juanma Barranquero <lekktu@gmail.com>
parents:
53992
diff
changeset
|
1117 `regexp-quote' it. |
39516 | 1118 |
1119 `(regexp REGEXP)' | |
54461
5c8be4779a36
(rx): Work at compile time, not run time.
Juanma Barranquero <lekktu@gmail.com>
parents:
53992
diff
changeset
|
1120 include REGEXP in string notation in the result." |
55102 | 1121 (cond ((null regexps) |
1122 (error "No regexp")) | |
1123 ((cdr regexps) | |
1124 (rx-to-string `(and ,@regexps) t)) | |
1125 (t | |
1126 (rx-to-string (car regexps) t)))) | |
1127 | |
1128 ;; ;; sregex.el replacement | |
39516 | 1129 |
55102 | 1130 ;; ;;;###autoload (provide 'sregex) |
1131 ;; ;;;###autoload (autoload 'sregex "rx") | |
1132 ;; (defalias 'sregex 'rx-to-string) | |
1133 ;; ;;;###autoload (autoload 'sregexq "rx" nil nil 'macro) | |
1134 ;; (defalias 'sregexq 'rx) | |
1135 | |
39516 | 1136 (provide 'rx) |
1137 | |
60930
a6ae354aa8ef
(rx-constituents): Add symbol-start and symbol-end.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55103
diff
changeset
|
1138 ;; arch-tag: 12d01a63-0008-42bb-ab8c-1c7d63be370b |
39516 | 1139 ;;; rx.el ends here |