m4rw3r / chomp

A fast monadic-style parser combinator designed to work on stable Rust.
Apache License 2.0
243 stars 19 forks source link

Many_till combinator seems to end up in an endless loop #63

Closed vegai closed 7 years ago

vegai commented 7 years ago

I wrote a naivish S-expression parser. I had a problem with the many_till combinator.

Here's the code: https://gist.github.com/anonymous/8a6fe19ab4e418eefad383455ae87a33 -- notice lines 56-60 where I would have wanted instead to do

many_till(parse_svalue, eof)

, but that just ended up in an endless loop with the simple "(a)" input. It's as if many_till did not notice that none of the three <|> -separated parsers matched. Is this a bug in chomp or am I misusing / missing something?

m4rw3r commented 7 years ago

I suspect it is take_while in parse_value which is the culprit. take_while can match zero tokens which means that if nothing alphanumeric is encountered in your case it will yield an empty string as match. This causes many_till (and other combinators) to loop endlessly since they will keep matching 0 tokens and the input will never be empty. Try with a take_while1 instead which will require at lest one matching token.

m4rw3r commented 7 years ago

I have improved and fixed the parser for S-expressions above: https://gist.github.com/m4rw3r/2f850c5f6895d1c5562adf84a94e48f2 I made the following changes: