utilities.ipp

来自「著名的Parser库Spirit在VC6上的Port」· IPP 代码 · 共 1,305 行 · 第 1/4 页

IPP
1,305
字号
                                }
                            }
                            unescaped = hex;
                        }

                        break;

                    default:
                        if (Flags & escape_flags::c_escapes)
                        {
                            // undefined escape invalid for c
                            return MatchTraitsT::no_match; 
                        }
                        else
                        {
                            unescaped = *first;
                            ++first;
                        }
                        break;
                }
            }
            else
            {
                unescaped = *first;
                ++first;
            }

            actor(unescaped);
            return hit;
        }
    }
    return MatchTraitsT::no_match; // overflow detected
}

//////////////////////////////////
template <unsigned long Flags>
template <typename IteratorT>
inline match
escape_char_parser<Flags>::parse(IteratorT& first, IteratorT const& last) const
{
    static range<> octal('0','7');
    static rule<IteratorT> escape
        =   +octal
        |   nocase['x'] >> +xdigit
        |   (anychar - nocase['x'] - octal);

    return ((anychar - '\\') | ('\\' >> escape)).parse(first, last);
}

//////////////////////////////////
template <unsigned long Flags>
template <typename IteratorT, typename MatchTraitsT>
inline typename MatchTraitsT::match_t
escape_char_parser<Flags>::ast_parse(IteratorT& first, IteratorT const& last, 
        MatchTraitsT const& mt) const
{
    static range<> octal('0','7');
    static rule<IteratorT> escape
        =   +octal
        |   nocase['x'] >> +xdigit
        |   (anychar - nocase['x'] - octal);

    return ((anychar - '\\') | ('\\' >> escape)).ast_parse(first, last, mt);
}

/*=============================================================================

    confix parsers implementation [Hartmut Kaiser]

=============================================================================*/

///////////////////////////////////////////////////////////////////////////////
//
//  confix_parser class implementation
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {

    template <typename NestedT, typename CategoryT>
    struct confix_parser_type;

///////////////////////////////////////////////////////////////////////////////
// There is nothing special for plain_parser_category types as ExprT.
    template <>
    struct confix_parser_type<nested, plain_parser_category> {

        template <
            typename IteratorT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static match
        parse(
            IteratorT& first, IteratorT const& last, ThisT const& this_,
            OpenT const& open, ExprT const& expr, CloseT const& close)
        {
            return (open >> *(this_ | expr - close) >> close).parse(
                    first, last);
        }

        template <
            typename IteratorT, typename MatchTraitsT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static typename MatchTraitsT::match_t
        ast_parse(
            IteratorT& first, IteratorT const& last, MatchTraitsT const& mt, 
            ThisT const& this_, OpenT const& open, ExprT const& expr, 
            CloseT const& close)
        {
            return (open >> *(this_ | expr - close) >> close).ast_parse(
                    first, last, mt);
        }
    };

    template <>
    struct confix_parser_type<non_nested, plain_parser_category> {

        template <
            typename IteratorT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static match
        parse(
            IteratorT& first, IteratorT const& last, ThisT const& /*this_*/,
            OpenT const& open, ExprT const& expr, CloseT const& close)
        {
            return (open >> *(expr - close) >> close).parse(first, last);
        }

        template <
            typename IteratorT, typename MatchTraitsT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static typename MatchTraitsT::match_t
        ast_parse(
            IteratorT& first, IteratorT const& last, MatchTraitsT const& mt, 
            ThisT const& /*this_*/, OpenT const& open, ExprT const& expr, 
            CloseT const& close)
        {
            return (open >> *(expr - close) >> close).ast_parse(
                    first, last, mt);
        }
    };

///////////////////////////////////////////////////////////////////////////////
// action_parser_category types as ExprT require some special handling
    template <>
    struct confix_parser_type<nested, action_parser_category> {

        template <
            typename IteratorT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static match
        parse(
            IteratorT& first, IteratorT const& last, ThisT const& this_,
            OpenT const& open, ExprT const& expr, CloseT const& close)
        {
            return (
                    open
                >>  (*(this_ | expr.subject() - close))[expr.predicate()]
                >>  close
            ).parse(first, last);
        }

        template <
            typename IteratorT, typename MatchTraitsT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static typename MatchTraitsT::match_t
        ast_parse(
            IteratorT& first, IteratorT const& last, MatchTraitsT const& mt, 
            ThisT const& this_, OpenT const& open, ExprT const& expr, 
            CloseT const& close)
        {
            return (
                    open
                >>  (*(this_ | expr.subject() - close))[expr.predicate()]
                >>  close
            ).ast_parse(first, last, mt);
        }
    };

    template <>
    struct confix_parser_type<non_nested, action_parser_category> {

        template <
            typename IteratorT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static match
        parse(
            IteratorT& first, IteratorT const& last, ThisT const& /*this_*/,
            OpenT const& open, ExprT const& expr, CloseT const& close)
        {
            return (
                    open
                >>  (*(expr.subject() - close))[expr.predicate()]
                >>  close
            ).parse(first, last);
        }

        template <
            typename IteratorT, typename MatchTraitsT, typename ThisT,
            typename OpenT, typename ExprT, typename CloseT
        >
        static typename MatchTraitsT::match_t
        ast_parse(
            IteratorT& first, IteratorT const& last, MatchTraitsT const& mt, 
            ThisT const& /*this_*/, OpenT const& open, ExprT const& expr, 
            CloseT const& close)
        {
            return (
                    open
                >>  (*(expr.subject() - close))[expr.predicate()]
                >>  close
            ).ast_parse(first, last, mt);
        }
    };
}   // end of namespace impl

