2017-06-25 32 views
0

是否可以自定義cljs.reader/read-string來處理類似"#(f 123)"的問題?如何自定義「#(...)」的cljs.reader.read-string的調度宏

我嘗試這樣做:

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

,但得到:

找不到標籤解析器(F IN( 「出師表」 的 「uuid」, 「排隊」, 「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]])" 

回答

0

tagged literals需求的標籤成爲一個合格的符號(你可以使用一個不合格的一個,但它氣餒) 。符號不能包含圓括號(或其他分隔符),因爲clojure(腳本)代碼需要是有效的s表達式。

但是,如果您想對此有所懷疑,您可以從錯誤消息中看到,cljs閱讀器認爲標記爲(f而不是(。所以,如果你這樣做

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

它會工作(在目前的cljs中,它可能會在某個時候中斷)。

+0

是的當然,我基本上是問如何擴展'dispatch-macros'函數的功能(https://github.com/clojure/clojurescript/blob/57819844b5a8da0464832f82426ac57ebdf2eaea/src/main/cljs/cljs/reader。 cljs#L443)通過'* tag-table *'方式在'read-dispatch'中分開使用,所以看起來可能有一種方法。在parens裏面的表達是可變的,所以我不能硬編碼'「(f」'。 – estolua