[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/parseclj 1b2b221c98 038/185: Add AST to Elisp and Clojure/
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/parseclj 1b2b221c98 038/185: Add AST to Elisp and Clojure/EDN printers |
Date: |
Tue, 28 Dec 2021 14:05:12 -0500 (EST) |
branch: elpa/parseclj
commit 1b2b221c98a4459081dbde0d314f997c29ab872a
Author: Daniel Barreto <dbarreto@talpor.com>
Commit: Daniel Barreto <dbarreto@talpor.com>
Add AST to Elisp and Clojure/EDN printers
---
clj-parse.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 64 insertions(+), 2 deletions(-)
diff --git a/clj-parse.el b/clj-parse.el
index 6c2fbc1da6..618d8f169b 100644
--- a/clj-parse.el
+++ b/clj-parse.el
@@ -67,7 +67,7 @@
;;
;; Note that this is kind of broken, we don't correctly detect if \u or \o
forms
;; don't have the right forms.
-(defun clj-parse-string (s)
+(defun clj-parse--string (s)
(replace-regexp-in-string
"\\\\o[0-8]\\{3\\}"
(lambda (x)
@@ -88,7 +88,7 @@
(t (substring x 1))))
(substring s 1 -1)))))
-(defun clj-parse-character (c)
+(defun clj-parse--character (c)
(let ((first-char (elt c 1)))
(cond
((equal c "\\newline") ?\n)
@@ -168,6 +168,68 @@
(defun clj-parse ()
(clj-parse-reduce #'clj-parse--ast-reduce1 #'clj-parse--ast-reduceN))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; To Elisp
+
+(defun clj-parse--reduce-elisp-leaf (leaf)
+ (cl-case (clj-lex-token-type leaf)
+ (:number (string-to-number (alist-get 'form leaf)))
+ (:nil nil)
+ (:true t)
+ (:false nil)
+ (:symbol (intern (alist-get 'form leaf)))
+ (:keyword (intern (alist-get 'form leaf)))
+ (:string (clj-parse--string (alist-get 'form leaf)))
+ (:character (clj-parse--character (alist-get 'form leaf)))))
+
+(defun clj-parse--reduce-to-elisp (node)
+ (if (clj-parse--is-leaf? node)
+ (clj-parse--reduce-elisp-leaf node)
+ (let ((subnodes (-remove (lambda (token) (eq (clj-lex-token-type token)
:discard)) (alist-get 'subnodes node))))
+ (cl-case (clj-lex-token-type node)
+ (:root (mapcar 'clj-parse--reduce-to-elisp subnodes))
+ (:list (mapcar 'clj-parse--reduce-to-elisp subnodes))
+ (:vector (apply #'vector (mapcar 'clj-parse--reduce-to-elisp
subnodes)))
+ (:set (mapcar 'clj-parse--reduce-to-elisp subnodes))
+ (:map (mapcar (lambda (pair)
+ (cons (clj-parse--reduce-to-elisp (car pair))
+ (clj-parse--reduce-to-elisp (cadr pair))))
+ (-partition 2 subnodes)))
+ ;; tagged literal
+ ))))
+
+(defun clj-parse-to-elisp ()
+ (clj-parse--reduce-to-elisp (clj-parse)))
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; To Clojure/EDN string
+
+(defun clj-parse--reduce-string-leaf (leaf)
+ (alist-get 'form leaf))
+
+(defun clj-parse--string-with-delimiters (nodes ld rd)
+ (s-concat ld
+ (s-join " " (mapcar 'clj-parse--reduce-to-string nodes))
+ rd))
+
+(defun clj-parse--reduce-to-string (node)
+ (if (clj-parse--is-leaf? node)
+ (clj-parse--reduce-string-leaf node)
+ (let ((subnodes (-remove (lambda (token) (eq (clj-lex-token-type token)
:discard)) (alist-get 'subnodes node))))
+ (cl-case (clj-lex-token-type node)
+ (:root (clj-parse--string-with-delimiters subnodes "" ""))
+ (:list (clj-parse--string-with-delimiters subnodes "(" ")"))
+ (:vector (clj-parse--string-with-delimiters subnodes "[" "]"))
+ (:set (clj-parse--string-with-delimiters subnodes "#{" "}"))
+ (:map (clj-parse--string-with-delimiters subnodes "{" "}"))
+ ;; tagged literals
+ ))))
+
+(defun clj-parse-to-string ()
+ (clj-parse--reduce-to-string (clj-parse)))
+
(provide 'clj-parse)
;;; clj-parse.el ends here
- [nongnu] elpa/parseclj b2e97ecb57 017/185: Lets try that again #travis, (continued)
- [nongnu] elpa/parseclj b2e97ecb57 017/185: Lets try that again #travis, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj d7adaa5177 018/185: Lets try that again #travis, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj abe7edb04b 019/185: Bunch of refactoring, but we're green now, supposedly, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 56bb020a9e 020/185: Fix the travis yaml?, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 972161dd23 022/185: Make sure we test against the version we think we are testing against, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 792ba04647 024/185: A more elaborate test, nesting works!, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 452fe7cc76 023/185: Implement nil, true, false, symbol, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 67171853f5 026/185: lex characters, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 4f54ba52fe 033/185: Add support for sets, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 4f647c8cff 036/185: Remove duplicate defination of clj-lex--token-token, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 1b2b221c98 038/185: Add AST to Elisp and Clojure/EDN printers,
ELPA Syncer <=
- [nongnu] elpa/parseclj 9404763e11 042/185: Make sure Travis uses the right Emacs, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 29411e2103 045/185: Add a design document to share vision and collect feedback, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 0702332e5d 046/185: Merge branch 'master' of https://github.com/lambdaisland/clj-parse, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 553f8618e1 044/185: Rework `clj-parse-deftest` macro, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj da89bf82a3 050/185: Allow colons inside symbols, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 81fe979450 051/185: Handle parsing of semicolon-based comments., ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj a424f87c1b 054/185: :#/# is a valid keyword. :::hello is not, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj f34b8460a8 055/185: Correctly parse numbers in scientific notation, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 6fe4ce6095 058/185: Add script to compare speed of edn.el and clj-parse.el, ELPA Syncer, 2021/12/28
- [nongnu] elpa/parseclj 7d70ee4c38 059/185: Add support for tagged literals, ELPA Syncer, 2021/12/28