diff --git a/lib/chibi/regexp.scm b/lib/chibi/regexp.scm index dddfbf05..0f9a655f 100644 --- a/lib/chibi/regexp.scm +++ b/lib/chibi/regexp.scm @@ -578,6 +578,9 @@ (else (lp (cddr ls) (cons (cons (car ls) (cadr ls)) res)))))) +(define (every pred ls) + (or (null? ls) (and (pred (car ls)) (every pred (cdr ls))))) + (define (char-set-sre? sre) (or (char? sre) (and (string? sre) (= 1 (string-length sre))) @@ -587,10 +590,32 @@ (memq (car sre) '(char-set / char-range & and ~ complement - difference)) (and (memq (car sre) '(|\|| or)) - (let lp ((ls (cdr sre))) - (or (null? ls) - (and (char-set-sre? (car ls)) - (lp (cdr ls)))))))))) + (every char-set-sre? (cdr sre))))))) + +(define (valid-sre? x) + (or (regexp? x) + (string? x) + (char-set-sre? x) + (and (pair? x) + (memq (car x) + '(|\|| or : seq $ submatch *$ submatch-list ? optional + * zero-or-more + one-or-more ?? non-greedy-optional + *? non-greedy-zero-or-more +? non-greedy-one-or-more + look-ahead neg-look-ahead look-behind neg-look-behind + w/case w/nocase w/unicode w/ascii word word+)) + (every valid-sre? (cdr x))) + (and (pair? x) + (memq (car x) + '(>= -> => submatch-named *-> *=> submatch-named-list)) + (pair? (cdr x)) + (every valid-sre? (cddr x))) + (and (pair? x) + (memq (car x) '(** repeated **? non-greedy-repeated)) + (pair? (cdr x)) + (pair? (cddr x)) + (or (not (cadr x)) (integer? (cadr x))) + (or (not (car (cddr x))) (integer? (car (cddr x)))) + (every valid-sre? (cdr (cddr x)))))) (define (sre->char-set sre . o) (let ((flags (if (pair? o) (car o) ~none))) diff --git a/lib/chibi/regexp.sld b/lib/chibi/regexp.sld index 09e87c70..3383ae63 100644 --- a/lib/chibi/regexp.sld +++ b/lib/chibi/regexp.sld @@ -1,6 +1,6 @@ (define-library (chibi regexp) - (export regexp regexp? rx regexp->sre char-set->sre + (export regexp regexp? valid-sre? rx regexp->sre char-set->sre regexp-matches regexp-matches? regexp-search regexp-replace regexp-replace-all regexp-fold regexp-extract regexp-split