• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test.hpp"
9 
10 ///////////////////////////////////////////////////////////////////////////////
11 //  test pattern definition capabilities
main()12 int main()
13 {
14     using namespace boost::spirit;
15     using namespace boost::spirit::lex;
16     using namespace spirit_test;
17 
18     // initialize tokens
19     typedef lex::token_def<std::string> token_def;
20 
21     std::size_t const CCOMMENT = 1;
22     std::size_t const CPPCOMMENT = 2;
23     std::size_t const TOKEN_ID_ABC = 1000;
24     std::size_t const TOKEN_ID_STR = 1001;
25     std::size_t const TOKEN_ID_WS = 1002;
26 
27     typedef std::string::iterator base_iterator_type;
28     typedef lex::lexertl::token<base_iterator_type> token_type;
29     typedef lex::lexertl::lexer<token_type> lexer_type;
30 
31     typedef lex::lexer<lexer_type> lexer_def;
32 
33     std::string str("def");
34 
35     {
36         // initialize lexer
37         lexer_def lex;
38 
39         lex.self.add_pattern
40             ("CCOMMENT", "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/")
41             ("CPPCOMMENT", "\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)")
42             ("WS", "[\\v\\f\\n\\r]+")
43         ;
44 
45         token_def c_comment ("{CCOMMENT}", CCOMMENT);
46         token_def cpp_comment ("{CPPCOMMENT}", CPPCOMMENT);
47         token_def ws_tok ("{WS}");
48 
49         lex.self.add
50             (c_comment)(cpp_comment)
51             ('1')('2')('3')
52             ("abc", TOKEN_ID_ABC)
53             (str, TOKEN_ID_STR)
54         ;
55         lex.self += token_def(' ') | '\t' | ws_tok;
56 
57         // test lexer for 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, "\n\n\v\f\r", ws_tok.id()));
61         BOOST_TEST(test (lex, " ", ' '));
62         BOOST_TEST(test (lex, "2", '2'));
63         BOOST_TEST(test (lex, "abc", TOKEN_ID_ABC));
64         BOOST_TEST(test (lex, "def", TOKEN_ID_STR));
65     }
66 
67     {
68         // initialize lexer
69         lexer_def lex;
70 
71         lex.self.add_pattern
72             ("CCOMMENT", "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/")
73             ("CPPCOMMENT", "\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)")
74             ("WS", "[\\v\\f\\n\\r]+")
75         ;
76 
77         token_def c_comment ("{CCOMMENT}", CCOMMENT);
78         token_def cpp_comment ("{CPPCOMMENT}", CPPCOMMENT);
79         token_def ws_tok ("{WS}");
80 
81         // init lexer
82         lex.self.add
83             (c_comment)(cpp_comment)
84             ('1')('2')('3')
85             ("abc", TOKEN_ID_ABC)
86             (str, TOKEN_ID_STR)
87         ;
88 
89         lex.self("WHITESPACE").add
90             (' ')('\t')
91             (ws_tok, TOKEN_ID_WS)
92         ;
93 
94         // test lexer for different input strings
95         BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
96         BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
97         BOOST_TEST(test (lex, "2", '2'));
98         BOOST_TEST(test (lex, "abc", TOKEN_ID_ABC));
99         BOOST_TEST(test (lex, "def", TOKEN_ID_STR));
100 
101         BOOST_TEST(!test (lex, "\n\n\v\f\r", TOKEN_ID_WS));
102         BOOST_TEST(test (lex, " ", ' ', "WHITESPACE"));
103         BOOST_TEST(test (lex, "\n\n\v\f\r", TOKEN_ID_WS, "WHITESPACE"));
104     }
105 
106     return boost::report_errors();
107 }
108