seek.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*//////////////////////////////////////////////////////////////////////////////
  2. Copyright (c) 2011 Jamboree
  3. Copyright (c) 2014 Lee Clagett
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //////////////////////////////////////////////////////////////////////////////*/
  7. #include <vector>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/home/x3/auxiliary/eoi.hpp>
  10. #include <boost/spirit/home/x3/core.hpp>
  11. #include <boost/spirit/home/x3/char.hpp>
  12. #include <boost/spirit/home/x3/string.hpp>
  13. #include <boost/spirit/home/x3/numeric.hpp>
  14. #include <boost/spirit/home/x3/operator/plus.hpp>
  15. #include <boost/spirit/home/x3/operator/sequence.hpp>
  16. #include <boost/spirit/home/x3/directive/seek.hpp>
  17. #include "test.hpp"
  18. ///////////////////////////////////////////////////////////////////////////////
  19. int main()
  20. {
  21. using namespace spirit_test;
  22. namespace x3 = boost::spirit::x3;
  23. // test eoi
  24. {
  25. BOOST_TEST(test("", x3::seek[x3::eoi]));
  26. BOOST_TEST(test(" ", x3::seek[x3::eoi], x3::space));
  27. BOOST_TEST(test("a", x3::seek[x3::eoi]));
  28. BOOST_TEST(test(" a", x3::seek[x3::eoi], x3::space));
  29. }
  30. // test literal finding
  31. {
  32. int i = 0;
  33. BOOST_TEST(
  34. test_attr("!@#$%^&*KEY:123", x3::seek["KEY:"] >> x3::int_, i)
  35. && i == 123
  36. );
  37. }
  38. // test sequence finding
  39. {
  40. int i = 0;
  41. BOOST_TEST(
  42. test_attr("!@#$%^&* KEY : 123", x3::seek[x3::lit("KEY") >> ':'] >> x3::int_, i, x3::space)
  43. && i == 123
  44. );
  45. }
  46. // test attr finding
  47. {
  48. std::vector<int> v;
  49. BOOST_TEST( // expect partial match
  50. test_attr("a06b78c3d", +x3::seek[x3::int_], v, false)
  51. && v.size() == 3 && v[0] == 6 && v[1] == 78 && v[2] == 3
  52. );
  53. }
  54. // test action
  55. {
  56. bool b = false;
  57. auto const action = [&b]() { b = true; };
  58. BOOST_TEST( // expect partial match
  59. test("abcdefg", x3::seek["def"][action], false)
  60. && b
  61. );
  62. }
  63. // test container
  64. {
  65. std::vector<int> v;
  66. BOOST_TEST(
  67. test_attr("abcInt:100Int:95Int:44", x3::seek[+("Int:" >> x3::int_)], v)
  68. && v.size() == 3 && v[0] == 100 && v[1] == 95 && v[2] == 44
  69. );
  70. }
  71. // test failure rollback
  72. {
  73. BOOST_TEST(test_failure("abcdefg", x3::seek[x3::int_]));
  74. }
  75. return boost::report_errors();
  76. }