analyze method
Analyse une classe et retourne son token, ou la raison du refus.
Implementation
JxParseOutcome analyze(String rawClassName) {
final raw = rawClassName.trim();
JxParseOutcome fail(JxParseErrorCode code, String message) =>
JxParseOutcome.failure(
JxParseFailure(raw: _preview(raw), code: code, message: message),
);
if (raw.isEmpty) return fail(JxParseErrorCode.empty, 'classe vide');
if (raw.length > maxClassLength) {
return fail(
JxParseErrorCode.tooLong,
'class too long (more than $maxClassLength characters)',
);
}
for (var i = 0; i < raw.length; i++) {
final code = raw.codeUnitAt(i);
if (code <= 0x20 || code == 0x7F) {
return fail(
JxParseErrorCode.invalidCharacter,
'space or control character not allowed',
);
}
}
// Modificateur d'importance : `!` final (`p:4!`) ou initial (`!p:4`).
var working = raw;
var isImportant = false;
if (working.endsWith('!')) {
isImportant = true;
working = working.substring(0, working.length - 1);
} else if (working.startsWith('!')) {
isImportant = true;
working = working.substring(1);
}
if (working.isEmpty) {
return fail(JxParseErrorCode.empty, 'class is only "!"');
}
final List<String> segments;
try {
segments = _splitSegments(working);
} on FormatException catch (e) {
return fail(JxParseErrorCode.unbalancedBrackets, e.message);
}
if (segments.any((s) => s.isEmpty)) {
return fail(
JxParseErrorCode.emptySegment,
'empty segment (extra ":" or missing value)',
);
}
// Consommation des variantes connues en tête, en laissant au moins un
// segment pour l'identifiant.
var index = 0;
while (index < segments.length - 1 && variants.contains(segments[index])) {
index++;
}
final rest = segments.sublist(index);
final variantList = segments.sublist(0, index);
String identifier;
String? value;
if (rest.length == 1) {
identifier = rest.first;
} else {
// Les segments inconnus en surplus sont traités comme des variantes ;
// le moteur signalera ensuite les variantes non supportées.
variantList.addAll(rest.sublist(0, rest.length - 2));
identifier = rest[rest.length - 2];
value = rest.last;
}
if (variantList.length > maxVariants) {
return fail(
JxParseErrorCode.tooManyVariants,
'too many variants (maximum $maxVariants)',
);
}
final seen = <String>{};
for (final variant in variantList) {
if (!_variantPattern.hasMatch(variant)) {
return fail(
JxParseErrorCode.invalidVariant,
'invalid variant "$variant"',
);
}
if (!seen.add(variant)) {
return fail(
JxParseErrorCode.duplicateVariant,
'repeated variant "$variant"',
);
}
}
// Valeurs négatives : `-m:4` ou `m:-4`.
var isNegative = false;
if (identifier.startsWith('-')) {
if (value == null) {
return fail(
JxParseErrorCode.invalidNegative,
'the "-" sign needs a value (for example "-m:4")',
);
}
isNegative = true;
identifier = identifier.substring(1);
}
if (value != null && value.startsWith('-')) {
if (isNegative) {
return fail(
JxParseErrorCode.invalidNegative,
'double negation ("-m:-4")',
);
}
isNegative = true;
value = value.substring(1);
if (value.isEmpty || value.startsWith('-')) {
return fail(
JxParseErrorCode.invalidNegative,
'incomplete negative value',
);
}
}
if (!_identifierPattern.hasMatch(identifier)) {
return fail(
JxParseErrorCode.invalidIdentifier,
identifier.contains('!')
? '"!" goes at the start or the end of the class'
: 'invalid utility "$identifier"',
);
}
if (value != null) {
final problem = _checkValue(value);
if (problem != null) return fail(problem.$1, problem.$2);
}
return JxParseOutcome.success(
JxClassToken(
raw: raw,
identifier: identifier,
value: value,
variants: List.unmodifiable(variantList),
isImportant: isImportant,
isNegative: isNegative,
),
);
}