-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// The abstraction of a notation system. | ||
abstract class NotationSystem<T> { | ||
/// Creates a new [NotationSystem]. | ||
const NotationSystem(); | ||
|
||
/// The regular expression to match against a source. | ||
RegExp get regExp; | ||
|
||
/// The first match of [regExp] in [source]. | ||
RegExpMatch? match(String source) => regExp.firstMatch(source); | ||
|
||
/// Parse [match] as a [T]. | ||
T parse(RegExpMatch match); | ||
} | ||
|
||
/// A notation system List extension. | ||
extension NotationSystemListExtension<T> on List<NotationSystem<T>> { | ||
/// Tries to parse [source] as a [T] in any [NotationSystem]. | ||
/// Otherwise, throws a [FormatException]. | ||
T parse(String source) { | ||
for (final system in this) { | ||
final match = system.match(source); | ||
if (match != null) return system.parse(match); | ||
} | ||
|
||
throw FormatException('Invalid ${T.runtimeType}', source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters