精神语法解析问题 [英] Spirit Grammar parse issue

查看:100
本文介绍了精神语法解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容

class BATSTradeMsg : public BATSMessageBase
{

    BATSTradeMsg(int timestamp, char msgtype, uint64_t orderId, char side, uint32_t shares,
                    std::string const &symbol, uint64_t price, uint64_t execId) :
            BATSMessageBase(timestamp, msgtype),
            m_orderId(orderId),
            m_side(side),
            m_shares(shares),
            m_symbol(symbol),
            m_price(price),
            m_execId(execId)
    {
    }

    uint64_t    m_orderId; // Base 36 Numeric values come over the wire in ascii
    char        m_side;
    uint32_t    m_shares;
    std::string m_symbol;
    uint64_t    m_price;
    uint64_t    m_execId; // Base 36 Numeric values come over the wire in ascii

};

// order and execution ids are 12 characters base 36
qi::uint_parser< uint64_t, 36, 12, 12 > p_orderId;
qi::uint_parser< uint64_t, 36, 12, 12 > p_execId;
qi::uint_parser< uint32_t, 10,  6,  6 > p_shares;
qi::uint_parser< uint32_t, 10, 10, 10 > m_price;
qi::uint_parser< uint32_t, 10,  8,  8 > p_ts;

if (msgtype == BATSTradeMsg::longMsgCode)
    m_wire_msg = ( p_ts >> qi::char_(msgtype)
                        >> p_orderId
                        >> qi::char_(BATSTradeMsg::sideFlag)
                        >> p_shares
                        >> qi::as_string[qi::repeat(8)[qi::char_]]
                        >> m_price
                        >> p_execId )
            [qi::_val = phi::construct<BATSTradeMsg>(
                    qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, qi::_6, qi::_7, qi::_8)];

else if ( msgtype == BATSTradeMsg::shortMsgCode )
    m_wire_msg = ( p_ts >> qi::char_(msgtype)
                        >> p_orderId
                        >> qi::char_(BATSTradeMsg::sideFlag)
                        >> p_shares
                        >> qi::as_string[qi::repeat(6)[qi::char_]]
                        >> m_price
                        >> p_execId )
            [qi::_val = phi::construct<BATSTradeMsg>(
                    qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, qi::_6, qi::_7, qi::_8)];

基本上有两种消息类型,长消息和短消息,唯一的区别是第6个字段可以是6或8个字符串.

Basically there are two message types, long and short, with the only difference being the 6th field can be a 6 or 8 character string.

但是,我意识到我无法做类似的事情,

However, i realised that i cannot do something like,

m_wire_msg = ( p_ts >> qi::char_(msgtype)
                        >> p_orderId
                        >> qi::char_(BATSTradeMsg::sideFlag)
                        >> p_shares
                        >> ( qi::as_string[qi::repeat(6)[qi::char_]] | qi::as_string[qi::repeat(8)[qi::char_]])
                        >> m_price
                        >> p_execId )
            [qi::_val = phi::construct<BATSTradeMsg>(
                    qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, qi::_6, qi::_7, qi::_8)];

并正确解析这两个消息,

and have it parse both these message correctly,

"28800168P1K27GA00000YB000300AAPL  00018319001K27GA00000Z"
"28800168r1K27GA00000YB000300AAPLSPOT00018319001K27GA00000Z"

推荐答案

以下是我的建议:

namespace BATS {
    enum class MessageCode : char { Long = 'r', Short = 'P' };

    struct MessageBase {
        int         timestamp;
        MessageCode msgtype;
    };

    struct TradeMsg : MessageBase {
        uint64_t    orderId; // Base 36 Numeric values come over the wire in ascii
        char        side;
        uint32_t    shares;
        std::string symbol;
        uint64_t    price;
        uint64_t    execId;  // Base 36 Numeric values come over the wire in ascii
    };
}

然后使用简单的Fusion适应方法代替语义动作¹:

Then use simple Fusion adaptation instead of semantic actions¹:

BOOST_FUSION_ADAPT_STRUCT(BATS::TradeMsg, timestamp, msgtype, orderId, side, shares, symbol, price, execId)

解析器

然后,解析器基本上变为:

Parser

The parser then basically becomes:

我认为面"可以是"B"或"S"(用于买入或卖出).

I assumed "side" could be "B" or "S" (for Buy or Sell).

template <typename It> 
struct Parser : qi::grammar<It, BATS::TradeMsg()> {
    Parser() : Parser::base_type(r_wire_msg) {

        // see below

        r_wire_msg 
            = r_long_wire_msg
            | r_short_wire_msg
            ;

        BOOST_SPIRIT_DEBUG_NODES((r_wire_msg)(r_short_wire_msg)(r_long_wire_msg))
    }

  private:
    // order and execution ids are 12 characters base 36
    qi::uint_parser<uint64_t, 36, 12, 12> p_orderId;
    qi::uint_parser<uint64_t, 36, 12, 12> p_execId;
    qi::uint_parser<uint32_t, 10,  6,  6> p_shares;
    qi::uint_parser<uint32_t, 10, 10, 10> p_price;
    qi::uint_parser<uint32_t, 10,  8,  8> p_ts;
    qi::rule<It, BATS::TradeMsg()> r_wire_msg, r_long_wire_msg, r_short_wire_msg;
};

当然,这两个子规则非常相似:

Of course the two sub-rules are very similar:

        r_long_wire_msg
             = p_ts 
            >> qi::char_(BATS::MessageCode::Long)
            >> p_orderId
            >> qi::char_("BS")
            >> p_shares
            >> qi::as_string[qi::repeat(8)[qi::char_]]
            >> p_price
            >> p_execId 
            ;

        r_short_wire_msg
             = p_ts 
            >> qi::char_(BATS::MessageCode::Short)
            >> p_orderId
            >> qi::char_("BS")
            >> p_shares
            >> qi::as_string[qi::repeat(6)[qi::char_]]
            >> p_price
            >> p_execId 
            ;

演示程序

这里剖析了3个测试用例:

Demo Program

Here's 3 test cases dissected:

  1. 问题(ERROR)中的简短"示例
  2. 我尝试修复简短"示例
  3. 问题中的长"示例

在Coliru上直播

int main() {
    using It = std::string::const_iterator;
    Parser<It> const parser;

    for (std::string const input : {
            "28800168P1K27GA00000YB000300AAPL  00018319001K27GA00000Z",
            "28800168r1K27GA00000YB000300AAPLSPOT00018319001K27GA00000Z" })
    {
        std::cout << "Input: " << std::quoted(input) << "\n";

        It f = begin(input), l = end(input);

        BATS::TradeMsg msg;
        if (parse(f, l, parser, msg)) {
            std::cout << "Parsed\n";
        } else {
            std::cout << "Parse failed\n";
        }

        if (f!=l)
            std::cout << "Remaining data: " << std::quoted(std::string(f,l), '\'') << "\n";
    }
}

打印

Input: "28800168P1K27GA00000YB000300AAPL  00018319001K27GA00000Z"
Parsed
Input: "28800168r1K27GA00000YB000300AAPLSPOT00018319001K27GA00000Z"
Parsed


¹ Boost Spirit:语义行为是邪恶的?" < ;-请注意,这是一个问题的标题


¹ Boost Spirit: "Semantic actions are evil"? <-- note, that's the title of a question

这篇关于精神语法解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