1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #include <boost/detail/lightweight_test.hpp>
7 #include <boost/spirit/include/lex_lexertl.hpp>
8 #include <boost/spirit/include/lex_lexertl_position_token.hpp>
11 ///////////////////////////////////////////////////////////////////////////////
14 using namespace boost::spirit
;
15 using namespace spirit_test
;
17 // the following test aims at the low level lexer and token_def objects,
18 // normally not visible to/directly used by the user
21 typedef lex::token_def
<std::string
> token_def
;
23 std::size_t const CCOMMENT
= 1;
24 std::size_t const CPPCOMMENT
= 2;
25 token_def
c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT
);
26 token_def
cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT
);
28 typedef std::string::iterator base_iterator_type
;
30 // test with default token type
31 typedef lex::lexertl::token
<base_iterator_type
> token_type
;
32 typedef lex::lexertl::lexer
<token_type
> lexer_type
;
33 typedef lex::lexer
<lexer_type
> lexer_def
;
39 lex
.self
+= cpp_comment
;
41 // test lexer for two different input strings
42 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
43 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
49 lex
.self
= c_comment
| cpp_comment
;
51 // test lexer for two different input strings
52 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
53 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
59 lex
.self
= token_def('+') | '-' | c_comment
;
60 lex
.self
+= lex::char_('*') | '/' | cpp_comment
;
62 // test lexer for two different input strings
63 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
64 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
65 BOOST_TEST(test (lex
, "+", '+'));
66 BOOST_TEST(test (lex
, "-", '-'));
67 BOOST_TEST(test (lex
, "*", '*'));
68 BOOST_TEST(test (lex
, "/", '/'));
71 // test with position_token
72 typedef lex::lexertl::position_token
<base_iterator_type
> position_token_type
;
73 typedef lex::lexertl::lexer
<position_token_type
> position_lexer_type
;
74 typedef lex::lexer
<position_lexer_type
> position_lexer_def
;
78 position_lexer_def lex
;
80 lex
.self
+= cpp_comment
;
82 // test lexer for two different input strings
83 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
84 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
89 position_lexer_def lex
;
90 lex
.self
= c_comment
| cpp_comment
;
92 // test lexer for two different input strings
93 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
94 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
99 position_lexer_def lex
;
100 lex
.self
= token_def('+') | '-' | c_comment
;
101 lex
.self
+= lex::char_('*') | '/' | cpp_comment
;
103 // test lexer for two different input strings
104 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
105 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
106 BOOST_TEST(test (lex
, "+", '+'));
107 BOOST_TEST(test (lex
, "-", '-'));
108 BOOST_TEST(test (lex
, "*", '*'));
109 BOOST_TEST(test (lex
, "/", '/'));
112 return boost::report_errors();