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 <string>
8
9#include "test.hpp"
10
11///////////////////////////////////////////////////////////////////////////////
12// a simple lexer class
13template <typename Lexer>
14struct lexertl_test
15 : boost::spirit::lex::lexer<Lexer>
16{
17 typedef boost::spirit::lex::token_def<std::string> token_def;
18
19 static std::size_t const CCOMMENT = 1;
20 static std::size_t const CPPCOMMENT = 2;
21
22 lexertl_test()
23 : c_comment("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT)
24 , cpp_comment("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT)
25 {
26 this->self = c_comment;
27 this->self += cpp_comment;
28 }
29
30 token_def c_comment, cpp_comment;
31};
32
33template <typename Lexer>
34struct wlexertl_test
35 : boost::spirit::lex::lexer<Lexer>
36{
37 typedef boost::spirit::lex::token_def<std::basic_string<wchar_t>, wchar_t>
38 token_def;
39
40 static std::size_t const CCOMMENT = 1;
41 static std::size_t const CPPCOMMENT = 2;
42
43 wlexertl_test()
44 : c_comment(L"\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT)
45 , cpp_comment(L"\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT)
46 {
47 this->self = c_comment;
48 this->self += cpp_comment;
49 }
50
51 token_def c_comment, cpp_comment;
52};
53
54///////////////////////////////////////////////////////////////////////////////
55int main()
56{
57 using namespace boost::spirit;
58 using namespace spirit_test;
59
60 // the following test aims at the low level lexer_ and token_ objects,
61 // normally not visible/used by the user
62 {
63 // initialize lexer
64 typedef std::string::iterator base_iterator_type;
65 typedef lex::lexertl::token<base_iterator_type> token_type;
66 typedef lex::lexertl::lexer<token_type> lexer_type;
67 typedef lexertl_test<lexer_type> lexer_def;
68
69 // test lexer for two different input strings
70 lexer_def lex;
71 BOOST_TEST(test (lex, "/* this is a comment */", lexer_def::CCOMMENT));
72 BOOST_TEST(test (lex, "// this is a comment as well\n", lexer_def::CPPCOMMENT));
73 }
74
75 {
76 // initialize lexer
77 typedef std::basic_string<wchar_t>::iterator base_iterator_type;
78 typedef lex::lexertl::token<base_iterator_type> token_type;
79 typedef lex::lexertl::lexer<token_type> lexer_type;
80 typedef wlexertl_test<lexer_type> lexer_def;
81
82 // test lexer for two different input strings
83 lexer_def lex;
84 BOOST_TEST(test (lex, L"/* this is a comment */", lexer_def::CCOMMENT));
85 BOOST_TEST(test (lex, L"// this is a comment as well\n", lexer_def::CPPCOMMENT));
86 }
87
88 return boost::report_errors();
89}
90
91

source code of boost/libs/spirit/test/lex/lexertl2.cpp