📄 rule.ipp
字号:
/*=============================================================================
The Production rule
Spirit V1.2
Copyright (c) 2001, Joel de Guzman
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_RULE_IPP
#define SPIRIT_RULE_IPP
///////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include "boost/spirit/rule.hpp"
#include "boost/spirit/primitives.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
namespace impl {
///////////////////////////////////////////////////////////////////////////////
//
// abstract_parser class
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename MatchTraitsT>
class abstract_parser {
public:
abstract_parser();
virtual ~abstract_parser();
virtual match
parse(IteratorT& first, IteratorT const& last) const = 0;
virtual typename MatchTraitsT::match_t
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const = 0;
};
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT>
inline abstract_parser<IteratorT, MatchTraitsT>::abstract_parser() {}
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT>
abstract_parser<IteratorT, MatchTraitsT>::~abstract_parser() {}
///////////////////////////////////////////////////////////////////////////////
//
// concrete_parser class
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
class concrete_parser
: public ParserT,
public abstract_parser<IteratorT, MatchTraitsT> {
public:
concrete_parser(ParserT const& parser);
virtual ~concrete_parser();
virtual match
parse(IteratorT& first, IteratorT const& last) const;
virtual typename MatchTraitsT::match_t
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const;
};
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
inline concrete_parser<ParserT, IteratorT, MatchTraitsT>::concrete_parser(
ParserT const& parser)
: ParserT(parser) {}
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
concrete_parser<ParserT, IteratorT, MatchTraitsT>::~concrete_parser() {}
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
match
concrete_parser<ParserT, IteratorT, MatchTraitsT>::
parse(IteratorT& first, IteratorT const& last) const
{
return ParserT::parse(first, last);
}
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
typename MatchTraitsT::match_t
concrete_parser<ParserT, IteratorT, MatchTraitsT>::
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const
{
return ParserT::ast_parse(first, last, mt);
}
///////////////////////////////////////////////////////////////////////////////
//
// rule_alias class
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
class rule_alias : public abstract_parser<IteratorT, MatchTraitsT> {
public:
rule_alias(base_rule<IteratorT, MatchTraitsT, DerivedT> const& alias);
virtual ~rule_alias();
virtual match
parse(IteratorT& first, IteratorT const& last) const;
virtual typename MatchTraitsT::match_t
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const;
private:
base_rule<IteratorT, MatchTraitsT, DerivedT> const& alias;
};
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
inline rule_alias<IteratorT, MatchTraitsT, DerivedT>::rule_alias(
base_rule<IteratorT, MatchTraitsT, DerivedT> const& alias_)
: alias(alias_) {}
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
rule_alias<IteratorT, MatchTraitsT, DerivedT>::~rule_alias() {}
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
match
rule_alias<IteratorT, MatchTraitsT, DerivedT>::
parse(IteratorT& first, IteratorT const& last) const
{
return alias.parse(first, last);
}
//////////////////////////////////
template <typename IteratorT, typename MatchTraitsT, typename DerivedT>
typename MatchTraitsT::match_t
rule_alias<IteratorT, MatchTraitsT, DerivedT>::
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const
{
return alias.ast_parse(first, last, mt);
}
///////////////////////////////////////////////////////////////////////////////
//
// alt_parser class
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
class alt_parser : public abstract_parser<IteratorT, MatchTraitsT> {
public:
alt_parser(abstract_parser<IteratorT, MatchTraitsT>* left,
ParserT const& right);
virtual ~alt_parser();
virtual match
parse(IteratorT& first, IteratorT const& last) const;
virtual typename MatchTraitsT::match_t
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const;
private:
abstract_parser<IteratorT, MatchTraitsT>* left;
ParserT right;
};
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
inline alt_parser<ParserT, IteratorT, MatchTraitsT>::alt_parser(
abstract_parser<IteratorT, MatchTraitsT>* left_, ParserT const& right_)
: left(left_),
right(right_) {}
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
alt_parser<ParserT, IteratorT, MatchTraitsT>::~alt_parser()
{
delete left;
}
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
match
alt_parser<ParserT, IteratorT, MatchTraitsT>::
parse(IteratorT& first, IteratorT const& last) const
{
if (match hit = left->parse(first, last))
return hit;
return right.parse(first, last);
}
//////////////////////////////////
template <typename ParserT, typename IteratorT, typename MatchTraitsT>
typename MatchTraitsT::match_t
alt_parser<ParserT, IteratorT, MatchTraitsT>::
ast_parse(IteratorT& first, IteratorT const& last,
MatchTraitsT const& mt) const
{
if (typename MatchTraitsT::match_t hit =
left->ast_parse(first, last, mt))
return hit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -