lexertl5.cpp 3.4 KB

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