Boost logo

Boost :

From: Emil Dotchevski (emil_at_[hidden])
Date: 2007-10-05 16:17:51


> Lets look at boost::spirit.
>
> Lets say that I want to throw a boost::spirit parsing exception.
>
> Information available to me includes:
>
> 1) the filename
> 2) line number of error
> 3) charactar postion of the error
>
> Here is how I propose that we throw the error.
>
> struct tag_file_name: boost::error_info_value<std::string> { };
> struct tag_line_number: boost::error_info_value<std::string> { };
> struct tag_charactar_position : boost::error_info_value<std::string> { };
>
> typedef boost::error_info_group<
> tag_file_name,
> tag_line_number,
> tag_charactar_position> spirit_parsing_error_t;
>
> int size;
> std::string path;
> boost::spirit::parse_info<char const*> info =
> boost::spirit::parse(def.c_str(), face_grammar(path,size));
> if (!info.full)
> throw my_error() << spirit_parsing_error_t(
> path, info.line, info.postition);

If all parsing exceptions contain a file name, line number, and
character position, wouldn't it make sense to group them in a struct:

struct spirit_parsing_context
{
    spirit_parsing_context( std::string file_name, int line, size_t position ):
        file_name(file_name),
        line(line),
        position(position)
    {
    }

    std::string file_name;
    int line;
    size_t position;
};

And then you can do:

throw my_error() << error_info<tag_spirit_parsing_context>(
    spirit_parsing_context(path, info.line, info.position));

I am not familiar with boost::spirit, but I would think that if it
commonly reports parsing errors that have all this information, then
spirit_parsing_context should be part of the library anyway; thus, all
you'd have to do is define the tag type:

struct tag_spirit_parsing_context: error_info_value<spirit_parsing_context> { };

Then you'd do:

throw my_error() << error_info<tag_spirit_parsing_context>(spc);

where "spc" is the object of type spirit_parsing_context that spirit
itself returned when the failure was reported.

Emil Dotchevski


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk