Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Properly parse clojure source code?

315 views
Skip to first unread message

zirkonit

unread,
Jan 15, 2015, 6:13:22 PM1/15/15
to clo...@googlegroups.com
I'm thoroughly confused. If I want to parse clojure code from string without evaluating or caring a lot about its context, I'm out of luck.

EDN tools choke on reader macros ( #(blah % blah) is not valid EDN ), yet more context-aware tools like read-string with *read-eval* set to false choke on namespace-specific tokens like ::om/pass.

What would be a best choice to parse Clojure code into analyzable data structure while both parsing all of it (so, not just clojure.edn/read), _and_ not going the entire namespaces/shadowing/etc dance?

Thomas Heller

unread,
Jan 15, 2015, 6:21:33 PM1/15/15
to clo...@googlegroups.com
https://github.com/clojure/tools.reader is probably your best bet.

Andy Fingerhut

unread,
Jan 15, 2015, 6:35:37 PM1/15/15
to clo...@googlegroups.com
I agree that tools.reader is probably the best way to go.

The following note is off the top of my head without double-checking the details, so please take it with a bit of "needs verification of details" thought.

Even with tools.reader, I believe you either need to eval ns forms, or do your own parsing of ns forms to be able to read things like ::om/pass, with namespace aliases in the keywords.  e.g. see documentation for *alias-map* http://clojure.github.io/tools.reader/#clojure.tools.reader/*alias-map*

Andy


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reid McKenzie

unread,
Jan 15, 2015, 6:37:23 PM1/15/15
to clo...@googlegroups.com
If what you want is a parser not a reader, I reworked this [1] recently
and to the limits of my testing it's "correct" in that it parses every
reader construct I thought of to throw at it. You could improve it, I
believe that #() nesting is supported by this grammar, but I was trying
not to complicate evaluation semantics with the parser grammar too much.
Debugging ANTLR grammars is not what I'd call friendly.

A "useful" subset of this grammar with nicer evaluation semantics is
used, a reader implemented and tested here [2]

[1] https://github.com/antlr/grammars-v4/blob/master/clojure/Clojure.g4
[2] https://github.com/oxlang/oxlang/blob/master/resources/grammar/Oxlang.g4

Reid
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to clojure+u...@googlegroups.com
> <mailto:clojure+u...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages