validate a txexpr with empty attrs

master
Matthew Butterick 2 years ago
parent 41d2175803
commit a49498717c

@ -115,11 +115,12 @@
(define+provide+safe (validate-txexpr x)
(any/c . -> . txexpr?)
(unless (list? x) (raise-argument-error 'validate-txexpr "tagged X-expression" x))
(unless (symbol? (car x)) (txexpr-error "tag" "must be a symbol" (car x) x))
(match (rest x)
[(list-rest (list (? list?) attrs ...) elems)
(match x
[(list* tag _) #:when (not (symbol? tag))
(txexpr-error "tag" "must be a symbol" tag x)]
[(list* tag (or (? null?) (list (? list?) _ ...)) elems)
(and (validate-txexpr-attrs x) (validate-txexpr-elements elems x) x)]
[(? list? elems) (and (validate-txexpr-elements elems x) x)]))
[(list* tag elems) (and (validate-txexpr-elements elems x) x)]))
(define (txexpr-unsafe tag attrs elements)
(cons tag (match attrs

@ -66,6 +66,7 @@
(check-not-exn (λ _ (validate-txexpr '(p))))
(check-not-exn (λ _ (validate-txexpr '(p "foo" "bar"))))
(check-not-exn (λ _ (validate-txexpr '(p ((key "value")) "foo" "bar"))))
(check-not-exn (λ _ (validate-txexpr '(p () "foo" "bar"))))
(check-not-exn (λ _ (validate-txexpr '(p 123)))) ; content is a valid-char
(check-exn exn:fail? (λ _ (validate-txexpr "foo"))) ; not a list with symbol
(check-exn exn:fail? (λ _ (validate-txexpr '(p "foo" "bar" ((key "value")))))) ; malformed

Loading…
Cancel
Save