On Tue, Jan 28, 2014 at 8:31 AM, Kashyap CK <ckkash...@gmail.com> wrote:
> The attempt parser does not work as expected for me. Perhaps my
> understanding is not correct. I the following code I was hoping that
> parsatron would attempt to parser "hello" and fail.

This is actually exactly what happened, but The Parsatron gave you an
atrocious error message that probably clouded that fact. By
complaining about 'w' I imagine that you thought the parser had
proceeded past `helloParser` and was currently working on parsing the
"world" part of the input.

What actually happened, was that the (p/attempt (helloParser)) failed,
which failed the entire outer `let->>` form, since The Parsatron won't
continue to parse sequentially combined parsers unless all succeed

Since I think you mentioned elsewhere that you've watched the talk,
this is the the 'ok' part of the [consumed/empty] x [ok/err] matrix.
`attempt` turns (consumed x err) into (empty x err) which means it has
still failed, but has rewound itself back to where it started to fail
so that another parser might take a crack at parsing it successfully.

You might consider something like this instead:

(p/defparser optional [p default-value]
  (p/either (p/attempt p) (p/always default-value)))

(p/defparser p []
  (p/let->> [h (optional (helloParser) nil)
             w (worldParser)]
    (p/always [h w])))

Hope that helps.

Nate Young

-- 
-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to