-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Two-colon keywords cause exceptions #21
Comments
There actually is a special keyword node but the issue here is that
The Clojure reader doesn't accept it:
|
user=> (require '[clojure.set :as xyz])
nil
user=> ::xyz/foo
:clojure.set/foo
user=> (read-string "::xyz/foo")
:clojure.set/foo |
I'll have to think about that. Allowing the parsing of these keywords is not a problem, but without keeping track of namespaces/aliases conversion of such a node to an s-expression would have to result in something like: '(if-let [alias (get (ns-aliases *ns*) 'xyz)]
(keyword (name (ns-name alias)) "foo")
(throw ...)) Which is not really something I'd expect a |
I'm not sure what you mean... You can't determine the keyword without evaluating the namespace, and conceivably the same keyword might turn out to have different namespaces. e.g. (require (rand-nth '([clojure.set :as xyz] [clojure.zip :as xyz])))
(prn ::xyz/foo) So the only way I can see around it, is to create some sort of custom structure for these unevaluated keywords, like: #rewrite-clj.node.Keyword {:namespace ":xyz", :name "foo"} Or vector AST style: [:list [:symbol "prn"] [:keyword ":xyx" "foo"]] |
As I said, parsing and storing as a node is not a problem and can be accomplished easily by adjusting this precondition. But there is the function
I prefer the second solution but it would mean that: (rewrite-clj.node/sexpr (rewrite-clj.parser/parse-string "::xyz/foo"))
;; => '(if-let [alias (get (ns-aliases *ns*) 'xyz)]
;; (keyword (name (ns-name alias)) "foo")
;; (throw ...)) |
Oh, I understand what you're saying now. I can see why you want that The really annoying part is that keywords like this can be quoted, so you could write something like: (n/sexpr (p/parse-string "(foo '{:x ::xyz/foo})")) |
I now think that the cleanest solution here is to cheat: (n/sexpr (p/parse-string "::xyz/foo")) ;; => :xyz/foo This should be fine in nearly all cases where |
Expansion to s-expression will return a namespaced keyword without resolving the alias. This is in accordance with the expectation that a `:token` node returns a token, not a nested structure. (An alternative approach would have been to generate the form `(read-string "::ns/x")`.)
I don't think I've ever actually needed the |
I thought so. :) Released in [rewrite-clj "0.4.8"] |
Keywords like
::xyz/foo
cause exceptions when read by rewrite-clj:Because
xyz
isn't a valid namespace alias in the namespace doing the parsing, rewrite-clj will throw an exception when it tries to read in the keyword::xyz/foo
.You might need to add a specific keyword node, rather than relying on
clojure.lang.Keyword
.The text was updated successfully, but these errors were encountered: