Skip to content

Commit bea9a9b

Browse files
author
Yannick Scherer
committed
Removed ':namespaced' token type and added a token settings map instead.
1 parent 3ee1989 commit bea9a9b

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

project.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}
77
:dependencies [[org.clojure/clojure "1.5.1"]
8-
[org.clojure/tools.reader "0.7.5"]
8+
[org.clojure/tools.reader "0.7.8"]
99
[fast-zip "0.3.0"]
10-
[potemkin "0.3.2"]]
10+
[potemkin "0.3.3"]]
1111
:profiles {:dev {:dependencies [[midje "1.5.1"]]
12-
:plugins [[lein-midje "3.1.1"]]}
12+
:plugins [[lein-midje "3.1.3-RC1"]]}
1313
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
1414
:1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]}
1515
:1.6 {:dependencies [[org.clojure/clojure "1.6.0-master-SNAPSHOT"]]}}
16-
:aliases {"midje-all" ["with-profile" "dev,1.4:dev,1.5:dev,1.6" "midje"]
16+
:aliases {"test-ancient" ["with-profile" "dev,1.4:dev,1.5:dev,1.6" "midje"]
1717
"deps-all" ["with-profile" "dev,1.4:dev,1.5:dev,1.6" "deps"]})

src/rewrite_clj/parser/core.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
[reader]
141141
(ignore reader)
142142
(let [nxt (edn/read reader)]
143-
(cond (keyword? nxt) [:namespaced nxt]
143+
(cond (keyword? nxt) [:token nxt {:namespaced? true}]
144144
(symbol? nxt) [:token (keyword nxt)]
145145
:else (throw-reader "Invalid token(s) following ':' prefix: " nxt))))
146146

src/rewrite_clj/printer.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020

2121
;; ## Printers
2222

23+
(defmethod print-edn :token
24+
[data]
25+
(let [[_ tk & rst] data]
26+
(when (and (keyword? tk) (:namespaced? (first rst)))
27+
(print ":"))
28+
(pr tk)))
29+
2330
(defmethod print-edn :forms [data] (print-children data))
24-
(defmethod print-edn :token [data] (pr (second data)))
2531
(defmethod print-edn :comment [data] (println (second data)))
2632
(defmethod print-edn :whitespace [data] (print (second data)))
2733
(defmethod print-edn :newline [data] (print (second data)))
@@ -40,7 +46,6 @@
4046
(defmethod print-edn :syntax-quote [data] (print-children "`" data))
4147
(defmethod print-edn :unquote [data] (print-children "~" data))
4248
(defmethod print-edn :unquote-splicing [data] (print-children "~@" data))
43-
(defmethod print-edn :namespaced [data] (pr ":") (prn (second data)))
4449

4550
(letfn [(print-line [^String s]
4651
(let [^String s (pr-str s)]
@@ -96,7 +101,6 @@
96101
(defmethod estimate-length :syntax-quote [data] (inc (estimate-children-length data)))
97102
(defmethod estimate-length :unquote [data] (inc (estimate-children-length data)))
98103
(defmethod estimate-length :unquote-splicing [data] (+ 2 (estimate-children-length data)))
99-
(defmethod estimate-length :namespaced [data] (inc (count (pr-str (second data)))))
100104
(defmethod estimate-length :multi-line [data]
101105
(let [parts (rest data)]
102106
(+ 2 (count parts)

test/rewrite_clj/parser_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"\"string\"" "string")
2727

2828
(fact "about parsing implicitly namespaced keywords"
29-
(p/parse-string "::key") => [:namespaced :key])
29+
(p/parse-string "::key") => [:token :key {:namespaced? true}])
3030

3131
(tabular
3232
(fact "about parsing prefixed data"

test/rewrite_clj/printer_test.clj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns ^{:doc "Tests for EDN printer."
2+
:author "Yannick Scherer"}
3+
rewrite-clj.printer-test
4+
(:require [midje.sweet :refer :all]
5+
[rewrite-clj.parser :refer [parse-string-all]]
6+
[rewrite-clj.printer :refer :all]))
7+
8+
(tabular
9+
(fact "about correct printing of EDN/Clojure"
10+
(let [tree (parse-string-all ?str)]
11+
(estimate-length tree) => (count ?str)
12+
(->string tree) => ?str))
13+
?str
14+
"0" "0.1" "1N"
15+
":key" ":ns/key" "::key"
16+
"sym" "sym#"
17+
"\"string\""
18+
19+
"@sym" "#'sym" "'sym" "~sym"
20+
"~@sym" "`sym" "#=sym"
21+
22+
"(first form) (second form)"
23+
"[:complex (list {:map 0})]"
24+
"#=(eval this)" "#date s"
25+
26+
"#\"regex\"" "#\"regex\\.\"" "#\"[reg|k].x\""
27+
28+
"^:private s"
29+
"^{:private true} s"
30+
"#^:private s"
31+
"#^{:private true} ^:privates s"
32+
33+
";; Hi!\n(def pi 3.14)")

0 commit comments

Comments
 (0)