使用提升精神q

时间:2018-01-05 22:21:48

标签: c++ parsing boost boost-spirit boost-spirit-qi

我正在尝试使用boost spirit qi解析一个字符串,其形式如下:

"\0help@masonlive.gmu.edu\0test\r\n"

具有以下语法: 这是hpp:

class EmailGrammar :
    public boost::spirit::qi::grammar< const boost::uint8_t*,
        boost::tuple< boost::iterator_range< const boost::uint8_t*>,
                      boost::iterator_range< const boost::uint8_t*> >()>
{
public:
    const static EmailGrammar instance;

    EmailGrammar ();    

    /* omitting uninteresting stuff here i.e. constructors and assignment */

private:
    boost::spirit::qi::rule< const boost::uint8_t*,
        boost::tuple<
            boost::iterator_range< const boost::uint8_t*>,
            boost::iterator_range< const boost::uint8_t* >()> m_start;
};

并且语法的cpp看起来像这样:

EmailGrammar::EmailGrammar() :
    EmailGrammar::base_type(m_start),
    m_start()
{
    namespace qi = boost::spirit::qi;
    m_start = 
             (
             qi::lit('\0')
             >> (
                    qi::raw[*(qi::char_ - qi::lit('\0'))]
                )
             >> qi::lit('\0')
             >> (
                    qi::raw[*(qi::char_ - qi::eol)]
                )
             >> qi::eol >> qi::eoi
             );
}

我打算用它来解析两个字符串并将它们分成两个独立的迭代器范围。

然后这样调用:

int main()
{
    typedef typename EmailGrammar::start_type::attr_type attr;

    std::string testStr("\0help@masonlive.gmu.edu\0test\r\n");

    // this is not done this way in the real code just as a test
    boost::iterator_range<const boost::uint8_t*> data =
        boost::make_iterator_range(
            reinterpret_cast< const boost::uint8_t* >(testStr.data()),
            reinterpret_cast< const boost::uint8_t* >(testStr.data() + testStr.length()));

    attr exposedAttribute;
    if (boost::spirit::qi::parse(data.begin(),
                                 data.end(),
                                 EmailGrammar::instance,
                                 exposedAttribute)
    {
        std::cout << "success" << std::endl;
    }
}

问题似乎在于解析null终止符。我认为这是因为当我将debug(m_rule);添加到代码中时,我得到了xml输出:

<unnamed-rule>
<try></try>
<fail/>
</unnamed-rule>

然而。如果我明确擦除例如第一个空终止符,我得到输出:

<unnamed-rule>
<try>help@masonlive.gmu.e</try>
<fail/>
</unnamed-rule>

这导致了问题:

  • 如何解析具有精神的null终止符我已经查找了文档并且除了在this页面底部提到空终止字符串之外,还没有找到任何信息。提到了精神上的默认模型。

  • 精灵是否会以一种方式向前看,如果解析器在前方看到它没有正确结束它会自动失败?

  • 我是否缺少任何可以用来阅读此类行为的文件?

1 个答案:

答案 0 :(得分:1)

整个问题很可能源于此:

std::string testStr("\0help@masonlive.gmu.edu\0test\r\n");

不符合你的想法。它创建一个空字符串。而是指定原始文字/缓冲区的长度:

std::string testStr("\0help@masonlive.gmu.edu\0test\r\n", 31);

奖金

如果你不想做数学/计算(你不应该!),请帮帮忙:

template <typename Char, size_t N>
std::string bake(Char const (&p)[N], bool include_terminator = false) {
    return { p, p + N - (include_terminator?0:1) };
}

然后您可以使用:

std::string const testStr = bake("\0help@masonlive.gmu.edu\0test\r\n");

<强> Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/boost_tuple.hpp>
namespace qi = boost::spirit::qi;

using It = uint8_t const*;
using Range = boost::iterator_range<It>;
using Attribute = boost::tuple<Range, Range>;

class EmailGrammar : public qi::grammar<It, Attribute()> {
  public:
    const static EmailGrammar instance;

    EmailGrammar() : EmailGrammar::base_type(m_start)
    {
        using namespace qi;

        m_start = 
            '\0' >> raw[*(char_ - '\0')] >> 
            '\0' >> raw[*(char_ - eol)] >> 
            eol >> eoi
            ;

        BOOST_SPIRIT_DEBUG_NODES((m_start))
    }

  private:
    qi::rule<It, Attribute()> m_start;
};

const EmailGrammar EmailGrammar::instance {};

template <typename Char, size_t N>
std::string bake(Char const (&p)[N], bool include_terminator = false) {
    return { p, p + N - (include_terminator?0:1) };
}

int main() {
    std::string const testStr = bake("\0help@masonlive.gmu.edu\0test\r\n");

    It f = reinterpret_cast<It>(testStr.data()),
       l = f + testStr.length();

    Attribute exposedAttribute;
    if (boost::spirit::qi::parse(f, l, EmailGrammar::instance, exposedAttribute)) {
        std::cout << "success" << std::endl;
    }
}

打印

<m_start>
  <try></try>
  <success></success>
  <attributes>[[[h, e, l, p, @, m, a, s, o, n, l, i, v, e, ., g, m, u, ., e, d, u], [t, e, s, t]]]</attributes>
</m_start>
success