lexertl4.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "test.hpp"
  8. ///////////////////////////////////////////////////////////////////////////////
  9. int main()
  10. {
  11. using namespace boost::spirit;
  12. using namespace boost::spirit::lex;
  13. using namespace spirit_test;
  14. // initialize tokens
  15. typedef lex::token_def<std::string> token_def;
  16. std::size_t const CCOMMENT = 1;
  17. std::size_t const CPPCOMMENT = 2;
  18. std::size_t const TOKEN_ID_ABC = 1000;
  19. std::size_t const TOKEN_ID_STR = 1001;
  20. std::size_t const TOKEN_ID_WS = 1002;
  21. token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
  22. token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
  23. token_def ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS);
  24. typedef std::string::iterator base_iterator_type;
  25. typedef lex::lexertl::token<base_iterator_type> token_type;
  26. typedef lex::lexertl::lexer<token_type> lexer_type;
  27. typedef lex::lexer<lexer_type> lexer_def;
  28. std::string str("def");
  29. {
  30. // initialize lexer
  31. lexer_def lex;
  32. token_def ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS);
  33. lex.self.add
  34. (c_comment)(cpp_comment)
  35. ('1')('2')('3')
  36. ("abc", TOKEN_ID_ABC)
  37. (str, TOKEN_ID_STR)
  38. ;
  39. lex.self += token_def(' ') | '\t' | ws_tok;
  40. // test lexer for 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. BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id()));
  44. BOOST_TEST(test (lex, " ", ' '));
  45. BOOST_TEST(test (lex, "2", '2'));
  46. BOOST_TEST(test (lex, "abc", TOKEN_ID_ABC));
  47. BOOST_TEST(test (lex, "def", TOKEN_ID_STR));
  48. }
  49. {
  50. // initialize lexer
  51. lexer_def lex;
  52. token_def ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS);
  53. lex.self.add
  54. (c_comment)(cpp_comment)
  55. ('1')('2')('3')
  56. ("abc", TOKEN_ID_ABC)
  57. (str, TOKEN_ID_STR)
  58. ;
  59. lex.self("WHITESPACE").add
  60. (' ')('\t')
  61. (ws_tok)
  62. ;
  63. // test lexer for different input strings
  64. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  65. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  66. BOOST_TEST(test (lex, "2", '2'));
  67. BOOST_TEST(test (lex, "abc", TOKEN_ID_ABC));
  68. BOOST_TEST(test (lex, "def", TOKEN_ID_STR));
  69. BOOST_TEST(!test (lex, "\n\n\v\f\r", TOKEN_ID_WS));
  70. BOOST_TEST(test (lex, " ", ' ', "WHITESPACE"));
  71. BOOST_TEST(test (lex, "\n\n\v\f\r", TOKEN_ID_WS, "WHITESPACE"));
  72. }
  73. return boost::report_errors();
  74. }