GithubHelp home page GithubHelp logo

lookAhead and error propagation about parsec HOT 4 CLOSED

haskell avatar haskell commented on August 17, 2024
lookAhead and error propagation

from parsec.

Comments (4)

aslatter avatar aslatter commented on August 17, 2024

Just to make sure I'm not being dense - the parser p in your example could never succeed with any input, correct?

from parsec.

redneb avatar redneb commented on August 17, 2024

Correct. I guess the example is a bit contrived, but my point is that p should report correctly why it failed. In this instance, many1 $ char 'a' is tried first and succeeds, then the input stream is rewound back to the beginning and then char 'b' is tried which fails because it didn't find an 'b'. But the error message does not indicate that.

Here's another example:

variable :: Parser String
variable = do
    x <- lookAhead (many1 letter)
    if x == "return"
        then fail "'return' is a reserved keyword"
        else string x

Then parse p "" "return" currently fails with

Left (line 1, column 7):
unexpected end of input
expecting letter

With my patch it gives the following error:

Left (line 1, column 1):
return is a reserved keyword

The problem is that many1 letter succeeds having consumed the 6 letters of "returned". But it also generates an message about the end of input when many1 tries to apply letter for a seventh time. Then the input stream is rewound by lookAhead, and fail is called on the beginning of input. But parserBind ignores that error because it prefers the error that occurred later in the input. I claim that this error is irrelevant here because we have rewound the input stream.

from parsec.

aslatter avatar aslatter commented on August 17, 2024

Oh, yes, I agree with you that your patch makes sense. I just wanted to make sure I understood your example.

If at all possible lookAhead should not update our parser-state when it succeeds.

from parsec.

aslatter avatar aslatter commented on August 17, 2024

Fixed in f7faa2a.

from parsec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.