• 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     token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
23     token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
24 
25     typedef std::string::iterator base_iterator_type;
26     typedef lex::lexertl::token<base_iterator_type> token_type;
27     typedef lex::lexertl::lexer<token_type> lexer_type;
28 
29     typedef lex::lexer<lexer_type> lexer_def;
30 
31     {
32         // initialize lexer
33         std::string str("def");
34         token_def ws_tok ("[\\v\\f\\n\\r]+");
35         lexer_def lex;
36         lex.self = c_comment;
37         lex.self += cpp_comment | '1' | '2' | '3' | "abc" | str;
38         lex.self += token_def(' ') | '\t' | ws_tok;
39 
40         // test lexer for two different input strings
41         BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
42         BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
43         BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id()));
44         BOOST_TEST(test (lex, " ", ' '));
45         BOOST_TEST(test (lex, "2", '2'));
46         BOOST_TEST(test (lex, "abc"));
47         BOOST_TEST(test (lex, "def"));
48     }
49 
50     {
51         // initialize lexer
52         lexer_def lex;
53         token_def ws_tok ("[\\v\\f\\n\\r]+");
54         lex.self = c_comment;
55         lex.self += cpp_comment | '1' | '2' | '3';
56         lex.self("WHITESPACE") = token_def(' ') | '\t' | ws_tok;
57 
58         // test lexer for two different input strings
59         BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
60         BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
61         BOOST_TEST(test (lex, "2", '2'));
62         BOOST_TEST(!test (lex, "\n\n\v\f\r", ws_tok.id()));
63         BOOST_TEST(test (lex, " ", ' ', "WHITESPACE"));
64         BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id(), "WHITESPACE"));
65     }
66 
67     return boost::report_errors();
68 }
69