• 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 ///////////////////////////////////////////////////////////////////////////////
main()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 CCOMMENT = 1;
21     std::size_t const CPPCOMMENT = 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     token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
27     token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
28     token_def ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS);
29 
30     typedef std::string::iterator base_iterator_type;
31     typedef lex::lexertl::token<base_iterator_type> token_type;
32     typedef lex::lexertl::lexer<token_type> lexer_type;
33 
34     typedef lex::lexer<lexer_type> lexer_def;
35 
36     std::string str("def");
37 
38     {
39         // initialize lexer
40         lexer_def lex;
41         token_def ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS);
42         lex.self.add
43             (c_comment)(cpp_comment)
44             ('1')('2')('3')
45             ("abc", TOKEN_ID_ABC)
46             (str, TOKEN_ID_STR)
47         ;
48         lex.self += token_def(' ') | '\t' | ws_tok;
49 
50         // test lexer for different input strings
51         BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
52         BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
53         BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id()));
54         BOOST_TEST(test (lex, " ", ' '));
55         BOOST_TEST(test (lex, "2", '2'));
56         BOOST_TEST(test (lex, "abc", TOKEN_ID_ABC));
57         BOOST_TEST(test (lex, "def", TOKEN_ID_STR));
58     }
59 
60     {
61         // initialize lexer
62         lexer_def lex;
63         token_def ws_tok ("[\\v\\f\\n\\r]+", TOKEN_ID_WS);
64 
65         lex.self.add
66             (c_comment)(cpp_comment)
67             ('1')('2')('3')
68             ("abc", TOKEN_ID_ABC)
69             (str, TOKEN_ID_STR)
70         ;
71 
72         lex.self("WHITESPACE").add
73             (' ')('\t')
74             (ws_tok)
75         ;
76 
77         // test lexer for different input strings
78         BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
79         BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
80         BOOST_TEST(test (lex, "2", '2'));
81         BOOST_TEST(test (lex, "abc", TOKEN_ID_ABC));
82         BOOST_TEST(test (lex, "def", TOKEN_ID_STR));
83 
84         BOOST_TEST(!test (lex, "\n\n\v\f\r", TOKEN_ID_WS));
85         BOOST_TEST(test (lex, " ", ' ', "WHITESPACE"));
86         BOOST_TEST(test (lex, "\n\n\v\f\r", TOKEN_ID_WS, "WHITESPACE"));
87     }
88 
89     return boost::report_errors();
90 }
91