actions.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. =============================================================================*/
  6. #include <boost/config/warning_disable.hpp>
  7. #include <boost/spirit/home/x3.hpp>
  8. #include <iostream>
  9. // Presented are various ways to attach semantic actions
  10. // * Using plain function pointer
  11. // * Using simple function object
  12. namespace client
  13. {
  14. namespace x3 = boost::spirit::x3;
  15. using x3::_attr;
  16. struct print_action
  17. {
  18. template <typename Context>
  19. void operator()(Context const& ctx) const
  20. {
  21. std::cout << _attr(ctx) << std::endl;
  22. }
  23. };
  24. }
  25. int main()
  26. {
  27. using boost::spirit::x3::int_;
  28. using boost::spirit::x3::parse;
  29. using client::print_action;
  30. { // example using function object
  31. char const *first = "{43}", *last = first + std::strlen(first);
  32. parse(first, last, '{' >> int_[print_action()] >> '}');
  33. }
  34. { // example using C++14 lambda
  35. using boost::spirit::x3::_attr;
  36. char const *first = "{44}", *last = first + std::strlen(first);
  37. auto f = [](auto& ctx){ std::cout << _attr(ctx) << std::endl; };
  38. parse(first, last, '{' >> int_[f] >> '}');
  39. }
  40. return 0;
  41. }