actions.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 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/config/warning_disable.hpp>
  7. #include <boost/spirit/include/qi.hpp>
  8. #include <boost/lambda/lambda.hpp>
  9. #include <boost/bind.hpp>
  10. #include <iostream>
  11. // Presented are various ways to attach semantic actions
  12. // * Using plain function pointer
  13. // * Using simple function object
  14. // * Using boost.bind with a plain function
  15. // * Using boost.bind with a member function
  16. // * Using boost.lambda
  17. //[tutorial_semantic_action_functions
  18. namespace client
  19. {
  20. namespace qi = boost::spirit::qi;
  21. // A plain function
  22. void print(int const& i)
  23. {
  24. std::cout << i << std::endl;
  25. }
  26. // A member function
  27. struct writer
  28. {
  29. void print(int const& i) const
  30. {
  31. std::cout << i << std::endl;
  32. }
  33. };
  34. // A function object
  35. struct print_action
  36. {
  37. void operator()(int const& i, qi::unused_type, qi::unused_type) const
  38. {
  39. std::cout << i << std::endl;
  40. }
  41. };
  42. }
  43. //]
  44. int main()
  45. {
  46. using boost::spirit::qi::int_;
  47. using boost::spirit::qi::parse;
  48. using client::print;
  49. using client::writer;
  50. using client::print_action;
  51. { // example using plain function
  52. char const *first = "{42}", *last = first + std::strlen(first);
  53. //[tutorial_attach_actions1
  54. parse(first, last, '{' >> int_[&print] >> '}');
  55. //]
  56. }
  57. { // example using simple function object
  58. char const *first = "{43}", *last = first + std::strlen(first);
  59. //[tutorial_attach_actions2
  60. parse(first, last, '{' >> int_[print_action()] >> '}');
  61. //]
  62. }
  63. { // example using boost.bind with a plain function
  64. char const *first = "{44}", *last = first + std::strlen(first);
  65. //[tutorial_attach_actions3
  66. parse(first, last, '{' >> int_[boost::bind(&print, _1)] >> '}');
  67. //]
  68. }
  69. { // example using boost.bind with a member function
  70. char const *first = "{44}", *last = first + std::strlen(first);
  71. //[tutorial_attach_actions4
  72. writer w;
  73. parse(first, last, '{' >> int_[boost::bind(&writer::print, &w, _1)] >> '}');
  74. //]
  75. }
  76. { // example using boost.lambda
  77. namespace lambda = boost::lambda;
  78. char const *first = "{45}", *last = first + std::strlen(first);
  79. using lambda::_1;
  80. //[tutorial_attach_actions5
  81. parse(first, last, '{' >> int_[std::cout << _1 << '\n'] >> '}');
  82. //]
  83. }
  84. return 0;
  85. }