rx.el 36 KB

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