|
1 | 1 | (ns basilisp.edn
|
2 | 2 | (:refer-basilisp :exclude [read read-string])
|
3 |
| - (:require [basilisp.string :as str])) |
| 3 | + (:require [basilisp.string :as str]) |
| 4 | + (:import datetime math uuid)) |
4 | 5 |
|
5 | 6 | (declare ^:private read-next
|
6 | 7 | ^:private read-sym-or-num)
|
|
14 | 15 | (python/object))
|
15 | 16 |
|
16 | 17 | (def ^:private default-edn-data-readers
|
17 |
| - "Map of default data readers, which wrap the default readers and convert the |
18 |
| - SyntaxErrors into standard ExceptionInfo types." |
19 |
| - (reduce-kv (fn [m k tag-reader] |
20 |
| - (assoc m k (fn [v] |
21 |
| - (try |
22 |
| - (tag-reader v) |
23 |
| - (catch basilisp.lang.reader/SyntaxError e |
24 |
| - (throw |
25 |
| - (ex-info (get (.-args e) 0) |
26 |
| - {:error :tag-reader-error}))))))) |
27 |
| - {} |
28 |
| - (dissoc default-data-readers 'py))) |
| 18 | + {'inst (fn [v] |
| 19 | + (try |
| 20 | + (basilisp.lang.util/inst-from-str v) |
| 21 | + (catch python/OverflowError _ |
| 22 | + (throw |
| 23 | + (ex-info (str "Unrecognized date/time syntax: " v) |
| 24 | + {:error :tag-reader-error}))) |
| 25 | + (catch python/ValueError _ |
| 26 | + (throw |
| 27 | + (ex-info (str "Unrecognized date/time syntax: " v) |
| 28 | + {:error :tag-reader-error}))))) |
| 29 | + 'uuid (fn [v] |
| 30 | + (try |
| 31 | + (basilisp.lang.util/uuid-from-str v) |
| 32 | + (catch python/TypeError _ |
| 33 | + (throw |
| 34 | + (ex-info (str "Unrecognized UUID format: " v) |
| 35 | + {:error :tag-reader-error}))) |
| 36 | + (catch python/ValueError _ |
| 37 | + (throw |
| 38 | + (ex-info (str "Unrecognized UUID syntax: " v) |
| 39 | + {:error :tag-reader-error})) )))}) |
29 | 40 |
|
30 | 41 | (def ^:private eof
|
31 | 42 | "EOF marker if none is supplied."
|
32 | 43 | (python/object))
|
33 | 44 |
|
| 45 | +(def ^:private numeric-constants |
| 46 | + {'Inf (python/float "inf") |
| 47 | + '-Inf (- (python/float "inf")) |
| 48 | + 'NaN (python/float "nan")}) |
| 49 | + |
34 | 50 | (def ^:private special-chars
|
35 | 51 | "A mapping of special character names to the characters they represent."
|
36 | 52 | {"newline" "\n"
|
|
189 | 205 | (case (.peek reader)
|
190 | 206 | "_" :comment
|
191 | 207 | "{" :set
|
| 208 | + "#" :constant |
192 | 209 | :tag)))
|
193 | 210 |
|
194 | 211 | (defmethod read-dispatch :comment
|
|
197 | 214 | (read-next reader opts)
|
198 | 215 | comment)
|
199 | 216 |
|
| 217 | +(defmethod read-dispatch :constant |
| 218 | + [reader opts] |
| 219 | + (assert-starts reader "#") |
| 220 | + (let [const-sym (read-sym-or-num reader opts)] |
| 221 | + (when-not (symbol? const-sym) |
| 222 | + (throw |
| 223 | + (ex-info "Reader constant must be a symbol" |
| 224 | + {:error :reader-constant-not-symbol |
| 225 | + :type (type const-sym) |
| 226 | + :value const-sym}))) |
| 227 | + (if-let [const (get numeric-constants const-sym)] |
| 228 | + const |
| 229 | + (throw |
| 230 | + (ex-info "Unrecognized reader constant" |
| 231 | + {:error :no-reader-constant-for-symbol |
| 232 | + :sym const-sym}))))) |
| 233 | + |
200 | 234 | (defmethod read-dispatch :set
|
201 | 235 | [reader opts]
|
202 | 236 | (assert-starts reader "{")
|
|
492 | 526 | {:error :eof}))
|
493 | 527 | e)))
|
494 | 528 |
|
| 529 | +;;;;;;;;;;;;; |
| 530 | +;; Writers ;; |
| 531 | +;;;;;;;;;;;;; |
| 532 | + |
| 533 | +(defprotocol EDNEncodeable |
| 534 | + (write* [this writer] |
| 535 | + "Write the object `this` to the stream `writer` encoded as EDN. |
| 536 | + |
| 537 | + Writer will be a file-like object supporting a `.write()` method.")) |
| 538 | + |
| 539 | +;; Rather than relying on the existing Lisp representations, we use custom |
| 540 | +;; implementations to avoid picking up any of the dynamic Vars which affect |
| 541 | +;; the result of `repr` calls. These include things like writing metadata or |
| 542 | +;; printing strings without quotes, neither of which is supported by the EDN |
| 543 | +;; spec. |
| 544 | + |
| 545 | +(defn ^:private write-seq |
| 546 | + [e writer start-token end-token] |
| 547 | + (.write writer start-token) |
| 548 | + (doseq [entry (map-indexed (fn [i v] [i v]) (seq e)) |
| 549 | + :let [[i v] entry]] |
| 550 | + (when (pos? i) |
| 551 | + (.write writer " ")) |
| 552 | + (write* v writer)) |
| 553 | + (.write writer end-token) |
| 554 | + nil) |
| 555 | + |
| 556 | +(extend-protocol EDNEncodeable |
| 557 | + basilisp.lang.interfaces/IPersistentMap |
| 558 | + (write* [this writer] |
| 559 | + (.write writer "{") |
| 560 | + (doseq [entry (map-indexed (fn [i [k v]] [i k v]) (seq this)) |
| 561 | + :let [[i k v] entry]] |
| 562 | + (when (pos? i) |
| 563 | + (.write writer " ")) |
| 564 | + (write* k writer) |
| 565 | + (.write writer " ") |
| 566 | + (write* v writer)) |
| 567 | + (.write writer "}") |
| 568 | + nil) |
| 569 | + basilisp.lang.interfaces/IPersistentList |
| 570 | + (write* [this writer] |
| 571 | + (write-seq this writer "(" ")")) |
| 572 | + basilisp.lang.interfaces/IPersistentSet |
| 573 | + (write* [this writer] |
| 574 | + (write-seq this writer "#{" "}")) |
| 575 | + basilisp.lang.interfaces/IPersistentVector |
| 576 | + (write* [this writer] |
| 577 | + (write-seq this writer "[" "]")) |
| 578 | + |
| 579 | + basilisp.lang.keyword/Keyword |
| 580 | + (write* [this writer] |
| 581 | + (.write writer ":") |
| 582 | + (if-let [ns (namespace this)] |
| 583 | + (.write writer (str ns "/" (name this))) |
| 584 | + (.write writer (str (name this)))) |
| 585 | + nil) |
| 586 | + basilisp.lang.symbol/Symbol |
| 587 | + (write* [this writer] |
| 588 | + (if-let [ns (namespace this)] |
| 589 | + (.write writer (str ns "/" (name this))) |
| 590 | + (.write writer (str (name this)))) |
| 591 | + nil) |
| 592 | + |
| 593 | + python/bool |
| 594 | + (write* [this writer] |
| 595 | + (.write writer (str/lower-case (python/repr this))) |
| 596 | + nil) |
| 597 | + python/int |
| 598 | + (write* [this writer] |
| 599 | + (.write writer (python/repr this)) |
| 600 | + nil) |
| 601 | + python/float |
| 602 | + (write* [this writer] |
| 603 | + (->> (cond |
| 604 | + (math/isinf this) (if (pos? this) "##Inf" "##-Inf") |
| 605 | + (math/isnan this) "##NaN" |
| 606 | + :else (python/repr this)) |
| 607 | + (.write writer)) |
| 608 | + nil) |
| 609 | + python/str |
| 610 | + (write* [this writer] |
| 611 | + (.write writer "\"") |
| 612 | + (.write writer this) |
| 613 | + (.write writer "\"") |
| 614 | + nil) |
| 615 | + nil |
| 616 | + (write* [this writer] |
| 617 | + (.write writer "nil") |
| 618 | + nil) |
| 619 | + |
| 620 | + datetime/datetime |
| 621 | + (write* [this writer] |
| 622 | + (.write writer "#inst \"") |
| 623 | + (.write writer (.isoformat this)) |
| 624 | + (.write writer "\"") |
| 625 | + nil) |
| 626 | + uuid/UUID |
| 627 | + (write* [this writer] |
| 628 | + (.write writer "#uuid \"") |
| 629 | + (.write writer (python/str this)) |
| 630 | + (.write writer "\"") |
| 631 | + nil)) |
| 632 | + |
495 | 633 | ;;;;;;;;;;;;;;;;;;;;;;
|
496 | 634 | ;; Public Interface ;;
|
497 | 635 | ;;;;;;;;;;;;;;;;;;;;;;
|
|
527 | 665 | (when-not (and (nil? s) (= "" s))
|
528 | 666 | (-> (io/StringIO s)
|
529 | 667 | (read opts)))))
|
| 668 | + |
| 669 | +(defn write |
| 670 | + "Serialize the object `o` as EDN to the writer object `writer` (which must be |
| 671 | + any file-like object supporting `.write()` method). |
| 672 | + |
| 673 | + All Basilisp data structures are serializable to EDN by default. UUIDs and |
| 674 | + Instants (Python `datetime`s) are also supported by default. Support for other |
| 675 | + types may be added by extending the `EDNEncodeable` protocol for that type." |
| 676 | + ([o] |
| 677 | + (write o *out*)) |
| 678 | + ([o writer] |
| 679 | + (write* o writer))) |
| 680 | + |
| 681 | +(defn write-string |
| 682 | + "Serialize the object `o` as EDN and return the serialized object as a string. |
| 683 | + |
| 684 | + All Basilisp data structures are serializable to EDN by default. UUIDs and |
| 685 | + Instants (Python `datetime`s) are also supported by default. Support for other |
| 686 | + types may be added by extending the `EDNEncodeable` protocol for that type." |
| 687 | + [o] |
| 688 | + (let [buf (io/StringIO "")] |
| 689 | + (write o buf) |
| 690 | + (.getvalue buf))) |
0 commit comments