any_parser.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 Joel de Guzman
  3. Copyright (c) 2013-2014 Agustin Berge
  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 <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/home/x3.hpp>
  9. #include <string>
  10. #include <cstring>
  11. #include <iostream>
  12. #include "test.hpp"
  13. int
  14. main()
  15. {
  16. using spirit_test::test_attr;
  17. using spirit_test::test;
  18. using namespace boost::spirit::x3::ascii;
  19. using boost::spirit::x3::any_parser;
  20. using boost::spirit::x3::make_context;
  21. using boost::spirit::x3::lit;
  22. using boost::spirit::x3::unused_type;
  23. using boost::spirit::x3::phrase_parse;
  24. using boost::spirit::x3::skip_flag;
  25. using boost::spirit::x3::skipper_tag;
  26. using boost::spirit::x3::_attr;
  27. typedef char const* iterator_type;
  28. typedef decltype(make_context<skipper_tag>(space)) context_type;
  29. { // basic tests
  30. auto a = lit('a');
  31. auto b = lit('b');
  32. auto c = lit('c');
  33. {
  34. any_parser<iterator_type> start =
  35. *(a | b | c);
  36. BOOST_TEST(test("abcabcacb", start));
  37. }
  38. }
  39. { // basic tests w/ skipper
  40. auto a = lit('a');
  41. auto b = lit('b');
  42. auto c = lit('c');
  43. {
  44. any_parser<iterator_type, unused_type, context_type> start =
  45. *(a | b | c);
  46. BOOST_TEST(test(" a b c a b c a c b ", start, space));
  47. }
  48. }
  49. { // basic tests w/ skipper but no final post-skip
  50. any_parser<iterator_type, unused_type, context_type> a = lit('a');
  51. any_parser<iterator_type, unused_type, context_type> b = lit('b');
  52. any_parser<iterator_type, unused_type, context_type> c = lit('c');
  53. {
  54. any_parser<iterator_type, unused_type, context_type> start = *(a | b) >> c;
  55. char const *s1 = " a b a a b b a c ... "
  56. , *const e1 = s1 + std::strlen(s1);
  57. BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::dont_post_skip)
  58. && s1 == e1 - 5);
  59. }
  60. }
  61. { // context tests
  62. char ch;
  63. any_parser<iterator_type, char> a = alpha;
  64. // this semantic action requires both the context and attribute
  65. //!!auto f = [&](auto&, char attr){ ch = attr; };
  66. //!!BOOST_TEST(test("x", a[f]));
  67. //!!BOOST_TEST(ch == 'x');
  68. // the semantic action may have the context passed
  69. auto f2 = [&](auto&){ ch = 'y'; };
  70. BOOST_TEST(test("x", a[f2]));
  71. BOOST_TEST(ch == 'y');
  72. // the semantic action may optionally not have any arguments at all
  73. auto f3 = [&]{ ch = 'z'; };
  74. BOOST_TEST(test("x", a[f3]));
  75. BOOST_TEST(ch == 'z');
  76. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  77. BOOST_TEST(ch == 'z');
  78. }
  79. { // auto rules tests
  80. char ch = '\0';
  81. any_parser<iterator_type, char> a = alpha;
  82. auto f = [&](auto& ctx){ ch = _attr(ctx); };
  83. BOOST_TEST(test("x", a[f]));
  84. BOOST_TEST(ch == 'x');
  85. ch = '\0';
  86. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  87. BOOST_TEST(ch == 'z');
  88. ch = '\0';
  89. BOOST_TEST(test("x", a[f]));
  90. BOOST_TEST(ch == 'x');
  91. ch = '\0';
  92. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  93. BOOST_TEST(ch == 'z');
  94. }
  95. { // auto rules tests: allow stl containers as attributes to
  96. // sequences (in cases where attributes of the elements
  97. // are convertible to the value_type of the container or if
  98. // the element itself is an stl container with value_type
  99. // that is convertible to the value_type of the attribute).
  100. std::string s;
  101. auto f = [&](auto& ctx){ s = _attr(ctx); };
  102. {
  103. any_parser<iterator_type, std::string> r
  104. = char_ >> *(',' >> char_)
  105. ;
  106. BOOST_TEST(test("a,b,c,d,e,f", r[f]));
  107. BOOST_TEST(s == "abcdef");
  108. }
  109. {
  110. any_parser<iterator_type, std::string> r
  111. = char_ >> *(',' >> char_);
  112. s.clear();
  113. BOOST_TEST(test("a,b,c,d,e,f", r[f]));
  114. BOOST_TEST(s == "abcdef");
  115. }
  116. {
  117. any_parser<iterator_type, std::string> r
  118. = char_ >> char_ >> char_ >> char_ >> char_ >> char_;
  119. s.clear();
  120. BOOST_TEST(test("abcdef", r[f]));
  121. BOOST_TEST(s == "abcdef");
  122. }
  123. }
  124. return boost::report_errors();
  125. }