lexertl1.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <boost/spirit/include/lex_lexertl_position_token.hpp>
  8. #include "test.hpp"
  9. ///////////////////////////////////////////////////////////////////////////////
  10. int main()
  11. {
  12. using namespace boost::spirit;
  13. using namespace spirit_test;
  14. // the following test aims at the low level lexer and token_def objects,
  15. // normally not visible to/directly used by the user
  16. // initialize tokens
  17. typedef lex::token_def<std::string> token_def;
  18. std::size_t const CCOMMENT = 1;
  19. std::size_t const CPPCOMMENT = 2;
  20. token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
  21. token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
  22. typedef std::string::iterator base_iterator_type;
  23. // test with default token type
  24. typedef lex::lexertl::token<base_iterator_type> token_type;
  25. typedef lex::lexertl::lexer<token_type> lexer_type;
  26. typedef lex::lexer<lexer_type> lexer_def;
  27. {
  28. // initialize lexer
  29. lexer_def lex;
  30. lex.self = c_comment;
  31. lex.self += cpp_comment;
  32. // test lexer for two different input strings
  33. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  34. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  35. }
  36. {
  37. // initialize lexer
  38. lexer_def lex;
  39. lex.self = c_comment | cpp_comment;
  40. // test lexer for two different input strings
  41. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  42. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  43. }
  44. {
  45. // initialize lexer
  46. lexer_def lex;
  47. lex.self = token_def('+') | '-' | c_comment;
  48. lex.self += lex::char_('*') | '/' | cpp_comment;
  49. // test lexer for two different input strings
  50. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  51. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  52. BOOST_TEST(test (lex, "+", '+'));
  53. BOOST_TEST(test (lex, "-", '-'));
  54. BOOST_TEST(test (lex, "*", '*'));
  55. BOOST_TEST(test (lex, "/", '/'));
  56. }
  57. // test with position_token
  58. typedef lex::lexertl::position_token<base_iterator_type> position_token_type;
  59. typedef lex::lexertl::lexer<position_token_type> position_lexer_type;
  60. typedef lex::lexer<position_lexer_type> position_lexer_def;
  61. {
  62. // initialize lexer
  63. position_lexer_def lex;
  64. lex.self = c_comment;
  65. lex.self += cpp_comment;
  66. // test lexer for two different input strings
  67. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  68. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  69. }
  70. {
  71. // initialize lexer
  72. position_lexer_def lex;
  73. lex.self = c_comment | cpp_comment;
  74. // test lexer for two different input strings
  75. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  76. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  77. }
  78. {
  79. // initialize lexer
  80. position_lexer_def lex;
  81. lex.self = token_def('+') | '-' | c_comment;
  82. lex.self += lex::char_('*') | '/' | cpp_comment;
  83. // test lexer for two different input strings
  84. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  85. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  86. BOOST_TEST(test (lex, "+", '+'));
  87. BOOST_TEST(test (lex, "-", '-'));
  88. BOOST_TEST(test (lex, "*", '*'));
  89. BOOST_TEST(test (lex, "/", '/'));
  90. }
  91. return boost::report_errors();
  92. }