Handling error with PetitParser

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Handling error with PetitParser

Stéphane Ducasse
We would like to see if we can use PetitParser to build syntax hilighting and for this
we need to see if there is a way to specify error handling.

Does any of you know?

So far we tried to use the RBSCanner but it does not really work for what we need

Stef
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: Handling error with PetitParser

Tudor Girba-2
What do you mean by error handling? A way to make the parser work not break on only partially correct code? That would be indeed great.

Perhaps we can deal with it by wrapping the parser objects with error handling. There already exists support for wrapping parsers. Take a look at:

PPParser>>>=>

Cheers,
Doru




On Fri, May 31, 2013 at 11:03 AM, Stéphane Ducasse <[hidden email]> wrote:
We would like to see if we can use PetitParser to build syntax hilighting and for this
we need to see if there is a way to specify error handling.

Does any of you know?

So far we tried to use the RBSCanner but it does not really work for what we need

Stef
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev



--

"Every thing has its own flow"

_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
cbc
Reply | Threaded
Open this post in threaded view
|

Re: Handling error with PetitParser

cbc
I've done similar things in some of the parsers I've worked with.  My approach was to include an 'unknown' parser in the code - if it can't correctly parse the code, it decides that it is unknown, and just grabs all of the input up until it can start parsing again.  It only works well in places where you know you can get back in sync at a later time.

If Stef wants to use it for Syntax Highlighting, I would assume that he has a place where the user is putting in code (or displaying it), and wants to highlight where the error is.  If just the first error is fine, then the existing structure should work - it does raise an error with the location that it couldn't parse beyond.  Unfortunately, this is only for the last error it encounters before it gives up - if it was 99% successful on the first option, and 0% successful on the last option, it will give you the error at the end of the last option - meaning the beginning of the code.  There is a way to change that (return the most-successful parse tree), but I don't have it handy at the moment.

Almost related to this, I've made a hack on the PPFailure class to return the context of the failure.  I got tired of counting character to figure out where the parser was failing - this one returns the code around the failure to give more context.  I'm not ready to ask to move it into the main PetitParser code (I've only used it a couple of days so far), but I am willing to share.  I've attached a change set with the code, but you'll need to add the context instance variable to PPFailure for it to work (not sure  how to add that to a change set), and then install the attached.  Feedback is welcome.

Thanks,
Chris


On Fri, May 31, 2013 at 2:15 AM, Tudor Girba <[hidden email]> wrote:
What do you mean by error handling? A way to make the parser work not break on only partially correct code? That would be indeed great.

Perhaps we can deal with it by wrapping the parser objects with error handling. There already exists support for wrapping parsers. Take a look at:

PPParser>>>=>

Cheers,
Doru




On Fri, May 31, 2013 at 11:03 AM, Stéphane Ducasse <[hidden email]> wrote:
We would like to see if we can use PetitParser to build syntax hilighting and for this
we need to see if there is a way to specify error handling.

Does any of you know?

So far we tried to use the RBSCanner but it does not really work for what we need

Stef
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev



--

"Every thing has its own flow"

_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev



_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev

PetitParser-Errors.1.cs (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Handling error with PetitParser

Tudor Girba-2
In reply to this post by Tudor Girba-2
Hi,

I played a bit, and it is definitely possible. Here is an example:

delimiter := $. asParser.
ignoreStatement := delimiter negate star.
statement := #word asParser plus >=> [:stream : continuation |
        | result |
        result := continuation value.
        result isPetitFailure
                ifTrue: [ ignoreStatement parseOn: stream ]
                ifFalse: [ result ] ].
statements := ( statement, delimiter ) star.

statements end parse: 'aaa.,,.aa.'

I have the original parser that parses simple statements based on #word. And then, from outside, I wrap the statement parser to handle a failure case with an ignoreStatement parser that ignores everything up to the first dot.

We could use this way of wrapping in a subclass of PPSmalltalkParser.

Cheers,
Doru



On May 31, 2013, at 11:15 AM, Tudor Girba <[hidden email]> wrote:

> What do you mean by error handling? A way to make the parser work not break on only partially correct code? That would be indeed great.
>
> Perhaps we can deal with it by wrapping the parser objects with error handling. There already exists support for wrapping parsers. Take a look at:
>
> PPParser>>>=>
>
> Cheers,
> Doru
>
>
>
>
> On Fri, May 31, 2013 at 11:03 AM, Stéphane Ducasse <[hidden email]> wrote:
> We would like to see if we can use PetitParser to build syntax hilighting and for this
> we need to see if there is a way to specify error handling.
>
> Does any of you know?
>
> So far we tried to use the RBSCanner but it does not really work for what we need
>
> Stef
> _______________________________________________
> Moose-dev mailing list
> [hidden email]
> https://www.iam.unibe.ch/mailman/listinfo/moose-dev
>
>
>
> --
> www.tudorgirba.com
>
> "Every thing has its own flow"

--
www.tudorgirba.com

"Innovation comes in the least expected form.
That is, if it is expected, it already happened."


_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: Handling error with PetitParser

Stéphane Ducasse
In reply to this post by cbc
Hi chris 

I've done similar things in some of the parsers I've worked with.  My approach was to include an 'unknown' parser in the code - if it can't correctly parse the code, it decides that it is unknown, and just grabs all of the input up until it can start parsing again.  It only works well in places where you know you can get back in sync at a later time.

If Stef wants to use it for Syntax Highlighting, I would assume that he has a place where the user is putting in code (or displaying it), and wants to highlight where the error is.  If just the first error is fine, then the existing structure should work - it does raise an error with the location that it couldn't parse beyond.  Unfortunately, this is only for the last error it encounters before it gives up - if it was 99% successful on the first option, and 0% successful on the last option, it will give you the error at the end of the last option - meaning the beginning of the code.  There is a way to change that (return the most-successful parse tree), but I don't have it handy at the moment.

Yes gisela should have a look at how we can make petitParser produce error on the longuest rules or something like that because now it stops too early for good highlighting :)


