1 ;;; rx.el --- sexp notation for regular expressions
3 ;; Copyright (C) 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Gerd Moellmann <gerd@gnu.org>
7 ;; Keywords: strings, regexps, extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is another implementation of sexp-form regular expressions.
27 ;; It was unfortunately written without being aware of the Sregex
28 ;; package coming with Emacs, but as things stand, Rx completely
29 ;; covers all regexp features, which Sregex doesn't, doesn't suffer
30 ;; from the bugs mentioned in the commentary section of Sregex, and
31 ;; uses a nicer syntax (IMHO, of course :-).
33 ;; This significantly extended version of the original, is almost
34 ;; compatible with Sregex. The only incompatibility I (fx) know of is
35 ;; that the `repeat' form can't have multiple regexp args.
37 ;; Now alternative forms are provided for a degree of compatibility
38 ;; with Shivers' attempted definitive SRE notation
39 ;; <URL:http://www.ai.mit.edu/~/shivers/sre.txt>. SRE forms not
40 ;; catered for include: dsm, uncase, w/case, w/nocase, ,@<exp>,
41 ;; ,<exp>, (word ...), word+, posix-string, and character class forms.
42 ;; Some forms are inconsistent with SRE, either for historical reasons
43 ;; or because of the implementation -- simple translation into Emacs
44 ;; regexp strings. These include: any, word. Also, case-sensitivity
45 ;; and greediness are controlled by variables external to the regexp,
46 ;; and you need to feed the forms to the `posix-' functions to get
47 ;; SRE's POSIX semantics. There are probably more difficulties.
49 ;; Rx translates a sexp notation for regular expressions into the
50 ;; usual string notation. The translation can be done at compile-time
51 ;; by using the `rx' macro. It can be done at run-time by calling
52 ;; function `rx-to-string'. See the documentation of `rx' for a
53 ;; complete description of the sexp notation.
55 ;; Some examples of string regexps and their sexp counterparts:
58 ;; (rx (and line-start (0+ (in "a-z"))))
61 ;; (rx (and "\n" (not blank))), or
62 ;; (rx (and "\n" (not (any " \t"))))
64 ;; "\\*\\*\\* EOOH \\*\\*\\*\n"
65 ;; (rx "*** EOOH ***\n")
67 ;; "\\<\\(catch\\|finally\\)\\>[^_]"
68 ;; (rx (and word-start (submatch (or "catch" "finally")) word-end
71 ;; "[ \t\n]*:\\([^:]+\\|$\\)"
72 ;; (rx (and (zero-or-more (in " \t\n")) ":"
73 ;; (submatch (or line-end (one-or-more (not (any ?:)))))))
75 ;; "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
76 ;; (rx (and line-start
77 ;; "content-transfer-encoding:"
80 ;; (+ (? ?\n)) blank))
82 ;; (concat "^\\(?:" something-else "\\)")
83 ;; (rx (and line-start (eval something-else))), statically or
84 ;; (rx-to-string '(and line-start ,something-else)), dynamically.
86 ;; (regexp-opt '(STRING1 STRING2 ...))
87 ;; (rx (or STRING1 STRING2 ...)), or in other words, `or' automatically
88 ;; calls `regexp-opt' as needed.
91 ;; (rx (or (and line-start ";;" (0+ space) ?\n)
92 ;; (and line-start ?\n)))
94 ;; "\\$[I]d: [^ ]+ \\([^ ]+\\) "
96 ;; (1+ (not (in " ")))
98 ;; (submatch (1+ (not (in " "))))
102 ;; (rx (and ?\\ ?\\ ?\[ (1+ word)))
111 (defconst rx-constituents
112 '((and .
(rx-and 1 nil
))
115 (sequence . and
) ; sregex
119 (nonl . not-newline
) ; SRE
120 (anything .
(rx-anything 0 nil
))
121 (any .
(rx-any 1 nil rx-check-any
)) ; inconsistent with SRE
124 (char . any
) ; sregex
125 (not-char .
(rx-not-char 1 nil rx-check-any
)) ; sregex
126 (not .
(rx-not 1 1 rx-check-not
))
127 (repeat .
(rx-repeat 2 nil
))
128 (= .
(rx-= 2 nil
)) ; SRE
129 (>= .
(rx->= 2 nil
)) ; SRE
130 (** .
(rx-** 2 nil
)) ; SRE
131 (submatch .
(rx-submatch 1 nil
)) ; SRE
132 (group . submatch
) ; sregex
133 (zero-or-more .
(rx-kleene 1 nil
))
134 (one-or-more .
(rx-kleene 1 nil
))
135 (zero-or-one .
(rx-kleene 1 nil
))
136 (\? . zero-or-one
) ; SRE
138 (* . zero-or-more
) ; SRE
141 (+ . one-or-more
) ; SRE
144 (optional . zero-or-one
)
145 (opt . zero-or-one
) ; sregex
146 (minimal-match .
(rx-greedy 1 1))
147 (maximal-match .
(rx-greedy 1 1))
148 (backref .
(rx-backref 1 1 rx-check-backref
))
150 (bol . line-start
) ; SRE
152 (eol . line-end
) ; SRE
153 (string-start .
"\\`")
154 (bos . string-start
) ; SRE
155 (bot . string-start
) ; sregex
157 (eos . string-end
) ; SRE
158 (eot . string-end
) ; sregex
159 (buffer-start .
"\\`")
163 (bow . word-start
) ; SRE
165 (eow . word-end
) ; SRE
166 (word-boundary .
"\\b")
167 (not-word-boundary .
"\\B") ; sregex
168 (symbol-start .
"\\_<")
169 (symbol-end .
"\\_>")
170 (syntax .
(rx-syntax 1 1))
171 (not-syntax .
(rx-not-syntax 1 1)) ; sregex
172 (category .
(rx-category 1 1 rx-check-category
))
173 (eval .
(rx-eval 1 1))
174 (regexp .
(rx-regexp 1 1 stringp
))
175 (regex . regexp
) ; sregex
176 (digit .
"[[:digit:]]")
177 (numeric . digit
) ; SRE
179 (control .
"[[:cntrl:]]") ; SRE
180 (cntrl . control
) ; SRE
181 (hex-digit .
"[[:xdigit:]]") ; SRE
182 (hex . hex-digit
) ; SRE
183 (xdigit . hex-digit
) ; SRE
184 (blank .
"[[:blank:]]") ; SRE
185 (graphic .
"[[:graph:]]") ; SRE
186 (graph . graphic
) ; SRE
187 (printing .
"[[:print:]]") ; SRE
188 (print . printing
) ; SRE
189 (alphanumeric .
"[[:alnum:]]") ; SRE
190 (alnum . alphanumeric
) ; SRE
191 (letter .
"[[:alpha:]]")
192 (alphabetic . letter
) ; SRE
193 (alpha . letter
) ; SRE
194 (ascii .
"[[:ascii:]]") ; SRE
195 (nonascii .
"[[:nonascii:]]")
196 (lower .
"[[:lower:]]") ; SRE
197 (lower-case . lower
) ; SRE
198 (punctuation .
"[[:punct:]]") ; SRE
199 (punct . punctuation
) ; SRE
200 (space .
"[[:space:]]") ; SRE
201 (whitespace . space
) ; SRE
202 (white . space
) ; SRE
203 (upper .
"[[:upper:]]") ; SRE
204 (upper-case . upper
) ; SRE
205 (word .
"[[:word:]]") ; inconsistent with SRE
206 (wordchar . word
) ; sregex
207 (not-wordchar .
"\\W"))
208 "Alist of sexp form regexp constituents.
209 Each element of the alist has the form (SYMBOL . DEFN).
210 SYMBOL is a valid constituent of sexp regular expressions.
211 If DEFN is a string, SYMBOL is translated into DEFN.
212 If DEFN is a symbol, use the definition of DEFN, recursively.
213 Otherwise, DEFN must be a list (FUNCTION MIN-ARGS MAX-ARGS PREDICATE).
214 FUNCTION is used to produce code for SYMBOL. MIN-ARGS and MAX-ARGS
215 are the minimum and maximum number of arguments the function-form
216 sexp constituent SYMBOL may have in sexp regular expressions.
217 MAX-ARGS nil means no limit. PREDICATE, if specified, means that
218 all arguments must satisfy PREDICATE.")
226 (open-parenthesis . ?\
()
227 (close-parenthesis . ?\
))
228 (expression-prefix . ?
\')
230 (paired-delimiter . ?$
)
232 (character-quote . ?
/)
235 (string-delimiter . ?|
)
236 (comment-delimiter . ?
!))
237 "Alist mapping Rx syntax symbols to syntax characters.
238 Each entry has the form (SYMBOL . CHAR), where SYMBOL is a valid
239 symbol in `(syntax SYMBOL)', and CHAR is the syntax character
240 corresponding to SYMBOL, as it would be used with \\s or \\S in
241 regular expressions.")
244 (defconst rx-categories
247 (upper-diacritical-mark . ?
2)
248 (lower-diacritical-mark . ?
3)
252 (vowel-modifying-diacritical-mark . ?
7)
254 (semivowel-lower . ?
9)
255 (not-at-end-of-line . ?
<)
256 (not-at-beginning-of-line . ?
>)
257 (alpha-numeric-two-byte . ?A
)
258 (chinse-two-byte . ?C
)
259 (greek-two-byte . ?G
)
260 (japanese-hiragana-two-byte . ?H
)
261 (indian-two-byte . ?I
)
262 (japanese-katakana-two-byte . ?K
)
263 (korean-hangul-two-byte . ?N
)
264 (cyrillic-two-byte . ?Y
)
265 (combining-diacritic . ?^
)
274 (japanese-katakana . ?k
)
278 (japanese-roman . ?r
)
284 "Alist mapping symbols to category characters.
285 Each entry has the form (SYMBOL . CHAR), where SYMBOL is a valid
286 symbol in `(category SYMBOL)', and CHAR is the category character
287 corresponding to SYMBOL, as it would be used with `\\c' or `\\C' in
288 regular expression strings.")
291 (defvar rx-greedy-flag t
292 "Non-nil means produce greedy regular expressions for `zero-or-one',
293 `zero-or-more', and `one-or-more'. Dynamically bound.")
296 (defun rx-info (op head
)
297 "Return parsing/code generation info for OP.
298 If OP is the space character ASCII 32, return info for the symbol `?'.
299 If OP is the character `?', return info for the symbol `??'.
300 See also `rx-constituents'.
301 If HEAD is non-nil, then OP is the head of a sexp, otherwise it's
302 a standalone symbol."
303 (cond ((eq op ?
) (setq op
'\?))
304 ((eq op ??
) (setq op
'\??
)))
306 (while (and (not (null op
)) (symbolp op
))
308 (setq op
(cdr (assq op rx-constituents
)))
309 (when (if head
(stringp op
) (consp op
))
310 ;; We found something but of the wrong kind. Let's look for an
311 ;; alternate definition for the other case.
313 (cdr (assq old-op
(cdr (memq (assq old-op rx-constituents
)
314 rx-constituents
))))))
315 (if (and new-op
(not (if head
(stringp new-op
) (consp new-op
))))
316 (setq op new-op
))))))
320 (defun rx-check (form)
321 "Check FORM according to its car's parsing info."
323 (error "rx `%s' needs argument(s)" form
))
324 (let* ((rx (rx-info (car form
) 'head
))
325 (nargs (1- (length form
)))
326 (min-args (nth 1 rx
))
327 (max-args (nth 2 rx
))
328 (type-pred (nth 3 rx
)))
329 (when (and (not (null min-args
))
331 (error "rx form `%s' requires at least %d args"
332 (car form
) min-args
))
333 (when (and (not (null max-args
))
335 (error "rx form `%s' accepts at most %d args"
336 (car form
) max-args
))
337 (when (not (null type-pred
))
338 (dolist (sub-form (cdr form
))
339 (unless (funcall type-pred sub-form
)
340 (error "rx form `%s' requires args satisfying `%s'"
341 (car form
) type-pred
))))))
344 (defun rx-group-if (regexp group
)
345 "Put shy groups around REGEXP if seemingly necessary when GROUP
348 ;; for some repetition
349 ((eq group
'*) (if (rx-atomic-p regexp
) (setq group nil
)))
354 "\\(?:[?*+]\\??\\|\\\\{[0-9]*,?[0-9]*\\\\}\\)\\'" regexp
)
355 (substring regexp
0 (match-beginning 0))
359 ((eq group
'|
) (setq group nil
))
362 ((rx-atomic-p regexp t
) (setq group nil
)))
364 (concat "\\(?:" regexp
"\\)")
369 ;; dynamically bound in some functions.
373 "Parse and produce code from FORM.
374 FORM is of the form `(and FORM1 ...)'."
377 (mapconcat (lambda (x) (rx-form x
':)) (cdr form
) nil
)
378 (and (memq rx-parent
'(* t
)) rx-parent
)))
382 "Parse and produce code from FORM, which is `(or FORM1 ...)'."
385 (if (memq nil
(mapcar 'stringp
(cdr form
)))
386 (mapconcat (lambda (x) (rx-form x
'|
)) (cdr form
) "\\|")
387 (regexp-opt (cdr form
)))
388 (and (memq rx-parent
'(: * t
)) rx-parent
)))
391 (defun rx-anything (form)
392 "Match any character."
394 (error "rx `anythng' syntax error: %s" form
))
395 (rx-or (list 'or
'not-newline ?
\n)))
398 (defun rx-any-delete-from-range (char ranges
)
399 "Delete by side effect character CHAR from RANGES.
400 Only both edges of each range is checked."
403 ((memq char ranges
) (setq ranges
(delq char ranges
)))
404 ((setq m
(assq char ranges
))
405 (if (eq (1+ char
) (cdr m
))
406 (setcar (memq m ranges
) (1+ char
))
407 (setcar m
(1+ char
))))
408 ((setq m
(rassq char ranges
))
409 (if (eq (1- char
) (car m
))
410 (setcar (memq m ranges
) (1- char
))
411 (setcdr m
(1- char
)))))
415 (defun rx-any-condense-range (args)
416 "Condense by side effect ARGS as range for Rx `any'."
419 ;; set STR list of all strings
420 ;; set L list of all ranges
421 (mapc (lambda (e) (cond ((stringp e
) (push e str
))
422 ((numberp e
) (push (cons e e
) l
))
425 ;; condense overlapped ranges in L
426 (let ((tail (setq l
(sort l
#'car-less-than-car
)))
428 (while (setq d
(cdr tail
))
429 (if (>= (cdar tail
) (1- (caar d
)))
431 (setcdr (car tail
) (max (cdar tail
) (cdar d
)))
432 (setcdr tail
(cdr d
)))
434 ;; Separate small ranges to single number, and delete dups.
439 ((= (car e
) (cdr e
)) (list (car e
)))
440 ((= (1+ (car e
)) (cdr e
)) (list (car e
) (cdr e
)))
446 (defun rx-check-any-string (str)
447 "Check string argument STR for Rx `any'."
450 (if (= 0 (length str
))
451 (error "String arg for Rx `any' must not be empty"))
452 (while (string-match ".-." str i
)
453 ;; string before range: convert it to characters
454 (if (< i
(match-beginning 0))
457 (append (substring str i
(match-beginning 0)) nil
))))
459 (setq i
(match-end 0)
460 c1
(aref str
(match-beginning 0))
461 c2
(aref str
(1- i
)))
463 ((< c1 c2
) (setq l
(nconc l
(list (cons c1 c2
)))))
464 ((= c1 c2
) (setq l
(nconc l
(list c1
))))))
466 (if (< i
(length str
))
467 (setq l
(nconc l
(append (substring str i
) nil
))))
471 (defun rx-check-any (arg)
472 "Check arg ARG for Rx `any'."
474 ((integerp arg
) (list arg
))
476 (let ((translation (condition-case nil
479 (if (or (null translation
)
480 (null (string-match "\\`\\[\\[:[-a-z]+:\\]\\]\\'" translation
)))
481 (error "Invalid char class `%s' in Rx `any'" arg
))
482 (list (substring translation
1 -
1)))) ; strip outer brackets
483 ((and (integerp (car-safe arg
)) (integerp (cdr-safe arg
)))
485 ((stringp arg
) (rx-check-any-string arg
))
487 "rx `any' requires string, character, char pair or char class args"))))
491 "Parse and produce code from FORM, which is `(any ARG ...)'.
494 (let* ((args (rx-any-condense-range
497 (mapcar #'rx-check-any
(cdr form
)))))
501 ;; single close bracket
502 ;; => "[]...-]" or "[]...--.]"
504 ;; set ] at the beginning
505 (setq args
(cons ?\
] (delq ?\
] args
)))
507 (if (or (memq ?- args
) (assq ?- args
))
508 (setq args
(nconc (rx-any-delete-from-range ?- args
)
510 ;; close bracket starts a range
511 ;; => "[]-....-]" or "[]-.--....]"
512 ((setq m
(assq ?\
] args
))
513 ;; bring it to the beginning
514 (setq args
(cons m
(delq m args
)))
515 (cond ((memq ?- args
)
517 (setq args
(nconc (delq ?- args
) (list ?-
))))
518 ((setq m
(assq ?- args
))
519 ;; next to the bracket's range, make the second range
520 (setcdr args
(cons m
(delq m args
))))))
521 ;; bracket in the end range
523 ((setq m
(rassq ?\
] args
))
524 ;; set ] at the beginning
525 (setq args
(cons ?\
] (rx-any-delete-from-range ?\
] args
)))
527 (if (or (memq ?- args
) (assq ?- args
))
528 (setq args
(nconc (rx-any-delete-from-range ?- args
)
530 ;; {no close bracket appears}
532 ;; bring single bar to the beginning
534 (setq args
(cons ?-
(delq ?- args
))))
535 ;; bar start a range, bring it to the beginning
536 ((setq m
(assq ?- args
))
537 (setq args
(cons m
(delq m args
))))
539 ;; hat at the beginning?
540 ((or (eq (car args
) ?^
) (eq (car-safe (car args
)) ?^
))
541 (setq args
(if (cdr args
)
542 `(,(cadr args
) ,(car args
) ,@(cddr args
))
543 (nconc (rx-any-delete-from-range ?^ args
)
546 (if (and (null (cdr args
)) (numberp (car args
))
548 (setq s
(regexp-quote (string (car args
))))))
549 (and (equal (car args
) ?^
) ;; unnecessary predicate?
550 (null (eq rx-parent
'!)))))
555 ((numberp e
) (string e
))
557 (if (and (= (1+ (car e
)) (cdr e
))
558 ;; rx-any-condense-range should
559 ;; prevent this case from happening.
560 (null (memq (car e
) '(?\
] ?-
)))
561 (null (memq (cdr e
) '(?\
] ?-
))))
562 (string (car e
) (cdr e
))
563 (string (car e
) ?-
(cdr e
))))
570 (defun rx-check-not (arg)
571 "Check arg ARG for Rx `not'."
572 (unless (or (and (symbolp arg
)
573 (string-match "\\`\\[\\[:[-a-z]+:\\]\\]\\'"
577 (eq arg
'word-boundary
)
579 (memq (car arg
) '(not any in syntax category
))))
580 (error "rx `not' syntax error: %s" arg
))
585 "Parse and produce code from FORM. FORM is `(not ...)'."
587 (let ((result (rx-form (cadr form
) '!))
589 (cond ((string-match "\\`\\[^" result
)
591 ((equal result
"[^]") "[^^]")
592 ((and (= (length result
) 4) (null (eq rx-parent
'!)))
593 (regexp-quote (substring result
2 3)))
594 ((concat "[" (substring result
2)))))
595 ((eq ?\
[ (aref result
0))
596 (concat "[^" (substring result
1)))
597 ((string-match "\\`\\\\[scbw]" result
)
598 (concat (upcase (substring result
0 2))
599 (substring result
2)))
600 ((string-match "\\`\\\\[SCBW]" result
)
601 (concat (downcase (substring result
0 2))
602 (substring result
2)))
604 (concat "[^" result
"]")))))
607 (defun rx-not-char (form)
608 "Parse and produce code from FORM. FORM is `(not-char ...)'."
610 (rx-not `(not (in ,@(cdr form
)))))
613 (defun rx-not-syntax (form)
614 "Parse and produce code from FORM. FORM is `(not-syntax SYNTAX)'."
616 (rx-not `(not (syntax ,@(cdr form
)))))
619 (defun rx-trans-forms (form &optional skip
)
620 "If FORM's length is greater than two, transform it to length two.
621 A form (HEAD REST ...) becomes (HEAD (and REST ...)).
622 If SKIP is non-nil, allow that number of items after the head, i.e.
623 `(= N REST ...)' becomes `(= N (and REST ...))' if SKIP is 1."
624 (unless skip
(setq skip
0))
625 (let ((tail (nthcdr (1+ skip
) form
)))
626 (if (= (length tail
) 1)
628 (let ((form (copy-sequence form
)))
629 (setcdr (nthcdr skip form
) (list (cons 'and tail
)))
634 "Parse and produce code from FORM `(= N ...)'."
636 (setq form
(rx-trans-forms form
1))
637 (unless (and (integerp (nth 1 form
))
639 (error "rx `=' requires positive integer first arg"))
640 (format "%s\\{%d\\}" (rx-form (nth 2 form
) '*) (nth 1 form
)))
644 "Parse and produce code from FORM `(>= N ...)'."
646 (setq form
(rx-trans-forms form
1))
647 (unless (and (integerp (nth 1 form
))
649 (error "rx `>=' requires positive integer first arg"))
650 (format "%s\\{%d,\\}" (rx-form (nth 2 form
) '*) (nth 1 form
)))
654 "Parse and produce code from FORM `(** N M ...)'."
656 (rx-form (cons 'repeat
(cdr (rx-trans-forms form
2))) '*))
659 (defun rx-repeat (form)
660 "Parse and produce code from FORM.
661 FORM is either `(repeat N FORM1)' or `(repeat N M FORMS...)'."
663 (if (> (length form
) 4)
664 (setq form
(rx-trans-forms form
2)))
665 (if (null (nth 2 form
))
666 (setq form
(cons (nth 0 form
) (cons (nth 1 form
) (nthcdr 3 form
)))))
667 (cond ((= (length form
) 3)
668 (unless (and (integerp (nth 1 form
))
670 (error "rx `repeat' requires positive integer first arg"))
671 (format "%s\\{%d\\}" (rx-form (nth 2 form
) '*) (nth 1 form
)))
672 ((or (not (integerp (nth 2 form
)))
674 (not (integerp (nth 1 form
)))
676 (< (nth 2 form
) (nth 1 form
)))
677 (error "rx `repeat' range error"))
679 (format "%s\\{%d,%d\\}" (rx-form (nth 3 form
) '*)
680 (nth 1 form
) (nth 2 form
)))))
683 (defun rx-submatch (form)
684 "Parse and produce code from FORM, which is `(submatch ...)'."
686 (if (= 2 (length form
))
687 ;; Only one sub-form.
688 (rx-form (cadr form
))
689 ;; Several sub-forms implicitly concatenated.
690 (mapconcat (lambda (re) (rx-form re
':)) (cdr form
) nil
))
694 (defun rx-backref (form)
695 "Parse and produce code from FORM, which is `(backref N)'."
697 (format "\\%d" (nth 1 form
)))
699 (defun rx-check-backref (arg)
700 "Check arg ARG for Rx `backref'."
701 (or (and (integerp arg
) (>= arg
1) (<= arg
9))
702 (error "rx `backref' requires numeric 1<=arg<=9: %s" arg
)))
704 (defun rx-kleene (form)
705 "Parse and produce code from FORM.
706 FORM is `(OP FORM1)', where OP is one of the `zero-or-one',
707 `zero-or-more' etc. operators.
708 If OP is one of `*', `+', `?', produce a greedy regexp.
709 If OP is one of `*?', `+?', `??', produce a non-greedy regexp.
710 If OP is anything else, produce a greedy regexp if `rx-greedy-flag'
713 (setq form
(rx-trans-forms form
))
714 (let ((suffix (cond ((memq (car form
) '(* + ?\s
)) "")
715 ((memq (car form
) '(*?
+? ??
)) "?")
718 (op (cond ((memq (car form
) '(* *?
0+ zero-or-more
)) "*")
719 ((memq (car form
) '(+ +?
1+ one-or-more
)) "+")
722 (concat (rx-form (cadr form
) '*) op suffix
)
723 (and (memq rx-parent
'(t *)) rx-parent
))))
726 (defun rx-atomic-p (r &optional lax
)
727 "Return non-nil if regexp string R is atomic.
728 An atomic regexp R is one such that a suffix operator
729 appended to R will apply to all of R. For example, \"a\"
730 \"[abc]\" and \"\\(ab\\|ab*c\\)\" are atomic and \"ab\",
731 \"[ab]c\", and \"ab\\|ab*c\" are not atomic.
733 This function may return false negatives, but it will not
734 return false positives. It is nevertheless useful in
735 situations where an efficiency shortcut can be taken only if a
736 regexp is atomic. The function can be improved to detect
737 more cases of atomic regexps. Presently, this function
738 detects the following categories of atomic regexp;
740 a group or shy group: \\(...\\)
741 a character class: [...]
742 a single character: a
744 On the other hand, false negatives will be returned for
745 regexps that are atomic but end in operators, such as
746 \"a+\". I think these are rare. Probably such cases could
747 be detected without much effort. A guarantee of no false
748 negatives would require a theoretic specification of the set
749 of all atomic regexps."
750 (let ((l (length r
)))
753 ((= l
2) (= (aref r
0) ?
\\))
754 ((= l
3) (string-match "\\`\\(?:\\\\[cCsS_]\\|\\[[^^]\\]\\)" r
))
757 ((string-match "\\`\\[^?\]?\\(?:\\[:[a-z]+:]\\|[^\]]\\)*\\]\\'" r
))
758 ((string-match "\\`\\\\(\\(?:[^\\]\\|\\\\[^\)]\\)*\\\\)\\'" r
)))))))
761 (defun rx-syntax (form)
762 "Parse and produce code from FORM, which is `(syntax SYMBOL)'."
764 (let* ((sym (cadr form
))
765 (syntax (cdr (assq sym rx-syntax
))))
767 ;; Try sregex compatibility.
769 ((characterp sym
) (setq syntax sym
))
771 (let ((name (symbol-name sym
)))
772 (if (= 1 (length name
))
773 (setq syntax
(aref name
0))))))
775 (error "Unknown rx syntax `%s'" sym
)))
776 (format "\\s%c" syntax
)))
779 (defun rx-check-category (form)
780 "Check the argument FORM of a `(category FORM)'."
781 (unless (or (integerp form
)
782 (cdr (assq form rx-categories
)))
783 (error "Unknown category `%s'" form
))
787 (defun rx-category (form)
788 "Parse and produce code from FORM, which is `(category SYMBOL)'."
790 (let ((char (if (integerp (cadr form
))
792 (cdr (assq (cadr form
) rx-categories
)))))
793 (format "\\c%c" char
)))
796 (defun rx-eval (form)
797 "Parse and produce code from FORM, which is `(eval FORM)'."
799 (rx-form (eval (cadr form
)) rx-parent
))
802 (defun rx-greedy (form)
803 "Parse and produce code from FORM.
804 If FORM is '(minimal-match FORM1)', non-greedy versions of `*',
805 `+', and `?' operators will be used in FORM1. If FORM is
806 '(maximal-match FORM1)', greedy operators will be used."
808 (let ((rx-greedy-flag (eq (car form
) 'maximal-match
)))
809 (rx-form (cadr form
) rx-parent
)))
812 (defun rx-regexp (form)
813 "Parse and produce code from FORM, which is `(regexp STRING)'."
815 (rx-group-if (cadr form
) rx-parent
))
818 (defun rx-form (form &optional rx-parent
)
819 "Parse and produce code for regular expression FORM.
820 FORM is a regular expression in sexp form.
821 RX-PARENT shows which type of expression calls and controls putting of
822 shy groups around the result and some more in other functions."
824 (rx-group-if (regexp-quote form
)
825 (if (and (eq rx-parent
'*) (< 1 (length form
)))
827 (cond ((integerp form
)
828 (regexp-quote (char-to-string form
)))
830 (let ((info (rx-info form nil
)))
831 (cond ((stringp info
)
834 (error "Unknown rx form `%s'" form
))
836 (funcall (nth 0 info
) form
)))))
838 (let ((info (rx-info (car form
) 'head
)))
840 (error "Unknown rx form `%s'" (car form
)))
841 (funcall (nth 0 info
) form
)))
843 (error "rx syntax error at `%s'" form
)))))
847 (defun rx-to-string (form &optional no-group
)
848 "Parse and produce code for regular expression FORM.
849 FORM is a regular expression in sexp form.
850 NO-GROUP non-nil means don't put shy groups around the result."
851 (rx-group-if (rx-form form
) (null no-group
)))
855 (defmacro rx
(&rest regexps
)
856 "Translate regular expressions REGEXPS in sexp form to a regexp string.
857 REGEXPS is a non-empty sequence of forms of the sort listed below.
859 Note that `rx' is a Lisp macro; when used in a Lisp program being
860 compiled, the translation is performed by the compiler.
861 See `rx-to-string' for how to do such a translation at run-time.
863 The following are valid subforms of regular expressions in sexp
867 matches string STRING literally.
870 matches character CHAR literally.
872 `not-newline', `nonl'
873 matches any character except a newline.
876 matches any character
881 matches any character in SET .... SET may be a character or string.
882 Ranges of characters can be specified as `A-Z' in strings.
883 Ranges may also be specified as conses like `(?A . ?Z)'.
885 SET may also be the name of a character class: `digit',
886 `control', `hex-digit', `blank', `graph', `print', `alnum',
887 `alpha', `ascii', `nonascii', `lower', `punct', `space', `upper',
888 `word', or one of their synonyms.
890 `(not (any SET ...))'
891 matches any character not in SET ...
894 matches the empty string, but only at the beginning of a line
895 in the text being matched
898 is similar to `line-start' but matches only at the end of a line
900 `string-start', `bos', `bot'
901 matches the empty string, but only at the beginning of the
902 string being matched against.
904 `string-end', `eos', `eot'
905 matches the empty string, but only at the end of the
906 string being matched against.
909 matches the empty string, but only at the beginning of the
910 buffer being matched against. Actually equivalent to `string-start'.
913 matches the empty string, but only at the end of the
914 buffer being matched against. Actually equivalent to `string-end'.
917 matches the empty string, but only at point.
920 matches the empty string, but only at the beginning of a word.
923 matches the empty string, but only at the end of a word.
926 matches the empty string, but only at the beginning or end of a
929 `(not word-boundary)'
931 matches the empty string, but not at the beginning or end of a
935 matches the empty string, but only at the beginning of a symbol.
938 matches the empty string, but only at the end of a symbol.
940 `digit', `numeric', `num'
944 matches ASCII control characters.
946 `hex-digit', `hex', `xdigit'
947 matches 0 through 9, a through f and A through F.
950 matches space and tab only.
953 matches graphic characters--everything except ASCII control chars,
957 matches printing characters--everything except ASCII control chars
960 `alphanumeric', `alnum'
961 matches letters and digits. (But at present, for multibyte characters,
962 it matches anything that has word syntax.)
964 `letter', `alphabetic', `alpha'
965 matches letters. (But at present, for multibyte characters,
966 it matches anything that has word syntax.)
969 matches ASCII (unibyte) characters.
972 matches non-ASCII (multibyte) characters.
974 `lower', `lower-case'
975 matches anything lower-case.
977 `upper', `upper-case'
978 matches anything upper-case.
980 `punctuation', `punct'
981 matches punctuation. (But at present, for multibyte characters,
982 it matches anything that has non-word syntax.)
984 `space', `whitespace', `white'
985 matches anything that has whitespace syntax.
988 matches anything that has word syntax.
991 matches anything that has non-word syntax.
994 matches a character with syntax SYNTAX. SYNTAX must be one
995 of the following symbols, or a symbol corresponding to the syntax
996 character, e.g. `\\.' for `\\s.'.
998 `whitespace' (\\s- in string notation)
1002 `open-parenthesis' (\\s()
1003 `close-parenthesis' (\\s))
1004 `expression-prefix' (\\s')
1005 `string-quote' (\\s\")
1006 `paired-delimiter' (\\s$)
1008 `character-quote' (\\s/)
1009 `comment-start' (\\s<)
1010 `comment-end' (\\s>)
1011 `string-delimiter' (\\s|)
1012 `comment-delimiter' (\\s!)
1014 `(not (syntax SYNTAX))'
1015 matches a character that doesn't have syntax SYNTAX.
1017 `(category CATEGORY)'
1018 matches a character with category CATEGORY. CATEGORY must be
1019 either a character to use for C, or one of the following symbols.
1021 `consonant' (\\c0 in string notation)
1023 `upper-diacritical-mark' (\\c2)
1024 `lower-diacritical-mark' (\\c3)
1028 `vowel-modifying-diacritical-mark' (\\c7)
1030 `semivowel-lower' (\\c9)
1031 `not-at-end-of-line' (\\c<)
1032 `not-at-beginning-of-line' (\\c>)
1033 `alpha-numeric-two-byte' (\\cA)
1034 `chinse-two-byte' (\\cC)
1035 `greek-two-byte' (\\cG)
1036 `japanese-hiragana-two-byte' (\\cH)
1037 `indian-tow-byte' (\\cI)
1038 `japanese-katakana-two-byte' (\\cK)
1039 `korean-hangul-two-byte' (\\cN)
1040 `cyrillic-two-byte' (\\cY)
1041 `combining-diacritic' (\\c^)
1050 `japanese-katakana' (\\ck)
1054 `japanese-roman' (\\cr)
1061 `(not (category CATEGORY))'
1062 matches a character that doesn't have category CATEGORY.
1064 `(and SEXP1 SEXP2 ...)'
1065 `(: SEXP1 SEXP2 ...)'
1066 `(seq SEXP1 SEXP2 ...)'
1067 `(sequence SEXP1 SEXP2 ...)'
1068 matches what SEXP1 matches, followed by what SEXP2 matches, etc.
1070 `(submatch SEXP1 SEXP2 ...)'
1071 `(group SEXP1 SEXP2 ...)'
1072 like `and', but makes the match accessible with `match-end',
1073 `match-beginning', and `match-string'.
1075 `(or SEXP1 SEXP2 ...)'
1076 `(| SEXP1 SEXP2 ...)'
1077 matches anything that matches SEXP1 or SEXP2, etc. If all
1078 args are strings, use `regexp-opt' to optimize the resulting
1081 `(minimal-match SEXP)'
1082 produce a non-greedy regexp for SEXP. Normally, regexps matching
1083 zero or more occurrences of something are \"greedy\" in that they
1084 match as much as they can, as long as the overall regexp can
1085 still match. A non-greedy regexp matches as little as possible.
1087 `(maximal-match SEXP)'
1088 produce a greedy regexp for SEXP. This is the default.
1090 Below, `SEXP ...' represents a sequence of regexp forms, treated as if
1091 enclosed in `(and ...)'.
1093 `(zero-or-more SEXP ...)'
1095 matches zero or more occurrences of what SEXP ... matches.
1098 like `zero-or-more', but always produces a greedy regexp, independent
1099 of `rx-greedy-flag'.
1102 like `zero-or-more', but always produces a non-greedy regexp,
1103 independent of `rx-greedy-flag'.
1105 `(one-or-more SEXP ...)'
1107 matches one or more occurrences of SEXP ...
1110 like `one-or-more', but always produces a greedy regexp.
1113 like `one-or-more', but always produces a non-greedy regexp.
1115 `(zero-or-one SEXP ...)'
1116 `(optional SEXP ...)'
1118 matches zero or one occurrences of A.
1121 like `zero-or-one', but always produces a greedy regexp.
1124 like `zero-or-one', but always produces a non-greedy regexp.
1128 matches N occurrences.
1131 matches N or more occurrences.
1135 matches N to M occurrences.
1138 matches what was matched previously by submatch N.
1141 evaluate FORM and insert result. If result is a string,
1145 include REGEXP in string notation in the result."
1146 (cond ((null regexps
)
1147 (error "No regexp"))
1149 (rx-to-string `(and ,@regexps
) t
))
1151 (rx-to-string (car regexps
) t
))))
1153 ;; ;; sregex.el replacement
1155 ;; ;;;###autoload (provide 'sregex)
1156 ;; ;;;###autoload (autoload 'sregex "rx")
1157 ;; (defalias 'sregex 'rx-to-string)
1158 ;; ;;;###autoload (autoload 'sregexq "rx" nil nil 'macro)
1159 ;; (defalias 'sregexq 'rx)