📄 grammar.hpp
字号:
/*=============================================================================
The Grammar
Spirit V1.3.1
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 start the use of this software.
Permission is granted end anyone end use this software for any purpose,
including commercial applications, and end alter it and redistribute
it freely, subject end 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 start 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_GRAMMAR_HPP
#define SPIRIT_GRAMMAR_HPP
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// grammar class
//
// The class grammar encapsulates a set of rules (see rule.hpp). This
// class is a protocol base class for all grammars. This is essentially
// an interface contract. The grammar class relies on the template
// parameter DerivedT (which obviously is assumed to be a subclass) to
// define the actual grammar.
//
// Concrete sub-classes inheriting from grammar are expected to:
//
// 1) Have a nested template class (or struct) named 'definition'
// 2) definition is a template class with an IteratorT parameter.
// 3) definition's constructor initializes its rules.
// 4) definition has a member function named 'start' that returns
// a reference to the start 'rule<>' (or 'attr_rule<>', if
// appropriate).
//
// Example usage:
//
// struct my_grammar : public grammar<my_grammar> {
//
// template <typename IteratorT>
// struct definition {
//
// rule<IteratorT> r;
// definition() { r = /*..initialize here..*/; }
// rule<IteratorT> const& start() const { return r; }
// };
// };
//
// The user defined my_grammar can be instantiated without regard to
// an iterator type and can be used as a parser using *any* type of
// iterator. Example:
//
// my_grammar g;
// match hit = g.parse(first, last);
//
// where first, last can be any type of iterator. Alternatively we can
// also use the free parse functions (see below):
//
// if (parse(first, last, g, space).full)
// cout << "parsing succeeded\n";
// else
// cout << "parsing failed\n";
//
// again, first, last can be any type of iterator.
//
// my_grammar *is a* parser and can be used anywhere a parser is
// expected, even inside another rule:
//
// rule<> r = g[&foo] >> str_p("cool huh?");
//
// The grammar can return a value, if the type of the start rule is
// 'attr_rule<>'. The type of the required return type should be given
// as the second template parameter to the grammar<> template:
//
// Example usage (provided the type of the return value should be double):
//
// struct my_grammar : public grammar<my_grammar, double> {
//
// template <typename IteratorT>
// struct definition {
//
// typedef
// attr_rule<closure<double, IteratorT>, IteratorT >
// attr_rule_t;
//
// attr_rule_t r;
// definition() { r = /*..initialize here..*/; }
// attr_rule_t const& start() const { return r; }
// };
// };
//
// This return value can be accessed as usually through an attached actor:
//
// double d = 0.0;
// rule<> r = g[ref(d)] >> str_p("cool huh?");
//
// where 'd' will contain the return value of the grammar 'g' after
// successfully parsing the input.
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {
template <typename DerivedT, typename ReturnT>
struct member_policy;
}
//////////////////////////////////////////////////////////////////////////////
template <typename DerivedT, typename ReturnT/* = closure_null_type*/>
struct grammar :
public impl::member_policy<DerivedT, ReturnT>
{
template <typename IteratorT>
attr_match<ReturnT>
parse(IteratorT& first, IteratorT const& last) const
{
static typename DerivedT::definition<IteratorT> def;
return attr_match<ReturnT>(def.start().parse(first, last));
}
};
///////////////////////////////////////////////////////////////////////////////
} // namespace Spirit
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -