error_handling.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // This sample demonstrates error handling as seen in the
  11. // Error Handling" chapter in the User's Guide.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <boost/spirit/include/classic_core.hpp>
  15. #include <boost/spirit/include/classic_exceptions.hpp>
  16. #include <iostream>
  17. #include <boost/assert.hpp>
  18. using namespace std;
  19. using namespace BOOST_SPIRIT_CLASSIC_NS;
  20. struct handler
  21. {
  22. template <typename ScannerT, typename ErrorT>
  23. error_status<>
  24. operator()(ScannerT const& /*scan*/, ErrorT const& /*error*/) const
  25. {
  26. cout << "exception caught...Test concluded successfully" << endl;
  27. return error_status<>(error_status<>::fail);
  28. }
  29. };
  30. int
  31. main()
  32. {
  33. cout << "/////////////////////////////////////////////////////////\n\n";
  34. cout << "\t\tExceptions Test...\n\n";
  35. cout << "/////////////////////////////////////////////////////////\n\n";
  36. assertion<int> expect(0);
  37. guard<int> my_guard;
  38. rule<> start =
  39. my_guard(ch_p('a') >> 'b' >> 'c' >> expect( ch_p('d') ))
  40. [
  41. handler()
  42. ];
  43. bool r = parse("abcx", start).full;
  44. BOOST_ASSERT(!r);
  45. return 0;
  46. }