📄 attr_rule.hpp
字号:
/*=============================================================================
Attributes rules
Spirit V1.3.1
Copyright (c) 2001, Hartmut Kaiser
This software is provided 'as-is', without any express or implied
warranty. In no event will the copyright holder be held liable for
any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Acknowledgements:
Special thanks to Dan Nuffer, John (EBo) David, Chris Uzdavinis,
and Doug Gregor. These people are most instrumental in steering
Spirit in the right direction.
Special thanks also to people who have contributed to the code base
and sample code, ported Spirit to various platforms and compilers,
gave suggestions, reported and provided bug fixes. Alexander
Hirner, Andy Elvey, Bogdan Kushnir, Brett Calcott, Bruce Florman,
Changzhe Han, Colin McPhail, Hakki Dogusan, Jan Bares, Joseph
Smith, Martijn W. van der Lee, Raghavendra Satish, Remi Delcos, Tom
Spilman, Vladimir Prus, W. Scott Dillman, David A. Greene, Bob
Bailey, Hartmut Kaiser.
Finally special thanks also to people who gave feedback and
valuable comments, particularly members of Spirit's Source Forge
mailing list and boost.org.
URL: http://spirit.sourceforge.net/
=============================================================================*/
#ifndef SPIRIT_ATTR_RULE_HPP
#define SPIRIT_ATTR_RULE_HPP
///////////////////////////////////////////////////////////////////////////////
#include "boost/spirit/MSVC/closure.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// attr_match class
//
// Is returned by the parse function of a closure_parser. Contains
// additionally the value of the closure_member<0>, which by design is
// used as the return value of a closure_parser.
//
// To access this value the code has to look like this:
//
// closure<string> loc;
// closure_member<0> str;
// rule<> r = loc[
// ... some rule definition which refers to 'loc(str)' ...
// ][actor];
//
// Where 'actor' is some action function or actor object, which takes
// one argument of the type of closure_member<0> (here: 'string').
//
// For closures embedded in a grammar the return value is automatically
// propagated to the related grammar object, where it is saved for later
// access.
//
///////////////////////////////////////////////////////////////////////////////
template <typename TypeT, typename BaseT = class match>
class attr_match :
public BaseT
{
public:
attr_match();
explicit attr_match(unsigned length);
template <typename OtherTypeT>
attr_match(attr_match<OtherTypeT, BaseT> const &hit)
: value(hit.attr_value()), BaseT(hit) {}
explicit attr_match(BaseT const &hit);
// explicit attr_match(match const &hit);
attr_match &operator += (match const& other);
TypeT const& attr_value() const;
void attr_value(TypeT const &val);
private:
TypeT value;
};
///////////////////////////////////////////////////////////////////////////////
// friend operators
template <typename TypeT, typename BaseT>
attr_match<TypeT, BaseT>
operator+ (attr_match<TypeT, BaseT> const& a, match const& b);
template <typename TypeT, typename BaseT>
attr_match<TypeT, BaseT>
operator+ (match const& a, attr_match<TypeT, BaseT> const& b);
///////////////////////////////////////////////////////////////////////////////
//
// param_parser class
//
// Helper template for initializing different closure_members before the
// entering the parsing context of the corresponding parser.
//
///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT, typename InitTupleT>
struct param_parser :
public unary<ParserT>,
public parser<param_parser<TupleT, ParserT, InitTupleT> >
{
typedef TupleT tuple_t;
typedef typename boost::tuples::element<0, TupleT>::type return_t;
typedef param_parser_category parser_category;
param_parser(InitTupleT const &init, TupleT *&ptr, ParserT const& parser);
template <typename IteratorT>
attr_match<return_t>
parse(IteratorT& first, IteratorT const& last) const
{
#ifdef SPIRIT_DEBUG
impl::local_saver<TupleT> l(ptr);
print_closure_info(false, debug.level, false, debug.name() + " (before init)", ptr);
*ptr = init;
print_closure_info(false, debug.level, false, debug.name() + " (after init)", ptr);
int save_level = debug.level;
debug.level++;
attr_match<return_t> hit (this->subject().parse(first, last));
debug.level = save_level;
print_closure_info(false, debug.level, false, debug.name(), ptr);
hit.attr_value(boost::tuples::get<0>(*ptr));
return hit;
#else
impl::local_saver<TupleT> l(ptr);
*ptr = init;
attr_match<return_t> hit (this->subject().parse(first, last));
hit.attr_value(boost::tuples::get<0>(*ptr));
return hit;
#endif
}
////////////////////////////////////////////////////////////////////////////////
template <typename ActionT>
closure_parser_action<param_parser<TupleT, ParserT, InitTupleT>, ActionT>
operator[](ActionT const& actor) const
{
// Borland 5.5 reports an internal compiler
// error if this is not defined here.
typedef closure_parser_action<
param_parser<TupleT, ParserT, InitTupleT>, ActionT>
actor_t;
return actor_t(*this, actor);
}
private:
TupleT *&ptr;
InitTupleT init;
};
///////////////////////////////////////////////////////////////////////////////
//
// parameters for the parsing process of the rule
//
///////////////////////////////////////////////////////////////////////////////
template <typename TupleT, typename ParserT>
struct param_parser_gen
{
param_parser_gen(TupleT *& ptr_, ParserT const &parser_)
: parser(parser_), ptr(ptr_) {}
////////////////////////////////////////////////////////////
template <typename T0>
param_parser<
TupleT, ParserT,
typename boost::tuples::tuple<T0>
>
operator()(T0 const &t0)
{
typedef typename boost::tuples::tuple<T0>
init_tuple_t;
return param_parser<TupleT, ParserT, init_tuple_t>(
boost::tuples::make_tuple(t0), ptr, parser);
}
////////////////////////////////////////////////////////////
template <typename T0, typename T1>
param_parser<
TupleT, ParserT,
typename boost::tuples::tuple<T0, T1>
>
operator()(T0 const &t0, T1 const &t1)
{
typedef typename boost::tuples::tuple<T0,T1>
init_tuple_t;
return param_parser<TupleT, ParserT, init_tuple_t>(
boost::tuples::make_tuple(t0,t1), ptr, parser);
}
////////////////////////////////////////////////////////////
template <typename T0, typename T1, typename T2>
param_parser<
TupleT, ParserT,
typename boost::tuples::tuple<T0, T1, T2>
>
operator()(T0 const &t0, T1 const &t1, T2 const &t2)
{
typedef typename boost::tuples::tuple<T0,T1,T2>
init_tuple_t;
return param_parser<TupleT, ParserT, init_tuple_t>(
boost::tuples::make_tuple(t0,t1,t2), ptr, parser);
}
////////////////////////////////////////////////////////////
template <typename T0, typename T1, typename T2, typename T3>
param_parser<
TupleT, ParserT,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -