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/spirit/include/lex_lexertl.hpp>
9 ///////////////////////////////////////////////////////////////////////////////
12 using namespace boost::spirit
;
13 using namespace boost::spirit::lex
;
14 using namespace spirit_test
;
17 typedef lex::token_def
<std::string
> token_def
;
19 std::size_t const CCOMMENT
= 1;
20 std::size_t const CPPCOMMENT
= 2;
21 std::size_t const TOKEN_ID_ABC
= 1000;
22 std::size_t const TOKEN_ID_STR
= 1001;
23 std::size_t const TOKEN_ID_WS
= 1002;
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
;
29 typedef lex::lexertl::token
<base_iterator_type
> token_type
;
30 typedef lex::lexertl::lexer
<token_type
> lexer_type
;
32 typedef lex::lexer
<lexer_type
> lexer_def
;
34 std::string
str("def");
39 token_def
ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS
);
41 (c_comment
)(cpp_comment
)
46 lex
.self
+= token_def(' ') | '\t' | ws_tok
;
48 // test lexer for 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
, "\n\n\v\f\r", ws_tok
.id()));
52 BOOST_TEST(test (lex
, " ", ' '));
53 BOOST_TEST(test (lex
, "2", '2'));
54 BOOST_TEST(test (lex
, "abc", TOKEN_ID_ABC
));
55 BOOST_TEST(test (lex
, "def", TOKEN_ID_STR
));
61 token_def
ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS
);
64 (c_comment
)(cpp_comment
)
70 lex
.self("WHITESPACE").add
75 // test lexer for different input strings
76 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
77 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
78 BOOST_TEST(test (lex
, "2", '2'));
79 BOOST_TEST(test (lex
, "abc", TOKEN_ID_ABC
));
80 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"));
87 return boost::report_errors();