confix.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*=============================================================================
  2. Copyright (c) 2009 Chris Hoeppler
  3. Copyright (c) 2014 Lee Clagett
  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/char.hpp>
  9. #include <boost/spirit/home/x3/core.hpp>
  10. #include <boost/spirit/home/x3/numeric.hpp>
  11. #include <boost/spirit/home/x3/operator.hpp>
  12. #include <boost/spirit/home/x3/string.hpp>
  13. #include <boost/spirit/home/x3/directive/confix.hpp>
  14. #include "test.hpp"
  15. int main()
  16. {
  17. namespace x3 = boost::spirit::x3;
  18. using namespace spirit_test;
  19. {
  20. const auto comment = x3::confix("/*", "*/");
  21. BOOST_TEST(test_failure("/abcdef*/", comment["abcdef"]));
  22. BOOST_TEST(test_failure("/* abcdef*/", comment["abcdef"]));
  23. BOOST_TEST(test_failure("/*abcdef */", comment["abcdef"]));
  24. BOOST_TEST(test("/*abcdef*/", comment["abcdef"]));
  25. {
  26. unsigned value = 0;
  27. BOOST_TEST(
  28. test_attr(" /* 123 */ ", comment[x3::uint_], value, x3::space));
  29. BOOST_TEST(value == 123);
  30. using x3::_attr;
  31. value = 0;
  32. const auto lambda = [&value](auto& ctx ){ value = _attr(ctx) + 1; };
  33. BOOST_TEST(test_attr("/*123*/", comment[x3::uint_][lambda], value));
  34. BOOST_TEST(value == 124);
  35. }
  36. }
  37. {
  38. const auto array = x3::confix('[', ']');
  39. {
  40. std::vector<unsigned> values;
  41. BOOST_TEST(test("[0,2,4,6,8]", array[x3::uint_ % ',']));
  42. BOOST_TEST(test_attr("[0,2,4,6,8]", array[x3::uint_ % ','], values));
  43. BOOST_TEST(
  44. values.size() == 5 &&
  45. values[0] == 0 &&
  46. values[1] == 2 &&
  47. values[2] == 4 &&
  48. values[3] == 6 &&
  49. values[4] == 8);
  50. }
  51. {
  52. std::vector<std::vector<unsigned>> values;
  53. BOOST_TEST(
  54. test("[[1,3,5],[0,2,4]]", array[array[x3::uint_ % ','] % ',']));
  55. BOOST_TEST(
  56. test_attr(
  57. "[[1,3,5],[0,2,4]]",
  58. array[array[x3::uint_ % ','] % ','],
  59. values));
  60. BOOST_TEST(
  61. values.size() == 2 &&
  62. values[0].size() == 3 &&
  63. values[0][0] == 1 &&
  64. values[0][1] == 3 &&
  65. values[0][2] == 5 &&
  66. values[1].size() == 3 &&
  67. values[1][0] == 0 &&
  68. values[1][1] == 2 &&
  69. values[1][2] == 4);
  70. }
  71. }
  72. return boost::report_errors();
  73. }