| 1 |
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-PPCRE; Base: 10 -*- |
|---|
| 2 |
;;; $Header: /usr/local/cvsrep/cl-ppcre/closures.lisp,v 1.44 2008/07/22 22:38:05 edi Exp $ |
|---|
| 3 |
|
|---|
| 4 |
;;; Here we create the closures which together build the final |
|---|
| 5 |
;;; scanner. |
|---|
| 6 |
|
|---|
| 7 |
;;; Copyright (c) 2002-2008, Dr. Edmund Weitz. All rights reserved. |
|---|
| 8 |
|
|---|
| 9 |
;;; Redistribution and use in source and binary forms, with or without |
|---|
| 10 |
;;; modification, are permitted provided that the following conditions |
|---|
| 11 |
;;; are met: |
|---|
| 12 |
|
|---|
| 13 |
;;; * Redistributions of source code must retain the above copyright |
|---|
| 14 |
;;; notice, this list of conditions and the following disclaimer. |
|---|
| 15 |
|
|---|
| 16 |
;;; * Redistributions in binary form must reproduce the above |
|---|
| 17 |
;;; copyright notice, this list of conditions and the following |
|---|
| 18 |
;;; disclaimer in the documentation and/or other materials |
|---|
| 19 |
;;; provided with the distribution. |
|---|
| 20 |
|
|---|
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
|---|
| 22 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 23 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 24 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|---|
| 25 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 26 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
|---|
| 27 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 28 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 |
|
|---|
| 33 |
(in-package :cl-ppcre) |
|---|
| 34 |
|
|---|
| 35 |
(declaim (inline *string*= *string*-equal)) |
|---|
| 36 |
(defun *string*= (string2 start1 end1 start2 end2) |
|---|
| 37 |
"Like STRING=, i.e. compares the special string *STRING* from START1 |
|---|
| 38 |
to END1 with STRING2 from START2 to END2. Note that there's no |
|---|
| 39 |
boundary check - this has to be implemented by the caller." |
|---|
| 40 |
(declare #.*standard-optimize-settings*) |
|---|
| 41 |
(declare (fixnum start1 end1 start2 end2)) |
|---|
| 42 |
(loop for string1-idx of-type fixnum from start1 below end1 |
|---|
| 43 |
for string2-idx of-type fixnum from start2 below end2 |
|---|
| 44 |
always (char= (schar *string* string1-idx) |
|---|
| 45 |
(schar string2 string2-idx)))) |
|---|
| 46 |
|
|---|
| 47 |
(defun *string*-equal (string2 start1 end1 start2 end2) |
|---|
| 48 |
"Like STRING-EQUAL, i.e. compares the special string *STRING* from |
|---|
| 49 |
START1 to END1 with STRING2 from START2 to END2. Note that there's no |
|---|
| 50 |
boundary check - this has to be implemented by the caller." |
|---|
| 51 |
(declare #.*standard-optimize-settings*) |
|---|
| 52 |
(declare (fixnum start1 end1 start2 end2)) |
|---|
| 53 |
(loop for string1-idx of-type fixnum from start1 below end1 |
|---|
| 54 |
for string2-idx of-type fixnum from start2 below end2 |
|---|
| 55 |
always (char-equal (schar *string* string1-idx) |
|---|
| 56 |
(schar string2 string2-idx)))) |
|---|
| 57 |
|
|---|
| 58 |
(defgeneric create-matcher-aux (regex next-fn) |
|---|
| 59 |
(declare #.*standard-optimize-settings*) |
|---|
| 60 |
(:documentation "Creates a closure which takes one parameter, |
|---|
| 61 |
START-POS, and tests whether REGEX can match *STRING* at START-POS |
|---|
| 62 |
such that the call to NEXT-FN after the match would succeed.")) |
|---|
| 63 |
|
|---|
| 64 |
(defmethod create-matcher-aux ((seq seq) next-fn) |
|---|
| 65 |
(declare #.*standard-optimize-settings*) |
|---|
| 66 |
;; the closure for a SEQ is a chain of closures for the elements of |
|---|
| 67 |
;; this sequence which call each other in turn; the last closure |
|---|
| 68 |
;; calls NEXT-FN |
|---|
| 69 |
(loop for element in (reverse (elements seq)) |
|---|
| 70 |
for curr-matcher = next-fn then next-matcher |
|---|
| 71 |
for next-matcher = (create-matcher-aux element curr-matcher) |
|---|
| 72 |
finally (return next-matcher))) |
|---|
| 73 |
|
|---|
| 74 |
(defmethod create-matcher-aux ((alternation alternation) next-fn) |
|---|
| 75 |
(declare #.*standard-optimize-settings*) |
|---|
| 76 |
;; first create closures for all alternations of ALTERNATION |
|---|
| 77 |
(let ((all-matchers (mapcar #'(lambda (choice) |
|---|
| 78 |
(create-matcher-aux choice next-fn)) |
|---|
| 79 |
(choices alternation)))) |
|---|
| 80 |
;; now create a closure which checks if one of the closures |
|---|
| 81 |
;; created above can succeed |
|---|
| 82 |
(lambda (start-pos) |
|---|
| 83 |
(declare (fixnum start-pos)) |
|---|
| 84 |
(loop for matcher in all-matchers |
|---|
| 85 |
thereis (funcall (the function matcher) start-pos))))) |
|---|
| 86 |
|
|---|
| 87 |
(defmethod create-matcher-aux ((register register) next-fn) |
|---|
| 88 |
(declare #.*standard-optimize-settings*) |
|---|
| 89 |
;; the position of this REGISTER within the whole regex; we start to |
|---|
| 90 |
;; count at 0 |
|---|
| 91 |
(let ((num (num register))) |
|---|
| 92 |
(declare (fixnum num)) |
|---|
| 93 |
;; STORE-END-OF-REG is a thin wrapper around NEXT-FN which will |
|---|
| 94 |
;; update the corresponding values of *REGS-START* and *REGS-END* |
|---|
| 95 |
;; after the inner matcher has succeeded |
|---|
| 96 |
(flet ((store-end-of-reg (start-pos) |
|---|
| 97 |
(declare (fixnum start-pos) |
|---|
| 98 |
(function next-fn)) |
|---|
| 99 |
(setf (svref *reg-starts* num) (svref *regs-maybe-start* num) |
|---|
| 100 |
(svref *reg-ends* num) start-pos) |
|---|
| 101 |
(funcall next-fn start-pos))) |
|---|
| 102 |
;; the inner matcher is a closure corresponding to the regex |
|---|
| 103 |
;; wrapped by this REGISTER |
|---|
| 104 |
(let ((inner-matcher (create-matcher-aux (regex register) |
|---|
| 105 |
#'store-end-of-reg))) |
|---|
| 106 |
(declare (function inner-matcher)) |
|---|
| 107 |
;; here comes the actual closure for REGISTER |
|---|
| 108 |
(lambda (start-pos) |
|---|
| 109 |
(declare (fixnum start-pos)) |
|---|
| 110 |
;; remember the old values of *REGS-START* and friends in |
|---|
| 111 |
;; case we cannot match |
|---|
| 112 |
(let ((old-*reg-starts* (svref *reg-starts* num)) |
|---|
| 113 |
(old-*regs-maybe-start* (svref *regs-maybe-start* num)) |
|---|
| 114 |
(old-*reg-ends* (svref *reg-ends* num))) |
|---|
| 115 |
;; we cannot use *REGS-START* here because Perl allows |
|---|
| 116 |
;; regular expressions like /(a|\1x)*/ |
|---|
| 117 |
(setf (svref *regs-maybe-start* num) start-pos) |
|---|
| 118 |
(let ((next-pos (funcall inner-matcher start-pos))) |
|---|
| 119 |
(unless next-pos |
|---|
| 120 |
;; restore old values on failure |
|---|
| 121 |
(setf (svref *reg-starts* num) old-*reg-starts* |
|---|
| 122 |
(svref *regs-maybe-start* num) old-*regs-maybe-start* |
|---|
| 123 |
(svref *reg-ends* num) old-*reg-ends*)) |
|---|
| 124 |
next-pos))))))) |
|---|
| 125 |
|
|---|
| 126 |
(defmethod create-matcher-aux ((lookahead lookahead) next-fn) |
|---|
| 127 |
(declare #.*standard-optimize-settings*) |
|---|
| 128 |
;; create a closure which just checks for the inner regex and |
|---|
| 129 |
;; doesn't care about NEXT-FN |
|---|
| 130 |
(let ((test-matcher (create-matcher-aux (regex lookahead) #'identity))) |
|---|
| 131 |
(declare (function next-fn test-matcher)) |
|---|
| 132 |
(if (positivep lookahead) |
|---|
| 133 |
;; positive look-ahead: check success of inner regex, then call |
|---|
| 134 |
;; NEXT-FN |
|---|
| 135 |
(lambda (start-pos) |
|---|
| 136 |
(and (funcall test-matcher start-pos) |
|---|
| 137 |
(funcall next-fn start-pos))) |
|---|
| 138 |
;; negative look-ahead: check failure of inner regex, then call |
|---|
| 139 |
;; NEXT-FN |
|---|
| 140 |
(lambda (start-pos) |
|---|
| 141 |
(and (not (funcall test-matcher start-pos)) |
|---|
| 142 |
(funcall next-fn start-pos)))))) |
|---|
| 143 |
|
|---|
| 144 |
(defmethod create-matcher-aux ((lookbehind lookbehind) next-fn) |
|---|
| 145 |
(declare #.*standard-optimize-settings*) |
|---|
| 146 |
(let ((len (len lookbehind)) |
|---|
| 147 |
;; create a closure which just checks for the inner regex and |
|---|
| 148 |
;; doesn't care about NEXT-FN |
|---|
| 149 |
(test-matcher (create-matcher-aux (regex lookbehind) #'identity))) |
|---|
| 150 |
(declare (function next-fn test-matcher) |
|---|
| 151 |
(fixnum len)) |
|---|
| 152 |
(if (positivep lookbehind) |
|---|
| 153 |
;; positive look-behind: check success of inner regex (if we're |
|---|
| 154 |
;; far enough from the start of *STRING*), then call NEXT-FN |
|---|
| 155 |
(lambda (start-pos) |
|---|
| 156 |
(declare (fixnum start-pos)) |
|---|
| 157 |
(and (>= (- start-pos (or *real-start-pos* *start-pos*)) len) |
|---|
| 158 |
(funcall test-matcher (- start-pos len)) |
|---|
| 159 |
(funcall next-fn start-pos))) |
|---|
| 160 |
;; negative look-behind: check failure of inner regex (if we're |
|---|
| 161 |
;; far enough from the start of *STRING*), then call NEXT-FN |
|---|
| 162 |
(lambda (start-pos) |
|---|
| 163 |
(declare (fixnum start-pos)) |
|---|
| 164 |
(and (or (< (- start-pos (or *real-start-pos* *start-pos*)) len) |
|---|
| 165 |
(not (funcall test-matcher (- start-pos len)))) |
|---|
| 166 |
(funcall next-fn start-pos)))))) |
|---|
| 167 |
|
|---|
| 168 |
(defmacro insert-char-class-tester ((char-class chr-expr) &body body) |
|---|
| 169 |
"Utility macro to replace each occurence of '\(CHAR-CLASS-TEST) |
|---|
| 170 |
within BODY with the correct test (corresponding to CHAR-CLASS) |
|---|
| 171 |
against CHR-EXPR." |
|---|
| 172 |
(with-rebinding (char-class) |
|---|
| 173 |
(with-unique-names (test-function) |
|---|
| 174 |
(flet ((substitute-char-class-tester (new) |
|---|
| 175 |
(subst new '(char-class-test) body |
|---|
| 176 |
:test #'equalp))) |
|---|
| 177 |
`(let ((,test-function (test-function ,char-class))) |
|---|
| 178 |
,@(substitute-char-class-tester |
|---|
| 179 |
`(funcall ,test-function ,chr-expr))))))) |
|---|
| 180 |
|
|---|
| 181 |
(defmethod create-matcher-aux ((char-class char-class) next-fn) |
|---|
| 182 |
(declare #.*standard-optimize-settings*) |
|---|
| 183 |
(declare (function next-fn)) |
|---|
| 184 |
;; insert a test against the current character within *STRING* |
|---|
| 185 |
(insert-char-class-tester (char-class (schar *string* start-pos)) |
|---|
| 186 |
(lambda (start-pos) |
|---|
| 187 |
(declare (fixnum start-pos)) |
|---|
| 188 |
(and (< start-pos *end-pos*) |
|---|
| 189 |
(char-class-test) |
|---|
| 190 |
(funcall next-fn (1+ start-pos)))))) |
|---|
| 191 |
|
|---|
| 192 |
(defmethod create-matcher-aux ((str str) next-fn) |
|---|
| 193 |
(declare #.*standard-optimize-settings*) |
|---|
| 194 |
(declare (fixnum *end-string-pos*) |
|---|
| 195 |
(function next-fn) |
|---|
| 196 |
;; this special value is set by CREATE-SCANNER when the |
|---|
| 197 |
;; closures are built |
|---|
| 198 |
(special end-string)) |
|---|
| 199 |
(let* ((len (len str)) |
|---|
| 200 |
(case-insensitive-p (case-insensitive-p str)) |
|---|
| 201 |
(start-of-end-string-p (start-of-end-string-p str)) |
|---|
| 202 |
(skip (skip str)) |
|---|
| 203 |
(str (str str)) |
|---|
| 204 |
(chr (schar str 0)) |
|---|
| 205 |
(end-string (and end-string (str end-string))) |
|---|
| 206 |
(end-string-len (if end-string |
|---|
| 207 |
(length end-string) |
|---|
| 208 |
nil))) |
|---|
| 209 |
(declare (fixnum len)) |
|---|
| 210 |
(cond ((and start-of-end-string-p case-insensitive-p) |
|---|
| 211 |
;; closure for the first STR which belongs to the constant |
|---|
| 212 |
;; string at the end of the regular expression; |
|---|
| 213 |
;; case-insensitive version |
|---|
| 214 |
(lambda (start-pos) |
|---|
| 215 |
(declare (fixnum start-pos end-string-len)) |
|---|
| 216 |
(let ((test-end-pos (+ start-pos end-string-len))) |
|---|
| 217 |
(declare (fixnum test-end-pos)) |
|---|
| 218 |
;; either we're at *END-STRING-POS* (which means that |
|---|
| 219 |
;; it has already been confirmed that end-string |
|---|
| 220 |
;; starts here) or we really have to test |
|---|
| 221 |
(and (or (= start-pos *end-string-pos*) |
|---|
| 222 |
(and (<= test-end-pos *end-pos*) |
|---|
| 223 |
(*string*-equal end-string start-pos test-end-pos |
|---|
| 224 |
0 end-string-len))) |
|---|
| 225 |
(funcall next-fn (+ start-pos len)))))) |
|---|
| 226 |
(start-of-end-string-p |
|---|
| 227 |
;; closure for the first STR which belongs to the constant |
|---|
| 228 |
;; string at the end of the regular expression; |
|---|
| 229 |
;; case-sensitive version |
|---|
| 230 |
(lambda (start-pos) |
|---|
| 231 |
(declare (fixnum start-pos end-string-len)) |
|---|
| 232 |
(let ((test-end-pos (+ start-pos end-string-len))) |
|---|
| 233 |
(declare (fixnum test-end-pos)) |
|---|
| 234 |
;; either we're at *END-STRING-POS* (which means that |
|---|
| 235 |
;; it has already been confirmed that end-string |
|---|
| 236 |
;; starts here) or we really have to test |
|---|
| 237 |
(and (or (= start-pos *end-string-pos*) |
|---|
| 238 |
(and (<= test-end-pos *end-pos*) |
|---|
| 239 |
(*string*= end-string start-pos test-end-pos |
|---|
| 240 |
0 end-string-len))) |
|---|
| 241 |
(funcall next-fn (+ start-pos len)))))) |
|---|
| 242 |
(skip |
|---|
| 243 |
;; a STR which can be skipped because some other function |
|---|
| 244 |
;; has already confirmed that it matches |
|---|
| 245 |
(lambda (start-pos) |
|---|
| 246 |
(declare (fixnum start-pos)) |
|---|
| 247 |
(funcall next-fn (+ start-pos len)))) |
|---|
| 248 |
((and (= len 1) case-insensitive-p) |
|---|
| 249 |
;; STR represent exactly one character; case-insensitive |
|---|
| 250 |
;; version |
|---|
| 251 |
(lambda (start-pos) |
|---|
| 252 |
(declare (fixnum start-pos)) |
|---|
| 253 |
(and (< start-pos *end-pos*) |
|---|
| 254 |
(char-equal (schar *string* start-pos) chr) |
|---|
| 255 |
(funcall next-fn (1+ start-pos))))) |
|---|
| 256 |
((= len 1) |
|---|
| 257 |
;; STR represent exactly one character; case-sensitive |
|---|
| 258 |
;; version |
|---|
| 259 |
(lambda (start-pos) |
|---|
| 260 |
(declare (fixnum start-pos)) |
|---|
| 261 |
(and (< start-pos *end-pos*) |
|---|
| 262 |
(char= (schar *string* start-pos) chr) |
|---|
| 263 |
(funcall next-fn (1+ start-pos))))) |
|---|
| 264 |
(case-insensitive-p |
|---|
| 265 |
;; general case, case-insensitive version |
|---|
| 266 |
(lambda (start-pos) |
|---|
| 267 |
(declare (fixnum start-pos)) |
|---|
| 268 |
(let ((next-pos (+ start-pos len))) |
|---|
| 269 |
(declare (fixnum next-pos)) |
|---|
| 270 |
(and (<= next-pos *end-pos*) |
|---|
| 271 |
(*string*-equal str start-pos next-pos 0 len) |
|---|
| 272 |
(funcall next-fn next-pos))))) |
|---|
| 273 |
(t |
|---|
| 274 |
;; general case, case-sensitive version |
|---|
| 275 |
(lambda (start-pos) |
|---|
| 276 |
(declare (fixnum start-pos)) |
|---|
| 277 |
(let ((next-pos (+ start-pos len))) |
|---|
| 278 |
(declare (fixnum next-pos)) |
|---|
| 279 |
(and (<= next-pos *end-pos*) |
|---|
| 280 |
(*string*= str start-pos next-pos 0 len) |
|---|
| 281 |
(funcall next-fn next-pos)))))))) |
|---|
| 282 |
|
|---|
| 283 |
(declaim (inline word-boundary-p)) |
|---|
| 284 |
(defun word-boundary-p (start-pos) |
|---|
| 285 |
"Check whether START-POS is a word-boundary within *STRING*." |
|---|
| 286 |
(declare #.*standard-optimize-settings*) |
|---|
| 287 |
(declare (fixnum start-pos)) |
|---|
| 288 |
(let ((1-start-pos (1- start-pos)) |
|---|
| 289 |
(*start-pos* (or *real-start-pos* *start-pos*))) |
|---|
| 290 |
;; either the character before START-POS is a word-constituent and |
|---|
| 291 |
;; the character at START-POS isn't... |
|---|
| 292 |
(or (and (or (= start-pos *end-pos*) |
|---|
| 293 |
(and (< start-pos *end-pos*) |
|---|
| 294 |
(not (word-char-p (schar *string* start-pos))))) |
|---|
| 295 |
(and (< 1-start-pos *end-pos*) |
|---|
| 296 |
(<= *start-pos* 1-start-pos) |
|---|
| 297 |
(word-char-p (schar *string* 1-start-pos)))) |
|---|
| 298 |
;; ...or vice versa |
|---|
| 299 |
(and (or (= start-pos *start-pos*) |
|---|
| 300 |
(and (< 1-start-pos *end-pos*) |
|---|
| 301 |
(<= *start-pos* 1-start-pos) |
|---|
| 302 |
(not (word-char-p (schar *string* 1-start-pos))))) |
|---|
| 303 |
(and (< start-pos *end-pos*) |
|---|
| 304 |
(word-char-p (schar *string* start-pos))))))) |
|---|
| 305 |
|
|---|
| 306 |
(defmethod create-matcher-aux ((word-boundary word-boundary) next-fn) |
|---|
| 307 |
(declare #.*standard-optimize-settings*) |
|---|
| 308 |
(declare (function next-fn)) |
|---|
| 309 |
(if (negatedp word-boundary) |
|---|
| 310 |
(lambda (start-pos) |
|---|
| 311 |
(and (not (word-boundary-p start-pos)) |
|---|
| 312 |
(funcall next-fn start-pos))) |
|---|
| 313 |
(lambda (start-pos) |
|---|
| 314 |
(and (word-boundary-p start-pos) |
|---|
| 315 |
(funcall next-fn start-pos))))) |
|---|
| 316 |
|
|---|
| 317 |
(defmethod create-matcher-aux ((everything everything) next-fn) |
|---|
| 318 |
(declare #.*standard-optimize-settings*) |
|---|
| 319 |
(declare (function next-fn)) |
|---|
| 320 |
(if (single-line-p everything) |
|---|
| 321 |
;; closure for single-line-mode: we really match everything, so we |
|---|
| 322 |
;; just advance the index into *STRING* by one and carry on |
|---|
| 323 |
(lambda (start-pos) |
|---|
| 324 |
(declare (fixnum start-pos)) |
|---|
| 325 |
(and (< start-pos *end-pos*) |
|---|
| 326 |
(funcall next-fn (1+ start-pos)))) |
|---|
| 327 |
;; not single-line-mode, so we have to make sure we don't match |
|---|
| 328 |
;; #\Newline |
|---|
| 329 |
(lambda (start-pos) |
|---|
| 330 |
(declare (fixnum start-pos)) |
|---|
| 331 |
(and (< start-pos *end-pos*) |
|---|
| 332 |
(char/= (schar *string* start-pos) #\Newline) |
|---|
| 333 |
(funcall next-fn (1+ start-pos)))))) |
|---|
| 334 |
|
|---|
| 335 |
(defmethod create-matcher-aux ((anchor anchor) next-fn) |
|---|
| 336 |
(declare #.*standard-optimize-settings*) |
|---|
| 337 |
(declare (function next-fn)) |
|---|
| 338 |
(let ((startp (startp anchor)) |
|---|
| 339 |
(multi-line-p (multi-line-p anchor))) |
|---|
| 340 |
(cond ((no-newline-p anchor) |
|---|
| 341 |
;; this must be an end-anchor and it must be modeless, so |
|---|
| 342 |
;; we just have to check whether START-POS equals |
|---|
| 343 |
;; *END-POS* |
|---|
| 344 |
(lambda (start-pos) |
|---|
| 345 |
(declare (fixnum start-pos)) |
|---|
| 346 |
(and (= start-pos *end-pos*) |
|---|
| 347 |
(funcall next-fn start-pos)))) |
|---|
| 348 |
((and startp multi-line-p) |
|---|
| 349 |
;; a start-anchor in multi-line-mode: check if we're at |
|---|
| 350 |
;; *START-POS* or if the last character was #\Newline |
|---|
| 351 |
(lambda (start-pos) |
|---|
| 352 |
(declare (fixnum start-pos)) |
|---|
| 353 |
(let ((*start-pos* (or *real-start-pos* *start-pos*))) |
|---|
| 354 |
(and (or (= start-pos *start-pos*) |
|---|
| 355 |
(and (<= start-pos *end-pos*) |
|---|
| 356 |
(> start-pos *start-pos*) |
|---|
| 357 |
(char= #\Newline |
|---|
| 358 |
(schar *string* (1- start-pos))))) |
|---|
| 359 |
(funcall next-fn start-pos))))) |
|---|
| 360 |
(startp |
|---|
| 361 |
;; a start-anchor which is not in multi-line-mode, so just |
|---|
| 362 |
;; check whether we're at *START-POS* |
|---|
| 363 |
(lambda (start-pos) |
|---|
| 364 |
(declare (fixnum start-pos)) |
|---|
| 365 |
(and (= start-pos (or *real-start-pos* *start-pos*)) |
|---|
| 366 |
(funcall next-fn start-pos)))) |
|---|
| 367 |
(multi-line-p |
|---|
| 368 |
;; an end-anchor in multi-line-mode: check if we're at |
|---|
| 369 |
;; *END-POS* or if the character we're looking at is |
|---|
| 370 |
;; #\Newline |
|---|
| 371 |
(lambda (start-pos) |
|---|
| 372 |
(declare (fixnum start-pos)) |
|---|
| 373 |
(and (or (= start-pos *end-pos*) |
|---|
| 374 |
(and (< start-pos *end-pos*) |
|---|
| 375 |
(char= #\Newline |
|---|
| 376 |
(schar *string* start-pos)))) |
|---|
| 377 |
(funcall next-fn start-pos)))) |
|---|
| 378 |
(t |
|---|
| 379 |
;; an end-anchor which is not in multi-line-mode, so just |
|---|
| 380 |
;; check if we're at *END-POS* or if we're looking at |
|---|
| 381 |
;; #\Newline and there's nothing behind it |
|---|
| 382 |
(lambda (start-pos) |
|---|
| 383 |
(declare (fixnum start-pos)) |
|---|
| 384 |
(and (or (= start-pos *end-pos*) |
|---|
| 385 |
(and (= start-pos (1- *end-pos*)) |
|---|
| 386 |
(char= #\Newline |
|---|
| 387 |
(schar *string* start-pos)))) |
|---|
| 388 |
(funcall next-fn start-pos))))))) |
|---|
| 389 |
|
|---|
| 390 |
(defmethod create-matcher-aux ((back-reference back-reference) next-fn) |
|---|
| 391 |
(declare #.*standard-optimize-settings*) |
|---|
| 392 |
(declare (function next-fn)) |
|---|
| 393 |
;; the position of the corresponding REGISTER within the whole |
|---|
| 394 |
;; regex; we start to count at 0 |
|---|
| 395 |
(let ((num (num back-reference))) |
|---|
| 396 |
(if (case-insensitive-p back-reference) |
|---|
| 397 |
;; the case-insensitive version |
|---|
| 398 |
(lambda (start-pos) |
|---|
| 399 |
(declare (fixnum start-pos)) |
|---|
| 400 |
(let ((reg-start (svref *reg-starts* num)) |
|---|
| 401 |
(reg-end (svref *reg-ends* num))) |
|---|
| 402 |
;; only bother to check if the corresponding REGISTER as |
|---|
| 403 |
;; matched successfully already |
|---|
| 404 |
(and reg-start |
|---|
| 405 |
(let ((next-pos (+ start-pos (- (the fixnum reg-end) |
|---|
| 406 |
(the fixnum reg-start))))) |
|---|
| 407 |
(declare (fixnum next-pos)) |
|---|
| 408 |
(and |
|---|
| 409 |
(<= next-pos *end-pos*) |
|---|
| 410 |
(*string*-equal *string* start-pos next-pos |
|---|
| 411 |
reg-start reg-end) |
|---|
| 412 |
(funcall next-fn next-pos)))))) |
|---|
| 413 |
;; the case-sensitive version |
|---|
| 414 |
(lambda (start-pos) |
|---|
| 415 |
(declare (fixnum start-pos)) |
|---|
| 416 |
(let ((reg-start (svref *reg-starts* num)) |
|---|
| 417 |
(reg-end (svref *reg-ends* num))) |
|---|
| 418 |
;; only bother to check if the corresponding REGISTER as |
|---|
| 419 |
;; matched successfully already |
|---|
| 420 |
(and reg-start |
|---|
| 421 |
(let ((next-pos (+ start-pos (- (the fixnum reg-end) |
|---|
| 422 |
(the fixnum reg-start))))) |
|---|
| 423 |
(declare (fixnum next-pos)) |
|---|
| 424 |
(and |
|---|
| 425 |
(<= next-pos *end-pos*) |
|---|
| 426 |
(*string*= *string* start-pos next-pos |
|---|
| 427 |
reg-start reg-end) |
|---|
| 428 |
(funcall next-fn next-pos))))))))) |
|---|
| 429 |
|
|---|
| 430 |
(defmethod create-matcher-aux ((branch branch) next-fn) |
|---|
| 431 |
(declare #.*standard-optimize-settings*) |
|---|
| 432 |
(let* ((test (test branch)) |
|---|
| 433 |
(then-matcher (create-matcher-aux (then-regex branch) next-fn)) |
|---|
| 434 |
(else-matcher (create-matcher-aux (else-regex branch) next-fn))) |
|---|
| 435 |
(declare (function then-matcher else-matcher)) |
|---|
| 436 |
(cond ((numberp test) |
|---|
| 437 |
(lambda (start-pos) |
|---|
| 438 |
(declare (fixnum test)) |
|---|
| 439 |
(if (and (< test (length *reg-starts*)) |
|---|
| 440 |
(svref *reg-starts* test)) |
|---|
| 441 |
(funcall then-matcher start-pos) |
|---|
| 442 |
(funcall else-matcher start-pos)))) |
|---|
| 443 |
(t |
|---|
| 444 |
(let ((test-matcher (create-matcher-aux test #'identity))) |
|---|
| 445 |
(declare (function test-matcher)) |
|---|
| 446 |
(lambda (start-pos) |
|---|
| 447 |
(if (funcall test-matcher start-pos) |
|---|
| 448 |
(funcall then-matcher start-pos) |
|---|
| 449 |
(funcall else-matcher start-pos)))))))) |
|---|
| 450 |
|
|---|
| 451 |
(defmethod create-matcher-aux ((standalone standalone) next-fn) |
|---|
| 452 |
(declare #.*standard-optimize-settings*) |
|---|
| 453 |
(let ((inner-matcher (create-matcher-aux (regex standalone) #'identity))) |
|---|
| 454 |
(declare (function next-fn inner-matcher)) |
|---|
| 455 |
(lambda (start-pos) |
|---|
| 456 |
(let ((next-pos (funcall inner-matcher start-pos))) |
|---|
| 457 |
(and next-pos |
|---|
| 458 |
(funcall next-fn next-pos)))))) |
|---|
| 459 |
|
|---|
| 460 |
(defmethod create-matcher-aux ((filter filter) next-fn) |
|---|
| 461 |
(declare #.*standard-optimize-settings*) |
|---|
| 462 |
(let ((fn (fn filter))) |
|---|
| 463 |
(lambda (start-pos) |
|---|
| 464 |
(let ((next-pos (funcall fn start-pos))) |
|---|
| 465 |
(and next-pos |
|---|
| 466 |
(funcall next-fn next-pos)))))) |
|---|
| 467 |
|
|---|
| 468 |
(defmethod create-matcher-aux ((void void) next-fn) |
|---|
| 469 |
(declare #.*standard-optimize-settings*) |
|---|
| 470 |
;; optimize away VOIDs: don't create a closure, just return NEXT-FN |
|---|
| 471 |
next-fn) |
|---|