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 possibility for pegtl::control to not throw exception on global error #40

Closed
cppden opened this issue Jan 16, 2017 · 8 comments
Closed

Comments

@cppden
Copy link

cppden commented Jan 16, 2017

Many, many thanks to your brilliant code!
Just discovered it and it seems to be a killer solution for my task.
However I need to parse messages and can't afford to wrap decoding in try/catch blocks since it's costly. Rather would be enough to get true/false from parse. Perhaps I couldn't find it in docs but trying to provide custom control doesn't help since the PEGTL code will call std::abort if no exception thrown.

Could you please consider some control policy to inhibit exceptions?

@ColinH
Copy link
Member

ColinH commented Jan 16, 2017

Thanks for the feedback! Regarding your issue, are you sure that the try-catch is a performance problem? With most modern compilers a try-catch has zero (!) performance cost as long as no exception is thrown.

@ColinH
Copy link
Member

ColinH commented Jan 16, 2017

Also, if you don't use any of the must-rules, then there shouldn't be any "global errors", and all errors will be reported with a return-value of false. (Depending on the grammar you might not be able to simply remove all must because then backtracking will be performed...)

@cppden
Copy link
Author

cppden commented Jan 16, 2017

afaik, try/catch costs some even if no exception is thrown, but you are right that the cost is near zero. Still the problem is that pegtl is used in inner loop of message processing deep inside chained FSMs since the decoding is lazy for performance reasons. Thus I just can't have one outer try/catch block (wish it would be an opposite case as I like exceptions for exceptional cases). Yet again, it's questionable whether malformed text is an exceptional case or not.
Thanks!

@ColinH
Copy link
Member

ColinH commented Jan 16, 2017

It really is zero cost, for example look at page 40 of this technical report which says that "All associated run-time costs occur only when an exception is thrown." Windows 64bit, and GCC and Clang, should all do this.

@d-frey
Copy link
Member

d-frey commented Jan 16, 2017

I'm not sure you understood Colin's second comment completely: If you do not use raise or any rule containing must in its name in your grammar, the PEGTL does not throw any exceptions. It is up to you to define the grammar properly and control this.

Is this sufficient in your case or are you bound to some given grammar that is using the above rules?

@cppden
Copy link
Author

cppden commented Jan 17, 2017

Hi! I did understand the must usage and thanks a lot for pointing this out.
The grammar can be changed and if it'll provide the same error detection as with must (namely whole text is parsed to the end and matches the grammar) then this is definitely will do what I need.
Thanks!

@ColinH
Copy link
Member

ColinH commented Jan 17, 2017

Great, so I'll close this issue for now, but feel free to contact us again here if there is anything that needs clarifying or that doesn't work...

@ColinH ColinH closed this as completed Jan 17, 2017
@cppden
Copy link
Author

cppden commented Jan 17, 2017

Thanks a lot guys! Lightning fast support and valuable/true help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants