omit.cpp 4.0 KB

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