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 "test.hpp"
8
9///////////////////////////////////////////////////////////////////////////////
10int main()
11{
12 using namespace boost::spirit;
13 using namespace boost::spirit::lex;
14 using namespace spirit_test;
15
16 // initialize tokens
17 typedef lex::token_def<std::string> token_def;
18
19 std::size_t const CCOMMENT = 1;
20 std::size_t const CPPCOMMENT = 2;
21 token_def c_comment ("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", CCOMMENT);
22 token_def cpp_comment ("\\/\\/[^\\n\\r]*(\\n|\\r|\\r\\n)", CPPCOMMENT);
23
24 typedef std::string::iterator base_iterator_type;
25 typedef lex::lexertl::token<base_iterator_type> token_type;
26 typedef lex::lexertl::lexer<token_type> lexer_type;
27
28 typedef lex::lexer<lexer_type> lexer_def;
29
30 {
31 // initialize lexer
32 std::string str("def");
33 token_def ws_tok ("[\\v\\f\\n\\r]+");
34 lexer_def lex;
35 lex.self = c_comment;
36 lex.self += cpp_comment | '1' | '2' | '3' | "abc" | str;
37 lex.self += token_def(' ') | '\t' | ws_tok;
38
39 // test lexer for two different input strings
40 BOOST_TEST(test (lex, "/* this is a comment */", CCOMMENT));
41 BOOST_TEST(test (lex, "// this is a comment as well\n", CPPCOMMENT));
42 BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id()));
43 BOOST_TEST(test (lex, " ", ' '));
44 BOOST_TEST(test (lex, "2", '2'));
45 BOOST_TEST(test (lex, "abc"));
46 BOOST_TEST(test (lex, "def"));
47 }
48
49 {
50 // initialize lexer
51 lexer_def lex;
52 token_def ws_tok ("[\\v\\f\\n\\r]+");
53 lex.self = c_comment;
54 lex.self += cpp_comment | '1' | '2' | '3';
55 lex.self("WHITESPACE") = token_def(' ') | '\t' | ws_tok;
56
57 // test lexer for two 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, "2", '2'));
61 BOOST_TEST(!test (lex, "\n\n\v\f\r", ws_tok.id()));
62 BOOST_TEST(test (lex, " ", ' ', "WHITESPACE"));
63 BOOST_TEST(test (lex, "\n\n\v\f\r", ws_tok.id(), "WHITESPACE"));
64 }
65
66 return boost::report_errors();
67}
68

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