lexertl2.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  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 <boost/detail/lightweight_test.hpp>
  6. #include <boost/spirit/include/lex_lexertl.hpp>
  7. #include <string>
  8. #include "test.hpp"
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // a simple lexer class
  11. template <typename Lexer>
  12. struct lexertl_test
  13. : boost::spirit::lex::lexer<Lexer>
  14. {
  15. typedef boost::spirit::lex::token_def<std::string> token_def;
  16. static std::size_t const CCOMMENT = 1;
  17. static std::size_t const CPPCOMMENT = 2;
  18. lexertl_test()
  19. : c_comment("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT)
  20. , cpp_comment("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT)
  21. {
  22. this->self = c_comment;
  23. this->self += cpp_comment;
  24. }
  25. token_def c_comment, cpp_comment;
  26. };
  27. template <typename Lexer>
  28. struct wlexertl_test
  29. : boost::spirit::lex::lexer<Lexer>
  30. {
  31. typedef boost::spirit::lex::token_def<std::basic_string<wchar_t>, wchar_t>
  32. token_def;
  33. static std::size_t const CCOMMENT = 1;
  34. static std::size_t const CPPCOMMENT = 2;
  35. wlexertl_test()
  36. : c_comment(L"\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT)
  37. , cpp_comment(L"\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT)
  38. {
  39. this->self = c_comment;
  40. this->self += cpp_comment;
  41. }
  42. token_def c_comment, cpp_comment;
  43. };
  44. ///////////////////////////////////////////////////////////////////////////////
  45. int main()
  46. {
  47. using namespace boost::spirit;
  48. using namespace spirit_test;
  49. // the following test aims at the low level lexer_ and token_ objects,
  50. // normally not visible/used by the user
  51. {
  52. // initialize lexer
  53. typedef std::string::iterator base_iterator_type;
  54. typedef lex::lexertl::token<base_iterator_type> token_type;
  55. typedef lex::lexertl::lexer<token_type> lexer_type;
  56. typedef lexertl_test<lexer_type> lexer_def;
  57. // test lexer for two different input strings
  58. lexer_def lex;
  59. BOOST_TEST(test (lex, "/* this is a comment */", lexer_def::CCOMMENT));
  60. BOOST_TEST(test (lex, "// this is a comment as well\n", lexer_def::CPPCOMMENT));
  61. }
  62. {
  63. // initialize lexer
  64. typedef std::basic_string<wchar_t>::iterator base_iterator_type;
  65. typedef lex::lexertl::token<base_iterator_type> token_type;
  66. typedef lex::lexertl::lexer<token_type> lexer_type;
  67. typedef wlexertl_test<lexer_type> lexer_def;
  68. // test lexer for two different input strings
  69. lexer_def lex;
  70. BOOST_TEST(test (lex, L"/* this is a comment */", lexer_def::CCOMMENT));
  71. BOOST_TEST(test (lex, L"// this is a comment as well\n", lexer_def::CPPCOMMENT));
  72. }
  73. return boost::report_errors();
  74. }