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