Closed cppden closed 7 years ago
In the current development version, you can query the current position from the input parameter. There are several methods available, like in.line()
, in.byte_in_line()
, in.byte()
(for the overall byte position), in.source()
and even in.position()
.
Example:
template<typename Input, typename... States>
void apply( const Input& in, States&&... st )
{
const pegtl::position_info pi = in.position();
}
(yes, we need to document the guaranteed interface for the input parameter before releasing 2.0.0)
It is up to you to use/store this information.
Or do you need the information outside of apply()
? It should work in a similar way for the control hooks, see https://github.com/ColinH/PEGTL/blob/master/doc/Control-Hooks.md#normal-control
Thanks! I need info outside of apply
since it's an overkill to define action for every rule of no interest only to spot a failing part. The control hooks look as a flexible solution and should grant my wish. :)
Still I tend to think such error-revealing control hook will be needed frequently. Thus it may be considered as a good candidate to be a part of PEGTL. Don't you agree?
Thanks again for the lib and the hint!
With "error-revealing control hook", do you mean like pegtl::tracer<>
but only logging the failures, or do you mean a different kind of how to hook into the library? If the latter, could you please elaborate where you would expect it?
The tracer is a handy tool but hard to use since doesn't expose the depth level and missing a wider view on parsed input, i.e. 5-10 chars with current char highlighted. What I was describing as error-revealing hook is a simpler class to only save the current position and the rule in order to provide it to the caller on parsing failures.
This does sound useful, but I can also imagine everybody wanting something slightly different. You can achieve what you are asking for relatively easy by writing your own control class, and having the failure()
-method add information about the position (from the input) and the rule (from the template parameter) to a data structure of your choice. Does that work for you?
When a rule uses
must
then the exception reveals error information related to place and rule where parsing has failed. However there seems to be no way to get such or similar information for rules w/omust
. If it doesn't hurt the current design of the PEGTL then it'd be a valuable addition to provide some way to obtain error information in any case when user needs it.