Parse consumes from input even if one parser inside fails #232
-
Dear all, first thank you very much for upgrading the parsing library to a new way of parsing. Though it took me some time to update from the old way to the new, I think it is very clear way of describing a parsing problem. I have following problem: My understand is, that if a Parsers fails, it should not consume anything from input, so that the untouched input can be presented to another parser using OneOf. My case:
The problem:
Any comments or hints? Best regards, PS: Below you find the playground code that I ran in the library.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @morstin! This behavior is actually documented here. The parsers in the library minimally backtrack: only at points where you compose them together using 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 The rare exception where you may want to call This minimal backtracking is a "feature": it allows most parser conformances to be simpler because they do not have to worry about cleaning up after themselves, and it minimizes extra work done in those parsers. Since this isn't a bug with the library, I'm going to convert this to a discussion, but let us know if you have any other questions! |
Beta Was this translation helpful? Give feedback.
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:
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, likeOneOf
, backtracking is handled automatically for you.The rare exception where you may want to…