lexertl3.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
  19. token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
  20. typedef std::string::iterator base_iterator_type;
  21. typedef lex::lexertl::token<base_iterator_type> token_type;
  22. typedef lex::lexertl::lexer<token_type> lexer_type;
  23. typedef lex::lexer<lexer_type> lexer_def;
  24. {
  25. // initialize lexer
  26. std::string str("def");
  27. token_def ws_tok ("[\\v\\f\\n\\r]+");
  28. lexer_def lex;
  29. lex.self = c_comment;
  30. lex.self += cpp_comment | '1' | '2' | '3' | "abc" | str;
  31. lex.self += token_def(' ') | '\t' | ws_tok;
  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. BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id()));
  36. BOOST_TEST(test (lex, " ", ' '));
  37. BOOST_TEST(test (lex, "2", '2'));
  38. BOOST_TEST(test (lex, "abc"));
  39. BOOST_TEST(test (lex, "def"));
  40. }
  41. {
  42. // initialize lexer
  43. lexer_def lex;
  44. token_def ws_tok ("[\\v\\f\\n\\r]+");
  45. lex.self = c_comment;
  46. lex.self += cpp_comment | '1' | '2' | '3';
  47. lex.self("WHITESPACE") = token_def(' ') | '\t' | ws_tok;
  48. // test lexer for two different input strings
  49. BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
  50. BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
  51. BOOST_TEST(test (lex, "2", '2'));
  52. BOOST_TEST(!test (lex, "\n\n\v\f\r", ws_tok.id()));
  53. BOOST_TEST(test (lex, " ", ' ', "WHITESPACE"));
  54. BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id(), "WHITESPACE"));
  55. }
  56. return boost::report_errors();
  57. }