Skip to content

Instantly share code, notes, and snippets.

@rcgoodfellow
Last active August 29, 2015 14:16
Show Gist options
  • Save rcgoodfellow/934cc0d60b960f4208f5 to your computer and use it in GitHub Desktop.
Save rcgoodfellow/934cc0d60b960f4208f5 to your computer and use it in GitHub Desktop.
spirit error reporting
#include "spirit_inc.hxx"
#include <iostream>
namespace x3 = boost::spirit::x3;
using std::cout;
using std::endl;
using std::string;
struct name_class;
struct object_class;
struct objj {};
x3::rule<object_class, objj> const object("object");
x3::rule<name_class, string> const name("name");
auto const name_def = +x3::alnum;
auto const object_def =
x3::lit("Object")
> name
>> -(x3::lit("(") > name > *( x3::lit(",") > *x3::alnum ) > x3::lit(")"))
;
BOOST_SPIRIT_DEFINE
(
object = object_def,
name = name_def
);
struct name_class
{
template <class Iterator, class Exception, class Context>
x3::error_handler_result
on_error(Iterator&, Iterator const&, Exception const&, Context const&)
{
cout << "name parsing error" << endl;
return x3::error_handler_result::fail;
}
};
struct object_class
{
template <class Iterator, class Exception, class Context>
x3::error_handler_result
on_error(Iterator&, Iterator const&, Exception const&, Context const&)
{
cout << "object parsing error" << endl;
return x3::error_handler_result::fail;
}
};
int main()
{
string obj{"Object Mu#ffin(a,b,c)"};
auto it = obj.begin(), end = obj.end();
boost::spirit::x3::ascii::space_type space;
bool r = phrase_parse(it, end, object, space);
if(r && it == end)
{
cout << "Success" << endl;
}
else
{
cout << "Fail" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment