You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
txexpr/main.rkt

139 lines
5.0 KiB
Racket

10 years ago
#lang racket/base
(require racket/contract racket/match xml racket/list)
10 years ago
(require sugar/define sugar/coerce)
10 years ago
10 years ago
;; a tagged-xexpr consists of a tag, optional attributes, and then elements.
10 years ago
10 years ago
(define+provide/contract (xexpr-tag? x)
10 years ago
(any/c . -> . boolean?)
(symbol? x))
10 years ago
(define+provide/contract (xexpr-attr? x)
10 years ago
(any/c . -> . boolean?)
(match x
10 years ago
[(list (list (? symbol?) (? string?)) ...) #t]
10 years ago
[else #f]))
10 years ago
(define+provide/contract (xexpr-element? x)
10 years ago
(any/c . -> . boolean?)
10 years ago
(or (string? x) (tagged-xexpr? x) (symbol? x)
(valid-char? x) (cdata? x)))
(define+provide/contract (xexpr-elements? x)
10 years ago
(any/c . -> . boolean?)
(match x
[(list elem ...) (andmap xexpr-element? elem)]
[else #f]))
;; is it a named x-expression?
;; todo: rewrite this recurively so errors can be pinpointed (for debugging)
10 years ago
(define+provide/contract (tagged-xexpr? x)
10 years ago
(any/c . -> . boolean?)
(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-attr? (car rest)) (andmap xexpr-element? (cdr rest))))] ; attr + content
[else #f])))
;; convert list of alternating keys & values to attr
;; todo: make contract. Which is somewhat complicated:
;; list of items, made of xexpr-attr or even numbers of symbol/string pairs
;; use splitf*-at with xexpr-attr? as test, then check lengths of resulting lists
10 years ago
(define+provide/contract (make-xexpr-attr . items)
10 years ago
(() #:rest (listof (λ(i) (or (xexpr-attr? i) (symbol? i) (string? i)))) . ->* . xexpr-attr?)
;; need this function to make sure that 'foo and "foo" are treated as the same hash key
(define (make-attr-list items)
(if (empty? items)
empty
(let ([key (->symbol (first items))]
[value (->string (second items))]
[rest (drop items 2)])
(append (list key value) (make-attr-list rest)))))
;; use flatten to splice xexpr-attrs into list
;; use hash to ensure keys are unique (later values will overwrite earlier)
(define attr-hash (apply hash (make-attr-list (flatten items))))
10 years ago
`(,@(map (λ(k) (list k (hash-ref attr-hash k)))
10 years ago
;; sort needed for predictable results for unit tests
(sort (hash-keys attr-hash) (λ(a b) (string<? (->string a) (->string b)))))))
;; create tagged-xexpr from parts (opposite of break-tagged-xexpr)
10 years ago
(define+provide/contract (make-tagged-xexpr name [attr empty] [content empty])
10 years ago
; xexpr/c provides a nicer error message,
; but is not sufficient on its own (too permissive)
((symbol?) (xexpr-attr? (listof xexpr-element?))
. ->* . tagged-xexpr?)
(filter-not empty? `(,name ,attr ,@content)))
;; decompose tagged-xexpr into parts (opposite of make-tagged-xexpr)
10 years ago
(define+provide/contract (break-tagged-xexpr x)
10 years ago
(tagged-xexpr? . -> .
(values symbol? xexpr-attr? (listof xexpr-element?)))
(match
; tagged-xexpr may or may not have attr
; if not, add empty attr so that decomposition only handles one case
10 years ago
(match x
[(list _ (? xexpr-attr?) _ ...) x]
[else `(,(car x) ,empty ,@(cdr x))])
10 years ago
[(list tag attr content ...) (values tag attr content)]))
;; convenience functions to retrieve only one part of tagged-xexpr
10 years ago
(define+provide/contract (tagged-xexpr-tag x)
10 years ago
(tagged-xexpr? . -> . xexpr-tag?)
10 years ago
(car x))
10 years ago
10 years ago
(define+provide/contract (tagged-xexpr-attr x)
10 years ago
(tagged-xexpr? . -> . xexpr-attr?)
10 years ago
(define-values (tag attr content) (break-tagged-xexpr x))
10 years ago
attr)
10 years ago
(define+provide/contract (tagged-xexpr-elements x)
10 years ago
(tagged-xexpr? . -> . (listof xexpr-element?))
10 years ago
(define-values (tag attrt elements) (break-tagged-xexpr x))
10 years ago
elements)
;; remove all attr blocks (helper function)
10 years ago
(define+provide/contract (remove-attrs x)
10 years ago
(tagged-xexpr? . -> . tagged-xexpr?)
(match x
[(? tagged-xexpr?) (let-values ([(tag attr elements) (break-tagged-xexpr x)])
(make-tagged-xexpr tag empty (remove-attrs elements)))]
[(? list?) (map remove-attrs x)]
[else x]))
10 years ago
(define+provide/contract (map-xexpr-elements proc x)
(procedure? tagged-xexpr? . -> . tagged-xexpr?)
(define-values (tag attr elements) (break-tagged-xexpr x))
(make-tagged-xexpr tag attr (map proc elements)))
;; function to split tag out of tagged-xexpr
(define+provide/contract (split-tag-from-xexpr tag tx)
(xexpr-tag? tagged-xexpr? . -> . (values (listof xexpr-element?) tagged-xexpr? ))
(define matches '())
(define (extract-tag x)
(cond
[(and (tagged-xexpr? x) (equal? tag (car x)))
; stash matched tag but return empty value
(begin
(set! matches (cons x matches))
empty)]
[(tagged-xexpr? x) (let-values([(tag attr body) (break-tagged-xexpr x)])
(make-tagged-xexpr tag attr (extract-tag body)))]
[(xexpr-elements? x) (filter-not empty? (map extract-tag x))]
[else x]))
(define tx-extracted (extract-tag tx)) ;; do this first to fill matches
(values (reverse matches) tx-extracted))