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/spirit/include/lex_lexertl.hpp> |
7 | #include "test.hpp" |
8 | |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | int main() |
11 | { |
12 | using namespace boost::spirit; |
13 | using namespace boost::spirit::lex; |
14 | using namespace spirit_test; |
15 | |
16 | // initialize tokens |
17 | typedef lex::token_def<std::string> token_def; |
18 | |
19 | std::size_t const = 1; |
20 | std::size_t const = 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; |
24 | |
25 | token_def ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/" , CCOMMENT); |
26 | token_def ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)" , CPPCOMMENT); |
27 | |
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; |
31 | |
32 | typedef lex::lexer<lexer_type> lexer_def; |
33 | |
34 | std::string str("def" ); |
35 | |
36 | { |
37 | // initialize lexer |
38 | lexer_def lex; |
39 | token_def ws_tok ("[\\v\\f\\n\\r]+" , TOKEN_ID_WS); |
40 | lex.self.add |
41 | (c_comment)(cpp_comment) |
42 | ('1')('2')('3') |
43 | ("abc" , TOKEN_ID_ABC) |
44 | (str, TOKEN_ID_STR) |
45 | ; |
46 | lex.self += token_def(' ') | '\t' | ws_tok; |
47 | |
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)); |
56 | } |
57 | |
58 | { |
59 | // initialize lexer |
60 | lexer_def lex; |
61 | token_def ws_tok ("[\\v\\f\\n\\r]+" , TOKEN_ID_WS); |
62 | |
63 | lex.self.add |
64 | (c_comment)(cpp_comment) |
65 | ('1')('2')('3') |
66 | ("abc" , TOKEN_ID_ABC) |
67 | (str, TOKEN_ID_STR) |
68 | ; |
69 | |
70 | lex.self("WHITESPACE" ).add |
71 | (' ')('\t') |
72 | (ws_tok) |
73 | ; |
74 | |
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)); |
81 | |
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" )); |
85 | } |
86 | |
87 | return boost::report_errors(); |
88 | } |
89 | |