2015-02-08 98 views
1

我試圖調試我想在Visual Studio項目中使用一個boost ::精神語法:這是我的代碼片段:錯誤激活

#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'

我不知道如果我做了正確的事情。我想調試我的語法,但是我只看到調試規則的提示(herehere),所以我試圖調整我的代碼。

我做錯了什麼,爲了打印調試信息時我必須做什麼,當我在phrase_parse上使用這個語法時?

回答

1

該字段不是靜態的。

你也不是顯示#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 
+0

我設置'在項目選項BOOST_SPIRIT_DEBUG'所以它的編譯器使用。我會嘗試你的方法。謝謝你的幫助。 – Jepessen 2015-02-09 13:36:22

+0

它編譯(謝謝),但我沒有得到輸出失敗的解析,我已經設置了'BOOST_SPIRIT_DEBUG'定義,所以我能做什麼?也許我的解析器是錯的? – Jepessen 2015-02-09 22:26:40

+0

我沒有看到任何解釋,如你所示的代碼。你可以發佈SSCCE嗎?因爲它是你不真正解析:) – sehe 2015-02-09 22:32:38