Skip to content

Greedy parse problem #145

@nacro90

Description

@nacro90

Hi, thanks for awesome library.

I have a problem with the parsing. I couldn't be sure if the behavior is wanted or not.

The parser code supposed to parse bold text format, which means surrounding by * without preceding or succeeding whitespace.

Parser<Sequence3> bold() => seq3(
      char("*"),
      seq3(
        whitespace().not(),
        char("*").neg().plus(),
        whitespace().not(),
      ).flatten(),
      char("*"),
    );

For this parser code, I wrote two tests for preceding and succeeding. Preceding works as expected but succeeding parses the input as ['*', 'invalid ', '*'] with following whitespace included. How can I avoid the parser not to parse the whitespace? I tried whitespace().neg() instead of not()but then it parses''and it can not parse the correct bold text likeboldbecause of negation of whitespace parses and consumes the`.

Working:

test("should not parse with succeeded whitespace after opening", () {
  // given
  // when
  final result = grammar.build(start: grammar.bold).accept('* invalid*');
  // then
  expect(result, isFalse);
});

Not working:

test("should not parse with preceding whitespace before closing", () {
  // given
  // when
  final result =
      trace(grammar.build(start: grammar.bold)).accept('*invalid *');
  // then
  expect(result, isFalse);
});
petitparser: ^5.1.0
Flutter 3.7.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 12cb4eb7a0 (6 days ago) • 2023-03-01 10:29:26 -0800
Engine • revision ada363ee93
Tools • Dart 2.19.3 • DevTools 2.20.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions