You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
7.8 KiB
Racket
194 lines
7.8 KiB
Racket
21 years ago
|
(module stx mzscheme
|
||
17 years ago
|
(require syntax/boundmap
|
||
21 years ago
|
"util.ss")
|
||
21 years ago
|
|
||
|
(provide parse)
|
||
21 years ago
|
|
||
|
(define (bad-args stx num)
|
||
21 years ago
|
(raise-syntax-error
|
||
21 years ago
|
#f
|
||
|
(format "incorrect number of arguments (should have ~a)" num)
|
||
21 years ago
|
stx))
|
||
21 years ago
|
|
||
21 years ago
|
;; char-range-arg: syntax-object syntax-object -> nat
|
||
|
;; If c contains is a character or length 1 string, returns the integer
|
||
|
;; for the character. Otherwise raises a syntax error.
|
||
|
(define (char-range-arg stx containing-stx)
|
||
|
(let ((c (syntax-e stx)))
|
||
|
(cond
|
||
|
((char? c) (char->integer c))
|
||
|
((and (string? c) (= (string-length c) 1))
|
||
|
(char->integer (string-ref c 0)))
|
||
|
(else
|
||
|
(raise-syntax-error
|
||
|
#f
|
||
|
"not a char or single-char string"
|
||
|
containing-stx stx)))))
|
||
|
(test-block ()
|
||
|
((char-range-arg #'#\1 #'here) (char->integer #\1))
|
||
|
((char-range-arg #'"1" #'here) (char->integer #\1)))
|
||
21 years ago
|
|
||
21 years ago
|
|
||
21 years ago
|
;; parse : syntax-object (box (list-of syntax-object)) -> s-re (see re.ss)
|
||
21 years ago
|
;; checks for errors and generates the plain s-exp form for s
|
||
21 years ago
|
;; Expands lex-abbrevs and applies lex-trans.
|
||
21 years ago
|
(define (parse stx disappeared-uses)
|
||
|
(let ((parse
|
||
21 years ago
|
(lambda (s)
|
||
|
(parse (syntax-recertify s stx (current-inspector) 'a)
|
||
|
disappeared-uses))))
|
||
21 years ago
|
(syntax-case stx (repetition union intersection complement concatenation
|
||
|
char-range char-complement)
|
||
|
(_
|
||
|
(identifier? stx)
|
||
|
(let ((expansion (syntax-local-value stx (lambda () #f))))
|
||
|
(unless (lex-abbrev? expansion)
|
||
|
(raise-syntax-error 'regular-expression
|
||
|
"undefined abbreviation"
|
||
|
stx))
|
||
21 years ago
|
(set-box! disappeared-uses (cons stx (unbox disappeared-uses)))
|
||
17 years ago
|
(parse ((lex-abbrev-get-abbrev expansion)))))
|
||
21 years ago
|
(_
|
||
|
(or (char? (syntax-e stx)) (string? (syntax-e stx)))
|
||
|
(syntax-e stx))
|
||
|
((repetition arg ...)
|
||
|
(let ((arg-list (syntax->list (syntax (arg ...)))))
|
||
|
(unless (= 3 (length arg-list))
|
||
21 years ago
|
(bad-args stx 2))
|
||
|
(let ((low (syntax-e (car arg-list)))
|
||
|
(high (syntax-e (cadr arg-list)))
|
||
21 years ago
|
(re (caddr arg-list)))
|
||
21 years ago
|
(unless (and (number? low) (exact? low) (integer? low) (>= low 0))
|
||
|
(raise-syntax-error #f
|
||
|
"not a non-negative exact integer"
|
||
|
stx
|
||
|
(car arg-list)))
|
||
|
(unless (or (and (number? high) (exact? high) (integer? high) (>= high 0))
|
||
|
(eq? high +inf.0))
|
||
|
(raise-syntax-error #f
|
||
|
"not a non-negative exact integer or +inf.0"
|
||
|
stx
|
||
|
(cadr arg-list)))
|
||
|
(unless (<= low high)
|
||
|
(raise-syntax-error
|
||
|
#f
|
||
|
"the first argument is not less than or equal to the second argument"
|
||
|
stx))
|
||
|
`(repetition ,low ,high ,(parse re)))))
|
||
21 years ago
|
((union re ...)
|
||
|
`(union ,@(map parse (syntax->list (syntax (re ...))))))
|
||
|
((intersection re ...)
|
||
|
`(intersection ,@(map parse (syntax->list (syntax (re ...))))))
|
||
|
((complement re ...)
|
||
|
(let ((re-list (syntax->list (syntax (re ...)))))
|
||
|
(unless (= 1 (length re-list))
|
||
21 years ago
|
(bad-args stx 1))
|
||
21 years ago
|
`(complement ,(parse (car re-list)))))
|
||
|
((concatenation re ...)
|
||
|
`(concatenation ,@(map parse (syntax->list (syntax (re ...))))))
|
||
|
((char-range arg ...)
|
||
|
(let ((arg-list (syntax->list (syntax (arg ...)))))
|
||
|
(unless (= 2 (length arg-list))
|
||
21 years ago
|
(bad-args stx 2))
|
||
21 years ago
|
(let ((i1 (char-range-arg (car arg-list) stx))
|
||
|
(i2 (char-range-arg (cadr arg-list) stx)))
|
||
|
(if (<= i1 i2)
|
||
|
`(char-range ,(integer->char i1) ,(integer->char i2))
|
||
|
(raise-syntax-error
|
||
21 years ago
|
#f
|
||
|
"the first argument does not precede or equal second argument"
|
||
21 years ago
|
stx)))))
|
||
|
((char-complement arg ...)
|
||
|
(let ((arg-list (syntax->list (syntax (arg ...)))))
|
||
|
(unless (= 1 (length arg-list))
|
||
21 years ago
|
(bad-args stx 1))
|
||
21 years ago
|
(let ((parsed (parse (car arg-list))))
|
||
21 years ago
|
(unless (char-set? parsed)
|
||
|
(raise-syntax-error #f
|
||
|
"not a character set"
|
||
|
stx
|
||
|
(car arg-list)))
|
||
21 years ago
|
`(char-complement ,parsed))))
|
||
|
((op form ...)
|
||
|
(identifier? (syntax op))
|
||
21 years ago
|
(let* ((o (syntax op))
|
||
21 years ago
|
(expansion (syntax-local-value
|
||
|
(syntax-recertify o stx (current-inspector) 'a)
|
||
|
(lambda () #f))))
|
||
21 years ago
|
(set-box! disappeared-uses (cons o (unbox disappeared-uses)))
|
||
21 years ago
|
(cond
|
||
|
((lex-trans? expansion)
|
||
|
(parse ((lex-trans-f expansion) stx)))
|
||
|
(expansion
|
||
|
(raise-syntax-error 'regular-expression
|
||
|
"not a lex-trans"
|
||
|
stx))
|
||
|
(else
|
||
|
(raise-syntax-error 'regular-expression
|
||
|
"undefined operator"
|
||
|
stx)))))
|
||
21 years ago
|
(_
|
||
|
(raise-syntax-error
|
||
|
'regular-expression
|
||
21 years ago
|
"not a char, string, identifier, or (op args ...)"
|
||
21 years ago
|
stx)))))
|
||
21 years ago
|
|
||
21 years ago
|
|
||
21 years ago
|
|
||
21 years ago
|
;; char-set? : s-re -> bool
|
||
|
;; A char-set is an re that matches only strings of length 1.
|
||
|
;; char-set? is conservative.
|
||
|
(define (char-set? s-re)
|
||
21 years ago
|
(cond
|
||
|
((char? s-re) #t)
|
||
|
((string? s-re) (= (string-length s-re) 1))
|
||
|
((list? s-re)
|
||
|
(let ((op (car s-re)))
|
||
|
(case op
|
||
21 years ago
|
((union intersection) (andmap char-set? (cdr s-re)))
|
||
21 years ago
|
((char-range char-complement) #t)
|
||
21 years ago
|
((repetition)
|
||
|
(and (= (cadr s-re) (caddr s-re)) (char-set? (cadddr s-re))))
|
||
|
((concatenation)
|
||
|
(and (= 2 (length s-re)) (char-set? (cadr s-re))))
|
||
21 years ago
|
(else #f))))
|
||
|
(else #f)))
|
||
21 years ago
|
(test-block ()
|
||
|
((char-set? #\a) #t)
|
||
|
((char-set? "12") #f)
|
||
|
((char-set? "1") #t)
|
||
|
((char-set? '(repetition 1 2 #\1)) #f)
|
||
|
((char-set? '(repetition 1 1 "12")) #f)
|
||
|
((char-set? '(repetition 1 1 "1")) #t)
|
||
|
((char-set? '(union "1" "2" "3")) #t)
|
||
|
((char-set? '(union "1" "" "3")) #f)
|
||
|
((char-set? '(intersection "1" "2" (union "3" "4"))) #t)
|
||
|
((char-set? '(intersection "1" "")) #f)
|
||
|
((char-set? '(complement "1")) #f)
|
||
|
((char-set? '(concatenation "1" "2")) #f)
|
||
|
((char-set? '(concatenation "" "2")) #f)
|
||
|
((char-set? '(concatenation "1")) #t)
|
||
|
((char-set? '(concatenation "12")) #f)
|
||
|
((char-set? '(char-range #\1 #\2)) #t)
|
||
|
((char-set? '(char-complement #\1)) #t))
|
||
|
|
||
|
(test-block ()
|
||
|
((parse #'#\a) #\a)
|
||
|
((parse #'"1") "1")
|
||
|
((parse #'(repetition 1 1 #\1)) '(repetition 1 1 #\1))
|
||
|
((parse #'(repetition 0 +inf.0 #\1)) '(repetition 0 +inf.0 #\1))
|
||
|
((parse #'(union #\1 (union "2") (union)))
|
||
|
'(union #\1 (union "2") (union)))
|
||
|
((parse #'(intersection #\1 (intersection "2") (intersection)))
|
||
|
'(intersection #\1 (intersection "2") (intersection)))
|
||
|
((parse #'(complement (union #\1 #\2)))
|
||
|
'(complement (union #\1 #\2)))
|
||
|
((parse #'(concatenation "1" "2" (concatenation)))
|
||
|
'(concatenation "1" "2" (concatenation)))
|
||
|
((parse #'(char-range "1" #\1)) '(char-range #\1 #\1))
|
||
|
((parse #'(char-range #\1 "1")) '(char-range #\1 #\1))
|
||
|
((parse #'(char-range "1" "3")) '(char-range #\1 #\3))
|
||
|
((parse #'(char-complement (union "1" "2")))
|
||
|
'(char-complement (union "1" "2"))))
|
||
17 years ago
|
)
|