On Thu, Aug 23, 2012 at 11:22 AM, Alexsandro Soares
<prof.asoa...@gmail.com> wrote:
> Can you provide the code for this?
Certainly.

The parser's current source position is stored in the InputState
record's `pos` field. That field is a SourcePos record consisting of
the current line and column position. Most parsers, however don't deal
directly with the raw InputState and SourcePositions, so we must write
a low-level function to examine those records, and return the value
we're interested in without consuming any input. The function would
look like this:

(defn lineno []
  (fn [state _ _ eok _]
    (bounce eok (:line (:pos state)) state)))

The first line defines the name of the fn we can use in let->>
statements to get the current line number.
The second line is the standard parser interface, a function that
takes the state and 4 continutation functions, of which we're only
interested in the one that doesn't consume any input and doesn't
produce an error.
The last line pulls the current line number from the state, and passes
it along with the unchanged state to the `eok` continuation (`bounce`
is the function we use to ensure that no stack is consumed in the
process).

Now, you can treat `lineno` just like any other parser (in truth, it
is very close in spirit to the `always` parser except that it operates
on the parsers current state).

(let->> [current-line (lineno)]
  (do-something-with current-line))

Hope this was helpful.

Nate

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to