⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operators.hpp

📁 著名的Parser库Spirit在VC6上的Port
💻 HPP
📖 第 1 页 / 共 2 页
字号:
inline alternative<strlit<cstring<wchar_t> >, B>
operator|(wchar_t const* a, parser<B> const& b)
{
    return alternative<strlit<cstring<wchar_t> >, B>(a, b.derived());
}


///////////////////////////////////////////////////////////////////////////////
//
//  intersection class
//
//      Handles expressions of the form:
//
//          a & b
//
//      where a and b are parsers. The expression returns a composite
//      parser that matches a and b. One (not both) of the operands may
//      be a literal char, wchar_t or a primitive string char const*,
//      wchar_t const*.
//
//      The expression is short circuit evaluated. b is never touched
//      when a is returns a no-match.
//
///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>
struct intersection
:   public binary<A, B>,
    public parser<intersection<A, B> > {

    intersection(A const& a, B const& b)
    :   binary<A, B>(a, b) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        IteratorT   ls = first;
        if (match l = this->left().parse(ls, last))
        {
            IteratorT   rs = first;
            if (match r = this->right().parse(rs, last))
            {
                bool    less = l.length() < r.length();
                first = less ? rs : ls;
                return less ? r : l;
            }
        }

        return match();
    }

};

////////////////////////////////////////////
template <typename A, typename B>
inline intersection<A, B>
operator&(parser<A> const& a, parser<B> const& b)
{
    return intersection<A, B>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename A>
inline intersection<A, chlit<char> >
operator&(parser<A> const& a, char b)
{
    return intersection<A, chlit<char> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline intersection<chlit<char>, B>
operator&(char a, parser<B> const& b)
{
    return intersection<chlit<char>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline intersection<A, strlit<cstring<char> > >
operator&(parser<A> const& a, char const* b)
{
    return intersection<A, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline intersection<strlit<cstring<char> >, B>
operator&(char const* a, parser<B> const& b)
{
    return intersection<strlit<cstring<char> >, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline intersection<A, chlit<wchar_t> >
operator&(parser<A> const& a, wchar_t b)
{
    return intersection<A, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline intersection<chlit<wchar_t>, B>
operator&(wchar_t a, parser<B> const& b)
{
    return intersection<chlit<wchar_t>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline intersection<A, strlit<cstring<wchar_t> > >
operator&(parser<A> const& a, wchar_t const* b)
{
    return intersection<A, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline intersection<strlit<cstring<wchar_t> >, B>
operator&(wchar_t const* a, parser<B> const& b)
{
    return intersection<strlit<cstring<wchar_t> >, B>(a, b.derived());
}

///////////////////////////////////////////////////////////////////////////////
//
//  difference: a - b; Matches a but not b
//
//      Handles expressions of the form:
//
//          a - b
//
//      where a and b are parsers. The expression returns a composite
//      parser that matches a but not b. One (not both) of the operands
//      may be a literal char, wchar_t or a primitive string char const*,
//      wchar_t const*.
//
///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>
struct difference
:   public binary<A, B>,
    public parser<difference<A, B> > {

    difference(A const& a, B const& b)
    :   binary<A, B>(a, b) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        IteratorT   ls = first;
        IteratorT   rs = first;
        match       hit, hit2;

        if ((hit = this->left().parse(ls, last)) &&
            !(hit2 = this->right().parse(rs, last)))
        {
            first = ls;
            return hit;
        }
        if (hit2.length() < hit.length())
        {
            first = ls;
            return hit;
        }
        return match();
    }
};

//////////////////////////////////////////////////
template <typename A, typename B>
inline difference<A, B>
operator-(parser<A> const& a, parser<B> const& b)
{
    return difference<A, B>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename A>
inline difference<A, chlit<char> >
operator-(parser<A> const& a, char b)
{
    return difference<A, chlit<char> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline difference<chlit<char>, B>
operator-(char a, parser<B> const& b)
{
    return difference<chlit<char>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline difference<A, strlit<cstring<char> > >
operator-(parser<A> const& a, char const* b)
{
    return difference<A, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline difference<strlit<cstring<char> >, B>
operator-(char const* a, parser<B> const& b)
{
    return difference<strlit<cstring<char> >, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline difference<A, chlit<wchar_t> >
operator-(parser<A> const& a, wchar_t b)
{
    return difference<A, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline difference<chlit<wchar_t>, B>
operator-(wchar_t a, parser<B> const& b)
{
    return difference<chlit<wchar_t>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline difference<A, strlit<cstring<wchar_t> > >
operator-(parser<A> const& a, wchar_t const* b)
{
    return difference<A, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline difference<strlit<cstring<wchar_t> >, B>
operator-(wchar_t const* a, parser<B> const& b)
{
    return difference<strlit<cstring<wchar_t> >, B>(a, b.derived());
}


///////////////////////////////////////////////////////////////////////////////
//
//  exclusive_or class
//
//      Handles expressions of the form:
//
//          a ^ b
//
//      where a and b are parsers. The expression returns a composite
//      parser that matches a or b but not both. One (not both) of the
//      operands may be a literal char, wchar_t or a primitive string
//      char const*, wchar_t const*.
//
///////////////////////////////////////////////////////////////////////////////
template <typename A, typename B>
struct exclusive_or
:   public binary<A, B>,
    public parser<exclusive_or<A, B> > {

    exclusive_or(A const& a, B const& b)
    :   binary<A, B>(a, b) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        IteratorT   ls = first;
        IteratorT   rs = first;
        match       l = this->left().parse(ls, last);
        match       r = this->right().parse(rs, last);

        if (bool(l) ^ bool(r))
        {
            first = l ? ls : rs;
            return l ? l : r;
        }

        return match();
    }

};

////////////////////////////////
template <typename A, typename B>
inline exclusive_or<A, B>
operator^(parser<A> const& a, parser<B> const& b)
{
    return exclusive_or<A, B>(a.derived(), b.derived());
}

//////////////////////////////////
template <typename A>
inline exclusive_or<A, chlit<char> >
operator^(parser<A> const& a, char b)
{
    return exclusive_or<A, chlit<char> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline exclusive_or<chlit<char>, B>
operator^(char a, parser<B> const& b)
{
    return exclusive_or<chlit<char>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline exclusive_or<A, strlit<cstring<char> > >
operator^(parser<A> const& a, char const* b)
{
    return exclusive_or<A, strlit<cstring<char> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline exclusive_or<strlit<cstring<char> >, B>
operator^(char const* a, parser<B> const& b)
{
    return exclusive_or<strlit<cstring<char> >, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline exclusive_or<A, chlit<wchar_t> >
operator^(parser<A> const& a, wchar_t b)
{
    return exclusive_or<A, chlit<wchar_t> >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline exclusive_or<chlit<wchar_t>, B>
operator^(wchar_t a, parser<B> const& b)
{
    return exclusive_or<chlit<wchar_t>, B>(a, b.derived());
}

//////////////////////////////////
template <typename A>
inline exclusive_or<A, strlit<cstring<wchar_t> > >
operator^(parser<A> const& a, wchar_t const* b)
{
    return exclusive_or<A, strlit<cstring<wchar_t> > >(a.derived(), b);
}

//////////////////////////////////
template <typename B>
inline exclusive_or<strlit<cstring<wchar_t> >, B>
operator^(wchar_t const* a, parser<B> const& b)
{
    return exclusive_or<strlit<cstring<wchar_t> >, B>(a, b.derived());
}


///////////////////////////////////////////////////////////////////////////////
//
//  optional class
//
//      Handles expressions of the form:
//
//          !a
//
//      where a is a parser. The expression returns a composite
//      parser that matches its subject zero (0) or one (1) time.
//
///////////////////////////////////////////////////////////////////////////////
template <typename S>
struct optional
:   public unary<S>,
    public parser<optional<S> > {

    optional(S const& a)
    :   unary<S>(a) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        IteratorT s = first;
        match   r = this->subject().parse(s, last);
        if (r)
        {
            first = s;
            return r;
        }
        else
        {
            return match(0);
        }
    }
};

//////////////////////////////
template <typename S>
optional<S>
operator!(parser<S> const& a)
{
    return optional<S>(a.derived());
}


///////////////////////////////////////////////////////////////////////////////
//
//  kleene_star class
//
//      Handles expressions of the form:
//
//          *a
//
//      where a is a parser. The expression returns a composite
//      parser that matches its subject zero (0) or more times.
//
///////////////////////////////////////////////////////////////////////////////
template <typename S>
struct kleene_star
:   public unary<S>,
    public parser<kleene_star<S> > {

    kleene_star(S const& a)
    :   unary<S>(a) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        match hit(0);
        while (true)
        {
            IteratorT s = first;
            match next = this->subject().parse(s, last);
            if (next)
            {
                first = s;
                hit += next;
            }
            else
            {
                return hit;
            }
        }
    }
};

//////////////////////////////////
template <typename S>
inline kleene_star<S>
operator*(parser<S> const& a)
{
    return kleene_star<S>(a.derived());
}


///////////////////////////////////////////////////////////////////////////////
//
//  positive class
//
//      Handles expressions of the form:
//
//          +a
//
//      where a is a parser. The expression returns a composite
//      parser that matches its subject one (1) or more times.
//
///////////////////////////////////////////////////////////////////////////////
template <typename S>
struct positive
:   public unary<S>,
    public parser<positive<S> > {

    positive(S const& a)
    :   unary<S>(a) {}

    template <typename IteratorT>
    match
    parse(IteratorT& first, IteratorT const& last) const
    {
        match   hit = this->subject().parse(first, last);
        if (hit)
        {
            while (true)
            {
                IteratorT s = first;
                match next = this->subject().parse(s, last);
                if (next)
                {
                    first = s;
                    hit += next;
                }
                else
                {
                    break;
                }
            }
        }
        return hit;
    }
};

//////////////////////////////////
template <typename S>
inline positive<S>
operator+(parser<S> const& a)
{
    return positive<S>(a.derived());
}

///////////////////////////////////////////////////////////////////////////////
}   //  namespace Spirit

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -