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

How to parse an CSV like escaped String with Boost Spirit?

对于我的快速解析器项目,我想像转义一样使用 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_('"')] >> '"';

我发现了这个 Whosebug 问题,并且使用 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 = ','];

(我不知道如何 link 回答,所以如果有兴趣搜索 "You gotta feel proud when you use something so beautiful as boost::spirit")

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

可能是不正确的
expression = "strlen( \"hello \"\"you\"\" \" )+1";
expression = "\"hello \"";
expression = "strlen(concat(\"hello\",\"you\")+3";

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

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

感谢任何帮助

更新: 这似乎对我有用,至少它解析了字符串 但是从字符串中删除了转义的 " ,是否有更好的调试输出可用于字符串? " " " " "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("\"\"")) >> '"'];

你可以把问题简化成这样。如何使双引号字符串接受 "double double quotes" 以转义嵌入的双引号字符?

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

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

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

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

其他说明:

  • 在您的在线示例中,no_skip 的使用很草率:它将 "foo bar"" foo bar " 解析为 foo bar(修剪空格)。相反,删除规则中的船长,使其隐式词位 ().
  • 您的解析器不接受空字符串(这可能是您想要的,但不确定)
  • 使用 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" '
----