-
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 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 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);
});
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The reason this doesn't work for the whitespace before the closing is that final parser = seq3(
char("*"),
seq2(
whitespace().not(),
[seq2(whitespace(), char('*')), char('*')]
.toChoiceParser()
.neg()
.plus(),
).flatten(),
char("*"),
); Easier and maybe closer to what you want is the following: final parser = seq3(
char("*"),
char('*').neg().plus().flatten(),
char("*"),
).where((sequence) => sequence.second.trim() == sequence.second); |
Beta Was this translation helpful? Give feedback.
-
Thank you so much |
Beta Was this translation helpful? Give feedback.
The reason this doesn't work for the whitespace before the closing is that
char("*").neg().plus()
reads over the whitespace and to right before the asterisk. You can fix this like so:Easier and maybe closer to what you want is the following: