Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 10, 2021. It is now read-only.

Commit05b7b98

Browse files
committed
Fixes#8 Support reader conditionals
1 parentfe3a42e commit05b7b98

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

‎src/rewrite_clj/parser/core.cljs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@
6363

6464
;; ### Reader Specialities
6565

66+
67+
(defn-parse-conditional [reader]
68+
;; we need to examine the next character, so consume one (known \?)
69+
(reader/next reader)
70+
;; we will always have a reader-macro-node as the result
71+
(node/reader-macro-node
72+
(let [read1 (fn [] (parse-printables reader:reader-macro1))]
73+
(cons (case (reader/peek reader)
74+
;; the easy case, just emit a token
75+
\( (node/token-node (symbol"?"))
76+
77+
;; the harder case, match \@, consume it and emit the token
78+
\@ (do (reader/next reader)
79+
(node/token-node (symbol"?@")))
80+
81+
;; otherwise no idea what we're reading but its \? prefixed
82+
(do (reader/unread reader \?)
83+
(first (read1))))
84+
(read1)))))
85+
86+
87+
6688
(defn-parse-sharp
6789
[^not-native reader]
6890
(reader/ignore reader)
@@ -75,8 +97,12 @@
7597
\' (node/var-node (parse-printables reader:var1true))
7698
\= (node/eval-node (parse-printables reader:eval1true))
7799
\_ (node/uneval-node (parse-printables reader:uneval1true))
100+
\? (parse-conditional reader)
78101
(node/reader-macro-node (parse-printables reader:reader-macro2))))
79102

103+
104+
105+
80106
(defn-parse-unmatched
81107
[^not-native reader]
82108
(reader/throw-reader
@@ -139,7 +165,6 @@
139165
:else parse-token))
140166

141167

142-
143168
(defnparse-next
144169
[^not-native rdr]
145170
(reader/read-with-meta rdr (dispatch (peek-char rdr))))

‎src/rewrite_clj/reader.cljs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118

119119

120120

121-
122121
(defnread-with-meta
123122
"Use the given function to read value, then attach row/col metadata."
124123
[^not-native reader read-fn]

‎test/rewrite_clj/node_test.cljs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,30 @@
1919
(is (= sample (-> sample p/parse-string n/string)))
2020
(is (= sample2 (-> sample2 p/parse-string n/string)))
2121
(is (= sample3 (-> sample3 p/parse-string n/string)))))
22+
23+
24+
(deftestreader-conditionals
25+
(testing"Simple reader conditional"
26+
(let [sample"#?(:clj bar)"
27+
res (p/parse-string sample)]
28+
(is (= sample (n/string res)))
29+
(is (=:reader-macro (n/tag res)))
30+
(is (= [:token:list] (map n/tag (n/children res))))))
31+
32+
(testing"Reader conditional with space before list"
33+
(let [sample"#? (:clj bar)"
34+
sample2"#?@ (:clj bar)"]
35+
(is (= sample (-> sample p/parse-string n/string)))
36+
(is (= sample2 (-> sample2 p/parse-string n/string)))))
37+
38+
39+
(testing"Reader conditional with splice"
40+
(let [sample
41+
"(:require [clojure.string :as s]
42+
#?@(:clj [[clj-time.format :as tf]
43+
[clj-time.coerce :as tc]]
44+
:cljs [[cljs-time.coerce :as tc]
45+
[cljs-time.format :as tf]]))"
46+
res (p/parse-string sample)]
47+
(is (= sample (n/string res))))))
48+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp