convert method
Converts input
and returns the result of the conversion.
Implementation
@override
Map<String, dynamic> convert(String input) {
List<String> lines = input.split('\n');
Map<String, dynamic> object = {};
List<dynamic> stack = [object];
bool expect = false;
final regex = RegExp(
'^("((?:\\\\.|[^\\\\"])+)"|([a-z0-9\\-\\_]+))([ \t]*("((?:\\\\.|[^\\\\"])*)(")?|([a-z0-9\\-\\_]+)))?',
);
int i = 0;
final j = lines.length;
bool comment = false;
for (; i < j; i++) {
var line = lines[i].trim();
if (line.startsWith('/*') && line.endsWith('*/')) {
continue;
}
if (line.startsWith('/*')) {
comment = true;
continue;
}
if (line.endsWith('*/')) {
comment = false;
continue;
}
if (comment) {
continue;
}
if (line == '' || line[0] == '/') {
continue;
}
if (line[0] == '{') {
expect = false;
continue;
}
if (expect) {
throw FormatException('Invalid syntax on line ${i + 1}.');
}
if (line[0] == '}') {
stack.removeLast();
continue;
}
while (true) {
var m = regex.firstMatch(line);
if (m == null) {
throw FormatException('Invalid syntax on line ${i + 1}.');
}
dynamic key = (m[2] != null) ? m[2] : m[3];
dynamic val = (m[6] != null) ? m[6] : m[8];
if (val == null) {
if ((stack[stack.length - 1] as Map)[key] == null) {
(stack[stack.length - 1] as Map)[key] = {};
}
stack.add((stack[stack.length - 1] as Map)[key]);
expect = true;
} else {
if (m[7] == null && m[8] == null) {
line += '\n${lines[++i]}';
continue;
}
if (val != '' && num.tryParse(val) != null) {
val = num.parse(val);
}
if (val == 'true') {
val = true;
}
if (val == 'false') {
val = false;
}
if (val == 'null') {
val = null;
}
if (val == 'undefined') {
val = null;
}
(stack[stack.length - 1] as Map)[key] = val;
}
break;
}
}
if (stack.length != 1) {
throw FormatException('Open parentheses somewhere.');
}
return object;
}