在激活调试的情况下编译语法时出错

Error when compiling a grammar with debug activated

我正在尝试调试我想在 Visual Studio 项目中使用的 boost::spirit 语法:这是我的代码片段:

#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

// This is pasted and copied from another header file

namespace StateMachine {
namespace Private {

struct LuaParameterData {
  std::wstring name;
  std::wstring type;
  std::wstring unit;
  std::wstring cardinality;
  std::wstring value;
};

} // namespace Private
} // namespace StateMachine

BOOST_FUSION_ADAPT_STRUCT(
  StateMachine::Private::LuaParameterData,
  (std::wstring, name)
  (std::wstring, type)
  (std::wstring, unit)
  (std::wstring, cardinality)
  (std::wstring, value)
)

// From here original file continues
namespace StateMachine {
namespace Private {

namespace qi = boost::spirit::qi;

template<typename Iterator>
struct LuaParameterDataParser : qi::grammar<Iterator, LuaParameterData(), qi::ascii::space_type>
{
  LuaParameterDataParser() : LuaParameterDataParser::base_type(start)
  {
    quotedString %= qi::lexeme['"' >> +(qi::ascii::char_ - '"') >> '"'];

    start %=
      qi::lit("\"parameter\"")
      >> ':'
      >> '{'
      >> qi::lit("\"name\""       ) >> ':' >> quotedString >> ','
      >> qi::lit("\"type\""       ) >> ':' >> quotedString >> ','
      >> qi::lit("\"unit\""       ) >> ':' >> quotedString >> ','
      >> qi::lit("\"cardinality\"") >> ':' >> quotedString >> ','
      >> qi::lit("\"value\""      ) >> ':' >> quotedString
      >> '}'
      ;
  }

  qi::rule<Iterator, std::string(), qi::ascii::space_type> quotedString;
  qi::rule<Iterator, LuaParameterData(), qi::ascii::space_type> start;
};

} // namespace Private
} // namespace StateMachine

BOOST_SPIRIT_DEBUG_RULE(StateMachine::Private::LuaParameterDataParser<std::string::const_iterator>::quotedString);

BOOST_SPIRIT_DEBUG 在项目属性中定义。

当我编译它时,在我使用 BOOST_SPIRIT_DEBUG_RULE 的最后一行出现以下错误:

error C3484: syntax error: expected '->' before the return type

error C2061: syntax error : identifier 'register_node'

我不知道我做的是否正确。我想调试我的语法,但我只看到调试规则的提示 (here and here),所以我尝试调整我的代码。

当我将此语法与 phrase_parse 一起使用时,我做错了什么以及为了打印调试信息我必须做什么?

字段不是静态的。

此外,您没有显示 #define BOOST_SPIRIT_DEBUG 行(也许您在编译器命令行中指定了它)。

典型的方法是使用 BOOST_SPIRIT_DEBUG_NODES() 例如

备注:

  • 删除 classic 包含
  • 使用 wstring 有什么特殊原因吗?

Compiling On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/include/io.hpp>
//#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

// This is pasted and copied from another header file

namespace StateMachine {
namespace Private {

    struct LuaParameterData {
        std::wstring name;
        std::wstring type;
        std::wstring unit;
        std::wstring cardinality;
        std::wstring value;
    };

} // namespace Private
} // namespace StateMachine

BOOST_FUSION_ADAPT_STRUCT(
  StateMachine::Private::LuaParameterData,
  (std::wstring, name)
  (std::wstring, type)
  (std::wstring, unit)
  (std::wstring, cardinality)
  (std::wstring, value)
)

namespace qi = boost::spirit::qi;

// From here original file continues
namespace StateMachine {
namespace Private {

    template<typename Iterator>
    struct LuaParameterDataParser : qi::grammar<Iterator, LuaParameterData(), qi::ascii::space_type>
    {
        LuaParameterDataParser() : LuaParameterDataParser::base_type(start)
        {
            quotedString = qi::lexeme['"' >> +(qi::ascii::char_ - '"') >> '"'];

            start =
                qi::lit("\"parameter\"")
                >> ':'
                >> '{'
                >> qi::lit("\"name\""       ) >> ':' >> quotedString >> ','
                >> qi::lit("\"type\""       ) >> ':' >> quotedString >> ','
                >> qi::lit("\"unit\""       ) >> ':' >> quotedString >> ','
                >> qi::lit("\"cardinality\"") >> ':' >> quotedString >> ','
                >> qi::lit("\"value\""      ) >> ':' >> quotedString
                >> '}'
                ;

            BOOST_SPIRIT_DEBUG_NODES((start)(quotedString));
        }

        qi::rule<Iterator, std::string(), qi::ascii::space_type> quotedString;
        qi::rule<Iterator, LuaParameterData(), qi::ascii::space_type> start;
    };

} // namespace Private
} // namespace StateMachine

int main() {
    using It = std::string::const_iterator;

    std::string const input = R"(
        "parameter" : {
            "name"        : "name"       , 
            "type"        : "type"       , 
            "unit"        : "unit"       , 
            "cardinality" : "cardinality", 
            "value"       : "value"       
        }
    )";
    It f = input.begin(), 
       l = input.end();

    StateMachine::Private::LuaParameterDataParser<It> p;
    StateMachine::Private::LuaParameterData data;
    bool ok = qi::phrase_parse(f, l, p, qi::ascii::space, data);

    if (ok) {
        std::wcout << L"Parsed: \n";
        std::wcout << L"\tname: " << data.name << L'\n';
        std::wcout << L"\ttype: " << data.type << L'\n';
        std::wcout << L"\tunit: " << data.unit << L'\n';
        std::wcout << L"\tcardinality: " << data.cardinality << L'\n';
        std::wcout << L"\tvalue: " << data.value << L'\n';
    } else {
        std::wcout << L"Parse failure\n";
    }

    if (f!=l)
        std::wcout << L"Remaining unparsed: '" << std::wstring(f,l) << L"'\n";
}

版画

<start>
  <try>\n        "parameter"</try>
  <quotedString>
    <try> "name"       , \n   </try>
    <success>       , \n          </success>
    <attributes>[[n, a, m, e]]</attributes>
  </quotedString>
  <quotedString>
    <try> "type"       , \n   </try>
    <success>       , \n          </success>
    <attributes>[[t, y, p, e]]</attributes>
  </quotedString>
  <quotedString>
    <try> "unit"       , \n   </try>
    <success>       , \n          </success>
    <attributes>[[u, n, i, t]]</attributes>
  </quotedString>
  <quotedString>
    <try> "cardinality", \n   </try>
    <success>, \n            "valu</success>
    <attributes>[[c, a, r, d, i, n, a, l, i, t, y]]</attributes>
  </quotedString>
  <quotedString>
    <try> "value"       \n    </try>
    <success>       \n        }\n  </success>
    <attributes>[[v, a, l, u, e]]</attributes>
  </quotedString>
  <success>\n    </success>
  <attributes>[[[110, 97, 109, 101], [116, 121, 112, 101], [117, 110, 105, 116], [99, 97, 114, 100, 105, 110, 97, 108, 105, 116, 121], [118, 97, 108, 117, 101]]]</attributes>
</start>
Parsed: 
    name: name
    type: type
    unit: unit
    cardinality: cardinality
    value: value