From 9eea5c6e97a4141290b85f9623512831b1bb8710 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 17 Feb 2014 11:48:54 -0800 Subject: [PATCH] reconsider some names --- main.rkt | 34 ++++++++++++++--------------- scribblings/tagged-xexpr.scrbl | 36 +++++++++++++++--------------- tests.rkt | 40 +++++++++++++++++----------------- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/main.rkt b/main.rkt index cda272e..0f9ad34 100644 --- a/main.rkt +++ b/main.rkt @@ -14,32 +14,32 @@ ;; 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?) (symbol? x)) -(define+provide/contract (xexpr-attr? x) +(define+provide/contract (tagged-xexpr-attr? x) (any/c . -> . boolean?) (match x [(list (? symbol?) (? string?)) #t] [else #f])) -(define+provide/contract (xexpr-attrs? x) +(define+provide/contract (tagged-xexpr-attrs? x) (any/c . -> . boolean?) (match x - [(list (? xexpr-attr?) ...) #t] + [(list (? tagged-xexpr-attr?) ...) #t] [else #f])) -(define+provide/contract (xexpr-element? x) +(define+provide/contract (tagged-xexpr-element? x) (any/c . -> . boolean?) (or (string? x) (tagged-xexpr? x) (symbol? x) (valid-char? x) (cdata? x))) -(define+provide/contract (xexpr-elements? x) +(define+provide/contract (tagged-xexpr-elements? x) (any/c . -> . boolean?) (match x - [(list elem ...) (andmap xexpr-element? elem)] + [(list elem ...) (andmap tagged-xexpr-element? elem)] [else #f])) ;; is it a named x-expression? @@ -49,8 +49,8 @@ (and (xexpr? x) ; meets basic xexpr contract (match x [(list (? symbol? name) rest ...) ; is a list starting with a symbol - (or (andmap xexpr-element? rest) ; the rest is content or ... - (and (xexpr-attrs? (car rest)) (andmap xexpr-element? (cdr rest))))] ; attr + content + (or (andmap tagged-xexpr-element? rest) ; the rest is content or ... + (and (tagged-xexpr-attrs? (car rest)) (andmap tagged-xexpr-element? (cdr rest))))] ; attr + content [else #f]))) @@ -60,7 +60,7 @@ (define+provide/contract (make-tagged-xexpr tag [attrs empty] [elements empty]) ; xexpr/c provides a nicer error message, ; 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?) (filter-not empty? `(,tag ,attrs ,@elements))) @@ -69,12 +69,12 @@ ;; decompose tagged-xexpr into parts (opposite of make-tagged-xexpr) (define+provide/contract (tagged-xexpr->values x) (tagged-xexpr? . -> . - (values symbol? xexpr-attrs? (listof xexpr-element?))) + (values symbol? tagged-xexpr-attrs? (listof tagged-xexpr-element?))) (match ; tagged-xexpr may or may not have attr ; if not, add empty attr so that decomposition only handles one case (match x - [(list _ (? xexpr-attrs?) _ ...) x] + [(list _ (? tagged-xexpr-attrs?) _ ...) x] [else `(,(car x) ,empty ,@(cdr x))]) [(list tag attr content ...) (values tag attr content)])) @@ -86,16 +86,16 @@ ;; convenience functions to retrieve only one part of tagged-xexpr (define+provide/contract (tagged-xexpr-tag x) - (tagged-xexpr? . -> . xexpr-tag?) + (tagged-xexpr? . -> . tagged-xexpr-tag?) (car 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)) attrs) (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)) elements) @@ -110,8 +110,8 @@ ;; todo: make contract. Which is somewhat complicated: ;; 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 -(define+provide/contract (merge-xexpr-attrs . items) - (() #:rest (listof (or/c xexpr-attr? xexpr-attrs? symbol? string?)) . ->* . xexpr-attrs?) +(define+provide/contract (merge-attrs . items) + (() #: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 (define (make-attr-list items) diff --git a/scribblings/tagged-xexpr.scrbl b/scribblings/tagged-xexpr.scrbl index 459c047..3ef36b2 100644 --- a/scribblings/tagged-xexpr.scrbl +++ b/scribblings/tagged-xexpr.scrbl @@ -85,17 +85,17 @@ The programming is trivial, but the annoyance is real. boolean?] @defproc[ -(xexpr-tag? +(tagged-xexpr-tag? [v any/c]) boolean?] @defproc[ -(xexpr-attr? +(tagged-xexpr-attr? [v any/c]) boolean?] @defproc[ -(xexpr-element? +(tagged-xexpr-element? [v any/c]) boolean?] @@ -114,12 +114,12 @@ Predicates for @racket[_tagged-xexpr]s that implement this grammar: @deftogether[( @defproc[ -(xexpr-attrs? +(tagged-xexpr-attrs? [v any/c]) boolean?] @defproc[ -(xexpr-elements? +(tagged-xexpr-elements? [v any/c]) boolean?] )] @@ -129,7 +129,7 @@ Shorthand for @code{(listof xexpr-attr?)} and @code{(listof xexpr-element?)}. @defproc[ (tagged-xexpr->values [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. @examples[#:eval my-eval @@ -155,17 +155,17 @@ Like @racket[tagged-xexpr->values], but returns the three components in a list. @defproc[ (tagged-xexpr-tag [tx tagged-xexpr?]) -xexpr-tag?] +tagged-xexpr-tag?] @defproc[ (tagged-xexpr-attrs [tx tagged-xexpr?]) -xexpr-attr?] +tagged-xexpr-attr?] @defproc[ (tagged-xexpr-elements [tx tagged-xexpr?]) -(listof xexpr-element?)] +(listof tagged-xexpr-element?)] )] 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[ (make-tagged-xexpr -[tag symbol?] -[attrs xexpr-attrs? @(empty)] -[elements xexpr-elements? @(empty)]) +[tag tagged-xexpr-tag?] +[attrs tagged-xexpr-attrs? @(empty)] +[elements tagged-xexpr-elements? @(empty)]) 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. @@ -194,8 +194,8 @@ Assemble a @racket[_tagged-xexpr] from its parts. If you don't have attributes, ] @defproc[ -(merge-xexpr-attrs -[attrs (listof (or/c xexpr-attr? xexpr-attrs? symbol? string?))] ...) +(merge-attrs +[attrs (listof (or/c tagged-xexpr-attr? tagged-xexpr-attrs? symbol? string?))] ...) 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. @@ -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-attrs (tagged-xexpr-attrs tx)) tx-attrs -(merge-xexpr-attrs tx-attrs 'editable "true") -(merge-xexpr-attrs tx-attrs 'id "override-value") +(merge-attrs tx-attrs 'editable "true") +(merge-attrs tx-attrs 'id "override-value") (define my-attr '(id "another-override")) -(merge-xexpr-attrs tx-attrs my-attr) -(merge-xexpr-attrs my-attr tx-attrs) +(merge-attrs tx-attrs my-attr) +(merge-attrs my-attr tx-attrs) ] @defproc[ diff --git a/tests.rkt b/tests.rkt index f61a187..600b086 100644 --- a/tests.rkt +++ b/tests.rkt @@ -7,20 +7,20 @@ (define-syntax-rule (values->list vs) (call-with-values (λ() vs) list)) -(check-true (xexpr-attrs? '())) -(check-true (xexpr-attrs? '((key "value")))) -(check-true (xexpr-attrs? '((key "value") (foo "bar")))) -(check-false (xexpr-attrs? '((key "value") "foo" "bar"))) ; content, not attr -(check-false (xexpr-attrs? '(key "value"))) ; not a nested list -(check-false (xexpr-attrs? '(("key" "value")))) ; two strings -(check-false (xexpr-attrs? '((key value)))) ; two symbols +(check-true (tagged-xexpr-attrs? '())) +(check-true (tagged-xexpr-attrs? '((key "value")))) +(check-true (tagged-xexpr-attrs? '((key "value") (foo "bar")))) +(check-false (tagged-xexpr-attrs? '((key "value") "foo" "bar"))) ; content, not attr +(check-false (tagged-xexpr-attrs? '(key "value"))) ; not a nested list +(check-false (tagged-xexpr-attrs? '(("key" "value")))) ; two strings +(check-false (tagged-xexpr-attrs? '((key value)))) ; two symbols -(check-true (xexpr-elements? '("p" "foo" "123"))) -(check-true (xexpr-elements? '("p" "foo" 123))) ; includes number -(check-true (xexpr-elements? '(p "foo" "123"))) ; includes symbol -(check-false (xexpr-elements? "foo")) ; not a list -(check-false (xexpr-elements? '(((key "value")) "foo" "bar"))) ; includes attr -(check-false (xexpr-elements? '("foo" "bar" ((key "value"))))) ; malformed +(check-true (tagged-xexpr-elements? '("p" "foo" "123"))) +(check-true (tagged-xexpr-elements? '("p" "foo" 123))) ; includes number +(check-true (tagged-xexpr-elements? '(p "foo" "123"))) ; includes symbol +(check-false (tagged-xexpr-elements? "foo")) ; not a list +(check-false (tagged-xexpr-elements? '(((key "value")) "foo" "bar"))) ; includes attr +(check-false (tagged-xexpr-elements? '("foo" "bar" ((key "value"))))) ; malformed (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"))) ; no name -(check-equal? (merge-xexpr-attrs 'foo "bar") '((foo "bar"))) -(check-equal? (merge-xexpr-attrs '(foo "bar")) '((foo "bar"))) -(check-equal? (merge-xexpr-attrs '((foo "bar"))) '((foo "bar"))) -(check-equal? (merge-xexpr-attrs "foo" 'bar) '((foo "bar"))) -(check-equal? (merge-xexpr-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 'foo "bar") '((foo "bar"))) +(check-equal? (merge-attrs '(foo "bar")) '((foo "bar"))) +(check-equal? (merge-attrs '((foo "bar"))) '((foo "bar"))) +(check-equal? (merge-attrs "foo" 'bar) '((foo "bar"))) +(check-equal? (merge-attrs "foo" "bar" "goo" "gar") '((foo "bar")(goo "gar"))) +(check-equal? (merge-attrs (merge-attrs "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))