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 token_def
c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT
);
22 token_def
cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT
);
24 typedef std::string::iterator base_iterator_type
;
25 typedef lex::lexertl::token
<base_iterator_type
> token_type
;
26 typedef lex::lexertl::lexer
<token_type
> lexer_type
;
28 typedef lex::lexer
<lexer_type
> lexer_def
;
32 std::string
str("def");
33 token_def
ws_tok ("[\\v\\f\\n\\r]+");
36 lex
.self
+= cpp_comment
| '1' | '2' | '3' | "abc" | str
;
37 lex
.self
+= token_def(' ') | '\t' | ws_tok
;
39 // test lexer for two different input strings
40 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
41 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
42 BOOST_TEST(test (lex
, "\n\n\v\f\r", ws_tok
.id()));
43 BOOST_TEST(test (lex
, " ", ' '));
44 BOOST_TEST(test (lex
, "2", '2'));
45 BOOST_TEST(test (lex
, "abc"));
46 BOOST_TEST(test (lex
, "def"));
52 token_def
ws_tok ("[\\v\\f\\n\\r]+");
54 lex
.self
+= cpp_comment
| '1' | '2' | '3';
55 lex
.self("WHITESPACE") = token_def(' ') | '\t' | ws_tok
;
57 // test lexer for two different input strings
58 BOOST_TEST(test (lex
, "/* this is a comment */", CCOMMENT
));
59 BOOST_TEST(test (lex
, "// this is a comment as well\n", CPPCOMMENT
));
60 BOOST_TEST(test (lex
, "2", '2'));
61 BOOST_TEST(!test (lex
, "\n\n\v\f\r", ws_tok
.id()));
62 BOOST_TEST(test (lex
, " ", ' ', "WHITESPACE"));
63 BOOST_TEST(test (lex
, "\n\n\v\f\r", ws_tok
.id(), "WHITESPACE"));
66 return boost::report_errors();