[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/parseedn 6d80a59673 14/32: Add support for alist, plist an
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/parseedn 6d80a59673 14/32: Add support for alist, plist and an explicit error for unsupported dotted pair notation. |
Date: |
Tue, 28 Dec 2021 14:04:42 -0500 (EST) |
branch: elpa/parseedn
commit 6d80a596730095a58a54cd9e4a7b841bc04ba670
Author: Jack Rusher <jack@rusher.com>
Commit: Jack Rusher <jack@rusher.com>
Add support for alist, plist and an explicit error for unsupported dotted
pair notation.
---
parseedn.el | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
test/parseedn-test.el | 4 ++++
2 files changed, 48 insertions(+), 5 deletions(-)
diff --git a/parseedn.el b/parseedn.el
index e969d23d42..74b83e7083 100644
--- a/parseedn.el
+++ b/parseedn.el
@@ -141,8 +141,8 @@ TAG-READERS is an optional association list. For more
information, see
(insert " ")
(parseedn-print-seq next))))
-(defun parseedn-print-kvs (map &optional ks)
- "Insert hash table MAP as an EDN map into the current buffer."
+(defun parseedn-print-hash-or-alist (map &optional ks)
+ "Insert hash table MAP or elisp a-list as an EDN map into the current
buffer."
(let ((keys (or ks (a-keys map))))
(parseedn-print (car keys))
(insert " ")
@@ -152,6 +152,34 @@ TAG-READERS is an optional association list. For more
information, see
(insert ", ")
(parseedn-print-kvs map next)))))
+(defun parseedn-print-plist (plist)
+ "Insert an elisp property list as an EDN map into the current buffer."
+ (parseedn-print (car plist))
+ (insert " ")
+ (parseedn-print (cadr plist))
+ (let ((next (cddr plist)))
+ (when (not (seq-empty-p next))
+ (insert ", ")
+ (parseedn-print-plist next))))
+
+(defun parseedn-alist-p (list)
+ "Non-null if and only if LIST is an alist with simple keys."
+ (while (consp list)
+ (setq list (if (and (consp (car list))
+ (atom (caar list)))
+ (cdr list)
+ 'not-alist)))
+ (null list))
+
+(defun parseedn-plist-p (list)
+ "Non-null if and only if LIST is a plist with keyword keys."
+ (while (consp list)
+ (setq list (if (and (keywordp (car list))
+ (consp (cdr list)))
+ (cddr list)
+ 'not-plist)))
+ (null list))
+
(defun parseedn-print (datum)
"Insert DATUM as EDN into the current buffer.
DATUM can be any Emacs Lisp value."
@@ -175,19 +203,30 @@ DATUM can be any Emacs Lisp value."
((eq t datum)
(insert "true"))
- ((symbolp datum)
+ ((or (keywordp datum) (symbolp datum))
(insert (symbol-name datum)))
((vectorp datum) (insert "[") (parseedn-print-seq datum) (insert "]"))
+ ((or (hash-table-p datum) (parseedn-alist-p datum))
+ (insert "{")
+ (parseedn-print-hash-or-alist datum)
+ (insert "}"))
+
+ ((parseedn-plist-p datum)
+ (insert "{")
+ (parseedn-print-plist datum)
+ (insert "}"))
+
((consp datum)
(cond
+ ((not (listp (cdr datum))) ; dotted pair
+ (error "Don't know how to print: %s" datum))
((eq 'edn-set (car datum))
(insert "#{") (parseedn-print-seq (cadr datum)) (insert "}"))
(t (insert "(") (parseedn-print-seq datum) (insert ")"))))
- ((hash-table-p datum)
- (insert "{") (parseedn-print-kvs datum) (insert "}"))))
+ (t (error "Don't know how to print: %s" datum))))
(defun parseedn-print-str (datum)
"Return a string containing DATUM as EDN.
diff --git a/test/parseedn-test.el b/test/parseedn-test.el
index 9ddbdb4409..3c8a0f1221 100644
--- a/test/parseedn-test.el
+++ b/test/parseedn-test.el
@@ -38,6 +38,10 @@
(should (equal (parseedn-print-str 1.2) "1.2"))
(should (equal (parseedn-print-str [1 2 3]) "[1 2 3]"))
(should (equal (parseedn-print-str t) "true"))
+ (should (equal (parseedn-print-str '((a . 1) (b . 2))) "{a 1, b 2}"))
+ (should (equal (parseedn-print-str '((a . 1) (b . ((c . 3))))) "{:a 1, :b
{:c 3}}"))
+ (should (equal (parseedn-print-str '(:a 1 :b 2)) "{:a 1, :b 2}"))
+ (should (equal (parseedn-print-str '(:a 1 :b (:c 3))) "{:a 1, :b {:c 3}}"))
(should (listp (member (parseedn-print-str
(let ((ht (make-hash-table)))
(puthash :a 1 ht)
- [nongnu] elpa/parseedn 9c2a37d02c 04/32: Add a missing require, (continued)
- [nongnu] elpa/parseedn 9c2a37d02c 04/32: Add a missing require, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 2a30c83198 02/32: Tweak the keywords, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 3f93aab1ed 23/32: Remove the a.el dependency, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 876dfead6b 16/32: Fix reference to parseedn-print-kvs -> parseedn-print-hash-or-alist, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn d25ebc5554 18/32: Add CHANGELOG.md, prep for v0.2.0, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn e7ff673cd9 24/32: Release 1.0, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 3e0b5d729e 09/32: Fix references in tests: s/parseclj/parseedn, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 3e74411f89 20/32: Add print handler for #uuid and #inst, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn e1de65e463 30/32: Merge pull request #10 from clojure-emacs/cl-case-to-cond, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 90cfe3df51 19/32: Add date/sha to CHANGELOG, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 6d80a59673 14/32: Add support for alist, plist and an explicit error for unsupported dotted pair notation.,
ELPA Syncer <=
- [nongnu] elpa/parseedn ff3cb233bc 07/32: Bump the copyright years, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 5470d4ed67 21/32: Fix the inst parsing test for the case where TZ=UTC, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn acbea6bb1e 03/32: Fix a typo, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn ddf824bc1d 08/32: Update the installation instructions, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 4efa23a851 15/32: oops! fix test., ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 1a3640d298 29/32: Replace `cl-case` with `cond`, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn b00eb42a1c 28/32: Update the required Emacs version, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn adf57f36f4 11/32: Fix printing of hash-maps with multiple entries, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 17c4b2f658 05/32: Fix a couple of typos, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseedn 342359abd1 10/32: Merge pull request #2 from clojure-emacs/fix-build-question-mark, ELPA Syncer, 2021/12/28