parse method
Parses the given source
string into a Map<String, dynamic>
.
Implementations must:
- Convert the raw string into structured key-value pairs.
- Return a valid map representation.
- Throw a FormatException if parsing fails due to invalid syntax or unexpected input.
Example:
final config = parser.parse('{"debug": true}');
print(config['debug']); // true
Implementation
@override
Map<String, dynamic> parse(String source) {
try {
// Remove comments and clean up the source
final cleanSource = _removeComments(source);
// Look for map literals or variable assignments
final mapMatch = RegExp(r'(?:final|const|var)?\s*\w*\s*=\s*(\{.*\})', dotAll: true)
.firstMatch(cleanSource);
if (mapMatch != null) {
return _parseMapLiteral(mapMatch.group(1)!);
}
// Try to parse as direct map literal
final directMapMatch = RegExp(r'^\s*(\{.*\})\s*$', dotAll: true)
.firstMatch(cleanSource);
if (directMapMatch != null) {
return _parseMapLiteral(directMapMatch.group(1)!);
}
throw ParserException('No valid Dart map found in source');
} catch (e) {
if (e is ParserException) rethrow;
throw ParserException('Failed to parse Dart: $e');
}
}