parse method
- String source
override
Attempts to parse the source string matching this element.
Returns a ParseResult containing the parsed lexeme and remaining string on success. On failure, returns a failed ParseResult.
Implementation
@override
ParseResult parse(String source) {
for (final element in elements) {
final result = element.parse(source);
if (result.success) {
// Return the first successful match found.
// Construct a new result attributed to this Alternatives rule,
// pushing the successful sub-element's result onto its stack.
final lexeme = source.substring(
0,
source.length - result.remaining.length,
);
return ParseResult(
true,
result.remaining,
element: toString(),
lexeme: lexeme,
stack: Stack<ParseResult>()..push(result),
);
}
// If result.success is false, we continue to the next alternative (backtracking).
}
// If no alternatives succeeded, return failure.
return ParseResult(false, source, element: toString());
}