| 1 |
;;; |
| 2 |
;;; text.edn - read and write Clojure's Extensible Data Notation |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2018-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;;; Spec: |
| 35 |
;;; https://github.com/edn-format/edn |
| 36 |
;;; |
| 37 |
;;; Mapping: |
| 38 |
;;; EDN Gauche |
| 39 |
;;; true #t |
| 40 |
;;; false #f |
| 41 |
;;; nil nil (symbol; but client must be aware that |
| 42 |
;;; it is not a symbol in Clojure world) |
| 43 |
;;; <number> <real> |
| 44 |
;;; <symbol> <symbol> (some restrictions in symbol names) |
| 45 |
;;; <keyword> <keyword> (Clojure symbols never begin with ':', so |
| 46 |
;;; it won't conflict with symbols) |
| 47 |
;;; <list> <list> (no improper list) |
| 48 |
;;; <vector> <vector> |
| 49 |
;;; <map> <hash-table> with edn-comparator |
| 50 |
;;; <set> <set> with edn-comparator |
| 51 |
;;; <taggged> <edn-object> (customizable) |
| 52 |
;;; |
| 53 |
;;; |
| 54 |
;;; Customization: |
| 55 |
;;; - register-edn-object-handler! registers mapping from tagged object |
| 56 |
;;; to Scheme object |
| 57 |
;;; - edn-write method can be used to write Scheme object in edn |
| 58 |
|
| 59 |
(define-module text.edn |
| 60 |
(use gauche.record) |
| 61 |
(use gauche.lazy) |
| 62 |
(use gauche.generator) |
| 63 |
(use gauche.threads) |
| 64 |
(use srfi.13) |
| 65 |
(use scheme.set) |
| 66 |
(use parser.peg) |
| 67 |
|
| 68 |
(export <edn-parse-error> |
| 69 |
|
| 70 |
edn-equal? edn-comparator |
| 71 |
edn-map edn-set |
| 72 |
<edn-object> edn-object? make-edn-object |
| 73 |
edn-object-tag edn-object-payload |
| 74 |
|
| 75 |
register-edn-object-handler! edn-object-handler |
| 76 |
|
| 77 |
edn-symbol-prefix edn-symbol-basename edn-valid-symbol-name? |
| 78 |
|
| 79 |
parse-edn parse-edn* parse-edn-string |
| 80 |
construct-edn construct-edn-string edn-write)) |
| 81 |
(select-module text.edn) |
| 82 |
|
| 83 |
;;; |
| 84 |
;;; Comparison |
| 85 |
;;; |
| 86 |
|
| 87 |
(define (edn-equal? a b) |
| 88 |
(cond [(hash-table? a) (and (hash-table? b) |
| 89 |
(hash-table=? edn-comparator a b))] |
| 90 |
[(set? a) (and (set? b) (set=? a b))] |
| 91 |
[(edn-object? a) (and (edn-object? b) |
| 92 |
(edn-equal? (edn-object-tag a) |
| 93 |
(edn-object-tag b)) |
| 94 |
(edn-equal? (edn-object-payload a) |
| 95 |
(edn-object-payload b)))] |
| 96 |
[else (equal? a b)])) |
| 97 |
|
| 98 |
(define edn-comparator |
| 99 |
(make-comparator #t edn-equal? #f default-hash)) |
| 100 |
|
| 101 |
;;; |
| 102 |
;;; Conditions |
| 103 |
;;; |
| 104 |
|
| 105 |
(define-condition-type <edn-parse-error> <error> #f |
| 106 |
(position)) |
| 107 |
|
| 108 |
(define (wrap-parser thunk) |
| 109 |
(guard (e [(<parse-error> e) |
| 110 |
(error <edn-parse-error> |
| 111 |
:position (~ e'position) |
| 112 |
:message (~ e'message))]) |
| 113 |
(thunk))) |
| 114 |
|
| 115 |
;;; |
| 116 |
;;; EDN-specific types |
| 117 |
;;; |
| 118 |
|
| 119 |
;; EDN map is a hashtable with edn-comparator. |
| 120 |
(define (edn-map . kv-list) |
| 121 |
(rlet1 h (make-hash-table edn-comparator) |
| 122 |
(do-plist [[k v] kv-list] |
| 123 |
;; todo: duplicate key check |
| 124 |
(hash-table-put! h k v)))) |
| 125 |
|
| 126 |
;; EDN set is a set with edn-comparator. |
| 127 |
(define (edn-set . vals) |
| 128 |
(list->set edn-comparator vals)) |
| 129 |
|
| 130 |
;; EDN tagged object |
| 131 |
(define-record-type <edn-object> make-edn-object edn-object? |
| 132 |
(tag edn-object-tag) |
| 133 |
(payload edn-object-payload)) |
| 134 |
|
| 135 |
(define-method write-object ((x <edn-object>) port) |
| 136 |
(format port "#<edn-object #~s ~a>" |
| 137 |
(edn-object-tag x) |
| 138 |
(write-to-string (edn-object-payload x) edn-write))) |
| 139 |
|
| 140 |
;; EDN symbol is mapped to a symbol; we just treat prefix as a part |
| 141 |
;; of symbol names. We do provide a utility to extract prefix |
| 142 |
;; and real name. |
| 143 |
(define (edn-symbol-prefix sym) |
| 144 |
(and-let1 p (string-scan (symbol->string sym) "/" 'before) |
| 145 |
(string->symbol p))) |
| 146 |
|
| 147 |
(define (edn-symbol-basename sym) |
| 148 |
(or (and-let1 p (string-scan (symbol->string sym) "/" 'after) |
| 149 |
(string->symbol p)) |
| 150 |
sym)) |
| 151 |
|
| 152 |
(define (edn-valid-symbol-name? str) |
| 153 |
(define valid-component? |
| 154 |
#/^([[:alpha:]*!_?$%*<>]|[+.-][[:alpha:]*!_?$%*<>:#+.-])[\w*!_?$%*<>:#+.-]*$/) |
| 155 |
(let1 s (string-split str "/" 'infix) |
| 156 |
(and (<= 1 (length s) 2) |
| 157 |
(every valid-component? s) |
| 158 |
(not (member (last s) '("nil" "true" "false")))))) |
| 159 |
|
| 160 |
;;; |
| 161 |
;;; Customization hook |
| 162 |
;;; |
| 163 |
|
| 164 |
;; handler :: Symbol, EdnObject -> Obj |
| 165 |
|
| 166 |
(define *edn-handlers* (atom (make-hash-table 'string=?))) |
| 167 |
|
| 168 |
(define (register-edn-object-handler! tag handler) |
| 169 |
(assume-type tag <symbol>) |
| 170 |
(let1 s (symbol->string tag) |
| 171 |
(assume (edn-valid-symbol-name? s)) |
| 172 |
(atomic *edn-handlers* (cut hash-table-put! <> s handler)))) |
| 173 |
|
| 174 |
(define (edn-object-handler tag) |
| 175 |
(let1 s (if (string? tag) tag (x->string tag)) |
| 176 |
(atomic *edn-handlers* (cut hash-table-get <> tag #f)))) |
| 177 |
|
| 178 |
;;; |
| 179 |
;;; Parser |
| 180 |
;;; |
| 181 |
|
| 182 |
(define (parse lseq) |
| 183 |
(define %term |
| 184 |
($lazy ($seq ($many_ %ws) |
| 185 |
($or %list %vec %map %set |
| 186 |
%tagged %atom %str %char)))) |
| 187 |
(define %ws |
| 188 |
($or ($many1_ ($. #[\s,])) |
| 189 |
($seq ($. #\;) |
| 190 |
($many_ ($. #[^\newline]))) |
| 191 |
($seq ($. "#_") %term))) |
| 192 |
(define %list |
| 193 |
($between ($. #\() ($many %term) ($. #\)))) |
| 194 |
(define %vec |
| 195 |
($between ($. #\[) ($lift list->vector ($many %term)) ($. #\]))) |
| 196 |
(define %set |
| 197 |
($between ($. "#{") ($lift ($ apply edn-set $) ($many %term)) ($. #\}))) |
| 198 |
(define %map |
| 199 |
($between ($. #\{) ($lift ($ apply edn-map $) ($many %term)) ($. #\}))) |
| 200 |
(define %word ; intermediate - used by %atom, %char and %tagged |
| 201 |
($->string ($many1 ($. #[\w*!?$%&=<>:#/+.-])))) |
| 202 |
(define %atom |
| 203 |
($let ([val %word]) |
| 204 |
(cond |
| 205 |
[(equal? val "true") ($return #t)] |
| 206 |
[(equal? val "false") ($return #f)] |
| 207 |
[(equal? val "nil") ($return 'nil)] |
| 208 |
[(string-prefix? ":" val) ($return |
| 209 |
(make-keyword (string-drop val 1)))] |
| 210 |
[(%parse-num val) => $return] |
| 211 |
[(edn-valid-symbol-name? val) ($return (string->symbol val))] |
| 212 |
[else ($fail (format "invalid token: ~s" val))]))) |
| 213 |
(define %tagged |
| 214 |
($let ([ ($. #\#) ] |
| 215 |
[tag %word] |
| 216 |
[content %term]) |
| 217 |
(if (edn-valid-symbol-name? tag) |
| 218 |
(if-let1 h (edn-object-handler tag) |
| 219 |
($return (h tag content)) |
| 220 |
($return (make-edn-object (string->symbol tag) content))) |
| 221 |
($fail (format "invalid tag: ~s" tag))))) |
| 222 |
(define %str |
| 223 |
($between ($. #\") |
| 224 |
($->string |
| 225 |
($many ($or ($seq ($. "\\\"") ($return "\"")) |
| 226 |
($seq ($. "\\t") ($return "\t")) |
| 227 |
($seq ($. "\\r") ($return "\r")) |
| 228 |
($seq ($. "\\n") ($return "\n")) |
| 229 |
($seq ($. "\\\\") ($return "\\")) |
| 230 |
($. #[^\\\"])))) |
| 231 |
($. #\"))) |
| 232 |
(define %char |
| 233 |
($let ([w ($seq ($. #\\) %word)] ) |
| 234 |
(rxmatch-case w |
| 235 |
[#/^newline$/ (_) ($return #\newline)] |
| 236 |
[#/^return$/ (_) ($return #\return)] |
| 237 |
[#/^space$/ (_) ($return #\space)] |
| 238 |
[#/^tab$/ (_) ($return #\tab)] |
| 239 |
[#/^u([0-9a-fA-F]+)$/ (_ v) |
| 240 |
($return (ucs->char (string->number v 16)))] |
| 241 |
[#/^.$/ (s) ($return (~ s 0))] |
| 242 |
[else ($fail (format "invalid char name: ~a" w))]))) |
| 243 |
(wrap-parser (cut peg-run-parser %term lseq))) |
| 244 |
|
| 245 |
(define (%parse-num word) |
| 246 |
(rxmatch-case word |
| 247 |
[#/^([+-])?0[xX]([0-9a-fA-F]+)$/ (_ sign digs) |
| 248 |
(* (if (equal? sign "-") -1 1) (string->number digs 16))] |
| 249 |
[#/^([+-])?0([0-7]+)$/ (_ sign digs) |
| 250 |
(* (if (equal? sign "-") -1 1) (string->number digs 8))] |
| 251 |
[#/^([+-])?(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?([MN])?$/ |
| 252 |
(_ sign int frac exp sfx) |
| 253 |
(* (if (equal? sign "-") -1 1) |
| 254 |
(if (or frac exp (equal? sfx "M")) |
| 255 |
(string->number (format "~a.~aE~a" int (or frac "0") (or exp "0"))) |
| 256 |
(string->number int)))] |
| 257 |
[else #f])) |
| 258 |
|
| 259 |
(define (parse-edn :optional (iport (current-input-port))) |
| 260 |
(values-ref (parse (port->char-lseq iport)) 0)) |
| 261 |
|
| 262 |
(define (parse-edn* :optional (iport (current-input-port))) |
| 263 |
(let1 s (port->char-lseq iport) |
| 264 |
(generator->list (^[] (if (null? s) |
| 265 |
(eof-object) |
| 266 |
(receive (v s.) (parse s) |
| 267 |
(set! s s.) |
| 268 |
v)))))) |
| 269 |
|
| 270 |
|
| 271 |
(define (parse-edn-string str) |
| 272 |
(values-ref (parse (x->lseq str)) 0)) |
| 273 |
|
| 274 |
;;; |
| 275 |
;;; Writer |
| 276 |
;;; |
| 277 |
|
| 278 |
(define-method edn-write ((x <boolean>)) (display (if x 'true 'false))) |
| 279 |
(define-method edn-write ((x <string>)) (write x)) |
| 280 |
(define-method edn-write ((x <symbol>)) (write x)) |
| 281 |
(define-method edn-write ((x <keyword>)) (write x)) |
| 282 |
(define-method edn-write ((x <real>)) (write x)) |
| 283 |
|
| 284 |
(define-method edn-write ((x <char>)) |
| 285 |
(cond |
| 286 |
[(char-set-contains? #[!-~] x) (format #t "\\~c" x)] |
| 287 |
[(eqv? x #\newline) (display "\\newline")] |
| 288 |
[(eqv? x #\return) (display "\\return")] |
| 289 |
[(eqv? x #\space) (display "\\space")] |
| 290 |
[(eqv? x #\tab) (display "\\tab")] |
| 291 |
[else (format #t "\\u~04x" (char->ucs x))])) |
| 292 |
|
| 293 |
(define (do-intersperse delim proc xs) |
| 294 |
(unless (null? xs) |
| 295 |
(proc (car xs)) |
| 296 |
(dolist [x (cdr xs)] |
| 297 |
(display delim) |
| 298 |
(proc x)))) |
| 299 |
|
| 300 |
(define-method edn-write ((x <list>)) |
| 301 |
(display "(") (do-intersperse " " edn-write x) (display ")")) |
| 302 |
(define-method edn-write ((x <vector>)) |
| 303 |
(display "[") (do-intersperse " " edn-write (vector->list x)) (display "]")) |
| 304 |
(define-method edn-write ((x (with-module scheme.set <set>))) |
| 305 |
(display "#{") (do-intersperse " " edn-write (set->list x)) (display "}")) |
| 306 |
(define-method edn-write ((x <hash-table>)) |
| 307 |
(display "{") |
| 308 |
(do-intersperse ", " (^e (edn-write (car e)) |
| 309 |
(display " ") |
| 310 |
(edn-write (cdr e))) |
| 311 |
(hash-table->alist x)) |
| 312 |
(display "}")) |
| 313 |
(define-method edn-write ((x <edn-object>)) |
| 314 |
(display "#") |
| 315 |
(edn-write (edn-object-tag x)) |
| 316 |
(display " ") |
| 317 |
(edn-write (edn-object-payload x))) |
| 318 |
|
| 319 |
(define-method edn-write (obj) |
| 320 |
(errorf "Cannot render ~s in EDN" obj)) |
| 321 |
|
| 322 |
(define (construct-edn obj :optional (oport (current-output-port))) |
| 323 |
(with-output-to-port oport (^[] (edn-write obj)))) |
| 324 |
(define (construct-edn-string obj) |
| 325 |
(with-output-to-string (^[] (edn-write obj)))) |