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 ///////////////////////////////////////////////////////////////////////////////
10 // test pattern definition capabilities
13 using namespace boost::spirit
;
14 using namespace boost::spirit::lex
;
15 using namespace spirit_test
;
18 typedef lex::token_def
<std::string
> token_def
;
20 std::size_t const CCOMMENT
= 1;
21 std::size_t const CPPCOMMENT
= 2;
22 std::size_t const TOKEN_ID_ABC
= 1000;
23 std::size_t const TOKEN_ID_STR
= 1001;
24 std::size_t const TOKEN_ID_WS
= 1002;
26 typedef std::string::iterator base_iterator_type
;
27 typedef lex::lexertl::token
<base_iterator_type
> token_type
;
28 typedef lex::lexertl::lexer
<token_type
> lexer_type
;
30 typedef lex::lexer
<lexer_type
> lexer_def
;
32 std::string
str("def");
39 ("CCOMMENT", "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/")
40 ("CPPCOMMENT", "\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)")
41 ("WS", "[\\v\\f\\n\\r]+")
44 token_def
c_comment ("{CCOMMENT}", CCOMMENT
);
45 token_def
cpp_comment ("{CPPCOMMENT}", CPPCOMMENT
);
46 token_def
ws_tok ("{WS}");
49 (c_comment
)(cpp_comment
)
54 lex
.self
+= token_def(' ') | '\t' | ws_tok
;
56 // test lexer for different input strings
57 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
58 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
59 BOOST_TEST(test (lex
, "\n\n\v\f\r", ws_tok
.id()));
60 BOOST_TEST(test (lex
, " ", ' '));
61 BOOST_TEST(test (lex
, "2", '2'));
62 BOOST_TEST(test (lex
, "abc", TOKEN_ID_ABC
));
63 BOOST_TEST(test (lex
, "def", TOKEN_ID_STR
));
71 ("CCOMMENT", "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/")
72 ("CPPCOMMENT", "\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)")
73 ("WS", "[\\v\\f\\n\\r]+")
76 token_def
c_comment ("{CCOMMENT}", CCOMMENT
);
77 token_def
cpp_comment ("{CPPCOMMENT}", CPPCOMMENT
);
78 token_def
ws_tok ("{WS}");
82 (c_comment
)(cpp_comment
)
88 lex
.self("WHITESPACE").add
93 // test lexer for different input strings
94 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
95 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
96 BOOST_TEST(test (lex
, "2", '2'));
97 BOOST_TEST(test (lex
, "abc", TOKEN_ID_ABC
));
98 BOOST_TEST(test (lex
, "def", TOKEN_ID_STR
));
100 BOOST_TEST(!test (lex
, "\n\n\v\f\r", TOKEN_ID_WS
));
101 BOOST_TEST(test (lex
, " ", ' ', "WHITESPACE"));
102 BOOST_TEST(test (lex
, "\n\n\v\f\r", TOKEN_ID_WS
, "WHITESPACE"));
105 return boost::report_errors();