symbols3.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*=============================================================================
  2. Copyright (c) 2013 Carl Barron
  3. Copyright (c) 2015 Joel de Guzman
  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 <boost/fusion/include/at.hpp>
  10. #include <boost/fusion/include/vector.hpp>
  11. #include <boost/fusion/include/adapt_struct.hpp>
  12. #include <boost/mpl/int.hpp>
  13. #include <boost/optional.hpp>
  14. #include <iostream>
  15. #include <numeric>
  16. #include <vector>
  17. #include "test.hpp"
  18. struct roman
  19. {
  20. boost::optional<int> a;
  21. boost::optional<int> b;
  22. boost::optional<int> c;
  23. };
  24. BOOST_FUSION_ADAPT_STRUCT(roman,
  25. a, b, c
  26. )
  27. int eval(roman const & c)
  28. {
  29. return c.a.get_value_or(0) + c.b.get_value_or(0) + c.c.get_value_or(0);
  30. }
  31. int
  32. main()
  33. {
  34. using spirit_test::test;
  35. using spirit_test::test_attr;
  36. using boost::spirit::x3::symbols;
  37. { // construction from initializer-list
  38. symbols<int> const ones =
  39. {
  40. {"I", 1}, {"II", 2}, {"III", 3}, {"IV", 4},
  41. {"V", 5}, {"VI", 6}, {"VII", 7}, {"VIII", 8},
  42. {"IX", 9}
  43. };
  44. symbols<int> const tens =
  45. {
  46. {"X", 10}, {"XX", 20}, {"XXX", 30}, {"XL", 40},
  47. {"L", 50}, {"LX", 60}, {"LXX", 70}, {"LXXX", 80},
  48. {"XC", 90}
  49. };
  50. symbols<int> const hundreds
  51. {
  52. {"C", 100}, {"CC", 200}, {"CCC", 300}, {"CD", 400},
  53. {"D", 500}, {"DC", 600}, {"DCC", 700}, {"DCCC", 800},
  54. {"CM", 900}
  55. };
  56. auto number = -hundreds >> -tens >> -ones;
  57. roman r;
  58. BOOST_TEST((test_attr("CDXLII", number, r)));
  59. BOOST_TEST(eval(r) == 442);
  60. }
  61. { // construction from initializer-list without attribute
  62. symbols<> foo = {"a1", "a2", "a3"};
  63. BOOST_TEST((test("a3", foo)));
  64. }
  65. { // assignment from initializer-list
  66. symbols<> foo;
  67. foo = {"a1", "a2", "a3"};
  68. BOOST_TEST((test("a3", foo)));
  69. }
  70. return boost::report_errors();
  71. }