I took a stab at it and came up with this:
(defn make-reader [s]
(java.io.PushbackReader. (java.io.CharArrayReader.
(into-array Character/TYPE (seq s)))))
(defn read-with-number
"like read but takes in a string and returns a function
of no arguments which will read the string and provide char number
as in Common Lisp"
[string]
(let [*current-char-number* (atom 0)
numbering-pushback-reader
(proxy [java.io.PushbackReader]
[(make-reader string)]
(read [] (swap! *current-char-number* inc) (proxy-super read
))
(unread [stuff] (swap! *current-char-number* dec)
(proxy-super unread stuff))
)]
(fn [] [(read numbering-pushback-reader) (deref *current-char-number*)])))
Now you can do:
mobius.physics> (def read-oh-yeah! (read-with-number "(vector 1 2 3)
(list 5 6)"))
#'mobius.physics/read-oh-yeah!
mobius.physics> (read-oh-yeah!)
[(vector 1 2 3) 14]
mobius.physics> (read-oh-yeah!)
[(list 5 6) 25]
is that what you're going for? please tell me how I can improve this!
--Robert McIntyre
On Sat, Aug 28, 2010 at 1:26 AM, [email protected]
<[email protected]> wrote:
> I'm working on a project in which it would be very useful to be able
> to easily determine how many characters were consumed in the course of
> a read operation, in a similar fashion to the way that Common Lisp's
> read-from-string returns as a second value the index of the next
> character of the input past the end of the object that was read. I
> want to, for example, read Clojure values from a buffer and keep track
> of where in the buffer they were read from.
>
> Anyone have any good ideas of how to accomplish this without some
> level of reimplementation of read?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en