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 | // test pattern definition capabilities |
11 | int main() |
12 | { |
13 | using namespace boost::spirit; |
14 | using namespace boost::spirit::lex; |
15 | using namespace spirit_test; |
16 | |
17 | // initialize tokens |
18 | typedef lex::token_def<std::string> token_def; |
19 | |
20 | std::size_t const = 1; |
21 | std::size_t const = 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; |
25 | |
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; |
29 | |
30 | typedef lex::lexer<lexer_type> lexer_def; |
31 | |
32 | std::string str("def" ); |
33 | |
34 | { |
35 | // initialize lexer |
36 | lexer_def lex; |
37 | |
38 | lex.self.add_pattern |
39 | ("CCOMMENT" , "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/" ) |
40 | ("CPPCOMMENT" , "\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)" ) |
41 | ("WS" , "[\\v\\f\\n\\r]+" ) |
42 | ; |
43 | |
44 | token_def ("{CCOMMENT}" , CCOMMENT); |
45 | token_def ("{CPPCOMMENT}" , CPPCOMMENT); |
46 | token_def ws_tok ("{WS}" ); |
47 | |
48 | lex.self.add |
49 | (c_comment)(cpp_comment) |
50 | ('1')('2')('3') |
51 | ("abc" , TOKEN_ID_ABC) |
52 | (str, TOKEN_ID_STR) |
53 | ; |
54 | lex.self += token_def(' ') | '\t' | ws_tok; |
55 | |
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)); |
64 | } |
65 | |
66 | { |
67 | // initialize lexer |
68 | lexer_def lex; |
69 | |
70 | lex.self.add_pattern |
71 | ("CCOMMENT" , "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/" ) |
72 | ("CPPCOMMENT" , "\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)" ) |
73 | ("WS" , "[\\v\\f\\n\\r]+" ) |
74 | ; |
75 | |
76 | token_def ("{CCOMMENT}" , CCOMMENT); |
77 | token_def ("{CPPCOMMENT}" , CPPCOMMENT); |
78 | token_def ws_tok ("{WS}" ); |
79 | |
80 | // init lexer |
81 | lex.self.add |
82 | (c_comment)(cpp_comment) |
83 | ('1')('2')('3') |
84 | ("abc" , TOKEN_ID_ABC) |
85 | (str, TOKEN_ID_STR) |
86 | ; |
87 | |
88 | lex.self("WHITESPACE" ).add |
89 | (' ')('\t') |
90 | (ws_tok, TOKEN_ID_WS) |
91 | ; |
92 | |
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)); |
99 | |
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" )); |
103 | } |
104 | |
105 | return boost::report_errors(); |
106 | } |
107 | |