如何为“#(...)”自定义cljs.reader.read-string的调度宏

时间:2017-06-25 22:16:23

标签: clojurescript

是否可以自定义cljs.reader/read-string来处理"#(f 123)"

之类的内容

我试过了:

(cljs.reader/register-tag-parser! \( (fn [x] "test"))
(cljs.reader/read-string "#(f 123")

但得到:

  

无法找到标签解析器(f in(“inst”“uuid”“queue”“js”“(”)


更新

经过一次挖掘后,我遇到https://github.com/clojure/clojurescript/blob/6ff80dc2f309f961d2e363148b5c0da65ac320f9/src/test/cljs/cljs/pprint_test.cljs#L106,证实目前无法实现这一目标:

(simple-tests pprint-reader-macro-test
  ;;I'm not sure this will work without significant work on cljs. Short story, cljs
  ;;reader only takes valid EDN, so #(* % %) won't work.
  ;;see http://stackoverflow.com/a/25712675/546321 for more details
  #_(with-pprint-dispatch code-dispatch
    (write (reader/read-string "(map #(first %) [[1 2 3] [4 5 6] [7]])")
           :stream nil))
  #_"(map #(first %) [[1 2 3] [4 5 6] [7]])"

1 个答案:

答案 0 :(得分:0)

tagged literals中的标记需要是一个合格的符号(您可以使用不合格的符号,但不鼓励它)。符号不能包括括号(或其他分隔符),因为clojure(脚本)代码需要是有效的s表达式。

但是,如果你想对它持开放态度,可以从错误消息中看到cljs读者认为标记是(f而不是(。所以,如果你这样做

(cljs.reader/register-tag-parser! "(f" (fn [x] "test"))

它会起作用(在当前的cljs中,它可能会在某些时候中断)。