From c5cbdd89438f7a11ef58086d3a1ae54e4fe15edf Mon Sep 17 00:00:00 2001 From: Lukas Renggli Date: Wed, 4 Dec 2024 18:52:12 +0100 Subject: [PATCH] Answer for https://github.com/petitparser/dart-petitparser/issues/184 --- test/regression_test.dart | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/regression_test.dart b/test/regression_test.dart index 16b80f33..327b1cee 100644 --- a/test/regression_test.dart +++ b/test/regression_test.dart @@ -56,6 +56,21 @@ class NestedGrammar3 { char('0').map((value) => "'$value' (singleCharacter)"); } +class ProtobufGrammar extends GrammarDefinition { + @override + Parser start() => ref0(file); + Parser file() => + ref0(definition).star() & (endOfInput().trim() | ref0(definition)); + Parser definition() => ref0(message); + Parser message() => + string('message').trim() & + ref0(identifier).trim() & + char('{').trim() & + char('}').trim(); + Parser identifier() => + (letter() & word().star()).flatten('identifier expected'); +} + void main() { test('flatten().trim()', () { final parser = word().plus().flatten().trim(); @@ -656,4 +671,22 @@ void main() { expect(parser, isParseSuccess('\u{2FFFF}', result: 0x2FFFF)); }); }); + group('github.com/petitparser/dart-petitparser/issues/184', () { + final parser = ProtobufGrammar().build(); + test('valid', () { + expect(parser.parse(' '), isSuccess()); + expect(parser.parse('message foo {} '), isSuccess()); + expect(parser.parse('message foo {} message bar {} '), isSuccess()); + }); + test('invalid', () { + expect(parser.parse('message '), + isFailure(message: 'identifier expected', position: 8)); + expect(parser.parse('message 123'), + isFailure(message: 'identifier expected', position: 8)); + expect(parser.parse('message Empty'), + isFailure(message: '"{" expected', position: 13)); + expect(parser.parse('message Empty {'), + isFailure(message: '"}" expected', position: 15)); + }); + }); }