I am stuck at one point with the Dart package petitparser: It seems that the "priority rule" ("parse p1, if that doesn't work parse p2 - ordered choice") is ignored by the toChoiceParser() if a plus() parser is added.
import 'package:petitparser/petitparser.dart';
// This parser should check from left to right if a nestedTerm, e.g. '(0)' or '(()', exists.
// If this is not the case, then it looks if a singleCharacter exists, either '(', ')' or '0' (lower priority).
// In case 1 everything works perfectly. But if the process is repeated any number of times, as in case 2,
// then it seems that it no longer recognizes that a nestedTerm exists and that this should actually lead
// to the same terminal output as in case 1 due to the higher priority. Where is my fallacy?
void main() {
final definition = ExpressionDefinition();
final parser = definition.build();
print(parser.parse('(0)').toString());
// Terminal output in case 1: ['(' (nestedTerm), '0' (singleCharacter), ')' (nestedTerm)]
// Terminal output in case 2: ['(' (singleCharacter), '0' (singleCharacter), ')' (singleCharacter)]
}
class ExpressionDefinition extends GrammarDefinition {
@override
Parser start() => ref0(term).end();
// Case 1 (parses only once):
Parser term() => ref0(nestedTerm) | ref0(singleCharacter);
// Case 2 (parses one or more times):
// Parser term() => (ref0(nestedTerm) | ref0(singleCharacter)).plus();
Parser nestedTerm() =>
(char('(')).map((value) => "'$value' (nestedTerm)") &
ref0(term) &
char(')').map((value) => "'$value' (nestedTerm)");
Parser singleCharacter() =>
char('(').map((value) => "'$value' (singleCharacter)") |
char(')').map((value) => "'$value' (singleCharacter)") |
char('0').map((value) => "'$value' (singleCharacter)");
}
However, for my current project, the "priority rule" should also work in this case (in this example case 2).
Can anyone find my fallacy? Thanks a lot for your support!