我试图解析一个文件,其中每行由;
分隔的属性组成。每个属性定义为key value
或key=value
,其中键和值可以用双引号"
括起来,以允许键和值包含特殊字符,如空格,等号
=
或分号;
。
为此,我首先使用boost::algorithm::make_split_iterator,然后,为了允许双引号,我使用boost::tokenizer
我需要解析每个键和值作为boost::iterator_range<const char*>
。我尝试编码如下代码,但我无法构建它。可能是标记器的定义是正确的,但是错误来自于iterator_range
的打印。如果需要的话,我可以提供更多的信息。
#include <boost/algorithm/string.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/tokenizer.hpp>
boost::iterator_range<const char*> line;
const auto topDelim = boost::token_finder(
[](const char c) { return (c == ';'); },
boost::token_compress_on);
for (auto attrIt = make_split_iterator(line, topDelim); !attrIt.eof() && !attrIt->empty(); attrIt++) {
std::string escape("\");
std::string delim(" =");
std::string quote(""");
boost::escaped_list_separator<char> els(escape, delim, quote);
boost::tokenizer<
boost::escaped_list_separator<char>,
boost::iterator_range<const char*>::iterator, // how to define iterator for iterator_range?
boost::iterator_range<const char*>
> tok(*attrIt, els);
for (auto t : tok) {
std::cout << t << std::endl;
}
构建错误:
/third_party/boost/boost-1_58_0/include/boost/token_functions.hpp: In instantiation of 'bool boost::escaped_list_separator<Char, Traits>::operator()(InputIterator&, InputIterator, Token&) [with InputIterator = const char*; Token = boost::iterator_range<const char*>; Char = char; Traits = std::char_traits<char>]':
/third_party/boost/boost-1_58_0/include/boost/token_iterator.hpp:70:36: required from 'void boost::token_iterator<TokenizerFunc, Iterator, Type>::initialize() [with TokenizerFunc = boost::escaped_list_separator<char>; Iterator = const char*; Type = boost::iterator_range<const char*>]'
/third_party/boost/boost-1_58_0/include/boost/token_iterator.hpp:77:63: required from 'boost::token_iterator<TokenizerFunc, Iterator, Type>::token_iterator(TokenizerFunc, Iterator, Iterator) [with TokenizerFunc = boost::escaped_list_separator<char>; Iterator = const char*; Type = boost::iterator_range<const char*>]'
/third_party/boost/boost-1_58_0/include/boost/tokenizer.hpp:86:33: required from 'boost::tokenizer<TokenizerFunc, Iterator, Type>::iter boost::tokenizer<TokenizerFunc, Iterator, Type>::begin() const [with TokenizerFunc = boost::escaped_list_separator<char>; Iterator = const char*; Type = boost::iterator_range<const char*>; boost::tokenizer<TokenizerFunc, Iterator, Type>::iter = boost::token_iterator<boost::escaped_list_separator<char>, const char*, boost::iterator_range<const char*> >]'
test.cpp:21:23: required from here
/third_party/boost/boost-1_58_0/include/boost/token_functions.hpp:188:19: error: no match for 'operator+=' (operand types are 'boost::iterator_range<const char*>' and 'const char')
188 | else tok+=*next;
| ~~~^~~~~~~
正如我所说,您需要解析,而不是拆分。具体来说,如果要将输入拆分为迭代器范围,则必须重复解析的工作,例如,用引号括起来的结构才能获得预期的(未引号括起来的)值。
我将按照你的规格使用Boost Spirit:
using Attribute = std::pair<std::string /*key*/, //
std::string /*value*/>;
using Line = std::vector<Attribute>;
using File = std::vector<Line>;
语法
现在使用X3我们可以编写表达式来定义语法:
auto file = x3::skip(x3::blank)[ line % x3::eol ];
在文件中,通常会跳过空白(std::isblank
)。
内容由一行或多行组成,以换行符分隔。
auto line = attribute % ';';
一行由一个或多个以';'
分隔的属性组成
auto attribute = field >> -x3::lit('=') >> field;
auto field = quoted | unquoted;
属性是两个字段,可以用=
分隔。请注意,每个字段要么是带引号的值,要么是不带引号的值。
现在,事情变得有点棘手了:在定义字段规则时,我们希望它们是"lexemes",也就是说,任何空白都不能被跳过。
auto unquoted = x3::lexeme[+(x3::graph - ';' - '=')];
注意graph
已经排除了空格(参见std::isgraph
)。此外,我们禁止裸';'
或'='
,这样我们就不会遇到下一个属性/字段。
对于可能包含空格和/或那些特殊字符的字段,定义加引号的词素:
auto quoted = x3::lexeme['"' >> *quoted_char >> '"'];
就是""
加上任意数量的引号,其中
auto quoted_char = '\' >> x3::char_ | ~x3::char_('"');
字符可以是任何用转义的字符或除结束引号以外的任何字符。
测试时间让我们练习*Live On Compiler Explorer
for (std::string const& str :
{
R"(a 1)",
R"(b = 2 )",
R"("c"="3")",
R"(a=1;two 222;three "3 3 3")",
R"(b=2;three 333;four "4 4 4"
c=3;four 444;five "5 5 5")",
// special cases
R"("e=" "5")",
R"("f=""7")",
R"("g="="8")",
R"(""Hello\ World\!"" '8')",
R"("h=10;i=11;" bogus;yup "nope")",
// not ok?
R"(h i j)",
// allowing empty lines/attributes?
"",
"a 1;",
";",
";;",
R"(a=1;two 222;three "3 3 3"
n=1;gjb 222;guerr "3 3 3"
)",
}) //
{
File contents;
if (parse(begin(str), end(str), parser::file, contents))
fmt::print("Parsed:nt- {}n", fmt::join(contents, "nt- "));
else
fmt::print("Not Parsedn");
}
打印
Parsed:
- {("a", "1")}
Parsed:
- {("b", "2")}
Parsed:
- {("c", "3")}
Parsed:
- {("a", "1"), ("two", "222"), ("three", "3 3 3")}
Parsed:
- {("b", "2"), ("three", "333"), ("four", "4 4 4")}
- {("c", "3"), ("four", "444"), ("five", "5 5 5")}
Parsed:
- {("e=", "5")}
Parsed:
- {("f=", "7")}
Parsed:
- {("g=", "8")}
Parsed:
- {(""Hello World!"", "'8'")}
Parsed:
- {("h=10;i=11;", "bogus"), ("yup", "nope")}
Not Parsed
Not Parsed
Not Parsed
Not Parsed
Not Parsed
Not Parsed
允许空元素
就像用:
替换line
一样简单auto line = -(attribute % ';');
也允许冗余分隔符:
auto line = -(attribute % +x3::lit(';')) >> *x3::lit(';');
查看Live On Compiler Explorer
坚持迭代器范围
我在上面解释了为什么我认为这是一个坏主意。考虑一下如何正确地解释这一行中的键/值:
""Hello\ World\!"" '8'
您只是不想在解析器之外处理语法。但是,也许您的数据是一个10gb的内存映射文件:
using Field = boost::iterator_range<std::string::const_iterator>;
using Attribute = std::pair<Field /*key*/, //
Field /*value*/>;
然后将x3::raw[]
添加到词素中:
auto quoted = x3::lexeme[x3::raw['"' >> *quoted_char >> '"']];
auto unquoted = x3::lexeme[x3::raw[+(x3::graph - ';' - '=')]];
查看Live On Compiler Explorer