namespace qi = boost::spirit::qi;
template<typename T>
class action
{
public:
action(std::vector<std::shared_ptr<part>>& parts) : m_parts{ parts } {}
void operator()(const std::vector<char>& cc, qi::unused_type, qi::unused_type) const
{
std::string s(cc.begin(), cc.end());
if (s.length() > 0) {
auto p = new T(s);
m_parts.push_back(std::shared_ptr<part>(p));
}
}
private:
std::vector<std::shared_ptr<part>>& m_parts;
};
std::vector<std::shared_ptr<part>> parse(const std::string& source) {
namespace ascii = boost::spirit::ascii;
using ascii::char_;
using qi::lit;
std::vector<std::shared_ptr<part>> parts;
auto prop_g = lit("{{=")
>> *char_(' ')
>> (*(char_ - char_("} ")))[action<property_part>(parts)]
>> *char_(' ')
>> "}}"
;
auto text_g = (+(char_ - '{'))[action<text_part>(parts)];
auto g = -text_g >> +(prop_g >> text_g);
qi::parse(source.begin(), source.end(), g);
return parts;
}
精神語法SIGSEGV導致上氣故障::解析奇巧設備上測試時調用。在任何語義動作被調用之前發生故障。相同的代碼適用於Xcode 6/iOS 8.4和VS 2015.我們使用的是Boost 1.59。
我們可以用Bison替換Spirit,意味着需要額外的構建步驟,或者使用Android NDK的Clang,讓我們脫離捱打的道路。
這個故障可以通過構建配置修復嗎?或者我們可以探索其他選項嗎?
謝謝,我會嘗試你的建議。我已經將語義操作添加到代碼片段中以完成解析器。 –
我仍然不得不在那裏添加一些東西:/以固定語法查看更新後的答案。考慮保持規則靜態/共享(它們可以是const)。另外請注意,它看起來像你在做宏觀擴張,但非常(非常!)效率低下。看看[這非常靈活的示例](http://stackoverflow.com/questions/9404558/a/9405546?s=3|0.4015#9405546),這[簡單的使用精神](http://stackoverflow.com/問題/ 17112494/a/17126962#17126962),相同的[不使用精神](http://stackoverflow.com/questions/17112494/a/17128601#17128601)。 – sehe
如果你真的想重複使用模板**和**高效,[這個答案使用Spirit + ICL來解析模板](http://stackoverflow.com/a/28521682/85371),可以非常有效地擴展反覆。 – sehe