comparison lisp/emacs-lisp/regexp-opt.el @ 50713:645707022f8f

(regexp-opt-depth): Don't count a "//(" which appears inside a character set. (regexp-opt-not-groupie*-re): New constant.
author Juanma Barranquero <lekktu@gmail.com>
date Sat, 26 Apr 2003 23:24:59 +0000
parents 0d8b17d428b5
children 695cf19ef79e
comparison
equal deleted inserted replaced
50712:e28c4c84cdf5 50713:645707022f8f
108 (open (cond ((stringp paren) paren) (paren "\\("))) 108 (open (cond ((stringp paren) paren) (paren "\\(")))
109 (sorted-strings (sort (copy-sequence strings) 'string-lessp)) 109 (sorted-strings (sort (copy-sequence strings) 'string-lessp))
110 (re (regexp-opt-group sorted-strings open))) 110 (re (regexp-opt-group sorted-strings open)))
111 (if words (concat "\\<" re "\\>") re)))) 111 (if words (concat "\\<" re "\\>") re))))
112 112
113 (defconst regexp-opt-not-groupie*-re
114 (let* ((harmless-ch "[^\\\\[]")
115 (esc-pair-not-lp "\\\\[^(]")
116 (class-harmless-ch "[^][]")
117 (class-lb-harmless "[^]:]")
118 (class-lb-colon-maybe-charclass ":\\([a-z]+:]\\)?")
119 (class-lb (concat "\\[\\(" class-lb-harmless
120 "\\|" class-lb-colon-maybe-charclass "\\)"))
121 (class
122 (concat "\\[^?]?"
123 "\\(" class-harmless-ch
124 "\\|" class-lb "\\)*"
125 "\\[?]")) ; special handling for bare [ at end of re
126 (shy-lp "\\\\(\\?:"))
127 (concat "\\(" harmless-ch "\\|" esc-pair-not-lp
128 "\\|" class "\\|" shy-lp "\\)*"))
129 "Matches any part of a regular expression EXCEPT for non-shy \"\\\\(\"s")
130
113 ;;;###autoload 131 ;;;###autoload
114 (defun regexp-opt-depth (regexp) 132 (defun regexp-opt-depth (regexp)
115 "Return the depth of REGEXP. 133 "Return the depth of REGEXP.
116 This means the number of regexp grouping constructs (parenthesised expressions) 134 This means the number of regexp grouping constructs (parenthesised expressions)
117 in REGEXP." 135 in REGEXP."
118 (save-match-data 136 (save-match-data
119 ;; Hack to signal an error if REGEXP does not have balanced parentheses. 137 ;; Hack to signal an error if REGEXP does not have balanced parentheses.
120 (string-match regexp "") 138 (string-match regexp "")
121 ;; Count the number of open parentheses in REGEXP. 139 ;; Count the number of open parentheses in REGEXP.
122 (let ((count 0) start) 140 (let ((count 0) start)
123 (while (string-match "\\(\\`\\|[^\\]\\)\\\\\\(\\\\\\\\\\)*([^?]" 141 (while
124 regexp start) 142 (progn
125 (setq count (1+ count) 143 (string-match regexp-opt-not-groupie*-re regexp start)
126 ;; Go back 2 chars (one for [^?] and one for [^\\]). 144 (setq start ( + (match-end 0) 2)) ; +2 for "\\(" after match-end.
127 start (- (match-end 0) 2))) 145 (<= start (length regexp)))
146 (setq count (1+ count)))
128 count))) 147 count)))
129 148
130 ;;; Workhorse functions. 149 ;;; Workhorse functions.
131 150
132 (eval-when-compile 151 (eval-when-compile