Adding rx and regexp->sre.

This commit is contained in:
Alex Shinn 2013-11-29 23:42:36 +09:00
parent e1e7508d8d
commit 05dcd92d72
2 changed files with 12 additions and 4 deletions

View file

@ -5,13 +5,20 @@
;;; An rx represents a start state and meta-info such as the number ;;; An rx represents a start state and meta-info such as the number
;;; and names of submatches. ;;; and names of submatches.
(define-record-type Rx (define-record-type Rx
(make-rx start-state num-matches num-save-indexes match-rules match-names) (make-rx start-state num-matches num-save-indexes match-rules match-names sre)
regexp? regexp?
(start-state rx-start-state rx-start-state-set!) (start-state rx-start-state rx-start-state-set!)
(num-matches rx-num-matches rx-num-matches-set!) (num-matches rx-num-matches rx-num-matches-set!)
(num-save-indexes rx-num-save-indexes rx-num-save-indexes-set!) (num-save-indexes rx-num-save-indexes rx-num-save-indexes-set!)
(match-rules rx-rules rx-rules-set!) (match-rules rx-rules rx-rules-set!)
(match-names rx-names rx-names-set!)) (match-names rx-names rx-names-set!)
(sre regexp->sre))
;; Syntactic sugar.
(define-syntax rx
(syntax-rules ()
((rx sre ...)
(regexp `(: sre ...)))))
;;; A state is a single nfa state with transition rules. ;;; A state is a single nfa state with transition rules.
(define-record-type State (define-record-type State
@ -819,7 +826,7 @@
sre sre
(let ((start (make-submatch-state sre flags (make-accept-state) 0))) (let ((start (make-submatch-state sre flags (make-accept-state) 0)))
(make-rx start current-match current-index (make-rx start current-match current-index
(list->vector (reverse match-rules)) match-names))))) (list->vector (reverse match-rules)) match-names sre)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Utilities ;; Utilities

View file

@ -1,6 +1,7 @@
(define-library (chibi regexp) (define-library (chibi regexp)
(export regexp regexp? regexp-matches regexp-matches? regexp-search (export regexp regexp? rx regexp->sre
regexp-matches regexp-matches? regexp-search
regexp-replace regexp-replace-all regexp-replace regexp-replace-all
regexp-fold regexp-extract regexp-split regexp-fold regexp-extract regexp-split
regexp-match? regexp-match-num-matches regexp-match? regexp-match-num-matches