📄 primitives.hpp
字号:
/*=============================================================================
Parser primitives
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 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_PRIMITIVES_HPP
#define SPIRIT_PRIMITIVES_HPP
///////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <cctype>
#if __GNUC__ > 2 || defined(__MWERKS__)
#include <cwctype>
#endif
#include <string>
#include "boost/spirit/spirit_fwd.hpp"
#include "boost/spirit/MSVC/parser.hpp"
#include "boost/spirit/MSVC/composite.hpp"
#include "boost/type_traits.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// char_action class
//
// Links a character parser with a user defined semantic action.
// The semantic action may be a function or a functor. A function
// should be compatible with the interface:
//
// void f(CharT ch);
//
// A functor should have a member operator() with a compatible signature
// as above. The matching character is passed into the function/functor.
// This is the default class that character parsers use when dealing with
// the construct:
//
// p[f]
//
// where p is a parser and f is a function or functor.
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT>
class char_action
: public unary<ParserT>,
public parser<char_action<ParserT, ActionT> > {
public:
typedef action_parser_category parser_category;
char_action(ParserT const& subject, ActionT const& actor_)
: unary<ParserT>(subject),
actor(actor_) {}
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
if (first != last)
{
typedef impl::strip_scanner<IteratorT> strip_scanner;
typename strip_scanner::iterator_type
begin = strip_scanner::get(first);
if (match hit = this->subject().parse(first, last))
{
actor(*begin);
return hit;
}
}
return match();
}
private:
typename embed_trait<ActionT>::type actor;
};
///////////////////////////////////////////////////////////////////////////////
//
// char_parser class
//
///////////////////////////////////////////////////////////////////////////////
template <typename DerivedT>
struct char_parser : public parser<DerivedT> {
template <typename ActionT>
char_action<DerivedT, ActionT>
operator[](ActionT const& actor) const
{
return char_action<DerivedT, ActionT>(this->derived(), actor);
}
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
if (first != last)
if ((this->derived()).test(*first))
{
++first;
return match(1);
}
return match();
}
};
///////////////////////////////////////////////////////////////////////////////
//
// chlit class
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT = char>
struct chlit : public char_parser<chlit<CharT> > {
// specifying public makes no sense at all .. but VC++6.0
// thinks that test is a private method .
public :
chlit(CharT ch_) : ch(ch_) {}
template <typename T>
bool test(T ch_) const
{
T ch_test = ch;
return ch_ == ch_test;
//return T(ch) == ch_;
}
CharT ch;
};
///////////////////////////////////////////////////////////////////////////////
//
// range class
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharAT = char, typename CharBT = CharAT>
struct range : public char_parser<range<CharAT, CharBT> > {
range(CharAT first_, CharBT last_) : first(first_) , last(last_)
{
assert(first <= last);
}
template <typename T>
bool test(T ch) const
{
return (CharAT(ch) >= first) && (CharBT(ch) <= last);
}
CharAT first;
CharBT last;
};
///////////////////////////////////////////////////////////////////////////////
//
// cstring class
//
///////////////////////////////////////////////////////////////////////////////
template <typename CharT = char>
class cstring {
public:
typedef CharT const* const_iterator;
typedef CharT value_type;
cstring(const_iterator str, unsigned len)
: first(str), last(str + len)
{
}
cstring(const_iterator str)
: first(str)
{
while (*str)
str++;
last = str;
}
const_iterator begin() const { return first;}
const_iterator end() const { return last; }
//std::size_t length() const { return last - first ;}
size_t length() const { return last - first ;}
private:
const_iterator first;
const_iterator last;
};
///////////////////////////////////////////////////////////////////////////////
//
// strlit class
//
///////////////////////////////////////////////////////////////////////////////
template <typename StringT = cstring<char> >
struct strlit : public parser<strlit<StringT> > {
// specifying public makes no sense at all .. but VC++6.0
// thinks that parse is a private method .
public :
////////////////////////////////////////////////////////////////////
typename typedef impl::IF<is_reference_wrapper<StringT>::value,
typename unwrap_reference_wrapper<StringT>::type&,
StringT>::RET value_type;
typedef typename unwrap_reference_wrapper<StringT>::type unwrapped_type;
typedef typename boost::remove_reference<unwrapped_type>::type string_type;
typedef typename remove_wrap<string_type>::type actual_type;
typedef typename actual_type::const_iterator raw_string_type;
/////////////////////////////////////////////////////////////////
strlit( StringT str_) : str(str_) {}
strlit(raw_string_type str_) : str(str_) {}
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
typedef typename impl::strip_scanner<IteratorT>
::iterator_type plain_iter;
plain_iter i1 = impl::strip_scanner<IteratorT>::get(first);
plain_iter i2 = impl::strip_scanner<IteratorT>::get(last);
actual_type s = str;
raw_string_type strEnd = s.end();
for (raw_string_type strFirst = s.begin();
strFirst != strEnd; ++strFirst, ++i1)
{
if ((i1 == i2) || ((*strFirst) != (*i1)))
return match();
}
first = i1;
return match(s.length());
}
value_type str;
};
///////////////////////////////////////////////////////////////////////////////
//
// Convenience functions
//
///////////////////////////////////////////////////////////////////////////////
//
// On some systems the is* functions are defined as macros
#if defined(_MSC_VER) || _STLPORT_VERSION >= 0x450
// is* functions already setup
#else
#ifndef isalnum
inline bool isalnum(int c) { return std::isalnum(c); }
#endif
#ifndef isalpha
inline bool isalpha(int c) { return std::isalpha(c); }
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -