]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/spirit/test/lex/regression_wide.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / spirit / test / lex / regression_wide.cpp
CommitLineData
7c673cae
FG
1// Copyright (c) 2001-2010 Hartmut Kaiser
2// Copyright (c) 2010 Sergey "GooRoo" Olendarenko
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#include <boost/detail/lightweight_test.hpp>
8#include <boost/config/warning_disable.hpp>
9
10#include <cstdlib>
11#include <iostream>
12#include <locale>
13#include <string>
14
15#include <boost/spirit/include/lex_lexertl.hpp>
92f5a8d4 16#include <boost/spirit/include/phoenix_function.hpp>
7c673cae 17#include <boost/spirit/include/phoenix_operator.hpp>
7c673cae
FG
18
19namespace lex = boost::spirit::lex;
20namespace phoenix = boost::phoenix;
21
22typedef std::basic_string<wchar_t> wstring_type;
23
24///////////////////////////////////////////////////////////////////////////////
25enum tokenids
26{
27 ID_IDENT = 1,
28 ID_CONSTANT,
29 ID_OPERATION,
30 ID_BRACKET
31};
32
33///////////////////////////////////////////////////////////////////////////////
34struct test_data
35{
b32b8144 36 tokenids tokenid;
7c673cae
FG
37 wstring_type value;
38};
39
40// alpha+x1*(2.836-x2[i])
41test_data data[] =
42{
43 { ID_IDENT, L"alpha" },
44 { ID_OPERATION, L"+" },
45 { ID_IDENT, L"x1" },
46 { ID_OPERATION, L"*" },
47 { ID_BRACKET, L"(" },
48 { ID_CONSTANT, L"2.836" },
49 { ID_OPERATION, L"-" },
50 { ID_IDENT, L"x2" },
51 { ID_BRACKET, L"[" },
52 { ID_IDENT, L"i" },
53 { ID_BRACKET, L"]" },
54 { ID_BRACKET, L")" }
55};
56
57///////////////////////////////////////////////////////////////////////////////
58struct test_impl
59{
60 typedef void result_type;
61 template <typename TokenId, typename Value>
62 struct result { typedef void type; };
63
64 template <typename TokenId, typename Value>
65 void operator()(TokenId const& tokenid, Value const& val) const
66 {
67 BOOST_TEST(sequence_counter < sizeof(data)/sizeof(data[0]));
92f5a8d4 68 BOOST_TEST(data[sequence_counter].tokenid == tokenids(tokenid));
7c673cae
FG
69 BOOST_TEST(0 == val.which());
70
71 typedef boost::iterator_range<wstring_type::iterator> iterator_range;
72 iterator_range r = boost::get<iterator_range>(val);
73 BOOST_TEST(data[sequence_counter].value ==
74 wstring_type(r.begin(), r.end()));
75
76 ++sequence_counter;
77 }
78
b32b8144 79 static std::size_t sequence_counter;
7c673cae 80};
b32b8144 81std::size_t test_impl::sequence_counter = 0;
7c673cae
FG
82
83phoenix::function<test_impl> const test = test_impl();
84
85///////////////////////////////////////////////////////////////////////////////
86template <typename Lexer>
87struct mega_tokens : lex::lexer<Lexer>
88{
89 mega_tokens()
90 : identifier(L"[a-zA-Z_][a-zA-Z0-9_]*", ID_IDENT)
91 , constant (L"[0-9]+(\\.[0-9]+)?", ID_CONSTANT)
92 , operation (L"[\\+\\-\\*/]", ID_OPERATION)
93 , bracket (L"[\\(\\)\\[\\]]", ID_BRACKET)
94 {
95 using lex::_tokenid;
96 using lex::_val;
97
98 this->self
99 = operation [ test(_tokenid, _val) ]
100 | identifier [ test(_tokenid, _val) ]
101 | constant [ test(_tokenid, _val) ]
102 | bracket [ test(_tokenid, _val) ]
103 ;
104 }
105
b32b8144
FG
106 lex::token_def<wstring_type, wchar_t, tokenids> identifier;
107 lex::token_def<double, wchar_t, tokenids> constant;
108 lex::token_def<wchar_t, wchar_t, tokenids> operation;
109 lex::token_def<wchar_t, wchar_t, tokenids> bracket;
7c673cae
FG
110};
111
112///////////////////////////////////////////////////////////////////////////////
113int main()
114{
115 typedef wstring_type::iterator base_iterator;
116 typedef lex::lexertl::token<
117 base_iterator, boost::mpl::vector<wchar_t, wstring_type, double>
b32b8144 118 , boost::mpl::true_, tokenids
7c673cae
FG
119 > token_type;
120 typedef lex::lexertl::actor_lexer<token_type> lexer_type;
7c673cae
FG
121
122 mega_tokens<lexer_type> mega_lexer;
123
124 wstring_type exampleStr = L"alpha+x1*(2.836-x2[i])";
125 base_iterator first = exampleStr.begin();
126
127 BOOST_TEST(lex::tokenize(first, exampleStr.end(), mega_lexer));
128 BOOST_TEST(test_impl::sequence_counter == sizeof(data)/sizeof(data[0]));
129
130 return boost::report_errors();
131}