rule3.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*=============================================================================
  2. Copyright (c) 2001-2012 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/std_pair.hpp>
  9. #include <string>
  10. #include <cstring>
  11. #include <iostream>
  12. #include "test.hpp"
  13. using boost::spirit::x3::_val;
  14. struct f
  15. {
  16. template <typename Context>
  17. void operator()(Context const& ctx) const
  18. {
  19. _val(ctx) += _attr(ctx);
  20. }
  21. };
  22. struct stationary : boost::noncopyable
  23. {
  24. explicit stationary(int i) : val{i} {}
  25. stationary& operator=(int i) { val = i; return *this; }
  26. int val;
  27. };
  28. namespace check_stationary {
  29. boost::spirit::x3::rule<class a_r, stationary> const a;
  30. boost::spirit::x3::rule<class b_r, stationary> const b;
  31. auto const a_def = '{' >> boost::spirit::x3::int_ >> '}';
  32. auto const b_def = a;
  33. BOOST_SPIRIT_DEFINE(a, b)
  34. }
  35. int main()
  36. {
  37. using spirit_test::test_attr;
  38. using spirit_test::test;
  39. using namespace boost::spirit::x3::ascii;
  40. using boost::spirit::x3::rule;
  41. using boost::spirit::x3::lit;
  42. using boost::spirit::x3::eps;
  43. using boost::spirit::x3::unused_type;
  44. { // synth attribute value-init
  45. std::string s;
  46. typedef rule<class r, std::string> rule_type;
  47. auto rdef = rule_type()
  48. = alpha [f()]
  49. ;
  50. BOOST_TEST(test_attr("abcdef", +rdef, s));
  51. BOOST_TEST(s == "abcdef");
  52. }
  53. { // synth attribute value-init
  54. std::string s;
  55. typedef rule<class r, std::string> rule_type;
  56. auto rdef = rule_type() =
  57. alpha /
  58. [](auto& ctx)
  59. {
  60. _val(ctx) += _attr(ctx);
  61. }
  62. ;
  63. BOOST_TEST(test_attr("abcdef", +rdef, s));
  64. BOOST_TEST(s == "abcdef");
  65. }
  66. {
  67. auto r = rule<class r, int>{} = eps[([] (auto& ctx) {
  68. using boost::spirit::x3::_val;
  69. static_assert(std::is_same<std::decay_t<decltype(_val(ctx))>, unused_type>::value,
  70. "Attribute must not be synthesized");
  71. })];
  72. BOOST_TEST(test("", r));
  73. }
  74. { // ensure no unneded synthesization, copying and moving occured
  75. stationary st { 0 };
  76. BOOST_TEST(test_attr("{42}", check_stationary::b, st));
  77. BOOST_TEST_EQ(st.val, 42);
  78. }
  79. return boost::report_errors();
  80. }