optional_ast_node.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. Duzy Chan:
  6. This test addresses the usage of boost::optional<foo> as an ast node.
  7. =============================================================================*/
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/home/x3.hpp>
  10. #include <boost/fusion/adapted/struct.hpp>
  11. #include <boost/fusion/include/vector.hpp>
  12. #include <iostream>
  13. #include "test.hpp"
  14. #include "utils.hpp"
  15. struct twoints
  16. {
  17. int a;
  18. int b;
  19. };
  20. struct adata
  21. {
  22. twoints a;
  23. boost::optional<twoints> b;
  24. twoints c;
  25. };
  26. BOOST_FUSION_ADAPT_STRUCT(twoints, a, b)
  27. BOOST_FUSION_ADAPT_STRUCT(adata, a, b, c)
  28. int
  29. main()
  30. {
  31. {
  32. // Duzy Chan: This case addresses the usage of boost::optional<foo>
  33. // as an ast node. Which should actually test the ability of
  34. // boost::spirit::x3::traits::move_to to handle with optional source
  35. // value.
  36. boost::spirit::x3::rule<class twoints, adata> top = "top";
  37. boost::spirit::x3::rule<class twoints, boost::optional<twoints>>
  38. twoints = "twoints";
  39. using boost::spirit::x3::int_;
  40. auto const top_def = twoints >> ',' >> -twoints >> ',' >> twoints;
  41. auto const twoints_def = int_ >> int_;
  42. BOOST_SPIRIT_DEFINE(top, twoints);
  43. twoints a, b;
  44. BOOST_TEST((test_attr("1 2,3 4,5 6", top, a)));
  45. BOOST_TEST((a.a.a == 1 && a.a.b == 2));
  46. BOOST_TEST((a.b && a.b->a == 3 && a.b->b == 4));
  47. BOOST_TEST((a.c.a == 5 && a.c.b == 6));
  48. BOOST_TEST((test_attr("1 2,,5 6", top), b));
  49. BOOST_TEST((b.a.a == 1 && b.a.b == 2));
  50. BOOST_TEST((!a.b));
  51. BOOST_TEST((b.c.a == 5 && b.c.b == 6));
  52. }
  53. return boost::report_errors();
  54. }