regression_wide.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2010 Sergey "GooRoo" Olendarenko
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/config/warning_disable.hpp>
  8. #include <cstdlib>
  9. #include <iostream>
  10. #include <locale>
  11. #include <string>
  12. #include <boost/spirit/include/lex_lexertl.hpp>
  13. #include <boost/spirit/include/phoenix_function.hpp>
  14. #include <boost/spirit/include/phoenix_operator.hpp>
  15. namespace lex = boost::spirit::lex;
  16. namespace phoenix = boost::phoenix;
  17. typedef std::basic_string<wchar_t> wstring_type;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. enum tokenids
  20. {
  21. ID_IDENT = 1,
  22. ID_CONSTANT,
  23. ID_OPERATION,
  24. ID_BRACKET
  25. };
  26. ///////////////////////////////////////////////////////////////////////////////
  27. struct test_data
  28. {
  29. tokenids tokenid;
  30. wstring_type value;
  31. };
  32. // alpha+x1*(2.836-x2[i])
  33. test_data data[] =
  34. {
  35. { ID_IDENT, L"alpha" },
  36. { ID_OPERATION, L"+" },
  37. { ID_IDENT, L"x1" },
  38. { ID_OPERATION, L"*" },
  39. { ID_BRACKET, L"(" },
  40. { ID_CONSTANT, L"2.836" },
  41. { ID_OPERATION, L"-" },
  42. { ID_IDENT, L"x2" },
  43. { ID_BRACKET, L"[" },
  44. { ID_IDENT, L"i" },
  45. { ID_BRACKET, L"]" },
  46. { ID_BRACKET, L")" }
  47. };
  48. ///////////////////////////////////////////////////////////////////////////////
  49. struct test_impl
  50. {
  51. typedef void result_type;
  52. template <typename TokenId, typename Value>
  53. struct result { typedef void type; };
  54. template <typename TokenId, typename Value>
  55. void operator()(TokenId const& tokenid, Value const& val) const
  56. {
  57. BOOST_TEST(sequence_counter < sizeof(data)/sizeof(data[0]));
  58. BOOST_TEST(data[sequence_counter].tokenid == tokenids(tokenid));
  59. BOOST_TEST(0 == val.which());
  60. typedef boost::iterator_range<wstring_type::iterator> iterator_range;
  61. iterator_range r = boost::get<iterator_range>(val);
  62. BOOST_TEST(data[sequence_counter].value ==
  63. wstring_type(r.begin(), r.end()));
  64. ++sequence_counter;
  65. }
  66. static std::size_t sequence_counter;
  67. };
  68. std::size_t test_impl::sequence_counter = 0;
  69. phoenix::function<test_impl> const test = test_impl();
  70. ///////////////////////////////////////////////////////////////////////////////
  71. template <typename Lexer>
  72. struct mega_tokens : lex::lexer<Lexer>
  73. {
  74. mega_tokens()
  75. : identifier(L"[a-zA-Z_][a-zA-Z0-9_]*", ID_IDENT)
  76. , constant (L"[0-9]+(\\.[0-9]+)?", ID_CONSTANT)
  77. , operation (L"[\\+\\-\\*/]", ID_OPERATION)
  78. , bracket (L"[\\(\\)\\[\\]]", ID_BRACKET)
  79. {
  80. using lex::_tokenid;
  81. using lex::_val;
  82. this->self
  83. = operation [ test(_tokenid, _val) ]
  84. | identifier [ test(_tokenid, _val) ]
  85. | constant [ test(_tokenid, _val) ]
  86. | bracket [ test(_tokenid, _val) ]
  87. ;
  88. }
  89. lex::token_def<wstring_type, wchar_t, tokenids> identifier;
  90. lex::token_def<double, wchar_t, tokenids> constant;
  91. lex::token_def<wchar_t, wchar_t, tokenids> operation;
  92. lex::token_def<wchar_t, wchar_t, tokenids> bracket;
  93. };
  94. ///////////////////////////////////////////////////////////////////////////////
  95. int main()
  96. {
  97. typedef wstring_type::iterator base_iterator;
  98. typedef lex::lexertl::token<
  99. base_iterator, boost::mpl::vector<wchar_t, wstring_type, double>
  100. , boost::mpl::true_, tokenids
  101. > token_type;
  102. typedef lex::lexertl::actor_lexer<token_type> lexer_type;
  103. mega_tokens<lexer_type> mega_lexer;
  104. wstring_type exampleStr = L"alpha+x1*(2.836-x2[i])";
  105. base_iterator first = exampleStr.begin();
  106. BOOST_TEST(lex::tokenize(first, exampleStr.end(), mega_lexer));
  107. BOOST_TEST(test_impl::sequence_counter == sizeof(data)/sizeof(data[0]));
  108. return boost::report_errors();
  109. }