6.0 candidate

typed-work
Matthew Butterick 10 years ago
parent 303a0ff52f
commit d490fc7ea5

@ -3,8 +3,9 @@
(require racket/match racket/string racket/list racket/bool "core-predicates.rkt") (require racket/match racket/string racket/list racket/bool "core-predicates.rkt")
(provide (all-defined-out) (all-from-out "core-predicates.rkt")) (provide (all-defined-out) (all-from-out "core-predicates.rkt"))
(require typed/sugar/debug) (require typed/sugar/debug)
(define/typed (validate-txexpr-attrs x #:context [txexpr-context #f]) (define/typed (validate-txexpr-attrs x #:context [txexpr-context #f])
(Txexpr-Attrs #:context Any -> Txexpr-Attrs) (Txexpr-Attrs [#:context Any] -> Txexpr-Attrs)
(define/typed (make-reason) (define/typed (make-reason)
(-> String) (-> String)
(if (not (list? x)) (if (not (list? x))
@ -21,7 +22,7 @@
(define/typed (validate-txexpr-element x #:context [txexpr-context #f]) (define/typed (validate-txexpr-element x #:context [txexpr-context #f])
(Txexpr-Element #:context Any -> Txexpr-Element) (Txexpr-Element [#:context Any] -> Txexpr-Element)
(cond (cond
[(or (string? x) (txexpr? x) (symbol? x) [(or (string? x) (txexpr? x) (symbol? x)
(valid-char? x) (cdata? x)) x] (valid-char? x) (cdata? x)) x]
@ -44,12 +45,18 @@
[else (error 'validate-txexpr (format "~v is not a list starting with a symbol" x))])) [else (error 'validate-txexpr (format "~v is not a list starting with a symbol" x))]))
(define/typed (make-txexpr tag [attrs null] [elements null]) (define/typed make-txexpr
(Symbol Txexpr-Attrs (Listof Txexpr-Element) -> Txexpr) (case-> (Symbol -> Txexpr)
(Symbol Txexpr-Attrs -> Txexpr)
(Symbol Txexpr-Attrs (Listof Txexpr-Element) -> Txexpr))
(case-lambda
[(tag) (make-txexpr tag null null)]
[(tag attrs) (make-txexpr tag attrs null)]
[(tag attrs elements)
(define result (cons tag (append (if (empty? attrs) empty (list attrs)) elements))) (define result (cons tag (append (if (empty? attrs) empty (list attrs)) elements)))
(if (txexpr? result) (if (txexpr? result)
result result
(error 'make-txexpr "This can't happen"))) (error 'make-txexpr "This can't happen"))]))
(define/typed (txexpr->values x) (define/typed (txexpr->values x)
@ -205,8 +212,12 @@
;; function to split tag out of txexpr ;; function to split tag out of txexpr
(define deleted-signal (gensym)) (define deleted-signal (gensym))
(define/typed (splitf-txexpr tx pred [proc (λ:([x : Xexpr]) deleted-signal)]) (define/typed splitf-txexpr
(Txexpr (Xexpr -> Boolean) (Xexpr -> Xexpr) -> (values Txexpr Txexpr-Elements)) (case-> (Txexpr (Xexpr -> Boolean) -> (values Txexpr Txexpr-Elements))
(Txexpr (Xexpr -> Boolean) (Xexpr -> Xexpr) -> (values Txexpr Txexpr-Elements)))
(case-lambda
[(tx pred) (splitf-txexpr tx pred (λ:([x : Xexpr]) deleted-signal))]
[(tx pred proc)
(define: matches : Txexpr-Elements null) (define: matches : Txexpr-Elements null)
(define/typed (do-extraction x) (define/typed (do-extraction x)
(Xexpr -> Xexpr) (Xexpr -> Xexpr)
@ -217,10 +228,10 @@
[(txexpr? x) (let-values([(tag attr elements) (txexpr->values x)]) [(txexpr? x) (let-values([(tag attr elements) (txexpr->values x)])
(make-txexpr tag attr (filter (λ:([e : Xexpr]) (not (equal? e deleted-signal))) (map do-extraction elements))))] (make-txexpr tag attr (filter (λ:([e : Xexpr]) (not (equal? e deleted-signal))) (map do-extraction elements))))]
[else x])) [else x]))
(define tx-extracted (do-extraction tx)) ;; do this first to fill matches (define: tx-extracted : Xexpr (do-extraction tx)) ;; do this first to fill matches
(values (if (txexpr? tx-extracted) (values (if (txexpr? tx-extracted)
tx-extracted tx-extracted
(error 'splitf-txexpr "Can't get here")) (reverse matches))) (error 'splitf-txexpr "Can't get here")) (reverse matches))]))
(define/typed (xexpr->html x) (define/typed (xexpr->html x)

Loading…
Cancel
Save