Skip to content

Commit

Permalink
Qualify @ ~ ~@ sexpr's under clojure.core
Browse files Browse the repository at this point in the history
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 #=.
  • Loading branch information
frenchy64 committed Aug 14, 2024
1 parent bf37e3d commit ea99838
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/rewrite_clj/node/quote.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
[children]
(if (sequential? children)
(->node
:unquote "~" 'unquote
:unquote "~" 'clojure.core/unquote
children)
(recur [children])))

Expand All @@ -135,6 +135,6 @@
[children]
(if (sequential? children)
(->node
:unquote-splicing "~@" 'unquote-splicing
:unquote-splicing "~@" 'clojure.core/unquote-splicing
children)
(recur [children])))
2 changes: 1 addition & 1 deletion src/rewrite_clj/node/reader_macro.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
(node-type [_node] :deref)
(printable-only?[_node] false)
(sexpr* [_node opts]
(list* 'deref (node/sexprs children opts)))
(list* 'clojure.core/deref (node/sexprs children opts)))
(length [_node]
(inc (node/sum-lengths children)))
(string [_node]
Expand Down
61 changes: 36 additions & 25 deletions test/rewrite_clj/parser_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
:author "Yannick Scherer"}
rewrite-clj.parser-test
(:refer-clojure :exclude [read-string])
(:require [clojure.string :as string]
[clojure.test :refer [deftest is]]
(:require [clojure.core :as cc]
[clojure.string :as string]
[clojure.test :refer [deftest is testing]]
[clojure.tools.reader.edn :refer [read-string]]
[rewrite-clj.node :as node]
[rewrite-clj.parser :as p])
#?(:clj (:import [clojure.lang ExceptionInfo]
[java.io File])))

(def this-nsym (ns-name *ns*))

(deftest t-parsing-the-first-few-whitespaces
(doseq [[ws parsed]
[[" " " "]
Expand Down Expand Up @@ -130,29 +133,37 @@
:default (is (Double/isNaN e)))))

(deftest t-parsing-reader-prefixed-data
(doseq [[s t ws sexpr]
[["@sym" :deref [] '(deref sym)]
["@ sym" :deref [:whitespace] '(deref sym)]
["'sym" :quote [] '(quote sym)]
["' sym" :quote [:whitespace] '(quote sym)]
["`sym" :syntax-quote [] '(quote sym)]
["` sym" :syntax-quote [:whitespace] '(quote sym)]
["~sym" :unquote [] '(unquote sym)]
["~ sym" :unquote [:whitespace] '(unquote sym)]
["~@sym" :unquote-splicing [] '(unquote-splicing sym)]
["~@ sym" :unquote-splicing [:whitespace] '(unquote-splicing sym)]
["#=sym" :eval [] '(eval 'sym)]
["#= sym" :eval [:whitespace] '(eval 'sym)]
["#'sym" :var [] '(var sym)]
["#'\nsym" :var [:newline] '(var sym)]]]
(let [n (p/parse-string s)
children (node/children n)
c (map node/tag children)]
(is (= t (node/tag n)))
(is (= :token (last c)))
(is (= sexpr (node/sexpr n)))
(is (= 'sym (node/sexpr (last children))))
(is (= ws (vec (butlast c)))))))
(doseq [[ s t ws sexpr ltag lcld]
[["@sym" :deref [] '@sym :token 'sym]
["@ sym" :deref [:whitespace] '@sym :token 'sym]
["'sym" :quote [] ''sym :token 'sym]
["' sym" :quote [:whitespace] ''sym :token 'sym]
["`sym" :syntax-quote [] ''sym :token 'sym]
["` sym" :syntax-quote [:whitespace] ''sym :token 'sym]
["~sym" :unquote [] '~sym :token 'sym]
["~ sym" :unquote [:whitespace] '~sym :token 'sym]
["~@sym" :unquote-splicing [] '~@sym :token 'sym]
["~@ sym" :unquote-splicing [:whitespace] '~@sym :token 'sym]
["~ @sym" :unquote [:whitespace] '~ @sym :deref '@sym]
["#=sym" :eval [] '(eval 'sym) :token 'sym]
["#= sym" :eval [:whitespace] '(eval 'sym) :token 'sym]
["#'sym" :var [] '#'sym :token 'sym]
["#'\nsym" :var [:newline] '#'sym :token 'sym]]]
(testing (pr-str s)
(binding [*ns* (the-ns this-nsym)]
(let [n (p/parse-string s)
children (node/children n)
c (map node/tag children)]
(is (= t (node/tag n)) "tag")
(is (= ltag (last c)) "ltag")
(is (= sexpr (node/sexpr n)) "sexpr")
(is (= s (node/string n)) "string")
;; ` and #= return different sexpr's than via clojure.core/read-string
(when-not (#{:syntax-quote :eval} t)
(is (= sexpr (binding [cc/*read-eval* false] (cc/read-string s)))
"read-string"))
(is (= lcld (node/sexpr (last children))) "lcld")
(is (= ws (vec (butlast c))) "ws"))))))

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

0 comments on commit ea99838

Please sign in to comment.