///////////////////////////////////////////////////////////////////////////////
//
// confix_parser class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <
    typename OpenT, typename ExprT, typename CloseT,
    typename NestedT, typename CategoryT
>
inline confix_parser<OpenT, ExprT, CloseT, NestedT, CategoryT>::confix_parser(
    OpenT const &open_, ExprT const &expr_, CloseT const &close_) :
    open(open_), expr(expr_), close(close_)
{
}

template <
    typename OpenT, typename ExprT, typename CloseT,
    typename NestedT, typename CategoryT
>
template <typename IteratorT>
inline match
confix_parser<OpenT, ExprT, CloseT, NestedT, CategoryT>
    ::parse(IteratorT& first, IteratorT const& last) const
{
    return impl::confix_parser_type<NestedT, CategoryT>::
        parse(first, last, *this, open, expr, close);
}

template <
    typename OpenT, typename ExprT, typename CloseT,
    typename NestedT, typename CategoryT
>
template <typename IteratorT, typename MatchTraitsT>
inline typename MatchTraitsT::match_t
confix_parser<OpenT, ExprT, CloseT, NestedT, CategoryT>
    ::ast_parse(IteratorT& first, IteratorT const& last, 
            MatchTraitsT const& mt) const
{
    return impl::confix_parser_type<NestedT, CategoryT>::
        ast_parse(first, last, mt, *this, open, expr, close);
}

///////////////////////////////////////////////////////////////////////////////
//
//  Generic generator function for creation of concrete confix parsers
//
///////////////////////////////////////////////////////////////////////////////
template<typename NestedT>
template<typename StartT, typename ExprT, typename EndT>
inline confix_parser<
    typename impl::parser_type<StartT>::parser_t,
    typename impl::parser_type<ExprT>::parser_t,
    typename impl::parser_type<EndT>::parser_t,
    NestedT,
    typename impl::parser_type<ExprT>::parser_t::parser_category
>
confix_parser_gen<NestedT>::operator()(
    StartT const &start_, ExprT const &expr_, EndT const &end_) const
{
    typedef typename impl::parser_type<StartT>::parser_t start_t;
    typedef typename impl::parser_type<ExprT>::parser_t expr_t;
    typedef typename impl::parser_type<EndT>::parser_t end_t;
    typedef
        typename impl::parser_type<ExprT>::parser_t::parser_category
        parser_category;

    typedef
        confix_parser<start_t, expr_t, end_t, NestedT, parser_category>
        return_t;

    return return_t(
                impl::parser_type<StartT>::get(start_),
                impl::parser_type<ExprT>::get(expr_),
                impl::parser_type<EndT>::get(end_)
           );
}

///////////////////////////////////////////////////////////////////////////////
//
//  Generic generator function for creation of concrete confix parsers
//  which have a action directly attached to the ExprT part of the parser
//  (see comment in utilities.hpp).
//
///////////////////////////////////////////////////////////////////////////////
template<typename NestedT>
template<typename StartT, typename ExprT, typename EndT>
inline confix_parser<
    typename impl::parser_type<StartT>::parser_t,
    typename impl::parser_type<ExprT>::parser_t,
    typename impl::parser_type<EndT>::parser_t,
    NestedT,
    plain_parser_category    // do not re-attach the actor
>
confix_parser_gen<NestedT>::direct(
    StartT const &start_, ExprT const &expr_, EndT const &end_) const
{
    typedef typename impl::parser_type<StartT>::parser_t start_t;
    typedef typename impl::parser_type<ExprT>::parser_t expr_t;
    typedef typename impl::parser_type<EndT>::parser_t end_t;
    typedef plain_parser_category parser_category;

    typedef
        confix_parser<start_t, expr_t, end_t, NestedT, parser_category>
        return_t;

    return return_t(

⌨️ 快捷键说明

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