actions.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #if defined(_MSC_VER)
  7. # pragma warning(disable: 4180) // qualifier applied to function type
  8. // has no meaning; ignored
  9. #endif
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/detail/workaround.hpp>
  12. #include <boost/spirit/include/qi_operator.hpp>
  13. #include <boost/spirit/include/qi_numeric.hpp>
  14. #include <boost/spirit/include/qi_char.hpp>
  15. #include <boost/spirit/include/qi_parse.hpp>
  16. #include <boost/spirit/include/qi_action.hpp>
  17. #include <boost/lambda/lambda.hpp>
  18. #include <boost/bind.hpp>
  19. #include <cstring>
  20. int x = 0;
  21. void fun1(int const& i)
  22. {
  23. x += i;
  24. }
  25. void fun2(int i)
  26. {
  27. x += i;
  28. }
  29. using boost::spirit::unused_type;
  30. struct fun_action
  31. {
  32. void operator()(int const& i, unused_type, unused_type) const
  33. {
  34. x += i;
  35. }
  36. };
  37. void fail (int, boost::spirit::unused_type, bool& pass)
  38. {
  39. pass = false;
  40. }
  41. struct setnext
  42. {
  43. setnext(char& next) : next(next) {}
  44. void operator()(char c, unused_type, unused_type) const
  45. {
  46. next = c;
  47. }
  48. char& next;
  49. };
  50. int main()
  51. {
  52. namespace qi = boost::spirit::qi;
  53. using boost::spirit::int_;
  54. {
  55. char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
  56. qi::parse(s1, e1, '{' >> int_[&fun1] >> '}');
  57. }
  58. {
  59. char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
  60. qi::parse(s1, e1, '{' >> int_[&fun2] >> '}');
  61. }
  62. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1400)
  63. {
  64. char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
  65. qi::parse(s1, e1, '{' >> int_[fun2] >> '}');
  66. }
  67. #else
  68. x += 42; // compensate for missing test case
  69. #endif
  70. {
  71. char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
  72. qi::parse(s1, e1, '{' >> int_[fun_action()] >> '}');
  73. }
  74. {
  75. char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
  76. qi::parse(s1, e1, '{' >> int_[boost::bind(&fun1, _1)] >> '}');
  77. }
  78. {
  79. namespace lambda = boost::lambda;
  80. char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
  81. qi::parse(s1, e1, '{' >> int_[lambda::var(x) += lambda::_1] >> '}');
  82. }
  83. BOOST_TEST(x == (42*6));
  84. {
  85. std::string input("1234 6543");
  86. char next = '\0';
  87. BOOST_TEST(qi::phrase_parse(input.begin(), input.end(),
  88. qi::int_[fail] | qi::digit[setnext(next)] , qi::space));
  89. BOOST_TEST(next == '1');
  90. }
  91. return boost::report_errors();
  92. }