Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fbc9a9f313
Fetching contributors…

Cannot retrieve contributors at this time

file 526 lines (427 sloc) 16.295 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: REGEX; Base: 10 -*-

(in-package "REGEX")


; disable inlining and the optimizations in the match functions.
;(eval-when (eval compile load)
; (pushnew :debug-regex *features*) )

(defmacro define-constant (name value &optional doc)
  `(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
     ,@(when doc (list doc))))

;;;;
;;;; Regex Configuration
;;;;


(defvar *regex-compile-verbose* nil)
(defun compile-verbose (&optional (flag t))
  (setq *regex-compile-verbose* flag))

(defvar *match-simple-strings-only* t)
(defun match-simple-strings-only (&optional (flag t))
  (setq *match-simple-strings-only* flag)
  (clear-pattern-cache))

;; this should always be set to t
(defvar *force-safe-match* t)
(defun force-safe-match (&optional (flag t))
  (setq *force-safe-match* flag)
  (clear-pattern-cache))

(defvar *escape-special-chars* nil)
(defun escape-special-chars (&optional (flag t))
  (setq *escape-special-chars* flag)
  (clear-pattern-cache))

(defvar *dot-matches-newline* nil)
(defun dot-matches-newline (&optional (flag t))
  (setq *dot-matches-newline* flag))

(defvar *allow-backmatch* t)
(defun allow-backmatch (&optional (flag t))
  (setq *allow-backmatch* flag)
  (clear-pattern-cache))

(defvar *allow-rangematch* t)
(defun allow-rangematch (&optional (flag t))
  (setq *allow-rangematch* flag)
  (clear-pattern-cache))

(defvar *allow-nongreedy-quantifiers* t)
(defun allow-nongreedy-quantifiers (&optional (flag t))
  (setq *allow-nongreedy-quantifiers* flag)
  (clear-pattern-cache))

(defvar *allow-nonregister-groups* t)
(defun allow-nonregister-groups (&optional (flag t))
  (setq *allow-nonregister-groups* flag)
  (clear-pattern-cache))

(defvar *registers-match-rightmost* nil)
(defun registers-match-rightmost (&optional (flag t))
  (setq *registers-match-rightmost* flag)
  (clear-pattern-cache))





;;;;
;;;; Regex parse tree.
;;;;

#-:debug-regex(declaim (inline make-char-node char-node-p char-node-char))
(defun make-char-node (chr)
  chr)
(defun char-node-p (node)
  (characterp node))
(defun char-node-char (char-node)
  char-node)


#-:debug-regex(declaim (inline make-string-node string-node-p string-node-string))
(defun make-string-node (str)
  str)
(defun string-node-p (node)
  (stringp node))
(defun text-node-p (node)
  (or (char-node-p node) (string-node-p node)))
(defun string-node-string (string-node)
  string-node)



#-:debug-regex(declaim (inline make-classseq-node classseq-node-p
                               classseq-node-seq))
(defun make-classseq-node (seq)
  `(classseq ,seq))
(defun classseq-node-p (node)
  (and (listp node) (eq (first node) 'classseq)))
(defun classseq-node-seq (classseq-node)
  (second classseq-node))



#-:debug-regex(declaim (inline make-backmatch-node backmatch-node-p
                               backmatch-node-regnum))
(defun make-backmatch-node (regnum)
  `(backmatch ,regnum))
(defun backmatch-node-p (node)
  (and (listp node) (eq (first node) 'backmatch)))
(defun backmatch-node-regnum (backmatch-node)
  (second backmatch-node))


#-:debug-regex(declaim (inline make-seq-node-list make-seq-node-args
                               seq-node-p
                               seq-node-children seq-node-numchildren
                               seq-node-child))
(defun make-seq-node-list (child-nodes)
  (let ((numchildren (length child-nodes)))
    (cond ((zerop numchildren) nil)
          ((= numchildren 1) (first child-nodes))
          (t `(seq ,@child-nodes)))))
(defun make-seq-node-args (&rest child-nodes)
  (make-seq-node-list child-nodes))
(defun seq-node-p (node)
  (and (listp node) (eq (first node) 'seq)))
(defun seq-node-children (seq-node)
  (rest seq-node))
(defun seq-node-numchildren (seq-node)
  (length (seq-node-children seq-node)))
(defun seq-node-child (seq-node idx)
  (elt (seq-node-children seq-node) idx))


#-:debug-regex(declaim (inline make-kleene-node
                               kleene-node-greedy-p kleene-node-nongreedy-p
                               kleene-node-child))
(defun make-kleene-node (child-node greedyp)
  (cond (greedyp
         `(* ,child-node))
        (t `(*? ,child-node))))
(defun kleene-node-p (node)
  (cond
   ((not (listp node)) nil)
   ((eq (first node) '*) t)
   ((eq (first node) '*?) t)
   (t nil)))
(defun kleene-node-greedy-p (node)
  (eq (first node) '*))
(defun kleene-node-nongreedy-p (node)
  (eq (first node) '*?))
(defun kleene-node-child (kleene-node)
  (second kleene-node))


#-:debug-regex(declaim (inline make-pkleene-node
                               pkleene-node-greedy-p
                               pkleene-node-nongreedy-p
                               pkleene-node-child))
(defun make-pkleene-node (child-node greedyp)
  (cond (greedyp
         `(+ ,child-node))
        (t `(+? ,child-node))))
(defun pkleene-node-p (node)
  (cond
   ((not (listp node)) nil)
   ((eq (first node) '+) t)
   ((eq (first node) '+?) t)
   (t nil)))
(defun pkleene-node-greedy-p (node)
  (eq (first node) '+))
(defun pkleene-node-nongreedy-p (node)
  (eq (first node) '+?))
(defun pkleene-node-child (pkleene-node)
  (second pkleene-node))


#-:debug-regex(declaim (inline make-optional-node
                               optional-node-p
                               optional-node-greedy-p
                               optional-node-nongreedy-p
                               optional-child-node))
(defun make-optional-node (child-node greedyp)
  (cond (greedyp
         `(? ,child-node))
        (t `(?? ,child-node))))
(defun optional-node-p (node)
  (cond
   ((not (listp node)) nil)
   ((eq (first node) '?) t)
   ((eq (first node) '??) t)
   (t nil)))
(defun optional-node-greedy-p (node)
  (eq (first node) '?))
(defun optional-node-nongreedy-p (node)
  (eq (first node) '??))
(defun optional-node-child (optional-node)
  (second optional-node))


#-:debug-regex(declaim (inline make-range-node
                               range-node-greedy-p
                               range-node-nongreedy-p
                               range-node-min
                               range-node-max
                               range-node-child))
(defun make-range-node (child-node min max greedyp)
  (unless (numberp min) (setq min 0))
  (unless (numberp max) (setq max nil))
  (let ((min (if (and (numberp min) (numberp max))
                 (min min max)
               min))
        (max (if (and (numberp min) (numberp max))
                 (max min max)
               max)))
    (cond (greedyp
           `(range (,min . ,max) ,child-node))
          (t `(ngrange (,min . ,max) ,child-node)))))
(defun range-node-p (node)
  (cond
   ((not (listp node)) nil)
   ((eq (first node) 'range) t)
   ((eq (first node) 'ngrange) t)
   (t nil)))
(defun range-node-greedy-p (node)
  (eq (first node) 'range))
(defun range-node-nongreedy-p (node)
  (eq (first node) 'ngrange))
(defun range-node-min (range-node)
  (car (second range-node)))
(defun range-node-max (range-node)
  (cdr (second range-node)))
(defun range-node-child (range-node)
  (third range-node))


#-:debug-regex(declaim (inline make-alt-node-list
                               make-alt-node-args
                               alt-node-p
                               alt-node-children
                               alt-node-numchildren
                               alt-node-first alt-node-second
                               alt-node-child))
(defun make-alt-node-list (child-nodes)
  `(alt ,@child-nodes))
(defun make-alt-node-args (&rest child-nodes)
  `(alt ,@child-nodes))
(defun alt-node-p (node)
  (and (listp node) (eq (first node) 'alt)))
(defun alt-node-children (alt-node)
  (rest alt-node))
(defun alt-node-numchildren (alt-node)
  (length (alt-node-children alt-node)))
(defun alt-node-first (alt-node)
  (first (alt-node-children alt-node)))
(defun alt-node-second (alt-node)
  (second (alt-node-children alt-node)))
(defun alt-node-child (alt-node idx)
  (elt (alt-node-children alt-node) idx))


#-:debug-regex(declaim (inline make-casealt-node-list
                               make-casealt-node-args
                               casealt-node-p
                               casealt-node-children
                               casealt-node-numchildren))
(defun make-casealt-node-list (child-nodes)
  `(casealt ,child-nodes))
(defun make-casealt-node-args (&rest child-nodes)
  `(casealt ,child-nodes))
(defun casealt-node-p (node)
  (and (listp node) (eq (first node) 'casealt)))
(defun casealt-node-children (casealt-node)
  (second casealt-node))
(defun casealt-node-numchildren (casealt-node)
  (length (casealt-node-children casealt-node)))



#-:debug-regex(declaim (inline make-register-node register-node-p
                               register-node-regnum register-node-child))
(defun make-register-node (regnum child)
  `(reg ,regnum ,child))
(defun register-node-p (node)
  (and (listp node) (eq (first node) 'reg)))
(defun register-node-regnum (reg-node)
  (second reg-node))
(defun register-node-child (reg-node)
  (third reg-node))


#-:debug-regex(declaim (inline make-regstart-node
                               regstart-node-p
                               regstart-node-regnum))
(defun make-regstart-node (regnum)
  `(regstart ,regnum))
(defun regstart-node-p (node)
  (and (listp node) (eq (first node) 'regstart)))
(defun regstart-node-regnum (rstart-node)
  (second rstart-node))


#-:debug-regex(declaim (inline make-regend-node
                               regend-node-p
                               regend-node-regnum))
(defun make-regend-node (regnum)
  `(regend ,regnum))
(defun regend-node-p (node)
  (and (listp node) (eq (first node) 'regend)))
(defun regend-node-regnum (rend-node)
  (second rend-node))


#-:debug-regex(declaim (inline make-charclass-node
                               charclass-node-negated-p
                               charclass-node-chars))
(defun make-charclass-node (chars &key negated)
  (cond (negated
         `(ncharclass ,(expand-char-class (coerce chars 'list))))
        (t `(charclass ,(expand-char-class (coerce chars 'list))))))
(defun positive-charclass-node-p (node)
  (and (listp node) (eq (first node) 'charclass)))
(defun charclass-node-p (node)
  (cond
   ((not (listp node)) nil)
   ((eq (first node) 'charclass) t)
   ((eq (first node) 'ncharclass) t)
   (t nil)))
(defun charclass-node-negated-p (charclass-node)
  (eq (first charclass-node) 'ncharclass))
(defun charclass-node-chars (charclass-node)
  (second charclass-node))
(defun char-or-class-node-p (node)
  (or (char-node-p node) (charclass-node-p node)))


#-:debug-regex(declaim (inline make-specclass-node
                               specclass-node-negated-p
                               specclass-node-class))
(defun make-specclass-node (class &key negated)
  (cond (negated
         `(nspecclass ,class))
        (t `(specclass ,class))))
(defun specclass-node-p (node)
  (cond
   ((not (listp node)) nil)
   ((eq (first node) 'specclass) t)
   ((eq (first node) 'nspecclass) t)
   (t nil)))
(defun specclass-node-negated-p (specclass-node)
  (eq (first specclass-node) 'nspecclass))
(defun specclass-node-class (specclass-node)
  (second specclass-node))


#-:debug-regex(declaim (inline make-start-anchor-node start-anchor-node-p))
(defun make-start-anchor-node ()
  '(start))
(defun start-anchor-node-p (node)
  (equalp node '(start)))


#-:debug-regex(declaim (inline make-end-anchor-node end-anchor-node-p))
(defun make-end-anchor-node ()
  '(end))
(defun end-anchor-node-p (node)
  (equalp node '(end)))


#-:debug-regex(declaim (inline make-any-node any-node-p))
(defun make-any-node ()
  '(any))
(defun any-node-p (node)
  (equalp node '(any)))


#-:debug-regex(declaim (inline make-hook-node hook-node-p
                               hook-node-function hook-node-symbol-p
                               hook-node-function-p hook-node-index-p))
(defun make-hook-node (fxn-id)
  `(hook ,fxn-id))
(defun hook-node-p (node)
  (and (listp node) (eq (first node) 'hook)))
(defun hook-node-function (hook-node)
  (second hook-node))
(defun hook-node-symbol-p (hook-node)
  (symbolp (hook-node-function hook-node)))
(defun hook-node-function-p (hook-node)
  (functionp (hook-node-function hook-node)))
(defun hook-node-index-p (hook-node)
  (integerp (hook-node-function hook-node)))



#-:debug-regex(declaim (inline make-success-node success-node-p
                               success-node-rc))
(defun make-success-node (rc)
  `(succeed ,rc))
(defun success-node-p (node)
  (and (listp node) (eq (first node) 'succeed)))
(defun success-node-rc (success-node)
  (second success-node))


;;;;
;;;; Misc handy macros
;;;;

(defun make-regs (n)
  (let ((regs (make-array n)))
    (dotimes (i n regs)
      (setf (svref regs i) (cons nil nil)))))

(declaim (inline register-start))
(defun register-start (regs n)
  (car (svref regs n)))
(define-compiler-macro register-start (regs n)
  `(car (svref ,regs ,n)))

(declaim (inline register-end))
(defun register-end (regs n)
  (cdr (svref regs n)))
(define-compiler-macro register-end (regs n)
  `(cdr (svref ,regs ,n)))

(defsetf register-start (regs n) (val)
  `(setf (car (svref ,regs ,n)) ,val))
(defsetf register-end (regs n) (val)
  `(setf (cdr (svref ,regs ,n)) ,val))

(defun register-matched-p (regs n)
  (and (numberp (register-start regs n))
       (numberp (register-end regs n))
       (<= (register-start regs n) (register-end regs n))))






(defstruct re-scanner
  (str "" :type string)
  (pos 0 :type fixnum)
  (end 0 :type fixnum)
  (mode 'in-regex :type symbol)
  (regnum 0 :type fixnum)
  (ungot-token nil)
  (ungot-value nil)
  )



(defstruct closure-info
  matchfn initfn linkfn)

(defun resolve-instr (closure-vec instr-num)
  (closure-info-matchfn (aref closure-vec instr-num)))

(defun resolve-linkfn (closure-vec instr-num)
  (closure-info-linkfn (aref closure-vec instr-num)))

(defun resolve-initfn (closure-vec instr-num)
  (closure-info-initfn (aref closure-vec instr-num)))


(defstruct matcher
  simple-string-matchfn string-matchfn numregs matchstr matchexpr acceptfn)

(defconstant +max-regex-str-cache+ 500
  "Max number of entries in the regex compiler cache.")

(defvar *pattern-cache* (make-hash-table :test #'equal))

(defun clear-pattern-cache ()
  (clrhash *pattern-cache*)
  nil)


(define-constant +special-class-names+
  '((":alpha:" alpha) (":upper:" upper) (":lower:" lower) (":digit:" digit)
    (":alnum:" alnum) (":xdigit:" xdigit) (":odigit:" odigit) (":punct:" punct)
    (":space:" space)))




(defun expand-char-class (chars)
"Expand an encoded char class into an explicit enumeration of all
the chars, e.g. 'a-f\A-F' --> 'abcdefABCDEF'."
  (parse-std-char-class chars))

(defun parse-std-char-class (in)
  (let ((out (make-string-output-stream)))
    (do ((chr (pop in) (pop in))
         (prv nil chr))
        ((null chr) (progn
                      (when prv (write-char prv out))
                      (get-output-stream-string out)))
      (case chr
        (dash
         (let ((nxt (pop in)))
           (cond
            ((and (null nxt) (null prv))
             (write-char #\- out))
            ((null nxt)
             (write-char prv out)
             (write-char #\- out))
            ((null prv)
             (write-char #\- out)
             (write-char nxt out))
            (t (generate-char-range out prv nxt)
               (setq prv nil)))
           (setq chr (pop in))))
        (t (when prv
             (write-char prv out)))))))

(defun generate-char-range (strm start end)
  (when (< (char-int end) (char-int start))
    (rotatef start end))
  (loop for ic from (char-int start) to (char-int end)
        do (write-char (code-char ic) strm)))

Something went wrong with that request. Please try again.