- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个问题与 boost-spirit-x3-parse-into-structs 严格相关
我有这个语法
#include <iostream>
//#define BOOST_SPIRIT_X3_DEBUG
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
struct sectionInfo
{
std::string name;
int number = 0;
float pitch = 0.0f;
int visible = 0;
float minCutsTblSize = 0.0f;
//technology section attributes
float gridResolution = 0.0f;
float lengthPrecision = 0.0f;
};
const char START_SECTION = '{';
const char END_SECTION = '}';
const char QUOTE = '"';
const char EQUALS = '=';
const char* LAYER_SECTION = "Layer";
const char* TECHNOLOGY_SECTION = "Technology";
const char* NUMBER_ATTR = "layerNumber";
const char* VISIBLE_ATTR = "visible";
const char* COLOR_ATTR = "color";
const char* PITCH_ATTR = "pitch";
const char* MIN_CUTS_TBL_SIZE_ATTR = "minCutsTblSize";
const char* GRID_RESOLUTION_ATTR = "gridResolution";
const char* LENGTH_PRECISION_ATTR = "lengthPrecision";
namespace Parser {
namespace x3 = boost::spirit::x3;
namespace detail {
template <typename T> auto propagate(T member) {
return [=](auto& ctx) { x3::traits::move_to(x3::_attr(ctx), x3::_val(ctx).*member); };
}
template <typename T = sectionInfo, typename P>
auto rule(const char* debug, P p) { return x3::rule<struct _, T> {debug} = x3::skip(x3::space)[p]; };
auto quoted = rule<std::string>("quoted", x3::lexeme[QUOTE >> +(x3::char_ - QUOTE) >> QUOTE]);
template <typename T> auto make_member_parser(bool T::* const member) { return x3::bool_[propagate(member)]; }
template <typename T> auto make_member_parser(int T::* const member) { return x3::int_[propagate(member)]; }
template <typename T> auto make_member_parser(double T::* const member) { return x3::double_[propagate(member)]; }
template <typename T> auto make_member_parser(float T::* const member) { return x3::double_[propagate(member)]; }
template <typename T> auto make_member_parser(std::string T::* const member) { return quoted[propagate(member)]; }
auto property = [](auto label, auto member) {
return x3::as_parser(label) >> EQUALS >> make_member_parser(member);
};
}
using detail::rule;
using detail::propagate;
using detail::property;
using detail::quoted;
auto number = property(NUMBER_ATTR, §ionInfo::number);
auto visible = property(VISIBLE_ATTR, §ionInfo::visible);
auto pitch = property(PITCH_ATTR, §ionInfo::pitch);
auto minCutsTblSize = property(MIN_CUTS_TBL_SIZE_ATTR, §ionInfo::minCutsTblSize);
auto lengthPrecision = property(LENGTH_PRECISION_ATTR, §ionInfo::lengthPrecision);
auto gridResolution = property(GRID_RESOLUTION_ATTR, §ionInfo::gridResolution);
auto skipLine = *(x3::char_ - x3::eol);
x3::rule<struct sectionInfoId, sectionInfo> const layer = "layer";
x3::rule<struct mainRuleId, std::vector<sectionInfo>> const mainRule = "mainRule";
auto layer_def =
LAYER_SECTION >> quoted[propagate(§ionInfo::name)] >> START_SECTION >> x3::eol
>> *( (number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution) >> +x3::eol )
>> END_SECTION;
auto skipper = x3::blank;
auto mainRule_def = *(*x3::eol >> layer >> *x3::eol);
BOOST_SPIRIT_DEFINE(layer, mainRule);
}
std::ostream& operator<<(std::ostream& os, const sectionInfo& s)
{
os
<< "name=" << " " << s.name << "\n"
<< "number=" << " " << s.number << "\n"
<< "visible=" << " " << s.visible << "\n"
<< "pitch=" << " " << s.pitch << "\n"
<< "minCutsTblSize=" << " " << s.minCutsTblSize << "\n"
<< "lengthPrecision=" << " " << s.lengthPrecision << "\n"
<< "gridResolution=" << " " << s.gridResolution << "\n\n";
return os;
}
int main() {
std::stringstream ss;
ss
<<"\r\nLayer \"UBMB\" {\r\n"
<< " layerNumber = 170\r\n"
<< " pitch = 33.6\r\n"
<< "}\r\n"
<< "\r\n"
<< "Layer \"RV\" {\r\n"
<< " gridResolution = 0.34\r\n"
<< " minCutsTblSize = 22.7\r\n"
<< " layerNumber = 85\r\n"
<< " visible = 2\r\n"
<< " pitch = 331\r\n"
<< "}\r\n"
<< " \r\n"
<< "Layer \"foffo\" {\r\n"
<< " layerNumber = 125\r\n"
<< " pitch = 0.005\r\n"
<< " gridResolution = 21.7\r\n"
<< " lengthPrecision = 0.15\r\n"
<< "}\r\n"
<< "\r\n";
std::vector<sectionInfo> sections;
auto sample = ss.str();
auto f = sample.begin(), l = sample.end();
bool ok = boost::spirit::x3::phrase_parse(
f, l,
Parser::mainRule,
Parser::skipper,
sections
);
if (ok && f==l)
{
std::cout << "\n\n Parsed successfully \n\n";
for(auto& s : sections)
{
std::cout << s;
}
}
else
std::cout << "Parse failed\n";
}
成功解析输入:
输出是:
name= UBMB number= 170 visible= 0 pitch= 33.6 minCutsTblSize= 0 lengthPrecision= 0 gridResolution= 0
name= RV number= 85 visible= 2 pitch= 331 minCutsTblSize= 22.7 lengthPrecision= 0 gridResolution= 0.34
name= foffo number= 125 visible= 0 pitch= 0.005 minCutsTblSize= 0 lengthPrecision= 0.15 gridResolution= 21.7
出现问题是因为我需要跳过一些行,即具有不感兴趣的属性的行(未在我的语法中定义)
编辑:例如,可能有一个我想跳过的属性 dummy = "foo"。
为了实现这一点,层规则
auto layer_def = LAYER_SECTION >> quoted[propagate(§ionInfo::name)] >> START_SECTION >> x3::eol
>> *( (number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution ) >> +x3::eol )
>> END_SECTION;
成为
auto layer_def = LAYER_SECTION >> quoted[propagate(§ionInfo::name)] >> START_SECTION >> x3::eol
>> *( (number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution | skipLine) >> +x3::eol )
>> END_SECTION;
解析器成功,但现在输出是
name= UBMB number= 125 visible= 2 pitch= 0.005 minCutsTblSize= 22.7 lengthPrecision= 0.15 gridResolution= 21.7
这是错误的(只有一个部分,在这里和那里使用的属性...)
很明显问题出在skipLine规则上
auto skipLine = *(x3::char_ - x3::eol);
我不知道为什么。我认为规则 *(char - eol) >> eol 很明显会匹配任何行,但我想它不是..
有什么线索吗?
最佳答案
首先,我会简化跳过。 skipper 不属于调用站点,因为它对语法很重要。将其封装到mainRule中,并简化:
auto mainRule_def = x3::skip(x3::blank) [ -layer % x3::eol ];
-layer
表达式是一个接受空行的技巧。现在使用解析器变成:
bool ok = boost::spirit::x3::parse(f, l, Parser::mainRule, sections);
下一步:skipLine
也吃掉了 '}'
,让一切都崩溃了:之后的一切都被视为同一层的(无效)属性,最后没有 END_SECTION
,所以语法不匹配。
简单地:
auto skipLine = *(x3::char_ - x3::eol - END_SECTION);
Pro Tip:
When in doubt, debug: Live On Coliru
// debug it
auto skipLine = x3::rule<struct skipLine_> {"skipLine"} = *(x3::char_ - x3::eol/* - END_SECTION*/);
有一些小的简化
#include <iostream>
#define BOOST_SPIRIT_X3_DEBUG
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/home/x3.hpp>
struct sectionInfo {
std::string name;
int number = 0;
float pitch = 0.0f;
int visible = 0;
float minCutsTblSize = 0.0f;
// technology section attributes
float gridResolution = 0.0f;
float lengthPrecision = 0.0f;
};
char const START_SECTION = '{';
char const END_SECTION = '}';
char const QUOTE = '"';
char const EQUALS = '=';
char const* LAYER_SECTION = "Layer";
char const* TECHNOLOGY_SECTION = "Technology";
char const* NUMBER_ATTR = "layerNumber";
char const* VISIBLE_ATTR = "visible";
char const* COLOR_ATTR = "color";
char const* PITCH_ATTR = "pitch";
char const* MIN_CUTS_TBL_SIZE_ATTR = "minCutsTblSize";
char const* GRID_RESOLUTION_ATTR = "gridResolution";
char const* LENGTH_PRECISION_ATTR = "lengthPrecision";
namespace Parser {
namespace x3 = boost::spirit::x3;
namespace detail {
template <typename T> auto propagate(T member) {
return [=](auto &ctx) { x3::traits::move_to(x3::_attr(ctx), x3::_val(ctx).*member); };
}
template <typename T = sectionInfo, typename P> auto rule(const char *debug, P p) {
return x3::rule<struct _, T>{ debug } = x3::skip(x3::space)[p];
};
auto quoted = rule<std::string>("quoted", x3::lexeme[QUOTE >> +(x3::char_ - QUOTE) >> QUOTE]);
#define MMP_(T, p) template <typename U> auto make_member_parser(T U::*const member) { return (p)[propagate(member)]; }
MMP_(bool, x3::bool_);
MMP_(int, x3::int_);
MMP_(double, x3::double_);
MMP_(float, x3::double_);
MMP_(std::string, quoted);
#undef MMP_
auto property = [](auto label, auto member) { return x3::as_parser(label) >> EQUALS >> make_member_parser(member); };
}
using detail::rule;
using detail::propagate;
using detail::property;
using detail::quoted;
auto number = property(NUMBER_ATTR, §ionInfo::number);
auto visible = property(VISIBLE_ATTR, §ionInfo::visible);
auto pitch = property(PITCH_ATTR, §ionInfo::pitch);
auto minCutsTblSize = property(MIN_CUTS_TBL_SIZE_ATTR, §ionInfo::minCutsTblSize);
auto lengthPrecision = property(LENGTH_PRECISION_ATTR, §ionInfo::lengthPrecision);
auto gridResolution = property(GRID_RESOLUTION_ATTR, §ionInfo::gridResolution);
auto skipLine = *(x3::char_ - x3::eol - END_SECTION);
x3::rule<struct sectionInfoId, sectionInfo> const layer = "layer";
x3::rule<struct mainRuleId, std::vector<sectionInfo> > const mainRule = "mainRule";
auto layer_def =
LAYER_SECTION >> quoted[propagate(§ionInfo::name)]
>> START_SECTION >> x3::eol
>> *((number | visible | pitch | minCutsTblSize | lengthPrecision | gridResolution | skipLine) >> +x3::eol)
>> END_SECTION
;
auto mainRule_def = x3::skip(x3::blank) [ -layer % x3::eol ];
BOOST_SPIRIT_DEFINE(layer, mainRule);
}
std::ostream &operator<<(std::ostream &os, const sectionInfo &s) {
return os
<< "name=" << " " << s.name << "\n"
<< "number=" << " " << s.number << "\n"
<< "visible=" << " " << s.visible << "\n"
<< "pitch=" << " " << s.pitch << "\n"
<< "minCutsTblSize=" << " " << s.minCutsTblSize << "\n"
<< "lengthPrecision=" << " " << s.lengthPrecision << "\n"
<< "gridResolution=" << " " << s.gridResolution << "\n\n";
}
int main() {
std::string const sample =
"\r\nLayer \"UBMB\" {\r\n"
" layerNumber = 170\r\n"
" pitch = 33.6\r\n"
"}\r\n"
"\r\n"
"Layer \"RV\" {\r\n"
" gridResolution = 0.34\r\n"
" minCutsTblSize = 22.7\r\n"
" layerNumber = 85\r\n"
" visible = 2\r\n"
" pitch = 331\r\n"
"}\r\n"
" \r\n"
"Layer \"foffo\" {\r\n"
" layerNumber = 125\r\n"
" pitch = 0.005\r\n"
" gridResolution = 21.7\r\n"
" lengthPrecision = 0.15\r\n"
"}\r\n"
"\r\n";
std::vector<sectionInfo> sections;
{
auto f = sample.begin(), l = sample.end();
bool ok = boost::spirit::x3::parse(f, l, Parser::mainRule, sections);
if (ok && f == l) {
std::cout << "\n\n Parsed successfully \n\n";
for (auto &s : sections) {
std::cout << s;
}
} else
std::cout << "Parse failed\n";
}
}
打印
Parsed successfully
name= UBMB
number= 170
visible= 0
pitch= 33.6
minCutsTblSize= 0
lengthPrecision= 0
gridResolution= 0
name= RV
number= 85
visible= 2
pitch= 331
minCutsTblSize= 22.7
lengthPrecision= 0
gridResolution= 0.34
name= foffo
number= 125
visible= 0
pitch= 0.005
minCutsTblSize= 0
lengthPrecision= 0.15
gridResolution= 21.7
关于c++ - Boost Spirit X3 eol 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45753683/
我正在尝试使用boost.spirit的qi库解析某些内容,而我遇到了一个问题。根据spirit docs,a >> b应该产生类型为tuple的东西。但这是boost::tuple(又名 fusio
似乎有/正在努力做到这一点,但到目前为止我看到的大多数资源要么已经过时(带有死链接),要么几乎没有信息来实际构建一个小的工作样本(例如,依赖于boost program_options 以构建可执行文
我对 Boost.Log 的状态有点困惑。这是 Boost 的官方部分,还是尚未被接受?当我用谷歌搜索时,我看到一些帖子谈论它在 2010 年是如何被接受的,等等,但是当我查看最后一个 Boost 库
Boost 提供了两种不同的实现 string_view ,这将成为 C++17 的一部分: boost::string_ref在 utility/string_ref.hpp boost::stri
最近,我被一家GIS公司雇用来重写他们的旧地理信息库。所以我目前正在寻找一个好的计算几何库。我看过CGAL,这真是了不起,但是我的老板想要免费的东西。 所以我现在正在检查Boost.Geometry。
假设我有一个无向图 G。假设我添加以下内容 add_edge(1,2,G); add_edge(1,3,G); add_edge(0,2,G); 现在我再说一遍: add_edge(0,2,G); 我
我使用 CMake 来查找 Boost。找到了 Boost,但 CMake 出错了 Imported targets not available for Boost version 请参阅下面的完整错
我是 boost::fusion 和 boost::mpl 库的新手。谁能告诉我这两个库之间的主要区别? 到目前为止,我只使用 fusion::vector 和其他一些简单的东西。现在我想使用 fus
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What are the benefits of using Boost.Phoenix? 所以我开始阅读 boos
我正在尝试获得一个使用 Boost.Timer 的简单示例,用于一些秒表性能测量,但我不明白为什么我无法成功地将 Boost.Timer 链接到 Boost.Chrono。我使用以下简单脚本从源代码构
我有这样的东西: enum EFood{ eMeat, eFruit }; class Food{ }; class Meat: public Food{ void someM
有人可以告诉我,我如何获得boost::Variant处理无序地图? typedef boost::variant lut_value;unordered_map table; 我认为有一个用于boo
我对 Boost.Geometry 中的环和多边形感到困惑。 在文档中,没有图形显示什么是环,什么是多边形。 谁能画图解释两个概念的区别? 最佳答案 在 Boost.Geometry 中,多边形被定义
我正在使用 boost.pool,但我不知道何时使用 boost::pool<>::malloc和 boost::pool<>::ordered_malloc ? 所以, boost::pool<>:
我正在尝试通过 *boost::fast_pool_allocator* 使用 *boost::container::flat_set*。但是,我收到编译错误。非常感谢您的意见和建议。为了突出这个问题
sau_timer::sau_timer(int secs, timerparam f) : strnd(io), t(io, boost::posix_time::seconds(secs)
我无法理解此功能的文档,我已多次看到以下内容 tie (ei,ei_end) = out_edges(*(vi+a),g); **g**::out_edge_iterator ei, ei_end;
我想在 C++ 中序列化分层数据结构。我正在处理的项目使用 boost,所以我使用 boost::property_tree::ptree 作为我的数据节点结构。 我们有像 Person 这样的高级结
我需要一些帮助来解决这个异常,我正在实现一个 NPAPI 插件,以便能够使用来自浏览器扩展的本地套接字,为此我正在使用 Firebreath 框架。 对于套接字和连接,我使用带有异步调用的 Boost
我尝试将 boost::bind 与 boost::factory 结合使用但没有成功 我有这个类 Zambas 有 4 个参数(2 个字符串和 2 个整数)和 class Zambas { publ
我是一名优秀的程序员,十分优秀!