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/detail/lightweight_test.hpp>
7 #include <boost/spirit/include/lex_lexertl.hpp>
8 #include <boost/spirit/include/lex_lexertl_position_token.hpp>
9 #include "test.hpp"
10
11 ///////////////////////////////////////////////////////////////////////////////
main()12 int main()
13 {
14 using namespace boost::spirit;
15 using namespace spirit_test;
16
17 // the following test aims at the low level lexer and token_def objects,
18 // normally not visible to/directly used by the user
19
20 // initialize tokens
21 typedef lex::token_def<std::string> token_def;
22
23 std::size_t const CCOMMENT = 1;
24 std::size_t const CPPCOMMENT = 2;
25 token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
26 token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
27
28 typedef std::string::iterator base_iterator_type;
29
30 // test with default token type
31 typedef lex::lexertl::token<base_iterator_type> token_type;
32 typedef lex::lexertl::lexer<token_type> lexer_type;
33 typedef lex::lexer<lexer_type> lexer_def;
34
35 {
36 // initialize lexer
37 lexer_def lex;
38 lex.self = c_comment;
39 lex.self += cpp_comment;
40
41 // test lexer for two different input strings
42 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
43 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
44 }
45
46 {
47 // initialize lexer
48 lexer_def lex;
49 lex.self = c_comment | cpp_comment;
50
51 // test lexer for two different input strings
52 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
53 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
54 }
55
56 {
57 // initialize lexer
58 lexer_def lex;
59 lex.self = token_def('+') | '-' | c_comment;
60 lex.self += lex::char_('*') | '/' | cpp_comment;
61
62 // test lexer for two different input strings
63 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
64 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
65 BOOST_TEST(test (lex, "+", '+'));
66 BOOST_TEST(test (lex, "-", '-'));
67 BOOST_TEST(test (lex, "*", '*'));
68 BOOST_TEST(test (lex, "/", '/'));
69 }
70
71 // test with position_token
72 typedef lex::lexertl::position_token<base_iterator_type> position_token_type;
73 typedef lex::lexertl::lexer<position_token_type> position_lexer_type;
74 typedef lex::lexer<position_lexer_type> position_lexer_def;
75
76 {
77 // initialize lexer
78 position_lexer_def lex;
79 lex.self = c_comment;
80 lex.self += cpp_comment;
81
82 // test lexer for two different input strings
83 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
84 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
85 }
86
87 {
88 // initialize lexer
89 position_lexer_def lex;
90 lex.self = c_comment | cpp_comment;
91
92 // test lexer for two different input strings
93 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
94 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
95 }
96
97 {
98 // initialize lexer
99 position_lexer_def lex;
100 lex.self = token_def('+') | '-' | c_comment;
101 lex.self += lex::char_('*') | '/' | cpp_comment;
102
103 // test lexer for two different input strings
104 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
105 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
106 BOOST_TEST(test (lex, "+", '+'));
107 BOOST_TEST(test (lex, "-", '-'));
108 BOOST_TEST(test (lex, "*", '*'));
109 BOOST_TEST(test (lex, "/", '/'));
110 }
111
112 return boost::report_errors();
113 }
114