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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*=============================================================================
Copyright (c) 2009 Francois Barel
Copyright (c) 2001-2011 Joel de Guzman
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)
==============================================================================*/
#if !defined(BOOST_SPIRIT_REPOSITORY_QI_SUBRULE_AUGUST_06_2009_0239AM)
#define BOOST_SPIRIT_REPOSITORY_QI_SUBRULE_AUGUST_06_2009_0239AM
#if defined(_MSC_VER)
#pragma once
#endif
#include <boost/spirit/home/qi/domain.hpp>
#include <boost/spirit/home/qi/meta_compiler.hpp>
#include <boost/spirit/home/qi/parser.hpp>
#include <boost/spirit/home/qi/reference.hpp>
#include <boost/spirit/home/qi/nonterminal/detail/parameterized.hpp>
#include <boost/spirit/home/qi/nonterminal/detail/parser_binder.hpp>
#include <boost/spirit/home/support/argument.hpp>
#include <boost/spirit/home/support/assert_msg.hpp>
#include <boost/spirit/home/qi/detail/attributes.hpp>
#include <boost/spirit/home/support/info.hpp>
#include <boost/spirit/home/support/unused.hpp>
#include <boost/spirit/home/support/nonterminal/extract_param.hpp>
#include <boost/spirit/home/support/nonterminal/locals.hpp>
#include <boost/spirit/repository/home/support/subrule_context.hpp>
#include <boost/static_assert.hpp>
#include <boost/fusion/include/as_map.hpp>
#include <boost/fusion/include/at_key.hpp>
#include <boost/fusion/include/cons.hpp>
#include <boost/fusion/include/front.hpp>
#include <boost/fusion/include/has_key.hpp>
#include <boost/fusion/include/join.hpp>
#include <boost/fusion/include/make_map.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/size.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/proto/extends.hpp>
#include <boost/proto/traits.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/remove_reference.hpp>
#if defined(BOOST_MSVC)
# pragma warning(push)
# pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
#endif
///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace repository { namespace qi
{
///////////////////////////////////////////////////////////////////////////
// subrule_group_parser:
// - parser representing a group of subrule definitions (one or more),
// invokes first subrule on entry,
///////////////////////////////////////////////////////////////////////////
template <typename Defs>
struct subrule_group_parser
: spirit::qi::parser<subrule_group_parser<Defs> >
{
// Fusion associative sequence, associating each subrule ID in this
// group (as an MPL integral constant) with its definition
typedef Defs defs_type;
typedef subrule_group_parser<Defs> this_type;
explicit subrule_group_parser(Defs const& defs)
: defs(defs)
{
}
// from a subrule ID, get the type of a reference to its definition
template <int ID>
struct def_type
{
typedef mpl::int_<ID> id_type;
// If you are seeing a compilation error here, you are trying
// to use a subrule which was not defined in this group.
BOOST_SPIRIT_ASSERT_MSG(
(fusion::result_of::has_key<
defs_type const, id_type>::type::value)
, subrule_used_without_being_defined, (mpl::int_<ID>));
typedef typename
fusion::result_of::at_key<defs_type const, id_type>::type
type;
};
// from a subrule ID, get a reference to its definition
template <int ID>
typename def_type<ID>::type def() const
{
return fusion::at_key<mpl::int_<ID> >(defs);
}
template <typename Context, typename Iterator>
struct attribute
// Forward to first subrule.
: mpl::identity<
typename remove_reference<
typename fusion::result_of::front<Defs>::type
>::type::second_type::attr_type> {};
template <typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse(Iterator& first, Iterator const& last
, Context& context, Skipper const& skipper
, Attribute& attr) const
{
// Forward to first subrule.
return parse_subrule(fusion::front(defs).second
, first, last, context, skipper, attr);
}
template <typename Iterator, typename Context
, typename Skipper, typename Attribute, typename Params>
bool parse(Iterator& first, Iterator const& last
, Context& context, Skipper const& skipper
, Attribute& attr, Params const& params) const
{
// Forward to first subrule.
return parse_subrule(fusion::front(defs).second
, first, last, context, skipper, attr, params);
}
template <int ID, typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse_subrule_id(Iterator& first, Iterator const& last
, Context& context, Skipper const& skipper
, Attribute& attr) const
{
return parse_subrule(def<ID>()
, first, last, context, skipper, attr);
}
template <int ID, typename Iterator, typename Context
, typename Skipper, typename Attribute, typename Params>
bool parse_subrule_id(Iterator& first, Iterator const& last
, Context& context, Skipper const& skipper
, Attribute& attr, Params const& params) const
{
return parse_subrule(def<ID>()
, first, last, context, skipper, attr, params);
}
template <typename Def
, typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse_subrule(Def const& def
, Iterator& first, Iterator const& last
, Context& /*caller_context*/, Skipper const& skipper
, Attribute& attr) const
{
// compute context type for this subrule
typedef typename Def::locals_type subrule_locals_type;
typedef typename Def::attr_type subrule_attr_type;
typedef typename Def::attr_reference_type subrule_attr_reference_type;
typedef typename Def::parameter_types subrule_parameter_types;
typedef
subrule_context<
this_type
, fusion::cons<
subrule_attr_reference_type, subrule_parameter_types>
, subrule_locals_type
>
context_type;
// do down-stream transformation, provides attribute for
// rhs parser
typedef traits::transform_attribute<
Attribute, subrule_attr_type, spirit::qi::domain>
transform;
typename transform::type attr_ = transform::pre(attr);
// If you are seeing a compilation error here, you are probably
// trying to use a subrule which has inherited attributes,
// without passing values for them.
context_type context(*this, attr_);
if (def.binder(first, last, context, skipper))
{
// do up-stream transformation, this integrates the results
// back into the original attribute value, if appropriate
transform::post(attr, attr_);
return true;
}
// inform attribute transformation of failed rhs
transform::fail(attr);
return false;
}
template <typename Def
, typename Iterator, typename Context
, typename Skipper, typename Attribute, typename Params>
bool parse_subrule(Def const& def
, Iterator& first, Iterator const& last
, Context& caller_context, Skipper const& skipper
, Attribute& attr, Params const& params) const
{
// compute context type for this subrule
typedef typename Def::locals_type subrule_locals_type;
typedef typename Def::attr_type subrule_attr_type;
typedef typename Def::attr_reference_type subrule_attr_reference_type;
typedef typename Def::parameter_types subrule_parameter_types;
typedef
subrule_context<
this_type
, fusion::cons<
subrule_attr_reference_type, subrule_parameter_types>
, subrule_locals_type
>
context_type;
// do down-stream transformation, provides attribute for
// rhs parser
typedef traits::transform_attribute<
Attribute, subrule_attr_type, spirit::qi::domain>
transform;
typename transform::type attr_ = transform::pre(attr);
// If you are seeing a compilation error here, you are probably
// trying to use a subrule which has inherited attributes,
// passing values of incompatible types for them.
context_type context(*this, attr_, params, caller_context);
if (def.binder(first, last, context, skipper))
{
// do up-stream transformation, this integrates the results
// back into the original attribute value, if appropriate
transform::post(attr, attr_);
return true;
}
// inform attribute transformation of failed rhs
transform::fail(attr);
return false;
}
template <typename Context>
info what(Context& context) const
{
// Forward to first subrule.
return fusion::front(defs).second.binder.p.what(context);
}
Defs defs;
};
///////////////////////////////////////////////////////////////////////////
// subrule_group:
// - a Proto terminal, so that a group behaves like any Spirit
// expression.
///////////////////////////////////////////////////////////////////////////
template <typename Defs>
struct subrule_group
: proto::extends<
typename proto::terminal<
subrule_group_parser<Defs>
>::type
, subrule_group<Defs>
>
{
typedef subrule_group_parser<Defs> parser_type;
typedef typename proto::terminal<parser_type>::type terminal;
static size_t const params_size =
// Forward to first subrule.
remove_reference<
typename fusion::result_of::front<Defs>::type
>::type::second_type::params_size;
explicit subrule_group(Defs const& defs)
: subrule_group::proto_extends(terminal::make(parser_type(defs)))
{
}
parser_type const& parser() const { return proto::value(*this); }
Defs const& defs() const { return parser().defs; }
template <typename Defs2>
subrule_group<
typename fusion::result_of::as_map<
typename fusion::result_of::join<
Defs const, Defs2 const>::type>::type>
operator,(subrule_group<Defs2> const& other) const
{
typedef subrule_group<
typename fusion::result_of::as_map<
typename fusion::result_of::join<
Defs const, Defs2 const>::type>::type> result_type;
return result_type(fusion::as_map(fusion::join(defs(), other.defs())));
}
// non-const versions needed to suppress proto's comma op kicking in
template <typename Defs2>
friend subrule_group<
typename fusion::result_of::as_map<
typename fusion::result_of::join<
Defs const, Defs2 const>::type>::type>
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
operator,(subrule_group&& left, subrule_group<Defs2>&& other)
#else
operator,(subrule_group& left, subrule_group<Defs2>& other)
#endif
{
return static_cast<subrule_group const&>(left)
.operator,(static_cast<subrule_group<Defs2> const&>(other));
}
// bring in the operator() overloads
parser_type const& get_parameterized_subject() const { return parser(); }
typedef parser_type parameterized_subject_type;
#include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
};
///////////////////////////////////////////////////////////////////////////
// subrule_definition: holds one particular definition of a subrule
///////////////////////////////////////////////////////////////////////////
template <
int ID_
, typename Locals
, typename Attr
, typename AttrRef
, typename Parameters
, size_t ParamsSize
, typename Subject
, bool Auto_
>
struct subrule_definition
{
typedef mpl::int_<ID_> id_type;
BOOST_STATIC_CONSTANT(int, ID = ID_);
typedef Locals locals_type;
typedef Attr attr_type;
typedef AttrRef attr_reference_type;
typedef Parameters parameter_types;
static size_t const params_size = ParamsSize;
typedef Subject subject_type;
typedef mpl::bool_<Auto_> auto_type;
BOOST_STATIC_CONSTANT(bool, Auto = Auto_);
typedef spirit::qi::detail::parser_binder<
Subject, auto_type> binder_type;
subrule_definition(Subject const& subject, std::string const& name)
: binder(subject), name(name)
{
}
binder_type const binder;
std::string const name;
};
///////////////////////////////////////////////////////////////////////////
// subrule placeholder:
// - on subrule definition: helper for creation of subrule_group,
// - on subrule invocation: Proto terminal and parser.
///////////////////////////////////////////////////////////////////////////
template <
int ID_
, typename T1 = unused_type
, typename T2 = unused_type
>
struct subrule
: proto::extends<
typename proto::terminal<
spirit::qi::reference<subrule<ID_, T1, T2> const>
>::type
, subrule<ID_, T1, T2>
>
, spirit::qi::parser<subrule<ID_, T1, T2> >
{
typedef mpl::int_<ID_> id_type;
BOOST_STATIC_CONSTANT(int, ID = ID_);
typedef subrule<ID_, T1, T2> this_type;
typedef spirit::qi::reference<this_type const> reference_;
typedef typename proto::terminal<reference_>::type terminal;
typedef proto::extends<terminal, this_type> base_type;
typedef mpl::vector<T1, T2> template_params;
// The subrule's locals_type: a sequence of types to be used as local variables
typedef typename
spirit::detail::extract_locals<template_params>::type
locals_type;
// The subrule's encoding type
typedef typename
spirit::detail::extract_encoding<template_params>::type
encoding_type;
// The subrule's signature
typedef typename
spirit::detail::extract_sig<template_params, encoding_type
, spirit::qi::domain>::type
sig_type;
// This is the subrule's attribute type
typedef typename
spirit::detail::attr_from_sig<sig_type>::type
attr_type;
BOOST_STATIC_ASSERT_MSG(
!is_reference<attr_type>::value,
"Reference qualifier on Qi subrule attribute type is meaningless");
typedef attr_type& attr_reference_type;
// parameter_types is a sequence of types passed as parameters to the subrule
typedef typename
spirit::detail::params_from_sig<sig_type>::type
parameter_types;
static size_t const params_size =
fusion::result_of::size<parameter_types>::type::value;
explicit subrule(std::string const& name_ = "unnamed-subrule")
: base_type(terminal::make(reference_(*this)))
, name_(name_)
{
}
// compute type of this subrule's definition for expr type Expr
template <typename Expr, bool Auto>
struct def_type_helper
{
// Report invalid expression error as early as possible.
// If you got an error_invalid_expression error message here,
// then the expression (Expr) is not a valid spirit qi expression.
BOOST_SPIRIT_ASSERT_MATCH(spirit::qi::domain, Expr);
typedef typename result_of::compile<
spirit::qi::domain, Expr>::type subject_type;
typedef subrule_definition<
ID_
, locals_type
, attr_type
, attr_reference_type
, parameter_types
, params_size
, subject_type
, Auto
> const type;
};
// compute type of subrule group containing only this
// subrule's definition for expr type Expr
template <typename Expr, bool Auto>
struct group_type_helper
{
typedef typename def_type_helper<Expr, Auto>::type def_type;
// create Defs map with only one entry: (ID -> def)
typedef typename
#ifndef BOOST_FUSION_HAS_VARIADIC_MAP
fusion::result_of::make_map<id_type, def_type>::type
#else
fusion::result_of::make_map<id_type>::template apply<def_type>::type
#endif
defs_type;
typedef subrule_group<defs_type> type;
};
template <typename Expr>
typename group_type_helper<Expr, false>::type
operator=(Expr const& expr) const
{
typedef group_type_helper<Expr, false> helper;
typedef typename helper::def_type def_type;
typedef typename helper::type result_type;
return result_type(fusion::make_map<id_type>(
def_type(compile<spirit::qi::domain>(expr), name_)));
}
#define BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(lhs_ref, rhs_ref) \
template <typename Expr> \
friend typename group_type_helper<Expr, true>::type \
operator%=(subrule lhs_ref sr, Expr rhs_ref expr) \
{ \
typedef group_type_helper<Expr, true> helper; \
typedef typename helper::def_type def_type; \
typedef typename helper::type result_type; \
return result_type(fusion::make_map<id_type>( \
def_type(compile<spirit::qi::domain>(expr), sr.name_))); \
} \
/**/
// non-const versions needed to suppress proto's %= kicking in
BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, const&)
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, &&)
#else
BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(const&, &)
#endif
BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(&, const&)
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(&, &&)
#else
BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR(&, &)
#endif
#undef BOOST_SPIRIT_SUBRULE_MODULUS_ASSIGN_OPERATOR
std::string const& name() const
{
return name_;
}
void name(std::string const& str)
{
name_ = str;
}
template <typename Context, typename Iterator>
struct attribute
{
typedef attr_type type;
};
template <typename Iterator, typename Group
, typename Attributes, typename Locals
, typename Skipper, typename Attribute>
bool parse(Iterator& first, Iterator const& last
, subrule_context<Group, Attributes, Locals>& context
, Skipper const& skipper, Attribute& attr) const
{
return context.group.template parse_subrule_id<ID_>(
first, last, context, skipper, attr);
}
template <typename Iterator, typename Context
, typename Skipper, typename Attribute>
bool parse(Iterator& /*first*/, Iterator const& /*last*/
, Context& /*context*/
, Skipper const& /*skipper*/, Attribute& /*attr*/) const
{
// If you are seeing a compilation error here, you are trying
// to use a subrule as a parser outside of a subrule group.
BOOST_SPIRIT_ASSERT_FAIL(Iterator
, subrule_used_outside_subrule_group, (id_type));
return false;
}
template <typename Iterator, typename Group
, typename Attributes, typename Locals
, typename Skipper, typename Attribute
, typename Params>
bool parse(Iterator& first, Iterator const& last
, subrule_context<Group, Attributes, Locals>& context
, Skipper const& skipper, Attribute& attr
, Params const& params) const
{
return context.group.template parse_subrule_id<ID_>(
first, last, context, skipper, attr, params);
}
template <typename Iterator, typename Context
, typename Skipper, typename Attribute
, typename Params>
bool parse(Iterator& /*first*/, Iterator const& /*last*/
, Context& /*context*/
, Skipper const& /*skipper*/, Attribute& /*attr*/
, Params const& /*params*/) const
{
// If you are seeing a compilation error here, you are trying
// to use a subrule as a parser outside of a subrule group.
BOOST_SPIRIT_ASSERT_FAIL(Iterator
, subrule_used_outside_subrule_group, (id_type));
return false;
}
template <typename Context>
info what(Context& /*context*/) const
{
return info(name_);
}
// bring in the operator() overloads
this_type const& get_parameterized_subject() const { return *this; }
typedef this_type parameterized_subject_type;
#include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
std::string name_;
};
}}}}
#if defined(BOOST_MSVC)
# pragma warning(pop)
#endif
#endif
|