Skip to content

Commit ea99838

Browse files
committed
Qualify @ ~ ~@ sexpr's under clojure.core
z/sexpr should return the same value as clojure.core/read-string as per the documentation. After this commit, the only special forms that return different values are ` and #=.
1 parent bf37e3d commit ea99838

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

src/rewrite_clj/node/quote.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
[children]
111111
(if (sequential? children)
112112
(->node
113-
:unquote "~" 'unquote
113+
:unquote "~" 'clojure.core/unquote
114114
children)
115115
(recur [children])))
116116

@@ -135,6 +135,6 @@
135135
[children]
136136
(if (sequential? children)
137137
(->node
138-
:unquote-splicing "~@" 'unquote-splicing
138+
:unquote-splicing "~@" 'clojure.core/unquote-splicing
139139
children)
140140
(recur [children])))

src/rewrite_clj/node/reader_macro.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(node-type [_node] :deref)
6969
(printable-only?[_node] false)
7070
(sexpr* [_node opts]
71-
(list* 'deref (node/sexprs children opts)))
71+
(list* 'clojure.core/deref (node/sexprs children opts)))
7272
(length [_node]
7373
(inc (node/sum-lengths children)))
7474
(string [_node]

test/rewrite_clj/parser_test.cljc

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
:author "Yannick Scherer"}
33
rewrite-clj.parser-test
44
(:refer-clojure :exclude [read-string])
5-
(:require [clojure.string :as string]
6-
[clojure.test :refer [deftest is]]
5+
(:require [clojure.core :as cc]
6+
[clojure.string :as string]
7+
[clojure.test :refer [deftest is testing]]
78
[clojure.tools.reader.edn :refer [read-string]]
89
[rewrite-clj.node :as node]
910
[rewrite-clj.parser :as p])
1011
#?(:clj (:import [clojure.lang ExceptionInfo]
1112
[java.io File])))
1213

14+
(def this-nsym (ns-name *ns*))
15+
1316
(deftest t-parsing-the-first-few-whitespaces
1417
(doseq [[ws parsed]
1518
[[" " " "]
@@ -130,29 +133,37 @@
130133
:default (is (Double/isNaN e)))))
131134

132135
(deftest t-parsing-reader-prefixed-data
133-
(doseq [[s t ws sexpr]
134-
[["@sym" :deref [] '(deref sym)]
135-
["@ sym" :deref [:whitespace] '(deref sym)]
136-
["'sym" :quote [] '(quote sym)]
137-
["' sym" :quote [:whitespace] '(quote sym)]
138-
["`sym" :syntax-quote [] '(quote sym)]
139-
["` sym" :syntax-quote [:whitespace] '(quote sym)]
140-
["~sym" :unquote [] '(unquote sym)]
141-
["~ sym" :unquote [:whitespace] '(unquote sym)]
142-
["~@sym" :unquote-splicing [] '(unquote-splicing sym)]
143-
["~@ sym" :unquote-splicing [:whitespace] '(unquote-splicing sym)]
144-
["#=sym" :eval [] '(eval 'sym)]
145-
["#= sym" :eval [:whitespace] '(eval 'sym)]
146-
["#'sym" :var [] '(var sym)]
147-
["#'\nsym" :var [:newline] '(var sym)]]]
148-
(let [n (p/parse-string s)
149-
children (node/children n)
150-
c (map node/tag children)]
151-
(is (= t (node/tag n)))
152-
(is (= :token (last c)))
153-
(is (= sexpr (node/sexpr n)))
154-
(is (= 'sym (node/sexpr (last children))))
155-
(is (= ws (vec (butlast c)))))))
136+
(doseq [[ s t ws sexpr ltag lcld]
137+
[["@sym" :deref [] '@sym :token 'sym]
138+
["@ sym" :deref [:whitespace] '@sym :token 'sym]
139+
["'sym" :quote [] ''sym :token 'sym]
140+
["' sym" :quote [:whitespace] ''sym :token 'sym]
141+
["`sym" :syntax-quote [] ''sym :token 'sym]
142+
["` sym" :syntax-quote [:whitespace] ''sym :token 'sym]
143+
["~sym" :unquote [] '~sym :token 'sym]
144+
["~ sym" :unquote [:whitespace] '~sym :token 'sym]
145+
["~@sym" :unquote-splicing [] '~@sym :token 'sym]
146+
["~@ sym" :unquote-splicing [:whitespace] '~@sym :token 'sym]
147+
["~ @sym" :unquote [:whitespace] '~ @sym :deref '@sym]
148+
["#=sym" :eval [] '(eval 'sym) :token 'sym]
149+
["#= sym" :eval [:whitespace] '(eval 'sym) :token 'sym]
150+
["#'sym" :var [] '#'sym :token 'sym]
151+
["#'\nsym" :var [:newline] '#'sym :token 'sym]]]
152+
(testing (pr-str s)
153+
(binding [*ns* (the-ns this-nsym)]
154+
(let [n (p/parse-string s)
155+
children (node/children n)
156+
c (map node/tag children)]
157+
(is (= t (node/tag n)) "tag")
158+
(is (= ltag (last c)) "ltag")
159+
(is (= sexpr (node/sexpr n)) "sexpr")
160+
(is (= s (node/string n)) "string")
161+
;; ` and #= return different sexpr's than via clojure.core/read-string
162+
(when-not (#{:syntax-quote :eval} t)
163+
(is (= sexpr (binding [cc/*read-eval* false] (cc/read-string s)))
164+
"read-string"))
165+
(is (= lcld (node/sexpr (last children))) "lcld")
166+
(is (= ws (vec (butlast c))) "ws"))))))
156167

157168
(deftest t-eval
158169
(let [n (p/parse-string "#=(+ 1 2)")]

0 commit comments

Comments
 (0)