dev-validator
Matthew Butterick 11 years ago
parent 64f4f196ef
commit 08abd8e235

@ -27,7 +27,7 @@
[else #f])) [else #f]))
(define (validate-txexpr-attrs? x #:context [txexpr-context #f]) (define (validate-txexpr-attrs x #:context [txexpr-context #f])
; ((any/c) (#:context (or/c #f txexpr?)) . ->* . txexpr-attrs?) ; ((any/c) (#:context (or/c #f txexpr?)) . ->* . txexpr-attrs?)
(define (make-reason) (define (make-reason)
(if (not (list? x)) (if (not (list? x))
@ -45,7 +45,7 @@
(define+provide+safe (txexpr-attrs? x) (define+provide+safe (txexpr-attrs? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(with-handlers ([exn:fail? (λ(exn) #f)]) (with-handlers ([exn:fail? (λ(exn) #f)])
(and (validate-txexpr-attrs? x) #t))) (and (validate-txexpr-attrs x) #t)))
(define+provide+safe (txexpr-elements? x) (define+provide+safe (txexpr-elements? x)
@ -54,7 +54,7 @@
[(list elem ...) (andmap txexpr-element? elem)] [(list elem ...) (andmap txexpr-element? elem)]
[else #f])) [else #f]))
(define (validate-txexpr-element? x #:context [txexpr-context #f]) (define (validate-txexpr-element x #:context [txexpr-context #f])
; ((any/c) (#:context (or/c #f txexpr?)) . ->* . txexpr-element?) ; ((any/c) (#:context (or/c #f txexpr?)) . ->* . txexpr-element?)
(cond (cond
[(or (string? x) (txexpr? x) (symbol? x) [(or (string? x) (txexpr? x) (symbol? x)
@ -67,28 +67,28 @@
(define+provide+safe (txexpr-element? x) (define+provide+safe (txexpr-element? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(with-handlers ([exn:fail? (λ(exn) #f)]) (with-handlers ([exn:fail? (λ(exn) #f)])
(and (validate-txexpr-element? x) #t))) (and (validate-txexpr-element x) #t)))
;; is it a named x-expression? ;; is it a named x-expression?
;; todo: rewrite this recurively so errors can be pinpointed (for debugging) ;; todo: rewrite this recurively so errors can be pinpointed (for debugging)
(define+provide+safe (validate-txexpr? x) (define+provide+safe (validate-txexpr x)
(any/c . -> . txexpr?) (any/c . -> . txexpr?)
(define (validate-txexpr-element-with-context? e) (validate-txexpr-element? e #:context x)) (define (validate-txexpr-element-with-context e) (validate-txexpr-element e #:context x))
(define (validate-txexpr-attrs-with-context? e) (validate-txexpr-attrs? e #:context x)) (define (validate-txexpr-attrs-with-context e) (validate-txexpr-attrs e #:context x))
(when (match x (when (match x
[(list (? symbol?)) #t] [(list (? symbol?)) #t]
[(list (? symbol? name) (and attr-list (list (list k v ...) ...)) rest ...) [(list (? symbol? name) (and attr-list (list (list k v ...) ...)) rest ...)
(and (validate-txexpr-attrs-with-context? attr-list) (and (validate-txexpr-attrs-with-context attr-list)
(andmap validate-txexpr-element-with-context? rest))] (andmap validate-txexpr-element-with-context rest))]
[(list (? symbol? name) rest ...)(andmap validate-txexpr-element-with-context? rest)] [(list (? symbol? name) rest ...)(andmap validate-txexpr-element-with-context rest)]
[else (error (format "validate-txexpr: first element is not a symbol in ~v" x))]) [else (error (format "validate-txexpr: first element is not a symbol in ~v" x))])
x)) x))
(define+provide+safe (txexpr? x) (define+provide+safe (txexpr? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(with-handlers ([exn:fail? (λ(exn) #f)]) (with-handlers ([exn:fail? (λ(exn) #f)])
(and (validate-txexpr? x) #t))) (and (validate-txexpr x) #t)))

@ -146,20 +146,18 @@ boolean?]
Shorthand for @code{(listof txexpr-attr?)} and @code{(listof txexpr-element?)}. Shorthand for @code{(listof txexpr-attr?)} and @code{(listof txexpr-element?)}.
@deftogether[(
@defproc[ @defproc[
(validate-txexpr? (validate-txexpr
[possible-txexpr any/c]) [possible-txexpr any/c])
txexpr?] txexpr?]
Like @racket[txexpr?], but raises a descriptive error if @racket[_possible-txexpr] is invalid, and otherwise returns @racket[_possible-txexpr] itself.
@examples[#:eval my-eval
@defproc[ (validate-txexpr '(root (mama.html son.html daughter.html) uncle.html))
(can-be-txexpr-attr-value? (validate-txexpr `(root (,+ son.html daughter.html) uncle.html))
[v any/c]) (validate-txexpr '(root (mama.html son.html son.html) mama.html))
boolean?] ]
)]
Predicates for input arguments that are trivially converted to an attribute @racket[_key] or @racket[_value]…

Loading…
Cancel
Save