rule1.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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::int_;
  21. using boost::spirit::x3::unused_type;
  22. using boost::spirit::x3::phrase_parse;
  23. using boost::spirit::x3::skip_flag;
  24. using boost::spirit::x3::traits::has_attribute;
  25. // check attribute advertising
  26. static_assert( has_attribute<rule<class r, int>, /*Context=*/unused_type>::value, "");
  27. static_assert(!has_attribute<rule<class r >, /*Context=*/unused_type>::value, "");
  28. static_assert( has_attribute<decltype(rule<class r, int>{} = int_), /*Context=*/unused_type>::value, "");
  29. static_assert(!has_attribute<decltype(rule<class r >{} = int_), /*Context=*/unused_type>::value, "");
  30. { // basic tests
  31. auto a = lit('a');
  32. auto b = lit('b');
  33. auto c = lit('c');
  34. rule<class r> r;
  35. {
  36. auto start =
  37. r = *(a | b | c);
  38. BOOST_TEST(test("abcabcacb", start));
  39. }
  40. {
  41. auto start =
  42. r = (a | b) >> (r | b);
  43. BOOST_TEST(test("aaaabababaaabbb", start));
  44. BOOST_TEST(test("aaaabababaaabba", start, false));
  45. // ignore the skipper!
  46. BOOST_TEST(test("aaaabababaaabba", start, space, false));
  47. }
  48. }
  49. { // basic tests w/ skipper
  50. auto a = lit('a');
  51. auto b = lit('b');
  52. auto c = lit('c');
  53. rule<class r> r;
  54. {
  55. auto start =
  56. r = *(a | b | c);
  57. BOOST_TEST(test(" a b c a b c a c b ", start, space));
  58. }
  59. {
  60. auto start =
  61. r = (a | b) >> (r | b);
  62. BOOST_TEST(test(" a a a a b a b a b a a a b b b ", start, space));
  63. BOOST_TEST(test(" a a a a b a b a b a a a b b a ", start, space, false));
  64. }
  65. }
  66. { // basic tests w/ skipper but no final post-skip
  67. auto a = rule<class a>()
  68. = lit('a');
  69. auto b = rule<class b>()
  70. = lit('b');
  71. auto c = rule<class c>()
  72. = lit('c');
  73. {
  74. auto start = rule<class start>() = *(a | b) >> c;
  75. char const *s1 = " a b a a b b a c ... "
  76. , *const e1 = s1 + std::strlen(s1);
  77. BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::dont_post_skip)
  78. && s1 == e1 - 5);
  79. }
  80. {
  81. rule<class start> start;
  82. auto p =
  83. start = (a | b) >> (start | c);
  84. {
  85. char const *s1 = " a a a a b a b a b a a a b b b c "
  86. , *const e1 = s1 + std::strlen(s1);
  87. BOOST_TEST(phrase_parse(s1, e1, p, space, skip_flag::post_skip)
  88. && s1 == e1);
  89. }
  90. {
  91. char const *s1 = " a a a a b a b a b a a a b b b c "
  92. , *const e1 = s1 + std::strlen(s1);
  93. BOOST_TEST(phrase_parse(s1, e1, p, space, skip_flag::dont_post_skip)
  94. && s1 == e1 - 1);
  95. }
  96. }
  97. }
  98. return boost::report_errors();
  99. }