EDN parser with location metadata and pluggable dispatch table.
This library can be useful when:
This library came out of sci, a small Clojure interpreter.
This library works with:
(require '[edamame.core :refer [parse-string]])
Locations are attached as metadata:
(def s "
[{:a 1}
{:b 2}]")
(map meta (parse-string s))
;;=>
({:row 2, :col 2}
{:row 3, :col 2})
(->> "{:a {:b {:c [a b c]}}}"
parse-string
(tree-seq coll? #(if (map? %) (vals %) %))
(map meta))
;;=>
({:row 1, :col 1}
{:row 1, :col 5}
{:row 1, :col 9}
{:row 1, :col 13}
{:row 1, :col 14}
{:row 1, :col 16}
{:row 1, :col 18})
Dispatch on a character, even if it's unsupported in EDN:
(parse-string "@foo" {:dispatch {\@ (fn [val] (list 'deref val))}})
;;=> (deref foo)
Dispatch on dispatch characters:
(parse-string "#\"foo\"" {:dispatch {\# {\" #(re-pattern %)}}})
;;=> #"foo"
(parse-string "#(inc 1 2 %)" {:dispatch {\# {\( (fn [expr] (read-string (str "#" expr)))}}})
;;=> (fn* [p1__11574#] (inc 1 2 p1__11574#))
Process reader conditionals:
(parse-string "[1 2 #?@(:cljs [3 4])]" {:features #{:cljs} :read-cond :allow})
;;=> [1 2 3 4]
(parse-string "[1 2 #?@(:cljs [3 4])]" {:features #{:cljs} :read-cond :preserve})
;;=> [1 2 #?@(:cljs [3 4])]
script/test/jvm
script/test/node
script/test/all
Experimental. Breaking changes are expected to happen at this phase.
Use as a dependency:
The code is largely inspired by rewrite-clj and derived projects.
Copyright © 2019 Michiel Borkent
Distributed under the Eclipse Public License 1.0. This project contains code from Clojure and ClojureScript which are also licensed under the EPL 1.0. See LICENSE.
Can you improve this documentation? These fine people already did:
Michiel Borkent & Tommi ReimanEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close