resolveAllReferences method
Implementation
Token resolveAllReferences(Map<String, Token> tokenMap) {
Token? token = this;
if (_hasColorReference) {
token = token._resolveColorReferences(tokenMap);
}
if (_hasMathExpression) {
token = token._resolveMathExpression(tokenMap);
}
if (token._hasTokenReferences == true) {
if (token.valueAsString?.isTokenReference == true) {
final reference =
tokenMap[token.valueByRef]?.resolveAllReferences(tokenMap);
if (reference == null) {
throw ResolveTokenException(
'Reference not found for `${token.valueByRef}`',
);
}
token = token.copyWith(
value: reference.value,
type: type ?? reference.type,
);
} else {
var resolved = token.valueAsString!;
var match = RegExp(r'{(.*?)}').firstMatch(resolved);
String? type = this.type;
while (match != null) {
final reference =
tokenMap[match.group(1)]?.resolveAllReferences(tokenMap);
if (reference == null) {
throw ResolveTokenException(
'Reference not found for `${match.group(1)}`',
);
}
// If the type is not set yet, set it to the type of the first reference
type = type ?? reference.type;
resolved = resolved.replaceRange(
match.start,
match.end,
reference.value.toString(),
);
match = RegExp(r'{(.*?)}').firstMatch(resolved);
}
token = token.copyWith(
value: resolved,
type: type,
);
}
}
return token._resolveValueReferences(tokenMap);
}