为什么会这样?Spirit x3规则正确地使用尖括号解析,但不正确地使用引号
Why does this Boost.Spirit x3 rule parse correctly with angle brackets, but incorrectly with quotes?
下面的程序尝试解析c++头包含字符串,例如"my/file.hpp"
和<my/file.hpp>
。由于我不理解的原因,我的代码无法解析"
标头。这是Spirit的一个bug,还是我错过了什么明显的东西?
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <string>
#include <cassert>
using namespace boost::spirit::x3;
int main() {
auto header_name_brackets = '<' >> *(~char_('>')) >> '>';
auto header_name_quotes = '"' >> *(~char_('>')) >> '"';
{
auto s = std::string{"<my/file.hpp>"};
std::string parsed;
assert(phrase_parse(s.begin(), s.end(), header_name_brackets, space, parsed));
}
{
auto s = std::string{""my/file.hpp""};
std::string parsed;
// this assert fails, but I don't know why.
assert(phrase_parse(s.begin(), s.end(), header_name_quotes, space, parsed));
}
}
既然你已经有了更多的答案(:)),这是我的0.02美元:
template <typename Prefix, typename Postfix>
static auto quoted_(Prefix const& prefix, Postfix const& postfix) {
using namespace boost::spirit::x3;
return rule<struct _, std::string, true> {} = omit[prefix] >> *(char_ - postfix) >> omit[postfix];
}
现在你可以写
auto header_name_brackets = quoted_('<', '>');
auto header_name_quotes = quoted_('"');
第二个假设了明显的便利过载。
另一个错误事实上,我认为有一个跳过分隔符内的空白的错误。通过添加lexeme[]
:
template <typename Prefix, typename Postfix>
static auto quoted_(Prefix const& prefix, Postfix const& postfix) {
using namespace boost::spirit::x3;
return rule<struct _, std::string, true> {} = lexeme [
omit[prefix] >> *(char_ - postfix) >> omit[postfix]
];
}
查看完整演示:
Live On Coliru
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <cassert>
template <typename Prefix, typename Postfix>
static auto quoted_(Prefix const& prefix, Postfix const& postfix) {
using namespace boost::spirit::x3;
return rule<struct _, std::string, true> {} = lexeme [
omit[prefix] >> *(char_ - postfix) >> omit[postfix]
];
}
template <typename Prefix>
static auto quoted_(Prefix const& prefix) { return quoted_(prefix, prefix); }
int main() {
using boost::spirit::x3::space;
auto header_name_brackets = quoted_('<', '>');
auto header_name_quotes = quoted_('"');
{
auto s = std::string{"<my/file.hpp>"};
std::string parsed;
assert(phrase_parse(s.begin(), s.end(), header_name_brackets, space, parsed));
}
{
auto s = std::string{""my/file.hpp""};
std::string parsed;
assert(phrase_parse(s.begin(), s.end(), header_name_quotes, space, parsed));
}
std::cout << "Byen";
}
这个适合我:
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <string>
#include <cassert>
using namespace boost::spirit::x3;
int main() {
auto header_name_brackets = '<' >> *(~char_('>')) >> '>';
auto header_name_quotes = '"' >> *(~char_('"')) >> '"';
{
auto s = std::string{"<my/file.hpp>"};
std::string parsed;
assert(phrase_parse(s.begin(), s.end(), header_name_brackets, space, parsed));
}
{
auto s = std::string{""my/file.hpp""};
std::string parsed;
// this assert fails, but I don't know why.
assert(phrase_parse(s.begin(), s.end(), header_name_quotes, space, parsed));
}
}
请注意,在第二种情况下,您需要匹配除"
以外的所有字符,就像在第一种情况下匹配>
一样。
你需要改变这个规则:
auto header_name_quotes = '"' >> *(~char_('>')) >> '"';
auto header_name_quotes = '"' >> *(~char_('"')) >> '"';
相关文章:
- 我是否不正确地集中了这些字符数组?
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 如何高效/正确地存储等距游戏的不同类(单个超类的所有子类型)的地图?
- STD ::在C 中的进步不能正确地行为
- 在 c++ 中不正确地使用显式关键字
- 项目代码同时正确和不正确地注册'N'
- 查找数组中所有正确和不正确的字符
- 使用图像 8UC1 c++ 的 unchar 垫正确打印不正确
- 我不知道如何正确地将数据输入到地图中
- 静态对象永远不会被正确地销毁
- 中缀到后缀转换器 + 评估程序正确打印到控制台,但不正确地打印到文本文件
- 不正确地将成员变量设置为 null
- 为什么我不能使用makefile正确地包含此文件
- 为什么BoostSpirit正确地将标识符解析为std::字符串,而不是解析为仅由std::string组成的自适应结构
- 变量的不完全类型-尽管我认为我已经正确地向前声明了
- C++-if语句不正确地检查字符串
- 当传递给函数时,如何正确地将一个字符数组的内容复制到另一个字符数组,而不使用STL
- 如果更改了,GNU make不能正确地构建头文件
- 行和字计数器函数不能正确地相加
- 为什么c++应用程序可能不正确地计算表达式