rule2.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 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 <string>
  9. #include <cstring>
  10. #include <iostream>
  11. #include "test.hpp"
  12. int
  13. main()
  14. {
  15. using spirit_test::test_attr;
  16. using spirit_test::test;
  17. using namespace boost::spirit::x3::ascii;
  18. using boost::spirit::x3::rule;
  19. using boost::spirit::x3::lit;
  20. using boost::spirit::x3::unused_type;
  21. using boost::spirit::x3::_attr;
  22. { // context tests
  23. char ch;
  24. auto a = rule<class a, char>() = alpha;
  25. // this semantic action requires the context
  26. auto f = [&](auto& ctx){ ch = _attr(ctx); };
  27. BOOST_TEST(test("x", a[f]));
  28. BOOST_TEST(ch == 'x');
  29. // this semantic action requires the (unused) context
  30. auto f2 = [&](auto&){ ch = 'y'; };
  31. BOOST_TEST(test("x", a[f2]));
  32. BOOST_TEST(ch == 'y');
  33. // the semantic action may optionally not have any arguments at all
  34. auto f3 = [&]{ ch = 'z'; };
  35. BOOST_TEST(test("x", a[f3]));
  36. BOOST_TEST(ch == 'z');
  37. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  38. BOOST_TEST(ch == 'z');
  39. }
  40. { // auto rules tests
  41. char ch = '\0';
  42. auto a = rule<class a, char>() = alpha;
  43. auto f = [&](auto& ctx){ ch = _attr(ctx); };
  44. BOOST_TEST(test("x", a[f]));
  45. BOOST_TEST(ch == 'x');
  46. ch = '\0';
  47. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  48. BOOST_TEST(ch == 'z');
  49. ch = '\0';
  50. BOOST_TEST(test("x", a[f]));
  51. BOOST_TEST(ch == 'x');
  52. ch = '\0';
  53. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  54. BOOST_TEST(ch == 'z');
  55. }
  56. { // auto rules tests: allow stl containers as attributes to
  57. // sequences (in cases where attributes of the elements
  58. // are convertible to the value_type of the container or if
  59. // the element itself is an stl container with value_type
  60. // that is convertible to the value_type of the attribute).
  61. std::string s;
  62. auto f = [&](auto& ctx){ s = _attr(ctx); };
  63. {
  64. auto r = rule<class r, std::string>()
  65. = char_ >> *(',' >> char_)
  66. ;
  67. BOOST_TEST(test("a,b,c,d,e,f", r[f]));
  68. BOOST_TEST(s == "abcdef");
  69. }
  70. {
  71. auto r = rule<class r, std::string>()
  72. = char_ >> *(',' >> char_);
  73. s.clear();
  74. BOOST_TEST(test("a,b,c,d,e,f", r[f]));
  75. BOOST_TEST(s == "abcdef");
  76. }
  77. {
  78. auto r = rule<class r, std::string>()
  79. = char_ >> char_ >> char_ >> char_ >> char_ >> char_;
  80. s.clear();
  81. BOOST_TEST(test("abcdef", r[f]));
  82. BOOST_TEST(s == "abcdef");
  83. }
  84. }
  85. return boost::report_errors();
  86. }