boost spirit exception example

3) Support for linking multiple error tags in a single expression might be useful and help in standarizing errors throughout an application.
Could you clarify what you mean?
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); This is much cleaner and gives a standard way of throwing errors that the spirit library generates. Using the "<<" for an insertion operator or the "add_info" member function just makes the throw statement look cluttered. I dont think that they would get used because the just look funny and non-standard. Creating a typedef for each each error grouping is much cleaner. Just for reference, the "<<" insertion operator would look like this: throw my_error() << boost::error_info<tag_file_name>(name) << boost::error_info<tag_line_number>(number) << boost::error_info<tag_charactar_position>(position); This is very verbose, but minimally acceptable!!. Also, Just for reference, the "add_info" insertion operator would look like this: throw my_error().add_info<tag_file_name>(name).add_info< tag_line_number>(number).add_info<tag_charactar_position>(position); This is too funky for me. I'd never use this syntax of "add_info" "boost::error_info_group" would just wrap "boost::error_info" and have defaulted template paramaters, so that if I wanted to create an extended spirit exception, I would just do this: throw my_error() << boost::error_info_group<tag_file_name,tag_line_number, tag_charactar_position, error_number>(name,line,pos,error_no); Simple!! I've already wrote the "error_info_group" wrapper if you want me to send it to you.

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
participants (2)
-
Emil Dotchevski
-
Tom Brinkman