我可以用dart-petiparser成功地解析逗号分隔的列表,但当遇到空格分隔的列表时,相同的代码会失败:
class MyDefinition extends GrammarDefinition {
@override
Parser start() => throw UnsupportedError('Not yet.');
Parser commas() =>
ref0(number).separatedBy(char(','), includeSeparators: false);
Parser spaces() =>
ref0(number).separatedBy(char(' '), includeSeparators: false);
Parser<int> number() => digit().plus().flatten().trim().map(int.parse);
}
test('commas', () {
var defn = MyDefinition();
var parser = defn.build(start: defn.commas);
expect(parser.parse("7,4,9,5").value, [7, 4, 9, 5]); // PASS
});
test('spaces', () {
var defn = MyDefinition();
var parser = defn.build(start: defn.spaces);
expect(parser.parse("7 4 9 5").value, [7, 4, 9, 5]); // FAIL
});
它失败的原因如下:
Expected: [7, 4, 9, 5]
Actual: [7]
Which: at location [1] is [7] which shorter than expected
我做错了什么?我浏览了一堆语法示例,并没有在任何地方找到以空格分隔的列表。