raw.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/home/x3.hpp>
  8. #include <boost/fusion/include/std_pair.hpp>
  9. #include <iostream>
  10. #include <string>
  11. #include "test.hpp"
  12. int main()
  13. {
  14. using spirit_test::test;
  15. using spirit_test::test_attr;
  16. using namespace boost::spirit::x3::ascii;
  17. using boost::spirit::x3::raw;
  18. using boost::spirit::x3::eps;
  19. using boost::spirit::x3::lit;
  20. using boost::spirit::x3::_attr;
  21. using boost::spirit::x3::parse;
  22. using boost::spirit::x3::int_;
  23. using boost::spirit::x3::char_;
  24. {
  25. boost::iterator_range<char const*> range;
  26. std::string str;
  27. BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
  28. BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
  29. BOOST_TEST((test_attr(" spirit", raw[*alpha], range, space)));
  30. BOOST_TEST((range.size() == 6));
  31. }
  32. {
  33. std::string str;
  34. BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
  35. BOOST_TEST((str == "spirit_test_123"));
  36. }
  37. {
  38. boost::iterator_range<char const*> range;
  39. BOOST_TEST((test("x", raw[alpha])));
  40. BOOST_TEST((test_attr("x", raw[alpha], range)));
  41. }
  42. {
  43. boost::iterator_range<char const*> range;
  44. BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
  45. BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
  46. }
  47. {
  48. boost::iterator_range<char const*> range;
  49. BOOST_TEST((test("x123x", lit('x') >> raw[+digit] >> lit('x'))));
  50. BOOST_TEST((test_attr("x123x", lit('x') >> raw[+digit] >> lit('x'), range)));
  51. BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
  52. }
  53. {
  54. using range = boost::iterator_range<std::string::iterator>;
  55. boost::variant<int, range> attr;
  56. std::string str("test");
  57. parse(str.begin(), str.end(), (int_ | raw[*char_]), attr);
  58. auto rng = boost::get<range>(attr);
  59. BOOST_TEST(std::string(rng.begin(), rng.end()) == "test");
  60. }
  61. {
  62. std::vector<boost::iterator_range<std::string::iterator>> attr;
  63. std::string str("123abcd");
  64. parse(str.begin(), str.end()
  65. , (raw[int_] >> raw[*char_])
  66. , attr
  67. );
  68. BOOST_TEST(attr.size() == 2);
  69. BOOST_TEST(std::string(attr[0].begin(), attr[0].end()) == "123");
  70. BOOST_TEST(std::string(attr[1].begin(), attr[1].end()) == "abcd");
  71. }
  72. {
  73. std::pair<int, boost::iterator_range<std::string::iterator>> attr;
  74. std::string str("123abcd");
  75. parse(str.begin(), str.end()
  76. , (int_ >> raw[*char_])
  77. , attr
  78. );
  79. BOOST_TEST(attr.first == 123);
  80. BOOST_TEST(std::string(attr.second.begin(), attr.second.end()) == "abcd");
  81. }
  82. return boost::report_errors();
  83. }