Last active
August 29, 2015 14:16
-
-
Save rcgoodfellow/934cc0d60b960f4208f5 to your computer and use it in GitHub Desktop.
spirit error reporting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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