set_token_value_phoenix.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2009 Carl Barron
  2. //
  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. #include <string>
  6. #include <iostream>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/lex.hpp>
  9. #include <boost/spirit/include/lex_lexertl.hpp>
  10. #include <boost/spirit/include/phoenix_core.hpp>
  11. #include <boost/spirit/include/phoenix_function.hpp>
  12. namespace lex = boost::spirit::lex;
  13. namespace phoenix = boost::phoenix;
  14. ///////////////////////////////////////////////////////////////////////////////
  15. struct square_impl
  16. {
  17. template <class>
  18. struct result { typedef int type; };
  19. template <class A>
  20. int operator () (const A &x) const
  21. { return (x) * (x); }
  22. };
  23. phoenix::function<square_impl> const square = square_impl();
  24. ///////////////////////////////////////////////////////////////////////////////
  25. template <class Lexer>
  26. struct test_tokens : lex::lexer<Lexer>
  27. {
  28. test_tokens()
  29. {
  30. a = "a";
  31. this->self = a [lex::_val = square(*lex::_start)];
  32. }
  33. lex::token_def<int> a;
  34. };
  35. struct catch_result
  36. {
  37. template <class Token>
  38. bool operator() (Token const& x) const
  39. {
  40. BOOST_TEST(x.value().which() == 1);
  41. BOOST_TEST(boost::get<int>(x.value()) == 9409); // 9409 == 'a' * 'a'
  42. return true;
  43. }
  44. };
  45. ///////////////////////////////////////////////////////////////////////////////
  46. int main()
  47. {
  48. typedef lex::lexertl::token<std::string::iterator
  49. , boost::mpl::vector<int> > token_type;
  50. std::string in = "a";
  51. std::string::iterator first(in.begin());
  52. test_tokens<lex::lexertl::actor_lexer<token_type> > the_lexer;
  53. BOOST_TEST(lex::tokenize(first, in.end(), the_lexer, catch_result()));
  54. return boost::report_errors();
  55. }