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. The tests were moved from
  clojure.tools.reader.edn/read-string
to
  clojure.tools.reader/read-string
because the latter implements Clojure's reader.

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 56dcbe5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 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
67 changes: 40 additions & 27 deletions test/rewrite_clj/parser_test.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(ns ^{:doc "Tests for EDN parser."
:author "Yannick Scherer"}
rewrite-clj.parser-test
(:refer-clojure :exclude [read-string])
(:require [clojure.string :as string]
[clojure.test :refer [deftest is]]
[clojure.tools.reader.edn :refer [read-string]]
[clojure.test :refer [deftest is testing]]
[clojure.tools.reader :as rdr]
[rewrite-clj.node :as node]
[rewrite-clj.parser :as p])
#?(:clj (:import [clojure.lang ExceptionInfo]
Expand Down Expand Up @@ -130,29 +129,42 @@
: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* #?(:cljs 'rewrite-clj.parser-test
:clj (the-ns 'rewrite-clj.parser-test))]
(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
#?(:cljs (rdr/read-string s)
:default (binding [rdr/*read-eval* false] (rdr/read-string s)))
#?@(:cljs []
:default [(binding [*read-eval* false] (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 Expand Up @@ -223,7 +235,8 @@
fq (frequencies (map node/tag children))]
(is (= t (node/tag n)))
(is (= (string/trim s) (node/string n)))
(is (= (node/sexpr n) (read-string s)))
(is (= (node/sexpr n) #?(:cljs (rdr/read-string s)
:default (binding [rdr/*read-eval* false] (rdr/read-string s)))))
(is (= w (:whitespace fq 0)))
(is (= c (:token fq 0))))))

Expand Down

0 comments on commit 56dcbe5

Please sign in to comment.