c++ - 如何使用 Boost Spirit 解析像转义字符串这样的 CSV?

标签 c++ boost boost-spirit boost-spirit-qi

对于我的快速解析器项目,我想像转义一样使用 CSV:"" 转义 "

例子:

 "\"hello\"",
 "   \"  hello \"  ",
 "  \"  hello \"\"stranger\"\" \"  ",

在线编译&试用:https://wandbox.org/permlink/5uchQM8guIN1k7aR

我当前的解析规则只解析前 2 个测试

qi::rule<std::string::const_iterator, qi::blank_type, utree()> double_quoted_string
    = '"' >> qi::no_skip[+~qi::char_('"')] >> '"';

我发现了这个 stackoverflow 问题,并且使用 spirit 给出了一个答案:

How can I read and parse CSV files in C++?

start       = field % ',';
field       = escaped | non_escaped;
escaped     = lexeme['"' >> *( char_ -(char_('"') | ',') | COMMA | DDQUOTE)  >> '"'];
non_escaped = lexeme[       *( char_ -(char_('"') | ',')                  )        ];
DDQUOTE     = lit("\"\"")       [_val = '"'];
COMMA       = lit(",")          [_val = ','];

(我不知道如何链接答案,所以如果有兴趣搜索“当你使用像 boost::spirit 这样美丽的东西时,你会感到自豪”)

遗憾的是它没有为我编译——甚至多年的 C++ 错误消息分析也没有让我为 spirit 错误消息泛滥做好准备:) 如果我理解它是正确的,规则将等待 , 作为字符串定界符,这对于我的表达式解析器项目来说可能是不正确的

expression = "strlen( \"hello \"\"you\"\" \" )+1";
expression = "\"hello \"";
expression = "strlen(concat(\"hello\",\"you\")+3";

或者在这种情况下,规则是否需要选择性地等待 )

我希望我不要问太多愚蠢的问题,但答案会帮助我进入 spirit 状态 除了字符串转义,表达式解析本身几乎可以正常工作

感谢任何帮助

更新:这似乎对我有用,至少它解析了字符串 但是从字符串中删除了转义的 ",是否有更好的调试输出可用于字符串?"""""h""e""l""l""o""""s""t""r""a""n""g""e""r""" 并不是那么可读

qi::rule<std::string::const_iterator, utree()> double_quoted_string
  = qi::lexeme['"' >> *(qi::char_ - (qi::char_('"')) | qi::lit("\"\"")) >> '"'];

最佳答案

您可以将问题简化为这样。如何使双引号字符串接受“双双引号”以转义嵌入的双引号字符?

一个没有转义的简单字符串解析器:

qi::rule<It, std::string()> s = '"' >> *~qi::char_('"') >> '"';

现在,要根据需要接受单个转义的 ",只需添加:

s = '"' >> *("\"\"" >> qi::attr('"') | ~qi::char_('"')) >> '"';

其他说明:

  • 在您的在线示例中,no_skip 的使用很草率:它会将 "foo bar""foo bar " 解析为 foo bar(修剪空白)。相反,从规则中删除 skipper 以使其隐式 lexeme(again)。
  • 您的解析器不接受空字符串(这可能是您想要的,但不确定)
  • 使用 utree 可能会使您的生活变得比您想要的更复杂

简化:

Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <iomanip>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;
namespace fu = boost::fusion;

int main()
{
    auto tests = std::vector<std::string>{
         R"( "hello" )",
         R"(    "  hello " )",
         R"(  "  hello ""escaped"" "  )",
    };
    for (const std::string& str : tests) {
        auto iter = str.begin(), end = str.end();

        qi::rule<std::string::const_iterator, std::string()> double_quoted_string
            = '"' >> *("\"\"" >> qi::attr('"') | ~qi::char_('"')) >> '"';

        std::string ut;
        bool r = qi::phrase_parse(iter, end, double_quoted_string >> qi::eoi, qi::blank, ut);

        std::cout << str << " ";
        if (r) {
            std::cout << "OK: " << std::quoted(ut, '\'') << "\n";
        }
        else {
            std::cout << "Failed\n";
        }
        if (iter != end) {
            std::cout << "Remaining unparsed: " << std::quoted(std::string(iter, end)) << "\n";
        }
        std::cout << "----\n";
    }
}

打印

 "hello"  OK: 'hello'
----
    "  hello "  OK: '  hello '
----
  "  hello ""escaped"" "   OK: '  hello "escaped" '
----

关于c++ - 如何使用 Boost Spirit 解析像转义字符串这样的 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60826588/

相关文章:

c++ - 你如何让 std::shared_ptr 不调用 delete()

c++ - 链表中的段错误和 Linux 的调试选项

c++ - 具有方法重载的接口(interface)类?

c++ - 如何使用 (lambda) 函数填充 C++ 容器?

c++ - 我如何以无序和可变的方式使用 boost::bimap?

c++ - 如何创建函数模板签名?

c++ - 如何从 boost::spirit::lex 标记确定行/列号?

regex - 如何在 Ubuntu 上使用 Netbeans 6.9 配置 Boost

c++ - 是否有比 boost::spirit::hold_any 更快的替代方法并且 hold_any 会导致内存泄漏

c++ - 振奋精神,推进申报问题