omit.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <boost/fusion/include/vector.hpp>
  9. #include <boost/fusion/include/at.hpp>
  10. #include <string>
  11. #include <iostream>
  12. #include "test.hpp"
  13. int
  14. main()
  15. {
  16. using namespace boost::spirit::x3::ascii;
  17. using boost::spirit::x3::omit;
  18. using boost::spirit::x3::unused_type;
  19. using boost::spirit::x3::unused;
  20. using boost::spirit::x3::int_;
  21. using boost::fusion::vector;
  22. using boost::fusion::at_c;
  23. using spirit_test::test;
  24. using spirit_test::test_attr;
  25. {
  26. BOOST_TEST(test("a", omit['a']));
  27. }
  28. {
  29. // omit[] means we don't receive the attribute
  30. char attr;
  31. BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> char_, attr)));
  32. BOOST_TEST((attr == 'c'));
  33. }
  34. {
  35. // If all elements except 1 is omitted, the attribute is
  36. // a single-element sequence. For this case alone, we allow
  37. // naked attributes (unwrapped in a fusion sequence).
  38. char attr;
  39. BOOST_TEST((test_attr("abc", omit[char_] >> 'b' >> char_, attr)));
  40. BOOST_TEST((attr == 'c'));
  41. }
  42. {
  43. // omit[] means we don't receive the attribute
  44. vector<> attr;
  45. BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> omit[char_], attr)));
  46. }
  47. {
  48. // omit[] means we don't receive the attribute
  49. // this test is merely a compile test, because using a unused as the
  50. // explicit attribute doesn't make any sense
  51. unused_type attr;
  52. BOOST_TEST((test_attr("abc", omit[char_ >> 'b' >> char_], attr)));
  53. }
  54. {
  55. // omit[] means we don't receive the attribute, if all elements of a
  56. // sequence have unused attributes, the whole sequence has an unused
  57. // attribute as well
  58. vector<char, char> attr;
  59. BOOST_TEST((test_attr("abcde",
  60. char_ >> (omit[char_] >> omit['c'] >> omit[char_]) >> char_, attr)));
  61. BOOST_TEST((at_c<0>(attr) == 'a'));
  62. BOOST_TEST((at_c<1>(attr) == 'e'));
  63. }
  64. {
  65. // "hello" has an unused_type. unused attrubutes are not part of the sequence
  66. vector<char, char> attr;
  67. BOOST_TEST((test_attr("a hello c", char_ >> "hello" >> char_, attr, space)));
  68. BOOST_TEST((at_c<0>(attr) == 'a'));
  69. BOOST_TEST((at_c<1>(attr) == 'c'));
  70. }
  71. {
  72. // if only one node in a sequence is left (all the others are omitted),
  73. // then we need "naked" attributes (not wraped in a tuple)
  74. int attr;
  75. BOOST_TEST((test_attr("a 123 c", omit['a'] >> int_ >> omit['c'], attr, space)));
  76. BOOST_TEST((attr == 123));
  77. }
  78. {
  79. // unused means we don't care about the attribute
  80. BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, unused)));
  81. }
  82. { // test action with omitted attribute
  83. char c = 0;
  84. auto f = [&](auto& ctx){ c = _attr(ctx); };
  85. BOOST_TEST(test("x123\"a string\"", (char_ >> omit[int_] >> "\"a string\"")[f]));
  86. BOOST_TEST(c == 'x');
  87. }
  88. { // test action with omitted attribute
  89. int n = 0;
  90. auto f = [&](auto& ctx){ n = _attr(ctx); };
  91. BOOST_TEST(test("x 123 \"a string\"", (omit[char_] >> int_ >> "\"a string\"")[f], space));
  92. BOOST_TEST(n == 123);
  93. }
  94. return boost::report_errors();
  95. }