Mercurial > emacs
annotate lisp/emacs-lisp/regexp-opt.el @ 18472:128090c7c0ef libc-970628 libc-970629 libc-970630 libc-970701 libc-970702 libc-970703 libc-970704 libc-970705
(-mingw32*): New OS.
author | Richard Kenner <kenner@gnu.org> |
---|---|
date | Fri, 27 Jun 1997 19:20:14 +0000 |
parents | 4a6dd3081185 |
children | 013cea3890cb |
rev | line source |
---|---|
18014 | 1 ;;; regexp-opt.el --- generate efficient regexps to match strings. |
2 | |
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Simon Marshall <simon@gnu.ai.mit.edu> | |
6 ;; Keywords: strings, regexps | |
18465 | 7 ;; Version: 1.05.01 |
18014 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
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 2, or (at your option) | |
14 ;; any later version. | |
15 | |
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. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; The "opt" in "regexp-opt" stands for "optim\\(al\\|i\\(se\\|ze\\)\\)". | |
29 ;; | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
30 ;; This package generates a regexp from a given list of strings (which matches |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
31 ;; one of those strings) so that the regexp generated by: |
18014 | 32 ;; |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
33 ;; (regexp-opt strings) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
34 ;; |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
35 ;; is equivalent to, but more efficient than, the regexp generated by: |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
36 ;; |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
37 ;; (mapconcat 'regexp-quote strings "\\|") |
18014 | 38 ;; |
39 ;; For example: | |
40 ;; | |
41 ;; (let ((strings '("cond" "if" "when" "unless" "while" | |
42 ;; "let" "let*" "progn" "prog1" "prog2" | |
43 ;; "save-restriction" "save-excursion" "save-window-excursion" | |
44 ;; "save-current-buffer" "save-match-data" | |
45 ;; "catch" "throw" "unwind-protect" "condition-case"))) | |
46 ;; (concat "(" (regexp-opt strings t) "\\>")) | |
47 ;; => "(\\(c\\(atch\\|ond\\(ition-case\\)?\\)\\|if\\|let\\*?\\|prog[12n]\\|save-\\(current-buffer\\|excursion\\|match-data\\|restriction\\|window-excursion\\)\\|throw\\|un\\(less\\|wind-protect\\)\\|wh\\(en\\|ile\\)\\)\\>" | |
48 ;; | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
49 ;; Searching using the above example `regexp-opt' regexp takes approximately |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
50 ;; two-thirds of the time taken using the equivalent `mapconcat' regexp. |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
51 |
18014 | 52 ;; Since this package was written to produce efficient regexps, not regexps |
53 ;; efficiently, it is probably not a good idea to in-line too many calls in | |
54 ;; your code, unless you use the following trick with `eval-when-compile': | |
55 ;; | |
56 ;; (defvar definition-regexp | |
57 ;; (eval-when-compile | |
58 ;; (concat "^(" | |
59 ;; (regexp-opt '("defun" "defsubst" "defmacro" "defalias" | |
60 ;; "defvar" "defconst") t) | |
61 ;; "\\>"))) | |
62 ;; | |
63 ;; The `byte-compile' code will be as if you had defined the variable thus: | |
64 ;; | |
65 ;; (defvar definition-regexp | |
66 ;; "^(\\(def\\(alias\\|const\\|macro\\|subst\\|un\\|var\\)\\)\\>") | |
67 ;; | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
68 ;; Note that if you use this trick for all instances of `regexp-opt' and |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
69 ;; `regexp-opt-depth' in your code, regexp-opt.el would only have to be loaded |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
70 ;; at compile time. But note also that using this trick means that should |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
71 ;; regexp-opt.el be changed, perhaps to fix a bug or to add a feature to |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
72 ;; improve the efficiency of `regexp-opt' regexps, you would have to recompile |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
73 ;; your code for such changes to have effect in your code. |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
74 |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
75 ;; Originally written for font-lock.el, from an idea from Stig's hl319.el, with |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
76 ;; thanks for ideas also to Michael Ernst, Bob Glickstein and Dan Nicolaescu. |
18014 | 77 ;; Please don't tell me that it doesn't produce optimal regexps; I know that |
78 ;; already. For example, the above explanation for the meaning of "opt" would | |
79 ;; be more efficient as "optim\\(al\\|i[sz]e\\)", but this requires complex | |
80 ;; forward looking. But (ideas or) code to improve things (are) is welcome. | |
81 | |
82 ;;; Code: | |
83 | |
84 ;;;###autoload | |
85 (defun regexp-opt (strings &optional paren) | |
86 "Return a regexp to match a string in STRINGS. | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
87 Each string should be unique in STRINGS and should not contain any regexps. |
18014 | 88 If optional PAREN non-nil, ensure that the returned regexp is enclosed by at |
89 least one regexp grouping construct. | |
90 The returned regexp is typically more efficient than the equivalent regexp: | |
91 | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
92 (let ((open-paren (if PAREN \"\\\\(\" \"\")) (close-paren (if PAREN \"\\\\)\" \"\"))) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
93 (concat open-paren (mapconcat 'regexp-quote STRINGS \"\\\\|\") close-paren)) |
18014 | 94 |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
95 but typically contains more regexp grouping constructs. |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
96 Use `regexp-opt-depth' to count them." |
18014 | 97 (save-match-data |
98 ;; Recurse on the sorted list. | |
99 (let ((max-lisp-eval-depth (* 1024 1024)) | |
100 (completion-ignore-case nil)) | |
101 (regexp-opt-group (sort (copy-sequence strings) 'string-lessp) paren)))) | |
102 | |
103 ;;;###autoload | |
104 (defun regexp-opt-depth (regexp) | |
105 "Return the depth of REGEXP. | |
106 This means the number of regexp grouping constructs (parenthesised expressions) | |
107 in REGEXP." | |
108 (save-match-data | |
109 ;; Hack to signal an error if REGEXP does not have balanced parentheses. | |
110 (string-match regexp "") | |
111 ;; Count the number of open parentheses in REGEXP. | |
112 (let ((count 0) start) | |
113 (while (string-match "\\\\(" regexp start) | |
114 (setq count (1+ count) start (match-end 0))) | |
115 count))) | |
116 | |
117 ;;; Workhorse functions. | |
118 | |
119 (eval-when-compile | |
120 (require 'cl)) | |
121 | |
122 (unless (fboundp 'make-bool-vector) | |
123 (defalias 'make-bool-vector 'make-vector)) | |
124 | |
125 (defun regexp-opt-group (strings &optional paren lax) | |
126 ;; | |
127 ;; Return a regexp to match a string in STRINGS. | |
128 ;; If PAREN non-nil, output regexp parentheses around returned regexp. | |
129 ;; If LAX non-nil, don't output parentheses if it doesn't require them. | |
130 ;; Merges keywords to avoid backtracking in Emacs' regexp matcher. | |
131 ;; | |
132 ;; The basic idea is to find the shortest common prefix, remove it and | |
133 ;; recurse. If there is no prefix, we divide the list into two so that (at | |
134 ;; least) one half will have at least a one-character common prefix. | |
135 ;; | |
136 ;; Also we delay the addition of grouping parenthesis as long as possible | |
137 ;; until we're sure we need them, and try to remove one-character sequences | |
138 ;; so we can use character sets rather than grouping parenthesis. | |
139 ;; | |
140 (let* ((open-group (if paren "\\(" "")) | |
141 (close-group (if paren "\\)" "")) | |
142 (open-charset (if lax "" open-group)) | |
143 (close-charset (if lax "" close-group))) | |
144 (cond | |
145 ;; | |
146 ;; If there is only one string, just return it. | |
147 ((= (length strings) 1) | |
148 (if (= (length (car strings)) 1) | |
149 (concat open-charset (regexp-quote (car strings)) close-charset) | |
150 (concat open-group (regexp-quote (car strings)) close-group))) | |
151 ;; | |
152 ;; If there is an empty string, remove it and recurse on the rest. | |
153 ((= (length (car strings)) 0) | |
154 (concat open-charset | |
155 (regexp-opt-group (cdr strings) t t) "?" | |
156 close-charset)) | |
157 ;; | |
158 ;; If all are one-character strings, just return a character set. | |
159 ((= (length strings) (apply '+ (mapcar 'length strings))) | |
160 (concat open-charset | |
161 (regexp-opt-charset strings) | |
162 close-charset)) | |
163 ;; | |
164 ;; We have a list of different length strings. | |
165 (t | |
166 (let ((prefix (try-completion "" (mapcar 'list strings))) | |
167 (letters (let ((completion-regexp-list '("^.$"))) | |
168 (all-completions "" (mapcar 'list strings))))) | |
169 (cond | |
170 ;; | |
171 ;; If there is a common prefix, remove it and recurse on the suffixes. | |
172 ((> (length prefix) 0) | |
173 (let* ((length (length prefix)) | |
174 (suffixes (mapcar (lambda (s) (substring s length)) strings))) | |
175 (concat open-group | |
176 (regexp-quote prefix) (regexp-opt-group suffixes t t) | |
177 close-group))) | |
178 ;; | |
179 ;; If there are several one-character strings, remove them and recurse | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
180 ;; on the rest (first so the final regexp finds the longest match). |
18014 | 181 ((> (length letters) 1) |
182 (let ((rest (let ((completion-regexp-list '("^..+$"))) | |
183 (all-completions "" (mapcar 'list strings))))) | |
184 (concat open-group | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
185 (regexp-opt-group rest) "\\|" (regexp-opt-charset letters) |
18014 | 186 close-group))) |
187 ;; | |
188 ;; Otherwise, divide the list into those that start with a particular | |
189 ;; letter and those that do not, and recurse on them. | |
190 (t | |
191 (let* ((char (substring (car strings) 0 1)) | |
192 (half1 (all-completions char (mapcar 'list strings))) | |
193 (half2 (nthcdr (length half1) strings))) | |
194 (concat open-group | |
195 (regexp-opt-group half1) "\\|" (regexp-opt-group half2) | |
196 close-group))))))))) | |
197 | |
198 (defun regexp-opt-charset (chars) | |
199 ;; | |
200 ;; Return a regexp to match a character in CHARS. | |
201 ;; | |
202 ;; The basic idea is to find character ranges. Also we take care in the | |
203 ;; position of character set meta characters in the character set regexp. | |
204 ;; | |
205 (let* ((charwidth 256) ; Yeah, right. | |
206 (charmap (make-bool-vector charwidth nil)) | |
207 (charset "") | |
208 (bracket "") (dash "") (caret "")) | |
209 ;; | |
210 ;; Make a character map but extract character set meta characters. | |
18149
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
211 (dolist (char (mapcar 'string-to-char chars)) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
212 (case char |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
213 (?\] |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
214 (setq bracket "]")) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
215 (?^ |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
216 (setq caret "^")) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
217 (?- |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
218 (setq dash "-")) |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
219 (otherwise |
2fec7f622b82
emit charsets after strings so that the final regexp finds the longest match.
Simon Marshall <simon@gnu.org>
parents:
18014
diff
changeset
|
220 (aset charmap char t)))) |
18014 | 221 ;; |
222 ;; Make a character set from the map using ranges where applicable. | |
18465 | 223 (dotimes (char charwidth) |
224 (let ((start char)) | |
225 (while (and (< char charwidth) (aref charmap char)) | |
226 (incf char)) | |
227 (cond ((> char (+ start 3)) | |
228 (setq charset (format "%s%c-%c" charset start (1- char)))) | |
229 ((> char start) | |
230 (setq charset (format "%s%c" charset (setq char start))))))) | |
18014 | 231 ;; |
232 ;; Make sure a caret is not first and a dash is first or last. | |
233 (if (and (string-equal charset "") (string-equal bracket "")) | |
234 (concat "[" dash caret "]") | |
235 (concat "[" bracket charset caret dash "]")))) | |
236 | |
237 (provide 'regexp-opt) | |
238 | |
239 ;;; regexp-opt.el ends here |