Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error messages for incomplete parsing #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ivan-Yudin
Copy link

Hi,

I have tried to improve the error messages produced
by parsers in the case of error-less incomplete parsings.
(The example is in the git commit message as well as
comments on changes done.)

The patch makes the definition of the function "parse1"
more explicit (it doesn't use the functions "bestError"
and "maximumBy" now) , but in this way one avoids
to go through the list of possible parsings several times.

There is a type change in the function "parse" exported by
the module Prim.hs, since we need to know the position
where the parsing stopped when emitting a message in
the function "parse1". This can be avoided by introducing
a new function "parse' " or putting the corresponding code
inside of the definition of "parse1" . I am not sure what is
better as of now the function "parse" is not used in other
parts of the Boomerang library, and it is unclear to me if
anybody imports it. (In most cases only the specific parsers
like parseString, parseStrings, parseTexts would be used.)

Best,
Ivan

PS. I have run the travis build on my master branch and it
was successful.

As result of applying

> runParser . prs

to a boomerang

> b :: Boomerang e tok a b

and initial position

> initalPosition (Nothing :: Maybe e)

one gets a list consisting of three type of elements:

- those that resulted from errors encoded in primitive parsers of the form

> Left e

where e contains error message and position where the error occur;

- those that produce complete successful parsing of the form

> Right ((f,tok), pos)

where tok is empty in an appropriate sense;

- and those where the parsing stopped before the full input whose exhausted of
  the form

> Right ((f,tok), pos)

with non-empty tok and pos been the first place starting from which no parsing
was made.

In the previous version the elements of last type were silently thrown away,
which created incomplete error messages. For example

> parseString Text.Boomerang.String.alpha "a("

was given the message

This patch fixes the above issue by explicitly handling the results of parsing
of the third kind in the function

> parse1

This led to the following change of API:

- the result of the function "parse" now includes an extra field that contains
  the position of the first non-parsed element;

- the class ErrorPosition got an extra method "setPosition" that permits to add
  position to the messages generated in "parse1" for incomplete parses;

- the instance of "ErrorPosition (ParserError p)" is changed according to the
  new interface of the class ErrorPosition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant