1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*=============================================================================
Copyright (c) 2008 Francois Barel
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/type_traits/is_same.hpp>
#include <boost/spirit/include/qi_operator.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <iterator>
#include "test.hpp"
namespace testns
{
BOOST_SPIRIT_TERMINAL_NAME_EX( ops, ops_type )
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4512) // assignment operator could not be generated.
#endif
///////////////////////////////////////////////////////////////////////////
// Parsers
///////////////////////////////////////////////////////////////////////////
template <typename T1>
struct ops_1_parser
: boost::spirit::qi::primitive_parser<ops_1_parser<T1> >
{
ops_1_parser(T1 t1)
: t1(t1)
{}
template <typename Context, typename Iterator>
struct attribute
{
typedef int type; // Number of parsed chars.
};
template <typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse(Iterator& first, Iterator const& last
, Context& /*context*/, Skipper const& skipper
, Attribute& attr) const
{
boost::spirit::qi::skip_over(first, last, skipper);
int count = 0;
Iterator it = first;
typedef typename std::iterator_traits<Iterator>::value_type Char;
for (T1 t = 0; t < t1; t++, count++)
if (it == last || *it++ != Char('+'))
return false;
boost::spirit::traits::assign_to(count, attr);
first = it;
return true;
}
template <typename Context>
boost::spirit::qi::info what(Context& /*context*/) const
{
return boost::spirit::qi::info("ops_1");
}
const T1 t1;
};
template <typename T1, typename T2>
struct ops_2_parser
: boost::spirit::qi::primitive_parser<ops_2_parser<T1, T2> >
{
ops_2_parser(T1 t1, T2 t2)
: t1(t1)
, t2(t2)
{}
template <typename Context, typename Iterator>
struct attribute
{
typedef int type; // Number of parsed chars.
};
template <typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse(Iterator& first, Iterator const& last
, Context& /*context*/, Skipper const& skipper
, Attribute& attr) const
{
boost::spirit::qi::skip_over(first, last, skipper);
int count = 0;
Iterator it = first;
typedef typename std::iterator_traits<Iterator>::value_type Char;
for (T1 t = 0; t < t1; t++, count++)
if (it == last || *it++ != Char('+'))
return false;
for (T2 t = 0; t < t2; t++, count++)
if (it == last || *it++ != Char('-'))
return false;
boost::spirit::traits::assign_to(count, attr);
first = it;
return true;
}
template <typename Context>
boost::spirit::qi::info what(Context& /*context*/) const
{
return boost::spirit::qi::info("ops_2");
}
const T1 t1;
const T2 t2;
};
template <typename T1, typename T2, typename T3>
struct ops_3_parser
: boost::spirit::qi::primitive_parser<ops_3_parser<T1, T2, T3> >
{
ops_3_parser(T1 t1, T2 t2, T3 t3)
: t1(t1)
, t2(t2)
, t3(t3)
{}
template <typename Context, typename Iterator>
struct attribute
{
typedef int type; // Number of parsed chars.
};
template <typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse(Iterator& first, Iterator const& last
, Context& /*context*/, Skipper const& skipper
, Attribute& attr) const
{
boost::spirit::qi::skip_over(first, last, skipper);
int count = 0;
Iterator it = first;
typedef typename std::iterator_traits<Iterator>::value_type Char;
for (T1 t = 0; t < t1; t++, count++)
if (it == last || *it++ != Char('+'))
return false;
for (T2 t = 0; t < t2; t++, count++)
if (it == last || *it++ != Char('-'))
return false;
for (T3 t = 0; t < t3; t++, count++)
if (it == last || *it++ != Char('*'))
return false;
boost::spirit::traits::assign_to(count, attr);
first = it;
return true;
}
template <typename Context>
boost::spirit::qi::info what(Context& /*context*/) const
{
return boost::spirit::qi::info("ops_3");
}
const T1 t1;
const T2 t2;
const T3 t3;
};
#ifdef _MSC_VER
# pragma warning(pop)
#endif
}
namespace boost { namespace spirit
{
///////////////////////////////////////////////////////////////////////////
// Enablers
///////////////////////////////////////////////////////////////////////////
template <typename T1>
struct use_terminal<qi::domain
, terminal_ex<testns::tag::ops, fusion::vector1<T1> > >
: mpl::true_ {};
template <typename T1, typename T2>
struct use_terminal<qi::domain
, terminal_ex<testns::tag::ops, fusion::vector2<T1, T2> > >
: mpl::true_ {};
template <typename T1, typename T2, typename T3>
struct use_terminal<qi::domain
, terminal_ex<testns::tag::ops, fusion::vector3<T1, T2, T3> > >
: mpl::true_ {};
template <>
struct use_lazy_terminal<qi::domain, testns::tag::ops, 1>
: mpl::true_ {};
template <>
struct use_lazy_terminal<qi::domain, testns::tag::ops, 2>
: mpl::true_ {};
template <>
struct use_lazy_terminal<qi::domain, testns::tag::ops, 3>
: mpl::true_ {};
}}
namespace boost { namespace spirit { namespace qi
{
///////////////////////////////////////////////////////////////////////////
// Parser generators: make_xxx function (objects)
///////////////////////////////////////////////////////////////////////////
template <typename Modifiers, typename T1>
struct make_primitive<
terminal_ex<testns::tag::ops, fusion::vector1<T1> >
, Modifiers>
{
typedef testns::ops_1_parser<T1> result_type;
template <typename Terminal>
result_type operator()(const Terminal& term, unused_type) const
{
return result_type(
fusion::at_c<0>(term.args)
);
}
};
template <typename Modifiers, typename T1, typename T2>
struct make_primitive<
terminal_ex<testns::tag::ops, fusion::vector2<T1, T2> >
, Modifiers>
{
typedef testns::ops_2_parser<T1, T2> result_type;
template <typename Terminal>
result_type operator()(const Terminal& term, unused_type) const
{
return result_type(
fusion::at_c<0>(term.args)
, fusion::at_c<1>(term.args)
);
}
};
template <typename Modifiers, typename T1, typename T2, typename T3>
struct make_primitive<
terminal_ex<testns::tag::ops, fusion::vector3<T1, T2, T3> >
, Modifiers>
{
typedef testns::ops_3_parser<T1, T2, T3> result_type;
template <typename Terminal>
result_type operator()(const Terminal& term, unused_type) const
{
return result_type(
fusion::at_c<0>(term.args)
, fusion::at_c<1>(term.args)
, fusion::at_c<2>(term.args)
);
}
};
}}}
namespace testns
{
template <typename T1, typename T>
void check_type_1(const T& /*t*/)
{
BOOST_STATIC_ASSERT(( boost::is_same<T
, typename boost::spirit::terminal<testns::tag::ops>::result<T1>::type >::value ));
}
template <typename T1, typename T2, typename T>
void check_type_2(const T& /*t*/)
{
BOOST_STATIC_ASSERT(( boost::is_same<T
, typename boost::spirit::terminal<testns::tag::ops>::result<T1, T2>::type >::value ));
}
template <typename T1, typename T2, typename T3, typename T>
void check_type_3(const T& /*t*/)
{
BOOST_STATIC_ASSERT(( boost::is_same<T
, typename boost::spirit::terminal<testns::tag::ops>::result<T1, T2, T3>::type >::value ));
}
}
int
main()
{
using spirit_test::test_attr;
using spirit_test::test;
using testns::ops;
using testns::check_type_1;
using testns::check_type_2;
using testns::check_type_3;
{ // immediate args
int c = 0;
#define IP1 ops(2)
check_type_1<int>(IP1);
BOOST_TEST(test_attr("++/", IP1 >> '/', c) && c == 2);
c = 0;
#define IP2 ops(2, 3)
check_type_2<int, int>(IP2);
BOOST_TEST(test_attr("++---/", IP2 >> '/', c) && c == 5);
c = 0;
#define IP3 ops(2, 3, 4)
check_type_3<int, int, int>(IP3);
BOOST_TEST(!test("++---***/", IP3 >> '/'));
#define IP4 ops(2, 3, 4)
check_type_3<int, int, int>(IP4);
BOOST_TEST(test_attr("++---****/", IP4 >> '/', c) && c == 9);
}
using boost::phoenix::val;
using boost::phoenix::actor;
using boost::phoenix::expression::value;
{ // all lazy args
int c = 0;
#define LP1 ops(val(1))
check_type_1<value<int>::type>(LP1);
BOOST_TEST(test_attr("+/", LP1 >> '/', c) && c == 1);
c = 0;
#define LP2 ops(val(1), val(4))
check_type_2<value<int>::type, value<int>::type>(LP2);
BOOST_TEST(test_attr("+----/", LP2 >> '/', c) && c == 5);
c = 0;
#define LP3 ops(val((char)2), val(3.), val(4))
check_type_3<value<char>::type, value<double>::type, value<int>::type>(LP3);
BOOST_TEST(!test("++---***/", LP3 >> '/'));
#define LP4 ops(val(1), val(2), val(3))
check_type_3<value<int>::type, value<int>::type, value<int>::type>(LP4);
BOOST_TEST(test_attr("+--***/", LP4 >> '/', c) && c == 6);
}
{ // mixed immediate and lazy args
namespace fusion = boost::fusion;
namespace phx = boost::phoenix;
int c = 0;
#define MP1 ops(val(3), 2)
check_type_2<value<int>::type, int>(MP1);
BOOST_TEST(test_attr("+++--/", MP1 >> '/', c) && c == 5);
c = 0;
#define MP2 ops(4, val(1))
check_type_2<int, value<int>::type>(MP2);
BOOST_TEST(test_attr("++++-/", MP2 >> '/', c) && c == 5);
c = 0;
#define MP3 ops(2, val(2), val(2))
check_type_3<int, value<int>::type, value<int>::type>(MP3);
BOOST_TEST(!test("++-**/", MP3 >> '/'));
#define MP4 ops(2, val(2), 2)
check_type_3<int, value<int>::type, int>(MP4);
BOOST_TEST(test_attr("++--**/", MP4 >> '/', c) && c == 6);
c = 0;
#define MP5 ops(val(5) - val(3), 2, val(2))
check_type_3<phx::expression::minus<value<int>::type, value<int>::type>::type, int, value<int>::type>(MP5);
BOOST_TEST(test_attr("++--**/", MP5 >> '/', c) && c == 6);
}
return boost::report_errors();
}
|