Does Attoparsec.parse ever return Partial?

121 views
Skip to first unread message

Eugene Perederey

unread,
Nov 17, 2011, 2:25:11 PM11/17/11
to Stanford CS240h 2011 Autumn
Is it safe to match its result to only Fail and Done?
Or what other function can I use to get both the matched value and the
rest of input?

Drew Haven

unread,
Nov 17, 2011, 2:29:24 PM11/17/11
to stanford-1...@googlegroups.com
From my experience it will often return a partial.  The most common case for me is when I use the many combinator to match a list of n items.  When it reaches the end of the n items, there might be more, so it returns "Partial _".  At that point I could just feed it an empty string to get the result and let it know that I'm done, but since I have been only parsing whole files I usually just use "parseOnly :: Parser a -> Either String a" (I think that's the type).  parseOnly is basically what you are talking about.  It only returns a value when it's done, or a failure.  It doesn't return the rest of the string though.  For that I think you'll have to use parse and feed it an empty string if you want to force a Partial to finish.

Another way to force it to finish is to just tack something on the end of your input that it won't parse properly.  When testing I found myself adding extra spaces/newlines at the end just to force this behavior.

Drew Haven
drew....@gmail.com

Bryan O'Sullivan

unread,
Nov 17, 2011, 2:29:57 PM11/17/11
to stanford-1...@googlegroups.com
On Thu, Nov 17, 2011 at 11:25 AM, Eugene Perederey <eugene.p...@gmail.com> wrote:
Is it safe to match its result to only Fail and Done?

No, it will return Partial any time you have provided it with insufficient input: that's kind of the whole point.
 
Or what other function can I use to get both the matched value and the
rest of input?

If you use the Partial continuation to tell it that there's no more input available, you're guaranteed that the next Result that you get will not be a Partial (although that isn't expressed via the type system).

Eugene Perederey

unread,
Nov 17, 2011, 2:50:22 PM11/17/11
to Stanford CS240h 2011 Autumn
Thanks. Looks like I need to explicitly take the rest of input with
takeByteString as a special field in my data format.


On Nov 17, 11:29 am, "Bryan O'Sullivan" <b...@serpentine.com> wrote:
> On Thu, Nov 17, 2011 at 11:25 AM, Eugene Perederey <
>

Bryan O'Sullivan

unread,
Nov 17, 2011, 2:57:36 PM11/17/11
to stanford-1...@googlegroups.com
On Thu, Nov 17, 2011 at 11:50 AM, Eugene Perederey <eugene.p...@gmail.com> wrote:
Thanks. Looks like I need to explicitly take the rest of input with
takeByteString as a special field in my data format.

No, that's what the ByteString field in the Done constructor contains.
Reply all
Reply to author
Forward
0 new messages