-
Notifications
You must be signed in to change notification settings - Fork 228
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
Anonymous rules must_err<> #97
Comments
I'd try to keep things simple. What you really want is a custom error message that is embedded in the grammar. There is no need to tie that to the previous rule that did not match, hence instead of template< char... Cs >
struct raise_message
{
using analyze_t = analysis::generic< analysis::rule_type::ANY >;
template< typename Input >
static void raise( const Input& in, const std::initializer_list< char >& l )
{
throw parse_error( std::string( &*l.begin(), l.size() ), in );
}
template< apply_mode A,
rewind_mode,
template< typename... > class Action,
template< typename... > class Control,
typename Input,
typename... States >
static bool match( Input& in, States&&... /*unused*/ )
{
raise( in, { Cs... } );
}
};
#define TAO_PEGTL_RAISE( x ) \
TAO_PEGTL_INTERNAL_STRING( raise_message, x ) (Note: The above is untested, I currently have some trouble running my development VM) About your final point: |
The member Regarding keeping it simple and the |
I don't think it is wrong, it is just another style. We don't want to force any style on you, instead we try to support as many (reasonable) styles as possible. Feel free to experiment with it, having your own custom-rules when using the PEGTL is quite normal. Our reasons for separating the error message definitions from the actual grammar are: The grammar is shorter and easier to read, We'll consider adding more convenience rules at another time, if you (or us) have more experience with the style you proposed. |
Ok, so be it (for now). I will continue to implement my parser mith my own style and see if it all comes together. |
I think it might be a good idea in terms of convinience to extend the availible match rules by
must_err<R, ERR>
.The thought behind that is defining an Error message for an anonymous set of rules.
The implementation of
must<R>
is described as:Having a scenario as following:
I would need to define an error_control for this anonymous segment. Rather that or naming this sequence but I really would like to avoid that. Also this is ambigious, as
Spacing, Identifier, Spacing
is a very generic term, which may need very different error messages, depending of it's apperance inside a rule.What I would suggest instead is:
Where
ERR_MSG( "..." )
is a macro likeTAO_PEGTL_STRING( "..." )
, just returning something more simple thanstring<>
(without match method).must_err<> then rises an tao::pegtl::parse_error, with msg ERR_MSG( "..." ). While overwriting the content of msg with the custom message (together with filename / linenumber etc..), I would suggest parse_error would get some more methods/members, that give access to:
I tried it out but as always my template programming skills are very limited.
For the sake of simplicity I extended the given string<> template with a static data() method and created my own rule must_err<>.
And copied and modified the must<> rule
Looking forward on your thoughts on this topic.
The text was updated successfully, but these errors were encountered: