|
1 | 1 | #lang racket/base
|
2 |
| -(require racket/contract/base) |
| 2 | +(require racket/runtime-path |
| 3 | + (for-syntax racket/base)) |
3 | 4 |
|
4 | 5 | ;; Core structures needed for `xml/xexpr'
|
5 | 6 |
|
6 |
| -(provide (all-defined-out)) |
| 7 | +(provide permissive-xexprs |
| 8 | + (struct-out source) |
| 9 | + (struct-out comment) |
| 10 | + (struct-out p-i) |
| 11 | + (struct-out pcdata) |
| 12 | + (struct-out cdata) |
| 13 | + valid-char?) |
7 | 14 |
|
8 | 15 | ; permissive-xexprs : parameter bool
|
9 | 16 | (define permissive-xexprs (make-parameter #f))
|
10 | 17 |
|
| 18 | +; support for serialization with contracts enforced on deserialization |
| 19 | +(module serialization-support racket/base |
| 20 | + (require racket/serialize-structs |
| 21 | + (for-syntax racket/syntax |
| 22 | + racket/struct-info |
| 23 | + racket/base)) |
| 24 | + (define ((cycles-not-allowed name)) |
| 25 | + (error name "invalid serialization;\n cycles not allowed")) |
| 26 | + (define-for-syntax ((make-define-serializable-struct deserialize-info-mpi |
| 27 | + get-checked-constructor) |
| 28 | + stx) |
| 29 | + (syntax-case stx () |
| 30 | + [(_ name-maybe-super (fld ...)) |
| 31 | + (with-syntax* ([(name super-fld-ref ...) |
| 32 | + (syntax-case #'name-maybe-super () |
| 33 | + [name |
| 34 | + (identifier? #'name) |
| 35 | + #'(name)] |
| 36 | + [(name super) |
| 37 | + #`(name |
| 38 | + #,@(reverse (list-ref (extract-struct-info (syntax-local-value #'super)) |
| 39 | + 3)))])] |
| 40 | + [(name-fld-ref ...) |
| 41 | + (for/list ([id (in-list (syntax->list #'(fld ...)))]) |
| 42 | + (format-id id "~a-~a" #'name id))] |
| 43 | + [deserialize-info:name-v0 |
| 44 | + (format-id #'name "deserialize-info:~a-v0" #'name)]) |
| 45 | + #`(begin |
| 46 | + (define-struct name-maybe-super (fld ...) |
| 47 | + #:property prop:serializable |
| 48 | + (make-serialize-info (λ (this) |
| 49 | + (vector (super-fld-ref this) ... |
| 50 | + (name-fld-ref this) ...)) |
| 51 | + (cons 'deserialize-info:name-v0 #,deserialize-info-mpi) |
| 52 | + #f |
| 53 | + (or (current-load-relative-directory) (current-directory))) |
| 54 | + #:transparent) |
| 55 | + (module+ deserialize-info |
| 56 | + (provide deserialize-info:name-v0) |
| 57 | + (define deserialize-info:name-v0 |
| 58 | + (make-deserialize-info (#,get-checked-constructor name) |
| 59 | + (cycles-not-allowed 'name))))))])) |
| 60 | + (provide (for-syntax make-define-serializable-struct))) |
| 61 | +(require 'serialization-support) |
| 62 | +(define-runtime-module-path-index deserialize-info-mpi '(submod "." deserialize-info)) |
| 63 | +(module+ deserialize-info |
| 64 | + (define-runtime-module-path-index mpi-for-contracts "structures.rkt") |
| 65 | + (define-syntax-rule (get-checked-constructor name) |
| 66 | + (dynamic-require mpi-for-contracts 'name))) |
| 67 | +(define-syntax define-xexpr-struct |
| 68 | + (make-define-serializable-struct #'deserialize-info-mpi #'get-checked-constructor)) |
| 69 | + |
11 | 70 | ; Source = (make-source Location Location)
|
12 |
| -(define-struct source (start stop) #:transparent) |
| 71 | +(define-struct source (start stop) |
| 72 | + ; NOT define-xexpr-struct (and not serializable) because this is used as an abstract base type: |
| 73 | + ; if a subtype is not intended to be serializable, it shouldn't be serialized by inheritance |
| 74 | + #:transparent) |
13 | 75 |
|
14 | 76 | ; Comment = (make-comment String)
|
15 |
| -(define-struct comment (text) #:transparent) |
| 77 | +(define-xexpr-struct comment (text)) |
16 | 78 |
|
17 | 79 | ; Processing-instruction = (make-p-i Location Location String String)
|
18 | 80 | ; also represents XMLDecl
|
19 |
| -(define-struct (p-i source) (target-name instruction) #:transparent) |
| 81 | +(define-xexpr-struct (p-i source) (target-name instruction)) |
20 | 82 |
|
21 | 83 | ; Pcdata = (make-pcdata Location Location String)
|
22 |
| -(define-struct (pcdata source) (string) #:transparent) |
| 84 | +(define-xexpr-struct (pcdata source) (string)) |
23 | 85 |
|
24 | 86 | ; Cdata = (make-cdata Location Location String)
|
25 |
| -(define-struct (cdata source) (string) #:transparent) |
| 87 | +(define-xexpr-struct (cdata source) (string)) |
26 | 88 |
|
27 | 89 | ; Section 2.2 of XML 1.1
|
28 | 90 | ; (XML 1.0 is slightly different and more restrictive)
|
|
0 commit comments