Skip to content

Commit adb55fa

Browse files
authored
Merge pull request #34 from clojure-emacs/cl-case-to-cond
Replace `cl-case` calls with `cond`
2 parents fcebf65 + 5cf5cd5 commit adb55fa

File tree

5 files changed

+70
-63
lines changed

5 files changed

+70
-63
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- [#34](https://github.com/clojure-emacs/parseclj/pull/34) Replace `cl-case` with `cond`
4+
35
## 1.0.4 (2021-09-30)
46

57
- Provide parseclj-alist-merge, since we can't use `(map-merge 'alist)` yet in Emacs 25/26.

parseclj-alist.el

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ For example: (parseclj-hash-table :foo 123 :bar 456)"
8282
;; Emacs 27: (map-merge 'alist l1 l2)
8383
(let ((keys (delete-dups (append (mapcar #'car l1) (mapcar #'car l2))))
8484
(res '()))
85-
(seq-doseq (key keys)
86-
(push (or (assoc key l2)
87-
(assoc key l1))
88-
res))
85+
(mapcar
86+
(lambda (key)
87+
(push (or (assoc key l2)
88+
(assoc key l1))
89+
res))
90+
keys)
8991
(nreverse res)))
9092

9193
(provide 'parseclj-alist)

parseclj-ast.el

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,29 @@ OPTIONS is an association list. See `parseclj-parse' for more information
120120
on available options."
121121
(let* ((pos (map-elt opening-token :pos))
122122
(type (parseclj-lex-token-type opening-token))
123-
(type (cl-case type
124-
(:lparen :list)
125-
(:lbracket :vector)
126-
(:lbrace :map)
127-
(t type))))
128-
(cl-case type
129-
(:root (cons (parseclj-ast-node :root pos :children children) stack))
130-
(:discard stack)
131-
(:tag (cons (parseclj-ast-node :tag
132-
pos
133-
:tag (intern (substring (map-elt opening-token :form) 1))
134-
:children children)
135-
stack))
136-
(:metadata (cons (parseclj-ast-node :with-meta
137-
pos
138-
:children children)
139-
stack))
140-
(:map-prefix (cons (parseclj-alist-assoc (car children)
141-
:map-prefix opening-token)
142-
stack))
143-
(t (cons
144-
(parseclj-ast-node type pos :children children)
145-
stack)))))
123+
(type (cond
124+
((eq :lparen type) :list)
125+
((eq :lbracket type) :vector)
126+
((eq :lbrace type) :map)
127+
(t type))))
128+
(cond
129+
((eq :root type) (cons (parseclj-ast-node :root pos :children children) stack))
130+
((eq :discard type) stack)
131+
((eq :tag type) (cons (parseclj-ast-node :tag
132+
pos
133+
:tag (intern (substring (map-elt opening-token :form) 1))
134+
:children children)
135+
stack))
136+
((eq :metadata type) (cons (parseclj-ast-node :with-meta
137+
pos
138+
:children children)
139+
stack))
140+
((eq :map-prefix type) (cons (parseclj-alist-assoc (car children)
141+
:map-prefix opening-token)
142+
stack))
143+
(t (cons
144+
(parseclj-ast-node type pos :children children)
145+
stack)))))
146146

