branch: elpa/parseclj
commit adb55fa579c99b6b3de573d44d52a5053e9ca949
Merge: fcebf65075 5cf5cd5f53
Author: Arne Brasseur <[email protected]>
Commit: GitHub <[email protected]>
Merge pull request #34 from clojure-emacs/cl-case-to-cond
Replace `cl-case` calls with `cond`
---
CHANGELOG.md | 2 ++
parseclj-alist.el | 10 ++++++----
parseclj-ast.el | 58 +++++++++++++++++++++++++++---------------------------
parseclj-lex.el | 38 ++++++++++++++++++-----------------
parseclj-parser.el | 25 ++++++++++++-----------
5 files changed, 70 insertions(+), 63 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 32565015c8..dfa6b0e93a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,7 @@
# Unreleased
+- [#34](https://github.com/clojure-emacs/parseclj/pull/34) Replace `cl-case`
with `cond`
+
## 1.0.4 (2021-09-30)
- Provide parseclj-alist-merge, since we can't use `(map-merge 'alist)` yet in
Emacs 25/26.
diff --git a/parseclj-alist.el b/parseclj-alist.el
index 824f491667..dcca15f598 100644
--- a/parseclj-alist.el
+++ b/parseclj-alist.el
@@ -82,10 +82,12 @@ For example: (parseclj-hash-table :foo 123 :bar 456)"
;; Emacs 27: (map-merge 'alist l1 l2)
(let ((keys (delete-dups (append (mapcar #'car l1) (mapcar #'car l2))))
(res '()))
- (seq-doseq (key keys)
- (push (or (assoc key l2)
- (assoc key l1))
- res))
+ (mapcar
+ (lambda (key)
+ (push (or (assoc key l2)
+ (assoc key l1))
+ res))
+ keys)
(nreverse res)))
(provide 'parseclj-alist)
diff --git a/parseclj-ast.el b/parseclj-ast.el
index 63b25612ae..94255c866b 100644
--- a/parseclj-ast.el
+++ b/parseclj-ast.el
@@ -120,29 +120,29 @@ OPTIONS is an association list. See `parseclj-parse' for
more information
on available options."
(let* ((pos (map-elt opening-token :pos))
(type (parseclj-lex-token-type opening-token))
- (type (cl-case type
- (:lparen :list)
- (:lbracket :vector)
- (:lbrace :map)
- (t type))))
- (cl-case type
- (:root (cons (parseclj-ast-node :root pos :children children) stack))
- (:discard stack)
- (:tag (cons (parseclj-ast-node :tag
- pos
- :tag (intern (substring (map-elt
opening-token :form) 1))
- :children children)
- stack))
- (:metadata (cons (parseclj-ast-node :with-meta
- pos
- :children children)
- stack))
- (:map-prefix (cons (parseclj-alist-assoc (car children)
- :map-prefix opening-token)
- stack))
- (t (cons
- (parseclj-ast-node type pos :children children)
- stack)))))
+ (type (cond
+ ((eq :lparen type) :list)
+ ((eq :lbracket type) :vector)
+ ((eq :lbrace type) :map)
+ (t type))))
+ (cond
+ ((eq :root type) (cons (parseclj-ast-node :root pos :children children)
stack))
+ ((eq :discard type) stack)
+ ((eq :tag type) (cons (parseclj-ast-node :tag
+ pos
+ :tag (intern (substring (map-elt
opening-token :form) 1))
+ :children children)
+ stack))
+ ((eq :metadata type) (cons (parseclj-ast-node :with-meta
+ pos
+ :children children)
+ stack))
+ ((eq :map-prefix type) (cons (parseclj-alist-assoc (car children)
+ :map-prefix
opening-token)
+ stack))
+ (t (cons
+ (parseclj-ast-node type pos :children children)
+ stack)))))
(defun parseclj-ast--reduce-branch-with-lexical-preservation (stack
opening-token children options)
"Reduce STACK with an AST branch node representing a collection of elements.
@@ -176,12 +176,12 @@ on available options."
(defun parseclj-ast--unparse-collection (node)
"Insert a string representation of the given AST branch NODE into buffer."
(let* ((token-type (parseclj-ast-node-type node))
- (delimiters (cl-case token-type
- (:root (cons "" ""))
- (:list (cons "(" ")"))
- (:vector (cons "[" "]"))
- (:set (cons "#{" "}"))
- (:map (cons "{" "}")))))
+ (delimiters (cond
+ ((eq :root token-type) (cons "" ""))
+ ((eq :list token-type) (cons "(" ")"))
+ ((eq :vector token-type) (cons "[" "]"))
+ ((eq :set token-type) (cons "#{" "}"))
+ ((eq :map token-type) (cons "{" "}")))))
(insert (car delimiters))
(let ((nodes (alist-get ':children node)))
(when-let (node (car nodes))
diff --git a/parseclj-lex.el b/parseclj-lex.el
index 8b30393251..8f2d6722e5 100644
--- a/parseclj-lex.el
+++ b/parseclj-lex.el
@@ -140,14 +140,15 @@ S goes through three transformations:
(make-string 1 (string-to-number (substring x 2) 16)))
(replace-regexp-in-string "\\\\[tbnrf'\"\\]"
(lambda (x)
- (cl-case (elt x 1)
- (?t "\t")
- (?f "\f")
- (?\" "\"")
- (?r "\r")
- (?n "\n")
- (?\\ "\\\\")
- (t (substring x 1))))
+ (let ((ch (elt x 1)))
+ (cond
+ ((eq ?t ch) "\t")
+ ((eq ?f ch) "\f")
+ ((eq ?\" ch) "\"")
+ ((eq ?r ch) "\r")
+ ((eq ?n ch) "\n")
+ ((eq ?\\ ch) "\\\\")
+ (t (substring x 1)))))
(substring s 1 -1)))))
(defun parseclj-lex--character-value (c)
@@ -164,16 +165,17 @@ S goes through three transformations:
(defun parseclj-lex--leaf-token-value (token)
"Parse the given leaf TOKEN to an Emacs Lisp value."
- (cl-case (parseclj-lex-token-type token)
- (:number (string-to-number (alist-get :form token)))
- (:nil nil)
- (:true t)
- (:false nil)
- (:symbol (intern (alist-get :form token)))
- (:keyword (intern (alist-get :form token)))
- (:string (parseclj-lex--string-value (alist-get :form token)))
- (:character (parseclj-lex--character-value (alist-get :form token)))
- (:symbolic-value (intern (substring (alist-get :form token) 2)))))
+ (let ((token-type (parseclj-lex-token-type token)))
+ (cond
+ ((eq :number token-type) (string-to-number (alist-get :form token)))
+ ((eq :nil token-type) nil)
+ ((eq :true token-type) t)
+ ((eq :false token-type) nil)
+ ((eq :symbol token-type) (intern (alist-get :form token)))
+ ((eq :keyword token-type) (intern (alist-get :form token)))
+ ((eq :string token-type) (parseclj-lex--string-value (alist-get :form
token)))
+ ((eq :character token-type) (parseclj-lex--character-value (alist-get
:form token)))
+ ((eq :symbolic-value token-type) (intern (substring (alist-get :form
token) 2))))))
;; Stream tokenization
diff --git a/parseclj-parser.el b/parseclj-parser.el
index aaad607d39..f83e760908 100644
--- a/parseclj-parser.el
+++ b/parseclj-parser.el
@@ -43,18 +43,19 @@ can be handled with `condition-case'."
(defun parseclj--find-opening-token (stack closing-token)
"Scan STACK for an opening-token matching CLOSING-TOKEN."
- (cl-case (parseclj-lex-token-type closing-token)
- (:rparen (parseclj-lex-token-type
- (seq-find (lambda (token)
- (member (parseclj-lex-token-type token)
- '(:lparen :lambda)))
- stack)))
- (:rbracket :lbracket)
- (:rbrace (parseclj-lex-token-type
- (seq-find (lambda (token)
- (member (parseclj-lex-token-type token)
- '(:lbrace :set)))
- stack)))))
+ (let ((token-type (parseclj-lex-token-type closing-token)))
+ (cond
+ ((eq :rparen token-type) (parseclj-lex-token-type
+ (seq-find (lambda (token)
+ (member (parseclj-lex-token-type
token)
+ '(:lparen :lambda)))
+ stack)))
+ ((eq :rbracket token-type) :lbracket)
+ ((eq :rbrace token-type) (parseclj-lex-token-type
+ (seq-find (lambda (token)
+ (member (parseclj-lex-token-type
token)
+ '(:lbrace :set)))
+ stack))))))
(defun parseclj--reduce-coll (stack closing-token reduce-branch options)
"Reduce collection based on the top of the STACK and a CLOSING-TOKEN.