Skip to content

Instantly share code, notes, and snippets.

@koral--
Created November 28, 2023 11:30
Show Gist options
  • Save koral--/16db13ef54f6950850b66f15111ec373 to your computer and use it in GitHub Desktop.
Save koral--/16db13ef54f6950850b66f15111ec373 to your computer and use it in GitHub Desktop.
ARB parser
void main() {
final input = stdin.readLineSync();
final result = PatternDefinition().build().parse(input ?? '');
if (result is Success) {
print('Pattern found, result: ${result.value}');
} else {
print('Pattern not found');
}
}
class PatternDefinition extends GrammarDefinition<int?> {
@override
Parser<int?> start() => ref0(value).end(); // 1
Parser<int?> value() => seq3( // 2
char('{').trim(), // 2a
digit().plus().flatten(), // 2b
char('}').trim(), // 2c
).map3((_, digits, __) => int.tryParse(digits)); // 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment