1999-09-23 10:27:41 -04:00
|
|
|
;;; Regular expression matching for scsh
|
|
|
|
;;; Copyright (c) 1994 by Olin Shivers.
|
|
|
|
|
|
|
|
;;; Match data for regexp matches.
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(define-record regexp-match
|
2001-08-09 09:53:18 -04:00
|
|
|
string
|
|
|
|
submatches)
|
1999-09-23 10:27:41 -04:00
|
|
|
|
|
|
|
(define (match:start match . maybe-index)
|
2001-08-09 09:53:18 -04:00
|
|
|
(match-start
|
|
|
|
(vector-ref (regexp-match:submatches match)
|
|
|
|
(:optional maybe-index 0))))
|
1999-09-23 10:27:41 -04:00
|
|
|
|
|
|
|
(define (match:end match . maybe-index)
|
2001-10-01 11:19:08 -04:00
|
|
|
(match-end
|
2001-08-09 09:53:18 -04:00
|
|
|
(vector-ref (regexp-match:submatches match)
|
|
|
|
(:optional maybe-index 0))))
|
1999-09-23 10:27:41 -04:00
|
|
|
|
|
|
|
(define (match:substring match . maybe-index)
|
|
|
|
(let* ((i (:optional maybe-index 0))
|
2001-08-09 09:53:18 -04:00
|
|
|
(submatch (vector-ref (regexp-match:submatches match) i)))
|
|
|
|
(and submatch (substring (regexp-match:string match)
|
|
|
|
(match-start submatch)
|
|
|
|
(match-end submatch)))))
|
1999-09-23 10:27:41 -04:00
|
|
|
|
|
|
|
;;; Compiling regexps
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; There's no legal Posix string expressing the empty match (e.g., (|))
|
|
|
|
;;; that will never match anything. So when we have one of these, we set
|
|
|
|
;;; the STRING field to #f. The matchers will spot this case and handle it
|
|
|
|
;;; specially.
|
|
|
|
|
|
|
|
;;; We compile the string two ways, on demand -- one for cre-search, and
|
|
|
|
;;; one for cre-search?.
|
|
|
|
|
1999-10-08 09:16:35 -04:00
|
|
|
;(define-record cre ; A compiled regular expression
|
|
|
|
; string ; The Posix string form of the regexp or #F.
|
|
|
|
; max-paren ; Max paren in STRING needed for submatches.
|
2001-08-09 09:53:18 -04:00
|
|
|
; (regexp #f) ; Compiled form or #F.
|
|
|
|
; (regexp/nm #f) ; Same as REGEXP, but compiled with no-submatch.
|
1999-10-08 09:16:35 -04:00
|
|
|
; tvec ; Translation vector for the submatches
|
|
|
|
; ((disclose self) (list "cre" (cre:string self))))
|
|
|
|
|
|
|
|
(define-record-type cre :cre
|
2001-08-09 09:53:18 -04:00
|
|
|
(really-make-cre string max-paren regexp regexp/nm tvec debug)
|
1999-10-08 09:16:35 -04:00
|
|
|
cre?
|
|
|
|
(string cre:string set-cre:string)
|
|
|
|
(max-paren cre:max-paren set-cre:max-paren)
|
2001-08-09 09:53:18 -04:00
|
|
|
(regexp cre:regexp set-cre:regexp)
|
|
|
|
(regexp/nm cre:regexp/nm set-cre:regexp/nm)
|
1999-10-08 09:16:35 -04:00
|
|
|
(tvec cre:tvec set-cre:tvec)
|
|
|
|
(debug cre:debug set-cre:debug))
|
|
|
|
|
|
|
|
(define-record-discloser :cre
|
|
|
|
(lambda (self) (list "cre" (cre:string self))))
|
|
|
|
|
|
|
|
(define (make-cre str max-paren tvec)
|
|
|
|
(really-make-cre str max-paren #f #f tvec #f))
|
|
|
|
|
2001-08-09 09:53:18 -04:00
|
|
|
(define (new-cre str tvec)
|
|
|
|
(make-cre str (max-live-posix-submatch tvec) tvec))
|
1999-09-23 10:27:41 -04:00
|
|
|
|
|
|
|
(define (max-live-posix-submatch tvec)
|
|
|
|
(vfold (lambda (sm mlpsm) (if sm (max mlpsm sm) mlpsm)) 0 tvec))
|
|
|
|
|
|
|
|
;;; Searching with compiled regexps
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; cre-search returns match info; cre-search? is just a predicate.
|
|
|
|
|
2001-08-09 09:53:18 -04:00
|
|
|
;; ### we do not look at START yet
|
1999-09-23 10:27:41 -04:00
|
|
|
(define (cre-search cre start-vec end-vec str start)
|
|
|
|
(let ((re-str (cre:string cre))) ;; RE-STR = #F => empty match.
|
2001-08-09 09:53:18 -04:00
|
|
|
(if (not re-str)
|
|
|
|
#f
|
|
|
|
(begin
|
|
|
|
(if (not (cre:regexp cre))
|
|
|
|
(set-cre:regexp cre (make-regexp re-str
|
|
|
|
(regexp-option extended)
|
|
|
|
(regexp-option submatches))))
|
|
|
|
(let ((ret (regexp-match (cre:regexp cre) str #t #f #f start)))
|
|
|
|
(if (not ret)
|
|
|
|
#f
|
|
|
|
(make-regexp-match str
|
|
|
|
(list->vector ret))))))))
|
|
|
|
|
2001-10-05 04:27:26 -04:00
|
|
|
(define (cre-search? cre str start)
|
1999-09-23 10:27:41 -04:00
|
|
|
(let ((re-str (cre:string cre))) ;; RE-STR = #F => empty match.
|
2001-08-09 09:53:18 -04:00
|
|
|
(if (not re-str)
|
|
|
|
#f
|
|
|
|
(begin
|
|
|
|
(if (not (cre:regexp/nm cre))
|
|
|
|
(set-cre:regexp/nm cre (make-regexp re-str
|
|
|
|
(regexp-option extended))))
|
|
|
|
(regexp-match (cre:regexp/nm cre) str #f #f #f)))))
|