branch: elpa/parseclj
commit e7686c49ed8e5670034489617c11a285c67179d1
Author: Arne Brasseur <[email protected]>
Commit: Arne Brasseur <[email protected]>
Greater parity with edn.el
- parse sets as (edn-set (...))
- parse maps as hash tables
- add a clj-parse-edn-read-str that acts like edn-read
---
clj-parse-test.el | 15 ++++++++++++---
clj-parse.el | 16 ++++++++++++----
2 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/clj-parse-test.el b/clj-parse-test.el
index a9888933ce..1a193def46 100644
--- a/clj-parse-test.el
+++ b/clj-parse-test.el
@@ -26,6 +26,15 @@
(require 'ert)
(require 'clj-parse)
+(defun clj-parse--equal (a b)
+ (cond
+ ((and (hash-table-p a) (hash-table-p b))
+ (a-equal a b))
+ ((and (consp a) (consp b))
+ (and (clj-parse--equal (car a) (car b))
+ (clj-parse--equal (cdr a) (cdr b))))
+ (t (equal a b))))
+
(defun clj-parse--deftest-mode (mode test-name test-string expected)
(let* ((parse-fn (intern (concat "clj-parse-" mode)))
(test-name (intern (concat (symbol-name parse-fn) "-" (symbol-name
test-name)))))
@@ -33,7 +42,7 @@
(with-temp-buffer
(insert ,test-string)
(goto-char 1)
- (should (equal (,parse-fn) ,expected))))))
+ (should (clj-parse--equal (,parse-fn) ,expected))))))
(defmacro clj-parse-deftest (test-name test-string mode-vs-expected-alist)
(declare (indent defun))
@@ -214,7 +223,7 @@
(:value . 123)))))))))))
(clj-parse-deftest map "{:count 123}"
- (("edn" '(((:count . 123))))
+ (("edn" (list (a-hash-table :count 123)))
("ast" '((:node-type . :root)
(:position . 0)
(:children . (((:node-type . :map)
@@ -229,7 +238,7 @@
(:value . 123)))))))))))
(clj-parse-deftest set "#{:x}"
- (("edn" '((:x)))
+ (("edn" '((edn-set (:x))))
("ast" '((:node-type . :root)
(:position . 0)
(:children . (((:node-type . :set)
diff --git a/clj-parse.el b/clj-parse.el
index a447ca8a43..163c7441e6 100644
--- a/clj-parse.el
+++ b/clj-parse.el
@@ -210,15 +210,23 @@
(cl-case type
(:lparen children)
(:lbracket (apply #'vector children))
- (:set children)
- (:lbrace (mapcar (lambda (pair)
- (cons (car pair) (cadr pair)))
- (-partition 2 children))))
+ (:set (list 'edn-set children))
+ (:lbrace (let* ((kvs (seq-partition children 2))
+ (hash-map (make-hash-table :test 'equal :size (length
kvs))))
+ (seq-do (lambda (pair)
+ (puthash (car pair) (cadr pair) hash-map))
+ kvs)
+ hash-map)))
stack))))
(defun clj-parse-edn ()
(clj-parse-reduce #'clj-parse--edn-reduce-leaf #'clj-parse--edn-reduce-node))
+(defun clj-parse-edn-str (s)
+ (with-temp-buffer
+ (insert s)
+ (goto-char 1)
+ (car (clj-parse-reduce #'clj-parse--edn-reduce-leaf
#'clj-parse--edn-reduce-node))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Printer implementations