utilities.ipp

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

IPP
1,305
字号
                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 comment parsers
//
///////////////////////////////////////////////////////////////////////////////
template<typename NestedT>
template<typename StartT, typename EndT>
inline confix_parser<
    typename impl::parser_type<StartT>::parser_t,
    anychar_,
    typename impl::parser_type<EndT>::parser_t,
    NestedT
>
comment_parser_gen<NestedT>::operator()(
    StartT const &start_, EndT const &end_) const
{
    typedef typename impl::parser_type<StartT>::parser_t start_t;
    typedef typename impl::parser_type<EndT>::parser_t end_t;

    typedef confix_parser<start_t, anychar_, end_t, NestedT> return_t;

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

///////////////////////////////////////////////////////////////////////////////
// 
//  list_action class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename ActionT>
inline 
list_action<ParserT, ActionT>::list_action(
    ParserT const& subject, ActionT const& actor_) :
    unary<ParserT>(subject), actor(actor_)
{
}

template <typename ParserT, typename ActionT>
template <typename IteratorT>
inline match
list_action<ParserT, ActionT>::parse(IteratorT& first, 
        IteratorT const& last) const
{
    typedef impl::strip_scanner<IteratorT> strip_scanner;
    typedef list_action_iterator<ParserT, IteratorT, 
        typename ParserT::string_t> iterator_t;

    typename strip_scanner::iterator_type begin = strip_scanner::get(first);
    iterator_t list_it (&subject(), first, last);

    actor(list_it, iterator_t());
    if (list_it.matched()) {
        first = list_it.actposition();
        return match (std::distance(begin, strip_scanner::get(first)));
    }
    return match();
}

template <typename ParserT, typename ActionT>
template <typename IteratorT, typename MatchTraitsT>
inline typename MatchTraitsT::match_t
list_action<ParserT, ActionT>::ast_parse(IteratorT& first, IteratorT const& last, MatchTraitsT const& mt) const
{
    typedef impl::strip_scanner<IteratorT> strip_scanner;
    typedef list_action_iterator<ParserT, IteratorT, 
        typename ParserT::string_t> iterator_t;

    typename strip_scanner::iterator_type begin = strip_scanner::get(first);
    IteratorT savedfirst(first);
    iterator_t list_it (&subject(), first, last);

    actor(list_it, iterator_t());
    if (list_it.matched()) {
        first = list_it.actposition();
        return MatchTraitsT::create_match(
                std::distance(begin, strip_scanner::get(first)), 
                savedfirst, 
                first);
    }
    return MatchTraitsT::no_match;
}

///////////////////////////////////////////////////////////////////////////////
// 
//  list_action_iterator class implementation
//
///////////////////////////////////////////////////////////////////////////////
template <typename ParserT, typename IteratorT, typename BaseT>
inline 
list_action_iterator<ParserT, IteratorT, BaseT>::list_action_iterator() :
    parser(0), current(0), last(0), end_marker(true), saw_oneitem(false)
{
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline 
list_action_iterator<ParserT, IteratorT, BaseT>::list_action_iterator(
    ParserT const *parser_, IteratorT &first_, IteratorT const &last_) :
    parser(parser_), current(first_), last(last_), end_marker(true), 
    saw_oneitem(false)
{
    end_marker = !read(true);     // get the first value
}

///////////////////////////////////////////////////////////////////////////////
// functions required for input iterators
template <typename ParserT, typename IteratorT, typename BaseT>
inline BaseT const &
list_action_iterator<ParserT, IteratorT, BaseT>::operator* () const 
{ 
    return value; 
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline list_action_iterator<ParserT, IteratorT, BaseT> &
list_action_iterator<ParserT, IteratorT, BaseT>::operator++() 
{ 
    read(); 
    return *this; 
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline list_action_iterator<ParserT, IteratorT, BaseT> 
list_action_iterator<ParserT, IteratorT, BaseT>::operator++ (int) 
{ 
list_action_iterator<ParserT, IteratorT, BaseT> tmp = *this; 

    read(); 
    return tmp; 
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline bool 
list_action_iterator<ParserT, IteratorT, BaseT>::operator== (
    list_action_iterator<ParserT, IteratorT, BaseT> const &rhs) const
{ 
    return (current == rhs.current && 
                last == rhs.last && 
                end_marker == rhs.end_marker) ||
           (end_marker && rhs.end_marker);
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline bool 
list_action_iterator<ParserT, IteratorT, BaseT>::operator!= (
    list_action_iterator<ParserT, IteratorT, BaseT> const &rhs) const
{ 
    return !(*this == rhs);
}

///////////////////////////////////////////////////////////////////////////////
// functions required for parser integration
template <typename ParserT, typename IteratorT, typename BaseT>
inline IteratorT const &
list_action_iterator<ParserT, IteratorT, BaseT>::actposition() const 
{ 
    return current; 
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline bool
list_action_iterator<ParserT, IteratorT, BaseT>::matched() const 
{ 
    return saw_oneitem; 
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline void 
list_action_iterator<ParserT, IteratorT, BaseT>::operator() (
    IteratorT const &begin_match, IteratorT const &end_match) const
{
    value = BaseT(begin_match, end_match-begin_match);
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline void 
list_action_iterator<ParserT, IteratorT, BaseT>::operator() (
    typename std::iterator_traits<IteratorT>::value_type const &match_) const
{
    value += match_;
}

template <typename ParserT, typename IteratorT, typename BaseT>
inline ParserT const &
list_action_iterator<ParserT, IteratorT, BaseT>::subject() const 
{ 
    assert(0 != parser);
    return *parser; 
}

///////////////////////////////////////////////////////////////////////////////
// get the next item from the parsed list
namespace impl {

    template <typename CategoryT>
    struct list_action_parser_type;

///////////////////////////////////////////////////////////////////////////////
// There is nothing special for plain_parser_category types as ItemT.
    template <>
    struct list_action_parser_type<plain_parser_category>
    {
        template <typename ActionIterT, typename ParserT, typename IteratorT>
        static match read_item (
            ActionIterT const &list_action_it, ParserT const &subject, 
            IteratorT &current, IteratorT const &last)
        {
            return (
                *(subject.item_p() - (subject.delim_p() | subject.end_p()))
            )[list_action_it].parse(current, last);
        }
    };

///////////////////////////////////////////////////////////////////////////////
// action_parser_category types as ItemT require some special handling
    template <>
    struct list_action_parser_type<action_parser_category>
    {
        template <typename ActionIterT, typename ParserT, typename IteratorT>
        static match read_item (
            ActionIterT const &list_action_it, ParserT const &subject, 
            IteratorT &current, IteratorT const &last)
        {
            return (
                (*(subject.item_p().subject() 
                    - (subject.delim_p() | subject.end_p())
                ) )[subject.item_p().predicate()]
            )[list_action_it].parse(current, last);
        }
    };

} // end of namespace impl

template <typename ParserT, typename IteratorT, typename BaseT>
inline match 
list_action_iterator<ParserT, IteratorT, BaseT>::read (bool firstread)
{
    match hit;

    if (!firstread) {
    // match delimiter
        hit = subject().delim_p().parse (current, last);
        if (!hit) {
        // try to match the 'end' sequence
            end_marker = true;
            return subject().end_p().parse(current, last);
        }
    }

    // parse the next item
    value.empty();
    hit = impl::list_action_parser_type<typename ParserT::parser_category>
            ::read_item (*this, subject(), current, last);

    if (!saw_oneitem)
        saw_oneitem = hit;
    end_marker = !hit;

    return hit;
}

///////////////////////////////////////////////////////////////////////////////
// 
//  list_parser class
//
///////////////////////////////////////////////////////////////////////////////
namespace impl {

    template <typename CategoryT>
    struct list_parser_type;

///////////////////////////////////////////////////////////////////////////////
// There is nothing special for plain_parser_category types as ItemT.
    template <>
    struct list_parser_type<plain_parser_category> 
    {
    // match list with 'normal' syntax
        template <typename IteratorT, typename ItemT, typename DelimT>
        static match
        parse(IteratorT& first, IteratorT const& last, 
            ItemT const &item, DelimT const &delim)
        {
            return (
                    *(item - delim)
                >>  *(delim >> *(item - delim))
            ).parse(first, last);
        }

        template <typename IteratorT, typename MatchTraitsT, typename ItemT, 
            typename DelimT>
        static typename MatchTraitsT::match_t
        ast_parse(IteratorT& first, IteratorT const& last, 
                MatchTraitsT const& mt, ItemT const &item, DelimT const &delim)
        {
            return (
                    *(item - delim)
                >>  *(delim >> *(item - delim))
            ).ast_parse(first, last, mt);
        }

    // match list with 'extended' syntax
        template <
            typename IteratorT, 
            typename ItemT, typename DelimT, typename EndT
        >
        static match
        parse(IteratorT& first, IteratorT const& last, 
            ItemT const &item, DelimT const &delim, EndT const &end)
        {
            return (
                    *(item - (delim | end))
                >>  *(delim >> *(item - (delim | end)))
                >>  !end
            ).parse(first, last);
        }

        template <

⌨️ 快捷键说明

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