Almost related to this, I've made a hack on the PPFailure class to return the context of the failure.  I got tired of counting character to figure out where the parser was failing - this one returns the code around the failure to give more context.  I'm not ready to ask to move it into the main PetitParser code (I've only used it a couple of days so far), but I am willing to share.

Excellent because I think that we need that. 

 I've attached a change set with the code, but you'll need to add the context instance variable to PPFailure for it to work (not sure  how to add that to a change set), and then install the attached.  Feedback is welcome.

Thanks,
Chris


On Fri, May 31, 2013 at 2:15 AM, Tudor Girba <[hidden email]> wrote:
What do you mean by error handling? A way to make the parser work not break on only partially correct code? That would be indeed great.

Perhaps we can deal with it by wrapping the parser objects with error handling. There already exists support for wrapping parsers. Take a look at:

PPParser>>>=>

Cheers,
Doru




On Fri, May 31, 2013 at 11:03 AM, Stéphane Ducasse <[hidden email]> wrote:
We would like to see if we can use PetitParser to build syntax hilighting and for this
we need to see if there is a way to specify error handling.

Does any of you know?

So far we tried to use the RBSCanner but it does not really work for what we need

Stef
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev



--

"Every thing has its own flow"

_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev


<PetitParser-Errors.1.cs>_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev


_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev
Reply | Threaded
Open this post in threaded view
|

Re: Handling error with PetitParser

Stéphane Ducasse
In reply to this post by Tudor Girba-2
Thanks for the information.
I passed it to gisela.

Stef

On May 31, 2013, at 11:15 AM, Tudor Girba <[hidden email]> wrote:

What do you mean by error handling? A way to make the parser work not break on only partially correct code? That would be indeed great.

Perhaps we can deal with it by wrapping the parser objects with error handling. There already exists support for wrapping parsers. Take a look at:

PPParser>>>=>

Cheers,
Doru




On Fri, May 31, 2013 at 11:03 AM, Stéphane Ducasse <[hidden email]> wrote:
We would like to see if we can use PetitParser to build syntax hilighting and for this
we need to see if there is a way to specify error handling.

Does any of you know?

So far we tried to use the RBSCanner but it does not really work for what we need

Stef
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev



--

"Every thing has its own flow"
_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev


_______________________________________________
Moose-dev mailing list
[hidden email]
https://www.iam.unibe.ch/mailman/listinfo/moose-dev