-
I saw previous question about this topic #98 but unfortunately it doesn't seem to work, or at least I don't understand what I'm doing wrong. class ProtobufGrammar extends GrammarDefinition {
@override
Parser start() => ref0(file).end();
Parser file() => ref0(definition).star();
Parser definition() => ref0(message);
Parser message() =>
string('message').trim() &
ref0(identifier).trim() &
char('{').trim() &
char('}').trim();
Parser identifier() => (letter() & word().star())
.flatten("identifier is expected");
}
void main() {
final parser = ProtobufGrammar().build();
test('message without name', () {
final input = 'message ';
final result = parser.parse(input);
// Would be great to have an error: identifier is expected
expect(result, isSuccess());
});
test('message with name', () {
final input = 'message Empty';
final result = parser.parse(input);
// Would be great to have an error: '{' is expected
expect(result, isSuccess());
});
test('Bad identifier', () {
final input = 'message f123';
final result = parser.parse(input);
expect(result, isSuccess());
});
} All test cases fail (that's correct, since input is malformed), with the same error message: I think it goes into: Parser message() =>
string('message').trim() & and since it can't satisfy |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It seems to work the way I expect if I omit |
Beta Was this translation helpful? Give feedback.
-
Thank you for the one-click reproducible example. This is really appreciated! As you observed, the issue is that the You can avoid the problem after the repeating
Parser start() => ref0(file);
Parser file() => ref0(definition).star() & (endOfInput().trim() | ref0(definition));
... |
Beta Was this translation helpful? Give feedback.
-
Thanks for a quick reply, seems to work! |
Beta Was this translation helpful? Give feedback.
Thank you for the one-click reproducible example. This is really appreciated!
As you observed, the issue is that the
star()
operator is happy to end before a failing definition, which then causes the end-of-input parser to fail with a not so helpful error message.You can avoid the problem after the repeating
star()
operator by checking that you either