reconsider some names

dev-validator
Matthew Butterick 11 years ago
parent 3fa82aa602
commit 9eea5c6e97

@ -14,32 +14,32 @@
;; a tagged-xexpr consists of a tag, optional attributes, and then elements. ;; a tagged-xexpr consists of a tag, optional attributes, and then elements.
(define+provide/contract (xexpr-tag? x) (define+provide/contract (tagged-xexpr-tag? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(symbol? x)) (symbol? x))
(define+provide/contract (xexpr-attr? x) (define+provide/contract (tagged-xexpr-attr? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(match x (match x
[(list (? symbol?) (? string?)) #t] [(list (? symbol?) (? string?)) #t]
[else #f])) [else #f]))
(define+provide/contract (xexpr-attrs? x) (define+provide/contract (tagged-xexpr-attrs? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(match x (match x
[(list (? xexpr-attr?) ...) #t] [(list (? tagged-xexpr-attr?) ...) #t]
[else #f])) [else #f]))
(define+provide/contract (xexpr-element? x) (define+provide/contract (tagged-xexpr-element? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(or (string? x) (tagged-xexpr? x) (symbol? x) (or (string? x) (tagged-xexpr? x) (symbol? x)
(valid-char? x) (cdata? x))) (valid-char? x) (cdata? x)))
(define+provide/contract (xexpr-elements? x) (define+provide/contract (tagged-xexpr-elements? x)
(any/c . -> . boolean?) (any/c . -> . boolean?)
(match x (match x
[(list elem ...) (andmap xexpr-element? elem)] [(list elem ...) (andmap tagged-xexpr-element? elem)]
[else #f])) [else #f]))
;; is it a named x-expression? ;; is it a named x-expression?
@ -49,8 +49,8 @@
(and (xexpr? x) ; meets basic xexpr contract (and (xexpr? x) ; meets basic xexpr contract
(match x (match x
[(list (? symbol? name) rest ...) ; is a list starting with a symbol [(list (? symbol? name) rest ...) ; is a list starting with a symbol
(or (andmap xexpr-element? rest) ; the rest is content or ... (or (andmap tagged-xexpr-element? rest) ; the rest is content or ...
(and (xexpr-attrs? (car rest)) (andmap xexpr-element? (cdr rest))))] ; attr + content (and (tagged-xexpr-attrs? (car rest)) (andmap tagged-xexpr-element? (cdr rest))))] ; attr + content
[else #f]))) [else #f])))
@ -60,7 +60,7 @@
(define+provide/contract (make-tagged-xexpr tag [attrs empty] [elements empty]) (define+provide/contract (make-tagged-xexpr tag [attrs empty] [elements empty])
; xexpr/c provides a nicer error message, ; xexpr/c provides a nicer error message,
; but is not sufficient on its own (too permissive) ; but is not sufficient on its own (too permissive)
((symbol?) (xexpr-attrs? (listof xexpr-element?)) ((symbol?) (tagged-xexpr-attrs? (listof tagged-xexpr-element?))
. ->* . tagged-xexpr?) . ->* . tagged-xexpr?)
(filter-not empty? `(,tag ,attrs ,@elements))) (filter-not empty? `(,tag ,attrs ,@elements)))
@ -69,12 +69,12 @@
;; decompose tagged-xexpr into parts (opposite of make-tagged-xexpr) ;; decompose tagged-xexpr into parts (opposite of make-tagged-xexpr)
(define+provide/contract (tagged-xexpr->values x) (define+provide/contract (tagged-xexpr->values x)
(tagged-xexpr? . -> . (tagged-xexpr? . -> .
(values symbol? xexpr-attrs? (listof xexpr-element?))) (values symbol? tagged-xexpr-attrs? (listof tagged-xexpr-element?)))
(match (match
; tagged-xexpr may or may not have attr ; tagged-xexpr may or may not have attr
; if not, add empty attr so that decomposition only handles one case ; if not, add empty attr so that decomposition only handles one case
(match x (match x
[(list _ (? xexpr-attrs?) _ ...) x] [(list _ (? tagged-xexpr-attrs?) _ ...) x]
[else `(,(car x) ,empty ,@(cdr x))]) [else `(,(car x) ,empty ,@(cdr x))])
[(list tag attr content ...) (values tag attr content)])) [(list tag attr content ...) (values tag attr content)]))
@ -86,16 +86,16 @@
;; convenience functions to retrieve only one part of tagged-xexpr ;; convenience functions to retrieve only one part of tagged-xexpr
(define+provide/contract (tagged-xexpr-tag x) (define+provide/contract (tagged-xexpr-tag x)
(tagged-xexpr? . -> . xexpr-tag?) (tagged-xexpr? . -> . tagged-xexpr-tag?)
(car x)) (car x))
(define+provide/contract (tagged-xexpr-attrs x) (define+provide/contract (tagged-xexpr-attrs x)
(tagged-xexpr? . -> . xexpr-attrs?) (tagged-xexpr? . -> . tagged-xexpr-attrs?)
(define-values (tag attrs content) (tagged-xexpr->values x)) (define-values (tag attrs content) (tagged-xexpr->values x))
attrs) attrs)
(define+provide/contract (tagged-xexpr-elements x) (define+provide/contract (tagged-xexpr-elements x)
(tagged-xexpr? . -> . (listof xexpr-element?)) (tagged-xexpr? . -> . (listof tagged-xexpr-element?))
(define-values (tag attrs elements) (tagged-xexpr->values x)) (define-values (tag attrs elements) (tagged-xexpr->values x))
elements) elements)
@ -110,8 +110,8 @@
;; todo: make contract. Which is somewhat complicated: ;; todo: make contract. Which is somewhat complicated:
;; list of items, made of xexpr-attrs or even numbers of symbol/string pairs ;; list of items, made of xexpr-attrs or even numbers of symbol/string pairs
;; use splitf*-at with xexpr-attrs? as test, then check lengths of resulting lists ;; use splitf*-at with xexpr-attrs? as test, then check lengths of resulting lists
(define+provide/contract (merge-xexpr-attrs . items) (define+provide/contract (merge-attrs . items)
(() #:rest (listof (or/c xexpr-attr? xexpr-attrs? symbol? string?)) . ->* . xexpr-attrs?) (() #:rest (listof (or/c tagged-xexpr-attr? tagged-xexpr-attrs? symbol? string?)) . ->* . tagged-xexpr-attrs?)
;; need this function to make sure that 'foo and "foo" are treated as the same hash key ;; need this function to make sure that 'foo and "foo" are treated as the same hash key
(define (make-attr-list items) (define (make-attr-list items)

@ -85,17 +85,17 @@ The programming is trivial, but the annoyance is real.
boolean?] boolean?]
@defproc[ @defproc[
(xexpr-tag? (tagged-xexpr-tag?
[v any/c]) [v any/c])
boolean?] boolean?]
@defproc[ @defproc[
(xexpr-attr? (tagged-xexpr-attr?
[v any/c]) [v any/c])
boolean?] boolean?]
@defproc[ @defproc[
(xexpr-element? (tagged-xexpr-element?
[v any/c]) [v any/c])
boolean?] boolean?]
@ -114,12 +114,12 @@ Predicates for @racket[_tagged-xexpr]s that implement this grammar:
@deftogether[( @deftogether[(
@defproc[ @defproc[
(xexpr-attrs? (tagged-xexpr-attrs?
[v any/c]) [v any/c])
boolean?] boolean?]
@defproc[ @defproc[
(xexpr-elements? (tagged-xexpr-elements?
[v any/c]) [v any/c])
boolean?] boolean?]
)] )]
@ -129,7 +129,7 @@ Shorthand for @code{(listof xexpr-attr?)} and @code{(listof xexpr-element?)}.
@defproc[ @defproc[
(tagged-xexpr->values (tagged-xexpr->values
[tx tagged-xexpr?]) [tx tagged-xexpr?])
(values [tag xexpr-tag?] [attrs xexpr-attrs?] [elements xexpr-elements?])] (values [tag tagged-xexpr-tag?] [attrs tagged-xexpr-attrs?] [elements tagged-xexpr-elements?])]
Dissolves a @racket[_tagged-xexpr] into its components and returns all three. Dissolves a @racket[_tagged-xexpr] into its components and returns all three.
@examples[#:eval my-eval @examples[#:eval my-eval
@ -155,17 +155,17 @@ Like @racket[tagged-xexpr->values], but returns the three components in a list.
@defproc[ @defproc[
(tagged-xexpr-tag (tagged-xexpr-tag
[tx tagged-xexpr?]) [tx tagged-xexpr?])
xexpr-tag?] tagged-xexpr-tag?]
@defproc[ @defproc[
(tagged-xexpr-attrs (tagged-xexpr-attrs
[tx tagged-xexpr?]) [tx tagged-xexpr?])
xexpr-attr?] tagged-xexpr-attr?]
@defproc[ @defproc[
(tagged-xexpr-elements (tagged-xexpr-elements
[tx tagged-xexpr?]) [tx tagged-xexpr?])
(listof xexpr-element?)] (listof tagged-xexpr-element?)]
)] )]
Accessor functions for the individual pieces of a @racket[_tagged-xexpr]. Accessor functions for the individual pieces of a @racket[_tagged-xexpr].
@ -177,9 +177,9 @@ Accessor functions for the individual pieces of a @racket[_tagged-xexpr].
@defproc[ @defproc[
(make-tagged-xexpr (make-tagged-xexpr
[tag symbol?] [tag tagged-xexpr-tag?]
[attrs xexpr-attrs? @(empty)] [attrs tagged-xexpr-attrs? @(empty)]
[elements xexpr-elements? @(empty)]) [elements tagged-xexpr-elements? @(empty)])
tagged-xexpr?] tagged-xexpr?]
Assemble a @racket[_tagged-xexpr] from its parts. If you don't have attributes, but you do have elements, you'll need to pass @racket[empty] as the second argument. Note that unlike @racket[xml->xexpr], if the attribute list is empty, it's not included in the resulting expression. Assemble a @racket[_tagged-xexpr] from its parts. If you don't have attributes, but you do have elements, you'll need to pass @racket[empty] as the second argument. Note that unlike @racket[xml->xexpr], if the attribute list is empty, it's not included in the resulting expression.
@ -194,8 +194,8 @@ Assemble a @racket[_tagged-xexpr] from its parts. If you don't have attributes,
] ]
@defproc[ @defproc[
(merge-xexpr-attrs (merge-attrs
[attrs (listof (or/c xexpr-attr? xexpr-attrs? symbol? string?))] ...) [attrs (listof (or/c tagged-xexpr-attr? tagged-xexpr-attrs? symbol? string?))] ...)
xexpr-attrs?] xexpr-attrs?]
Combine a series of attributes into a single @racket[_tagged-xexpr-attrs] item. This function addresses three annoyances that surface in working with tagged-xexpr attributes. Combine a series of attributes into a single @racket[_tagged-xexpr-attrs] item. This function addresses three annoyances that surface in working with tagged-xexpr attributes.
@ -210,11 +210,11 @@ Combine a series of attributes into a single @racket[_tagged-xexpr-attrs] item.
(define tx '(div [[id "top"][class "red"]] "Hello" (p "World"))) (define tx '(div [[id "top"][class "red"]] "Hello" (p "World")))
(define tx-attrs (tagged-xexpr-attrs tx)) (define tx-attrs (tagged-xexpr-attrs tx))
tx-attrs tx-attrs
(merge-xexpr-attrs tx-attrs 'editable "true") (merge-attrs tx-attrs 'editable "true")
(merge-xexpr-attrs tx-attrs 'id "override-value") (merge-attrs tx-attrs 'id "override-value")
(define my-attr '(id "another-override")) (define my-attr '(id "another-override"))
(merge-xexpr-attrs tx-attrs my-attr) (merge-attrs tx-attrs my-attr)
(merge-xexpr-attrs my-attr tx-attrs) (merge-attrs my-attr tx-attrs)
] ]
@defproc[ @defproc[

@ -7,20 +7,20 @@
(define-syntax-rule (values->list vs) (define-syntax-rule (values->list vs)
(call-with-values (λ() vs) list)) (call-with-values (λ() vs) list))
(check-true (xexpr-attrs? '())) (check-true (tagged-xexpr-attrs? '()))
(check-true (xexpr-attrs? '((key "value")))) (check-true (tagged-xexpr-attrs? '((key "value"))))
(check-true (xexpr-attrs? '((key "value") (foo "bar")))) (check-true (tagged-xexpr-attrs? '((key "value") (foo "bar"))))
(check-false (xexpr-attrs? '((key "value") "foo" "bar"))) ; content, not attr (check-false (tagged-xexpr-attrs? '((key "value") "foo" "bar"))) ; content, not attr
(check-false (xexpr-attrs? '(key "value"))) ; not a nested list (check-false (tagged-xexpr-attrs? '(key "value"))) ; not a nested list
(check-false (xexpr-attrs? '(("key" "value")))) ; two strings (check-false (tagged-xexpr-attrs? '(("key" "value")))) ; two strings
(check-false (xexpr-attrs? '((key value)))) ; two symbols (check-false (tagged-xexpr-attrs? '((key value)))) ; two symbols
(check-true (xexpr-elements? '("p" "foo" "123"))) (check-true (tagged-xexpr-elements? '("p" "foo" "123")))
(check-true (xexpr-elements? '("p" "foo" 123))) ; includes number (check-true (tagged-xexpr-elements? '("p" "foo" 123))) ; includes number
(check-true (xexpr-elements? '(p "foo" "123"))) ; includes symbol (check-true (tagged-xexpr-elements? '(p "foo" "123"))) ; includes symbol
(check-false (xexpr-elements? "foo")) ; not a list (check-false (tagged-xexpr-elements? "foo")) ; not a list
(check-false (xexpr-elements? '(((key "value")) "foo" "bar"))) ; includes attr (check-false (tagged-xexpr-elements? '(((key "value")) "foo" "bar"))) ; includes attr
(check-false (xexpr-elements? '("foo" "bar" ((key "value"))))) ; malformed (check-false (tagged-xexpr-elements? '("foo" "bar" ((key "value"))))) ; malformed
(check-true (tagged-xexpr? '(p "foo" "bar"))) (check-true (tagged-xexpr? '(p "foo" "bar")))
@ -30,14 +30,14 @@
(check-false (tagged-xexpr? '(p "foo" "bar" ((key "value"))))) ; malformed (check-false (tagged-xexpr? '(p "foo" "bar" ((key "value"))))) ; malformed
(check-false (tagged-xexpr? '("p" "foo" "bar"))) ; no name (check-false (tagged-xexpr? '("p" "foo" "bar"))) ; no name
(check-equal? (merge-xexpr-attrs 'foo "bar") '((foo "bar"))) (check-equal? (merge-attrs 'foo "bar") '((foo "bar")))
(check-equal? (merge-xexpr-attrs '(foo "bar")) '((foo "bar"))) (check-equal? (merge-attrs '(foo "bar")) '((foo "bar")))
(check-equal? (merge-xexpr-attrs '((foo "bar"))) '((foo "bar"))) (check-equal? (merge-attrs '((foo "bar"))) '((foo "bar")))
(check-equal? (merge-xexpr-attrs "foo" 'bar) '((foo "bar"))) (check-equal? (merge-attrs "foo" 'bar) '((foo "bar")))
(check-equal? (merge-xexpr-attrs "foo" "bar" "goo" "gar") '((foo "bar")(goo "gar"))) (check-equal? (merge-attrs "foo" "bar" "goo" "gar") '((foo "bar")(goo "gar")))
(check-equal? (merge-xexpr-attrs (merge-xexpr-attrs "foo" "bar" "goo" "gar") "hee" "haw") (check-equal? (merge-attrs (merge-attrs "foo" "bar" "goo" "gar") "hee" "haw")
'((foo "bar")(goo "gar")(hee "haw"))) '((foo "bar")(goo "gar")(hee "haw")))
(check-equal? (merge-xexpr-attrs '((foo "bar")(goo "gar")) "foo" "haw") '((foo "haw")(goo "gar"))) (check-equal? (merge-attrs '((foo "bar")(goo "gar")) "foo" "haw") '((foo "haw")(goo "gar")))
(check-equal? (make-tagged-xexpr 'p) '(p)) (check-equal? (make-tagged-xexpr 'p) '(p))

Loading…
Cancel
Save