147147
(defun parseclj-ast--reduce-branch-with-lexical-preservation (stack opening-token children options)
148148
"Reduce STACK with an AST branch node representing a collection of elements.
@@ -176,12 +176,12 @@ on available options."
176176
(defun parseclj-ast--unparse-collection (node)
177177
"Insert a string representation of the given AST branch NODE into buffer."
178178
(let* ((token-type (parseclj-ast-node-type node))
179-
(delimiters (cl-case token-type
180-
(:root (cons "" ""))
181-
(:list (cons "(" ")"))
182-
(:vector (cons "[" "]"))
183-
(:set (cons "#{" "}"))
184-
(:map (cons "{" "}")))))
179+
(delimiters (cond
180+
((eq :root token-type) (cons "" ""))
181+
((eq :list token-type) (cons "(" ")"))
182+
((eq :vector token-type) (cons "[" "]"))
183+
((eq :set token-type) (cons "#{" "}"))
184+
((eq :map token-type) (cons "{" "}")))))
185185
(insert (car delimiters))
186186
(let ((nodes (alist-get ':children node)))
187187
(when-let (node (car nodes))

parseclj-lex.el

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ S goes through three transformations:
140140
(make-string 1 (string-to-number (substring x 2) 16)))
141141
(replace-regexp-in-string "\\\\[tbnrf'\"\\]"
142142
(lambda (x)
143-
(cl-case (elt x 1)
144-
(?t "\t")
145-
(?f "\f")
146-
(?\" "\"")
147-
(?r "\r")
148-
(?n "\n")
149-
(?\\ "\\\\")
150-
(t (substring x 1))))
143+
(let ((ch (elt x 1)))
144+
(cond
145+
((eq ?t ch) "\t")
146+
((eq ?f ch) "\f")
147+
((eq ?\" ch) "\"")
148+
((eq ?r ch) "\r")
149+
((eq ?n ch) "\n")
150+
((eq ?\\ ch) "\\\\")
151+
(t (substring x 1)))))
151152
(substring s 1 -1)))))
152153

153154
(defun parseclj-lex--character-value (c)
@@ -164,16 +165,17 @@ S goes through three transformations:
164165

165166
(defun parseclj-lex--leaf-token-value (token)
166167
"Parse the given leaf TOKEN to an Emacs Lisp value."
167-
(cl-case (parseclj-lex-token-type token)
168-
(:number (string-to-number (alist-get :form token)))
169-
(:nil nil)
170-
(:true t)
171-
(:false nil)
172-
(:symbol (intern (alist-get :form token)))
173-
(:keyword (intern (alist-get :form token)))
174-
(:string (parseclj-lex--string-value (alist-get :form token)))
175-
(:character (parseclj-lex--character-value (alist-get :form token)))
176-
(:symbolic-value (intern (substring (alist-get :form token) 2)))))
168+
(let ((token-type (parseclj-lex-token-type token)))
169+
(cond
170+
((eq :number token-type) (string-to-number (alist-get :form token)))
171+
((eq :nil token-type) nil)
172+
((eq :true token-type) t)
173+
((eq :false token-type) nil)
174+
((eq :symbol token-type) (intern (alist-get :form token)))
175+
((eq :keyword token-type) (intern (alist-get :form token)))
176+
((eq :string token-type) (parseclj-lex--string-value (alist-get :form token)))
177+
((eq :character token-type) (parseclj-lex--character-value (alist-get :form token)))
178+
((eq :symbolic-value token-type) (intern (substring (alist-get :form token) 2))))))
177179

178180
;; Stream tokenization
179181

parseclj-parser.el

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ can be handled with `condition-case'."
4343

4444
(defun parseclj--find-opening-token (stack closing-token)
4545
"Scan STACK for an opening-token matching CLOSING-TOKEN."
46-
(cl-case (parseclj-lex-token-type closing-token)
47-
(:rparen (parseclj-lex-token-type
48-
(seq-find (lambda (token)
49-
(member (parseclj-lex-token-type token)
50-
'(:lparen :lambda)))
51-
stack)))
52-
(:rbracket :lbracket)
53-
(:rbrace (parseclj-lex-token-type
54-
(seq-find (lambda (token)
55-
(member (parseclj-lex-token-type token)
56-
'(:lbrace :set)))
57-
stack)))))
46+
(let ((token-type (parseclj-lex-token-type closing-token)))
47+
(cond
48+
((eq :rparen token-type) (parseclj-lex-token-type
49+
(seq-find (lambda (token)
50+
(member (parseclj-lex-token-type token)
51+
'(:lparen :lambda)))
52+
stack)))
53+
((eq :rbracket token-type) :lbracket)
54+
((eq :rbrace token-type) (parseclj-lex-token-type
55+
(seq-find (lambda (token)
56+
(member (parseclj-lex-token-type token)
57+
'(:lbrace :set)))
58+
stack))))))
5859

5960
(defun parseclj--reduce-coll (stack closing-token reduce-branch options)
6061
"Reduce collection based on the top of the STACK and a CLOSING-TOKEN.

0 commit comments

Comments
 (0)