Skip to content

Parse consumes from input even if one parser inside fails #232

Answered by stephencelis
morstin asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @morstin! This behavior is actually documented here. The parsers in the library minimally backtrack: only at points where you compose them together using OneOf, Optionally, and a few others.

There are two main ways to invoke a parser:

// 1. The `inout` version that consumes input.
try parser.parse(&input)

// 2. The non-`inout` version that doesn't care about what input was consumed.
try parser.parse(input)

The vast majority of the time you should call 2., the non-inout version, where you don't need to worry about what was consumed. As long as you build your parsers out of other parsers, like OneOf, backtracking is handled automatically for you.

The rare exception where you may want to…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@morstin
Comment options

Answer selected by morstin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #231 on June 19, 2022 15:33.