ICU Message Syntax Parser (#112390)
* init * code generation * improve syntax error, add tests * add tests and fix bugs * code generation fix * fix all tests :) * fix bug * init * fix all code gen issues * FIXED ALL TESTS :D * add license * remove trailing spaces * remove print * tests fix * specify type annotation * fix test * lint * fix todos * fix subclass issues * final fix; flutter gallery runs * escaping for later pr * fix comment * address PR comments * more * more descriptive errors * last fixes
This commit is contained in:
@@ -13,6 +13,7 @@ import '../flutter_manifest.dart';
|
||||
import 'gen_l10n_templates.dart';
|
||||
import 'gen_l10n_types.dart';
|
||||
import 'localizations_utils.dart';
|
||||
import 'message_parser.dart';
|
||||
|
||||
/// Run the localizations generation script with the configuration [options].
|
||||
LocalizationsGenerator generateLocalizations({
|
||||
@@ -84,22 +85,30 @@ String _defaultSyntheticPackagePath(FileSystem fileSystem) => fileSystem.path.jo
|
||||
/// localizations tool.
|
||||
String _syntheticL10nPackagePath(FileSystem fileSystem) => fileSystem.path.join(_defaultSyntheticPackagePath(fileSystem), 'gen_l10n');
|
||||
|
||||
// Generate method parameters and also infer the correct types from the usage of the placeholders
|
||||
// For example, if placeholders are used for plurals and no type was specified, then the type will
|
||||
// automatically set to 'num'. Similarly, if such placeholders are used for selects, then the type
|
||||
// will be set to 'String'. For such placeholders that are used for both, we should throw an error.
|
||||
// TODO(thkim1011): Let's store the output of this function in the Message class, so that we don't
|
||||
// recompute this. See https://github.com/flutter/flutter/issues/112709
|
||||
List<String> generateMethodParameters(Message message) {
|
||||
assert(message.placeholders.isNotEmpty);
|
||||
final Placeholder? countPlaceholder = message.isPlural ? message.getCountPlaceholder() : null;
|
||||
return message.placeholders.map((Placeholder placeholder) {
|
||||
final String? type = placeholder == countPlaceholder ? 'num' : placeholder.type;
|
||||
return '$type ${placeholder.name}';
|
||||
return '${placeholder.type} ${placeholder.name}';
|
||||
}).toList();
|
||||
}
|
||||
|
||||
// Similar to above, but is used for passing arguments into helper functions.
|
||||
List<String> generateMethodArguments(Message message) {
|
||||
return message.placeholders.map((Placeholder placeholder) => placeholder.name).toList();
|
||||
}
|
||||
|
||||
String generateDateFormattingLogic(Message message) {
|
||||
if (message.placeholders.isEmpty || !message.placeholdersRequireFormatting) {
|
||||
return '@(none)';
|
||||
}
|
||||
|
||||
final Iterable<String> formatStatements = message.placeholders
|
||||
.where((Placeholder placeholder) => placeholder.isDate)
|
||||
.where((Placeholder placeholder) => placeholder.requiresDateFormatting)
|
||||
.map((Placeholder placeholder) {
|
||||
final String? placeholderFormat = placeholder.format;
|
||||
if (placeholderFormat == null) {
|
||||
@@ -130,7 +139,7 @@ String generateDateFormattingLogic(Message message) {
|
||||
}
|
||||
return dateFormatCustomTemplate
|
||||
.replaceAll('@(placeholder)', placeholder.name)
|
||||
.replaceAll('@(format)', generateString(placeholderFormat));
|
||||
.replaceAll('@(format)', "'${generateString(placeholderFormat)}'");
|
||||
});
|
||||
|
||||
return formatStatements.isEmpty ? '@(none)' : formatStatements.join();
|
||||
@@ -142,7 +151,7 @@ String generateNumberFormattingLogic(Message message) {
|
||||
}
|
||||
|
||||
final Iterable<String> formatStatements = message.placeholders
|
||||
.where((Placeholder placeholder) => placeholder.isNumber)
|
||||
.where((Placeholder placeholder) => placeholder.requiresNumFormatting)
|
||||
.map((Placeholder placeholder) {
|
||||
final String? placeholderFormat = placeholder.format;
|
||||
if (!placeholder.hasValidNumberFormat || placeholderFormat == null) {
|
||||
@@ -158,7 +167,7 @@ String generateNumberFormattingLogic(Message message) {
|
||||
if (parameter.value is num) {
|
||||
return '${parameter.name}: ${parameter.value}';
|
||||
} else {
|
||||
return '${parameter.name}: ${generateString(parameter.value.toString())}';
|
||||
return "${parameter.name}: '${generateString(parameter.value.toString())}'";
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -178,279 +187,24 @@ String generateNumberFormattingLogic(Message message) {
|
||||
return formatStatements.isEmpty ? '@(none)' : formatStatements.join();
|
||||
}
|
||||
|
||||
/// To make it easier to parse plurals or select messages, temporarily replace
|
||||
/// each "{placeholder}" parameter with "#placeholder#" for example.
|
||||
String _replacePlaceholdersBraces(
|
||||
String translationForMessage,
|
||||
Iterable<Placeholder> placeholders,
|
||||
String replacementBraces,
|
||||
) {
|
||||
assert(replacementBraces.length == 2);
|
||||
String easyMessage = translationForMessage;
|
||||
for (final Placeholder placeholder in placeholders) {
|
||||
easyMessage = easyMessage.replaceAll(
|
||||
'{${placeholder.name}}',
|
||||
'${replacementBraces[0]}${placeholder.name}${replacementBraces[1]}',
|
||||
);
|
||||
}
|
||||
return easyMessage;
|
||||
}
|
||||
|
||||
/// Replaces message with the interpolated variable name of the given placeholders
|
||||
/// with the ability to change braces to something other than {...}.
|
||||
///
|
||||
/// Examples:
|
||||
///
|
||||
/// * Replacing `{userName}`.
|
||||
/// ```dart
|
||||
/// final message = 'Hello my name is {userName}';
|
||||
/// final transformed = _replacePlaceholdersWithVariables(message, placeholders);
|
||||
/// // transformed == 'Hello my name is $userName'
|
||||
/// ```
|
||||
/// * Replacing `#choice#`.
|
||||
/// ```dart
|
||||
/// final message = 'I would like to have some #choice#';
|
||||
/// final transformed = _replacePlaceholdersWithVariables(message, placeholders, '##');
|
||||
/// transformed == 'I would like to have some $choice'
|
||||
/// ```
|
||||
String _replacePlaceholdersWithVariables(String message, Iterable<Placeholder> placeholders, [String braces = '{}']) {
|
||||
assert(braces.length == 2);
|
||||
String messageWithValues = message;
|
||||
for (final Placeholder placeholder in placeholders) {
|
||||
String variable = placeholder.name;
|
||||
if (placeholder.requiresFormatting) {
|
||||
variable += 'String';
|
||||
}
|
||||
messageWithValues = messageWithValues.replaceAll(
|
||||
'${braces[0]}${placeholder.name}${braces[1]}',
|
||||
_needsCurlyBracketStringInterpolation(messageWithValues, placeholder.name)
|
||||
? '\${$variable}'
|
||||
: '\$$variable'
|
||||
);
|
||||
}
|
||||
return messageWithValues;
|
||||
}
|
||||
|
||||
String _generatePluralMethod(Message message, String translationForMessage) {
|
||||
if (message.placeholders.isEmpty) {
|
||||
throw L10nException(
|
||||
'Unable to find placeholders for the plural message: ${message.resourceId}.\n'
|
||||
'Check to see if the plural message is in the proper ICU syntax format '
|
||||
'and ensure that placeholders are properly specified.'
|
||||
);
|
||||
}
|
||||
|
||||
final String easyMessage = _replacePlaceholdersBraces(translationForMessage, message.placeholders, '##');
|
||||
|
||||
final Placeholder countPlaceholder = message.getCountPlaceholder();
|
||||
const Map<String, String> pluralIds = <String, String>{
|
||||
'=0': 'zero',
|
||||
'=1': 'one',
|
||||
'=2': 'two',
|
||||
'few': 'few',
|
||||
'many': 'many',
|
||||
'other': 'other',
|
||||
};
|
||||
|
||||
final List<String> pluralLogicArgs = <String>[];
|
||||
for (final String pluralKey in pluralIds.keys) {
|
||||
final RegExp expRE = RegExp('($pluralKey)\\s*{([^}]+)}');
|
||||
final RegExpMatch? match = expRE.firstMatch(easyMessage);
|
||||
if (match != null && match.groupCount == 2) {
|
||||
final String argValue = _replacePlaceholdersWithVariables(generateString(match.group(2)!), message.placeholders, '##');
|
||||
pluralLogicArgs.add(' ${pluralIds[pluralKey]}: $argValue');
|
||||
}
|
||||
}
|
||||
|
||||
final List<String> parameters = message.placeholders.map((Placeholder placeholder) {
|
||||
final String? placeholderType = placeholder == countPlaceholder ? 'num' : placeholder.type;
|
||||
return '$placeholderType ${placeholder.name}';
|
||||
}).toList();
|
||||
|
||||
final String comment = message.description ?? 'No description provided in @${message.resourceId}';
|
||||
|
||||
if (translationForMessage.startsWith('{') && translationForMessage.endsWith('}')) {
|
||||
return pluralMethodTemplate
|
||||
.replaceAll('@(comment)', comment)
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(dateFormatting)', generateDateFormattingLogic(message))
|
||||
.replaceAll('@(numberFormatting)', generateNumberFormattingLogic(message))
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(count)', countPlaceholder.name)
|
||||
.replaceAll('@(pluralLogicArgs)', pluralLogicArgs.join(',\n'))
|
||||
.replaceAll('@(none)\n', '');
|
||||
}
|
||||
|
||||
const String variable = 'pluralString';
|
||||
final String string = _replaceWithVariable(translationForMessage, variable);
|
||||
return pluralMethodTemplateInString
|
||||
.replaceAll('@(comment)', comment)
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(dateFormatting)', generateDateFormattingLogic(message))
|
||||
.replaceAll('@(numberFormatting)', generateNumberFormattingLogic(message))
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(variable)', variable)
|
||||
.replaceAll('@(count)', countPlaceholder.name)
|
||||
.replaceAll('@(pluralLogicArgs)', pluralLogicArgs.join(',\n'))
|
||||
.replaceAll('@(none)\n', '')
|
||||
.replaceAll('@(string)', string);
|
||||
}
|
||||
|
||||
String _replaceWithVariable(String translation, String variable) {
|
||||
String prefix = generateString(translation.substring(0, translation.indexOf('{')));
|
||||
prefix = prefix.substring(0, prefix.length - 1);
|
||||
String suffix = generateString(translation.substring(translation.lastIndexOf('}') + 1));
|
||||
suffix = suffix.substring(1);
|
||||
|
||||
// escape variable when the suffix can be combined with the variable
|
||||
if (suffix.isNotEmpty && !suffix.startsWith(' ')) {
|
||||
variable = '{$variable}';
|
||||
}
|
||||
return prefix + r'$' + variable + suffix;
|
||||
}
|
||||
|
||||
String _generateSelectMethod(Message message, String translationForMessage) {
|
||||
if (message.placeholders.isEmpty) {
|
||||
throw L10nException(
|
||||
'Unable to find placeholders for the select message: ${message.resourceId}.\n'
|
||||
'Check to see if the select message is in the proper ICU syntax format '
|
||||
'and ensure that placeholders are properly specified.'
|
||||
);
|
||||
}
|
||||
|
||||
final String easyMessage = _replacePlaceholdersBraces(translationForMessage, message.placeholders, '##');
|
||||
|
||||
final List<String> cases = <String>[];
|
||||
|
||||
final RegExpMatch? selectMatch = LocalizationsGenerator._selectRE.firstMatch(easyMessage);
|
||||
String? choice;
|
||||
if (selectMatch != null && selectMatch.groupCount == 2) {
|
||||
choice = selectMatch.group(1);
|
||||
final String pattern = selectMatch.group(2)!;
|
||||
final RegExp patternRE = RegExp(r'\s*([\w\d]+)\s*\{(.*?)\}');
|
||||
for (final RegExpMatch patternMatch in patternRE.allMatches(pattern)) {
|
||||
if (patternMatch.groupCount == 2) {
|
||||
String value = patternMatch.group(2)!
|
||||
.replaceAll("'", r"\'")
|
||||
.replaceAll('"', r'\"');
|
||||
value = _replacePlaceholdersWithVariables(value, message.placeholders, '##');
|
||||
cases.add(
|
||||
" '${patternMatch.group(1)}': '$value'",
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw L10nException(
|
||||
'Incorrect select message format for: ${message.resourceId}.\n'
|
||||
'Check to see if the select message is in the proper ICU syntax format.'
|
||||
);
|
||||
}
|
||||
|
||||
final List<String> parameters = message.placeholders.map((Placeholder placeholder) {
|
||||
final String placeholderType = placeholder.type ?? 'object';
|
||||
return '$placeholderType ${placeholder.name}';
|
||||
}).toList();
|
||||
|
||||
final String description = message.description ?? 'No description provided in @${message.resourceId}';
|
||||
|
||||
if (translationForMessage.startsWith('{') && translationForMessage.endsWith('}')) {
|
||||
return selectMethodTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(choice)', choice!)
|
||||
.replaceAll('@(cases)', cases.join(',\n').trim())
|
||||
.replaceAll('@(description)', description);
|
||||
}
|
||||
|
||||
const String variable = 'selectString';
|
||||
final String string = _replaceWithVariable(translationForMessage, variable);
|
||||
return selectMethodTemplateInString
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(variable)', variable)
|
||||
.replaceAll('@(choice)', choice!)
|
||||
.replaceAll('@(cases)', cases.join(',\n').trim())
|
||||
.replaceAll('@(description)', description)
|
||||
.replaceAll('@(string)', string);
|
||||
}
|
||||
|
||||
bool _needsCurlyBracketStringInterpolation(String messageString, String placeholder) {
|
||||
final int placeholderIndex = messageString.indexOf(placeholder);
|
||||
// This means that this message does not contain placeholders/parameters,
|
||||
// since one was not found in the message.
|
||||
if (placeholderIndex == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final bool isPlaceholderEndOfSubstring = placeholderIndex + placeholder.length + 2 == messageString.length;
|
||||
|
||||
if (placeholderIndex > 2 && !isPlaceholderEndOfSubstring) {
|
||||
// Normal case
|
||||
// Examples:
|
||||
// "'The number of {hours} elapsed is: 44'" // no curly brackets.
|
||||
// "'哈{hours}哈'" // no curly brackets.
|
||||
// "'m#hours#m'" // curly brackets.
|
||||
// "'I have to work _#hours#_' sometimes." // curly brackets.
|
||||
final RegExp commonCaseRE = RegExp('[^a-zA-Z_][#{]$placeholder[#}][^a-zA-Z_]');
|
||||
return !commonCaseRE.hasMatch(messageString);
|
||||
} else if (placeholderIndex == 2) {
|
||||
// Example:
|
||||
// "'{hours} elapsed.'" // no curly brackets
|
||||
// '#placeholder# ' // no curly brackets
|
||||
// '#placeholder#m' // curly brackets
|
||||
final RegExp startOfString = RegExp('[#{]$placeholder[#}][^a-zA-Z_]');
|
||||
return !startOfString.hasMatch(messageString);
|
||||
} else {
|
||||
// Example:
|
||||
// "'hours elapsed: {hours}'"
|
||||
// "'Time elapsed: {hours}'" // no curly brackets
|
||||
// ' #placeholder#' // no curly brackets
|
||||
// 'm#placeholder#' // curly brackets
|
||||
final RegExp endOfString = RegExp('[^a-zA-Z_][#{]$placeholder[#}]');
|
||||
return !endOfString.hasMatch(messageString);
|
||||
}
|
||||
}
|
||||
|
||||
String _generateMethod(Message message, String translationForMessage) {
|
||||
String generateMessage() {
|
||||
return _replacePlaceholdersWithVariables(generateString(translationForMessage), message.placeholders);
|
||||
}
|
||||
|
||||
if (message.isPlural) {
|
||||
return _generatePluralMethod(message, translationForMessage);
|
||||
}
|
||||
|
||||
if (message.isSelect) {
|
||||
return _generateSelectMethod(message, translationForMessage);
|
||||
}
|
||||
|
||||
if (message.placeholdersRequireFormatting) {
|
||||
return formatMethodTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', generateMethodParameters(message).join(', '))
|
||||
.replaceAll('@(dateFormatting)', generateDateFormattingLogic(message))
|
||||
.replaceAll('@(numberFormatting)', generateNumberFormattingLogic(message))
|
||||
.replaceAll('@(message)', generateMessage())
|
||||
.replaceAll('@(none)\n', '');
|
||||
}
|
||||
|
||||
if (message.placeholders.isNotEmpty) {
|
||||
return methodTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', generateMethodParameters(message).join(', '))
|
||||
.replaceAll('@(message)', generateMessage());
|
||||
}
|
||||
|
||||
return getterTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(message)', generateMessage());
|
||||
}
|
||||
/// List of possible cases for plurals defined the ICU messageFormat syntax.
|
||||
Map<String, String> pluralCases = <String, String>{
|
||||
'0': 'zero',
|
||||
'1': 'one',
|
||||
'2': 'two',
|
||||
'zero': 'zero',
|
||||
'one': 'one',
|
||||
'two': 'two',
|
||||
'few': 'few',
|
||||
'many': 'many',
|
||||
'other': 'other',
|
||||
};
|
||||
|
||||
String generateBaseClassMethod(Message message, LocaleInfo? templateArbLocale) {
|
||||
final String comment = message.description ?? 'No description provided for @${message.resourceId}.';
|
||||
final String templateLocaleTranslationComment = '''
|
||||
/// In $templateArbLocale, this message translates to:
|
||||
/// **${generateString(message.value)}**''';
|
||||
/// **'${generateString(message.value)}'**''';
|
||||
|
||||
if (message.placeholders.isNotEmpty) {
|
||||
return baseClassMethodTemplate
|
||||
@@ -806,6 +560,10 @@ class LocalizationsGenerator {
|
||||
/// ['es', 'en'] is passed in, the 'es' locale will take priority over 'en'.
|
||||
final List<LocaleInfo> preferredSupportedLocales;
|
||||
|
||||
// Whether we need to import intl or not. This flag is updated after parsing
|
||||
// all of the messages.
|
||||
bool requiresIntlImport = false;
|
||||
|
||||
/// The list of all arb path strings in [inputDirectory].
|
||||
List<String> get arbPathStrings {
|
||||
return _allBundles.bundles.map((AppResourceBundle bundle) => bundle.file.path).toList();
|
||||
@@ -870,8 +628,6 @@ class LocalizationsGenerator {
|
||||
/// Logger to be used during the execution of the script.
|
||||
Logger logger;
|
||||
|
||||
static final RegExp _selectRE = RegExp(r'\{([\w\s,]*),\s*select\s*,\s*([\w\d]+\s*\{.*\})+\s*\}');
|
||||
|
||||
static bool _isNotReadable(FileStat fileStat) {
|
||||
final String rawStatString = fileStat.modeString();
|
||||
// Removes potential prepended permission bits, such as '(suid)' and '(guid)'.
|
||||
@@ -1087,7 +843,7 @@ class LocalizationsGenerator {
|
||||
// files in inputDirectory. Also initialized: supportedLocales.
|
||||
void loadResources() {
|
||||
_allMessages = _templateBundle.resourceIds.map((String id) => Message(
|
||||
_templateBundle.resources, id, areResourceAttributesRequired,
|
||||
_templateBundle.resources, id, areResourceAttributesRequired,
|
||||
));
|
||||
for (final String resourceId in _templateBundle.resourceIds) {
|
||||
if (!_isValidGetterAndMethodName(resourceId)) {
|
||||
@@ -1148,25 +904,11 @@ class LocalizationsGenerator {
|
||||
|
||||
return _generateMethod(
|
||||
message,
|
||||
bundle.file.basename,
|
||||
bundle.translationFor(message) ?? templateBundle.translationFor(message)!,
|
||||
);
|
||||
});
|
||||
|
||||
for (final Message message in messages) {
|
||||
if (message.isPlural) {
|
||||
if (message.placeholders.isEmpty) {
|
||||
throw L10nException(
|
||||
'Unable to find placeholders for the plural message: ${message.resourceId}.\n'
|
||||
'Check to see if the plural message is in the proper ICU syntax format '
|
||||
'and ensure that placeholders are properly specified.');
|
||||
}
|
||||
final Placeholder countPlaceholder = message.getCountPlaceholder();
|
||||
if (countPlaceholder.type != null && countPlaceholder.type != 'num') {
|
||||
logger.printWarning("Placeholders for plurals are automatically converted to type 'num' for the message: ${message.resourceId}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return classFileTemplate
|
||||
.replaceAll('@(header)', header.isEmpty ? '' : '$header\n\n')
|
||||
.replaceAll('@(language)', describeLocale(locale.toString()))
|
||||
@@ -1175,7 +917,7 @@ class LocalizationsGenerator {
|
||||
.replaceAll('@(class)', '$className${locale.camelCase()}')
|
||||
.replaceAll('@(localeName)', locale.toString())
|
||||
.replaceAll('@(methods)', methods.join('\n\n'))
|
||||
.replaceAll('@(requiresIntlImport)', _requiresIntlImport() ? "import 'package:intl/intl.dart' as intl;\n\n" : '');
|
||||
.replaceAll('@(requiresIntlImport)', requiresIntlImport ? "import 'package:intl/intl.dart' as intl;\n\n" : '');
|
||||
}
|
||||
|
||||
String _generateSubclass(
|
||||
@@ -1194,7 +936,7 @@ class LocalizationsGenerator {
|
||||
|
||||
final Iterable<String> methods = messages
|
||||
.where((Message message) => bundle.translationFor(message) != null)
|
||||
.map((Message message) => _generateMethod(message, bundle.translationFor(message)!));
|
||||
.map((Message message) => _generateMethod(message, bundle.file.basename, bundle.translationFor(message)!));
|
||||
|
||||
return subclassTemplate
|
||||
.replaceAll('@(language)', describeLocale(locale.toString()))
|
||||
@@ -1328,7 +1070,7 @@ class LocalizationsGenerator {
|
||||
.replaceAll('@(messageClassImports)', sortedClassImports.join('\n'))
|
||||
.replaceAll('@(delegateClass)', delegateClass)
|
||||
.replaceAll('@(requiresFoundationImport)', useDeferredLoading ? '' : "import 'package:flutter/foundation.dart';")
|
||||
.replaceAll('@(requiresIntlImport)', _requiresIntlImport() ? "import 'package:intl/intl.dart' as intl;" : '')
|
||||
.replaceAll('@(requiresIntlImport)', requiresIntlImport ? "import 'package:intl/intl.dart' as intl;" : '')
|
||||
.replaceAll('@(canBeNullable)', usesNullableGetter ? '?' : '')
|
||||
.replaceAll('@(needsNullCheck)', usesNullableGetter ? '' : '!')
|
||||
// Removes all trailing whitespace from the generated file.
|
||||
@@ -1337,11 +1079,207 @@ class LocalizationsGenerator {
|
||||
.replaceAll('\n\n\n', '\n\n');
|
||||
}
|
||||
|
||||
bool _requiresIntlImport() => _allMessages.any((Message message) {
|
||||
return message.isPlural
|
||||
|| message.isSelect
|
||||
|| message.placeholdersRequireFormatting;
|
||||
});
|
||||
String _generateMethod(Message message, String filename, String translationForMessage) {
|
||||
// Determine if we must import intl for date or number formatting.
|
||||
if (message.placeholdersRequireFormatting) {
|
||||
requiresIntlImport = true;
|
||||
}
|
||||
|
||||
final Node node = Parser(message.resourceId, filename, translationForMessage).parse();
|
||||
// If parse tree is only a string, then return a getter method.
|
||||
if (node.children.every((Node child) => child.type == ST.string)) {
|
||||
// Use the parsed translation to handle escaping with the same behavior.
|
||||
return getterTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(message)', "'${generateString(node.children.map((Node child) => child.value!).join())}'");
|
||||
}
|
||||
|
||||
final List<String> helperMethods = <String>[];
|
||||
|
||||
// Get a unique helper method name.
|
||||
int methodNameCount = 0;
|
||||
String getHelperMethodName() {
|
||||
return '_${message.resourceId}${methodNameCount++}';
|
||||
}
|
||||
|
||||
// Do a DFS post order traversal, generating dependent
|
||||
// placeholder, plural, select helper methods, and combine these into
|
||||
// one message. Returns the method/placeholder to use in parent string.
|
||||
HelperMethod generateHelperMethods(Node node, { bool isRoot = false }) {
|
||||
final Set<Placeholder> dependentPlaceholders = <Placeholder>{};
|
||||
switch (node.type) {
|
||||
case ST.message:
|
||||
final List<HelperMethod> helpers = node.children.map<HelperMethod>((Node node) {
|
||||
if (node.type == ST.string) {
|
||||
return HelperMethod(<Placeholder>{}, string: node.value);
|
||||
}
|
||||
final HelperMethod helper = generateHelperMethods(node);
|
||||
dependentPlaceholders.addAll(helper.dependentPlaceholders);
|
||||
return helper;
|
||||
}).toList();
|
||||
final String messageString = generateReturnExpr(helpers);
|
||||
|
||||
// If the message is just a normal string, then only return the string.
|
||||
if (dependentPlaceholders.isEmpty) {
|
||||
return HelperMethod(dependentPlaceholders, string: messageString);
|
||||
}
|
||||
|
||||
// For messages, if we are generating the actual overridden method, then we should also deal with
|
||||
// date and number formatting here.
|
||||
final String helperMethodName = getHelperMethodName();
|
||||
final HelperMethod messageHelper = HelperMethod(dependentPlaceholders, helper: helperMethodName);
|
||||
if (isRoot) {
|
||||
helperMethods.add(methodTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', generateMethodParameters(message).join(', '))
|
||||
.replaceAll('@(dateFormatting)', generateDateFormattingLogic(message))
|
||||
.replaceAll('@(numberFormatting)', generateNumberFormattingLogic(message))
|
||||
.replaceAll('@(message)', messageString)
|
||||
.replaceAll('@(none)\n', '')
|
||||
);
|
||||
} else {
|
||||
helperMethods.add(messageHelperTemplate
|
||||
.replaceAll('@(name)', helperMethodName)
|
||||
.replaceAll('@(parameters)', messageHelper.methodParameters)
|
||||
.replaceAll('@(message)', messageString)
|
||||
);
|
||||
}
|
||||
return messageHelper;
|
||||
|
||||
case ST.placeholderExpr:
|
||||
assert(node.children[1].type == ST.identifier);
|
||||
final Node identifier = node.children[1];
|
||||
// Check that placeholders exist.
|
||||
// TODO(thkim1011): Make message.placeholders a map so that we don't need to do linear time search.
|
||||
// See https://github.com/flutter/flutter/issues/112709
|
||||
final Placeholder placeholder = message.placeholders.firstWhere(
|
||||
(Placeholder placeholder) => placeholder.name == identifier.value,
|
||||
orElse: () {
|
||||
throw L10nException('''
|
||||
Make sure that the specified placeholder is defined in your arb file.
|
||||
$translationForMessage
|
||||
${Parser.indentForError(identifier.positionInMessage)}''');
|
||||
}
|
||||
);
|
||||
dependentPlaceholders.add(placeholder);
|
||||
return HelperMethod(dependentPlaceholders, placeholder: placeholder);
|
||||
|
||||
case ST.pluralExpr:
|
||||
requiresIntlImport = true;
|
||||
final Map<String, String> pluralLogicArgs = <String, String>{};
|
||||
// Recall that pluralExpr are of the form
|
||||
// pluralExpr := "{" ID "," "plural" "," pluralParts "}"
|
||||
assert(node.children[1].type == ST.identifier);
|
||||
assert(node.children[5].type == ST.pluralParts);
|
||||
|
||||
final Node identifier = node.children[1];
|
||||
final Node pluralParts = node.children[5];
|
||||
|
||||
// Check that identifier exists and is of type int or num.
|
||||
final Placeholder placeholder = message.placeholders.firstWhere(
|
||||
(Placeholder placeholder) => placeholder.name == identifier.value,
|
||||
orElse: () {
|
||||
throw L10nException('''
|
||||
Make sure that the specified plural placeholder is defined in your arb file.
|
||||
$translationForMessage
|
||||
${List<String>.filled(identifier.positionInMessage, ' ').join()}^''');
|
||||
}
|
||||
);
|
||||
dependentPlaceholders.add(placeholder);
|
||||
// TODO(thkim1011): Uncomment the following lines after Message refactor.
|
||||
// See https://github.com/flutter/flutter/issues/112709.
|
||||
// if (placeholder.type != 'num' && placeholder.type != 'int') {
|
||||
// throw L10nException('''
|
||||
// The specified placeholder must be of type int or num.
|
||||
// $translationForMessage
|
||||
// ${List<String>.filled(identifier.positionInMessage, ' ').join()}^''');
|
||||
// }
|
||||
|
||||
for (final Node pluralPart in pluralParts.children.reversed) {
|
||||
String pluralCase;
|
||||
Node pluralMessage;
|
||||
if (pluralPart.children[0].value == '=') {
|
||||
assert(pluralPart.children[1].type == ST.number);
|
||||
assert(pluralPart.children[3].type == ST.message);
|
||||
pluralCase = pluralPart.children[1].value!;
|
||||
pluralMessage = pluralPart.children[3];
|
||||
} else {
|
||||
assert(pluralPart.children[0].type == ST.identifier || pluralPart.children[0].type == ST.other);
|
||||
assert(pluralPart.children[2].type == ST.message);
|
||||
pluralCase = pluralPart.children[0].value!;
|
||||
pluralMessage = pluralPart.children[2];
|
||||
}
|
||||
if (!pluralLogicArgs.containsKey(pluralCases[pluralCase])) {
|
||||
final HelperMethod pluralPartHelper = generateHelperMethods(pluralMessage);
|
||||
pluralLogicArgs[pluralCases[pluralCase]!] = ' ${pluralCases[pluralCase]}: ${pluralPartHelper.helperOrPlaceholder},';
|
||||
dependentPlaceholders.addAll(pluralPartHelper.dependentPlaceholders);
|
||||
} else {
|
||||
logger.printWarning('''
|
||||
The plural part specified below is overrided by a later plural part.
|
||||
$translationForMessage
|
||||
${Parser.indentForError(pluralPart.positionInMessage)}
|
||||
''');
|
||||
}
|
||||
}
|
||||
final String helperMethodName = getHelperMethodName();
|
||||
final HelperMethod pluralHelper = HelperMethod(dependentPlaceholders, helper: helperMethodName);
|
||||
helperMethods.add(pluralHelperTemplate
|
||||
.replaceAll('@(name)', helperMethodName)
|
||||
.replaceAll('@(parameters)', pluralHelper.methodParameters)
|
||||
.replaceAll('@(count)', identifier.value!)
|
||||
.replaceAll('@(pluralLogicArgs)', pluralLogicArgs.values.join('\n'))
|
||||
);
|
||||
return pluralHelper;
|
||||
|
||||
case ST.selectExpr:
|
||||
requiresIntlImport = true;
|
||||
// Recall that pluralExpr are of the form
|
||||
// pluralExpr := "{" ID "," "plural" "," pluralParts "}"
|
||||
assert(node.children[1].type == ST.identifier);
|
||||
assert(node.children[5].type == ST.selectParts);
|
||||
|
||||
final Node identifier = node.children[1];
|
||||
// Check that identifier exists
|
||||
final Placeholder placeholder = message.placeholders.firstWhere(
|
||||
(Placeholder placeholder) => placeholder.name == identifier.value,
|
||||
orElse: () {
|
||||
throw L10nException('''
|
||||
Make sure that the specified select placeholder is defined in your arb file.
|
||||
$translationForMessage
|
||||
${Parser.indentForError(identifier.positionInMessage)}''');
|
||||
}
|
||||
);
|
||||
dependentPlaceholders.add(placeholder);
|
||||
final List<String> selectLogicArgs = <String>[];
|
||||
final Node selectParts = node.children[5];
|
||||
|
||||
for (final Node selectPart in selectParts.children) {
|
||||
assert(selectPart.children[0].type == ST.identifier || selectPart.children[0].type == ST.other);
|
||||
assert(selectPart.children[2].type == ST.message);
|
||||
final String selectCase = selectPart.children[0].value!;
|
||||
final Node selectMessage = selectPart.children[2];
|
||||
final HelperMethod selectPartHelper = generateHelperMethods(selectMessage);
|
||||
selectLogicArgs.add(" '$selectCase': ${selectPartHelper.helperOrPlaceholder},");
|
||||
dependentPlaceholders.addAll(selectPartHelper.dependentPlaceholders);
|
||||
}
|
||||
final String helperMethodName = getHelperMethodName();
|
||||
final HelperMethod selectHelper = HelperMethod(dependentPlaceholders, helper: helperMethodName);
|
||||
|
||||
helperMethods.add(selectHelperTemplate
|
||||
.replaceAll('@(name)', helperMethodName)
|
||||
.replaceAll('@(parameters)', selectHelper.methodParameters)
|
||||
.replaceAll('@(choice)', identifier.value!)
|
||||
.replaceAll('@(selectCases)', selectLogicArgs.join('\n'))
|
||||
);
|
||||
return HelperMethod(dependentPlaceholders, helper: helperMethodName);
|
||||
// ignore: no_default_cases
|
||||
default:
|
||||
throw Exception('Cannot call "generateHelperMethod" on node type ${node.type}');
|
||||
}
|
||||
}
|
||||
generateHelperMethods(node, isRoot: true);
|
||||
return helperMethods.last.replaceAll('@(helperMethods)', helperMethods.sublist(0, helperMethods.length - 1).join('\n\n'));
|
||||
}
|
||||
|
||||
List<String> writeOutputFiles({ bool isFromYaml = false }) {
|
||||
// First, generate the string contents of all necessary files.
|
||||
|
||||
@@ -137,68 +137,35 @@ const String getterTemplate = '''
|
||||
const String methodTemplate = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
@(dateFormatting)
|
||||
@(numberFormatting)
|
||||
@(helperMethods)
|
||||
return @(message);
|
||||
}''';
|
||||
|
||||
const String formatMethodTemplate = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
@(dateFormatting)
|
||||
@(numberFormatting)
|
||||
return @(message);
|
||||
}''';
|
||||
const String messageHelperTemplate = '''
|
||||
String @(name)(@(parameters)) {
|
||||
return @(message);
|
||||
}''';
|
||||
|
||||
const String pluralMethodTemplate = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
@(dateFormatting)
|
||||
@(numberFormatting)
|
||||
return intl.Intl.pluralLogic(
|
||||
@(count),
|
||||
locale: localeName,
|
||||
@(pluralLogicArgs),
|
||||
);
|
||||
}''';
|
||||
const String pluralHelperTemplate = '''
|
||||
String @(name)(@(parameters)) {
|
||||
return intl.Intl.pluralLogic(
|
||||
@(count),
|
||||
locale: localeName,
|
||||
@(pluralLogicArgs)
|
||||
);
|
||||
}''';
|
||||
|
||||
const String pluralMethodTemplateInString = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
@(dateFormatting)
|
||||
@(numberFormatting)
|
||||
final String @(variable) = intl.Intl.pluralLogic(
|
||||
@(count),
|
||||
locale: localeName,
|
||||
@(pluralLogicArgs),
|
||||
);
|
||||
|
||||
return @(string);
|
||||
}''';
|
||||
|
||||
const String selectMethodTemplate = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
return intl.Intl.select(
|
||||
@(choice),
|
||||
{
|
||||
@(cases)
|
||||
},
|
||||
desc: '@(description)'
|
||||
);
|
||||
}''';
|
||||
|
||||
const String selectMethodTemplateInString = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
final String @(variable) = intl.Intl.select(
|
||||
@(choice),
|
||||
{
|
||||
@(cases)
|
||||
},
|
||||
desc: '@(description)'
|
||||
);
|
||||
|
||||
return @(string);
|
||||
}''';
|
||||
const String selectHelperTemplate = '''
|
||||
String @(name)(@(parameters)) {
|
||||
return intl.Intl.selectLogic(
|
||||
@(choice),
|
||||
{
|
||||
@(selectCases)
|
||||
},
|
||||
);
|
||||
}''';
|
||||
|
||||
const String classFileTemplate = '''
|
||||
@(header)@(requiresIntlImport)import '@(fileName)';
|
||||
|
||||
@@ -129,6 +129,25 @@ class L10nException implements Exception {
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
class L10nParserException extends L10nException {
|
||||
L10nParserException(
|
||||
this.error,
|
||||
this.fileName,
|
||||
this.messageId,
|
||||
this.messageString,
|
||||
this.charNumber
|
||||
): super('''
|
||||
$error
|
||||
[$fileName:$messageId] $messageString
|
||||
${List<String>.filled(4 + fileName.length + messageId.length + charNumber, ' ').join()}^''');
|
||||
|
||||
final String error;
|
||||
final String fileName;
|
||||
final String messageId;
|
||||
final String messageString;
|
||||
final int charNumber;
|
||||
}
|
||||
|
||||
// One optional named parameter to be used by a NumberFormat.
|
||||
//
|
||||
// Some of the NumberFormat factory constructors have optional named parameters.
|
||||
@@ -202,16 +221,16 @@ class Placeholder {
|
||||
final String resourceId;
|
||||
final String name;
|
||||
final String? example;
|
||||
final String? type;
|
||||
String? type;
|
||||
final String? format;
|
||||
final List<OptionalParameter> optionalParameters;
|
||||
final bool? isCustomDateFormat;
|
||||
|
||||
bool get requiresFormatting => <String>['DateTime', 'double', 'num'].contains(type) || (type == 'int' && format != null);
|
||||
bool get isNumber => <String>['double', 'int', 'num'].contains(type);
|
||||
bool get requiresFormatting => requiresDateFormatting || requiresNumFormatting;
|
||||
bool get requiresDateFormatting => type == 'DateTime';
|
||||
bool get requiresNumFormatting => <String>['int', 'num', 'double'].contains(type) && format != null;
|
||||
bool get hasValidNumberFormat => _validNumberFormats.contains(format);
|
||||
bool get hasNumberFormatWithParameters => _numberFormatsWithNamedParameters.contains(format);
|
||||
bool get isDate => 'DateTime' == type;
|
||||
bool get hasValidDateFormat => _validDateFormats.contains(format);
|
||||
|
||||
static String? _stringAttribute(
|
||||
@@ -290,6 +309,8 @@ class Placeholder {
|
||||
// The value of this Message is "Hello World". The Message's value is the
|
||||
// localized string to be shown for the template ARB file's locale.
|
||||
// The docs for the Placeholder explain how placeholder entries are defined.
|
||||
// TODO(thkim1011): We need to refactor this Message class to own all the messages in each language.
|
||||
// See https://github.com/flutter/flutter/issues/112709.
|
||||
class Message {
|
||||
Message(Map<String, Object?> bundle, this.resourceId, bool isResourceAttributeRequired)
|
||||
: assert(bundle != null),
|
||||
@@ -298,7 +319,12 @@ class Message {
|
||||
description = _description(bundle, resourceId, isResourceAttributeRequired),
|
||||
placeholders = _placeholders(bundle, resourceId, isResourceAttributeRequired),
|
||||
_pluralMatch = _pluralRE.firstMatch(_value(bundle, resourceId)),
|
||||
_selectMatch = _selectRE.firstMatch(_value(bundle, resourceId));
|
||||
_selectMatch = _selectRE.firstMatch(_value(bundle, resourceId)) {
|
||||
if (isPlural) {
|
||||
final Placeholder placeholder = getCountPlaceholder();
|
||||
placeholder.type = 'num';
|
||||
}
|
||||
}
|
||||
|
||||
static final RegExp _pluralRE = RegExp(r'\s*\{([\w\s,]*),\s*plural\s*,');
|
||||
static final RegExp _selectRE = RegExp(r'\s*\{([\w\s,]*),\s*select\s*,');
|
||||
@@ -769,3 +795,50 @@ final Set<String> _iso639Languages = <String>{
|
||||
'zh',
|
||||
'zu',
|
||||
};
|
||||
|
||||
// Used in LocalizationsGenerator._generateMethod.generateHelperMethod.
|
||||
class HelperMethod {
|
||||
HelperMethod(this.dependentPlaceholders, {this.helper, this.placeholder, this.string }):
|
||||
assert((() {
|
||||
// At least one of helper, placeholder, string must be nonnull.
|
||||
final bool a = helper == null;
|
||||
final bool b = placeholder == null;
|
||||
final bool c = string == null;
|
||||
return (!a && b && c) || (a && !b && c) || (a && b && !c);
|
||||
})());
|
||||
|
||||
Set<Placeholder> dependentPlaceholders;
|
||||
String? helper;
|
||||
Placeholder? placeholder;
|
||||
String? string;
|
||||
|
||||
String get helperOrPlaceholder {
|
||||
if (helper != null) {
|
||||
return '$helper($methodArguments)';
|
||||
} else if (string != null) {
|
||||
return '$string';
|
||||
} else {
|
||||
if (placeholder!.requiresFormatting) {
|
||||
return '${placeholder!.name}String';
|
||||
} else {
|
||||
return placeholder!.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String get methodParameters {
|
||||
assert(helper != null);
|
||||
return dependentPlaceholders.map((Placeholder placeholder) =>
|
||||
(placeholder.requiresFormatting)
|
||||
? 'String ${placeholder.name}String'
|
||||
: '${placeholder.type} ${placeholder.name}').join(', ');
|
||||
}
|
||||
|
||||
String get methodArguments {
|
||||
assert(helper != null);
|
||||
return dependentPlaceholders.map((Placeholder placeholder) =>
|
||||
(placeholder.requiresFormatting)
|
||||
? '${placeholder.name}String'
|
||||
: placeholder.name).join(', ');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,7 +292,34 @@ String generateString(String value) {
|
||||
// Reintroduce escaped backslashes into generated Dart string.
|
||||
.replaceAll(backslash, r'\\');
|
||||
|
||||
return "'$value'";
|
||||
return value;
|
||||
}
|
||||
|
||||
/// Given a list of strings, placeholders, or helper function calls, concatenate
|
||||
/// them into one expression to be returned.
|
||||
String generateReturnExpr(List<HelperMethod> helpers) {
|
||||
if (helpers.isEmpty) {
|
||||
return "''";
|
||||
} else if (
|
||||
helpers.length == 1
|
||||
&& helpers[0].string == null
|
||||
&& (helpers[0].placeholder?.type == 'String' || helpers[0].helper != null)
|
||||
) {
|
||||
return helpers[0].helperOrPlaceholder;
|
||||
} else {
|
||||
final String string = helpers.reversed.fold<String>('', (String string, HelperMethod helper) {
|
||||
if (helper.string != null) {
|
||||
return generateString(helper.string!) + string;
|
||||
}
|
||||
final RegExp alphanumeric = RegExp(r'^([0-9a-zA-Z]|_)+$');
|
||||
if (alphanumeric.hasMatch(helper.helperOrPlaceholder) && !(string.isNotEmpty && alphanumeric.hasMatch(string[0]))) {
|
||||
return '\$${helper.helperOrPlaceholder}$string';
|
||||
} else {
|
||||
return '\${${helper.helperOrPlaceholder}}$string';
|
||||
}
|
||||
});
|
||||
return "'$string'";
|
||||
}
|
||||
}
|
||||
|
||||
/// Typed configuration from the localizations config file.
|
||||
|
||||
543
packages/flutter_tools/lib/src/localizations/message_parser.dart
Normal file
543
packages/flutter_tools/lib/src/localizations/message_parser.dart
Normal file
@@ -0,0 +1,543 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// The whole design for the lexing and parsing step can be found in this design doc.
|
||||
// See https://flutter.dev/go/icu-message-parser.
|
||||
|
||||
// Symbol Types
|
||||
import 'gen_l10n_types.dart';
|
||||
|
||||
enum ST {
|
||||
// Terminal Types
|
||||
openBrace,
|
||||
closeBrace,
|
||||
comma,
|
||||
equalSign,
|
||||
other,
|
||||
plural,
|
||||
select,
|
||||
string,
|
||||
number,
|
||||
identifier,
|
||||
empty,
|
||||
// Nonterminal Types
|
||||
message,
|
||||
|
||||
placeholderExpr,
|
||||
|
||||
pluralExpr,
|
||||
pluralParts,
|
||||
pluralPart,
|
||||
|
||||
selectExpr,
|
||||
selectParts,
|
||||
selectPart,
|
||||
}
|
||||
|
||||
// The grammar of the syntax.
|
||||
Map<ST, List<List<ST>>> grammar = <ST, List<List<ST>>>{
|
||||
ST.message: <List<ST>>[
|
||||
<ST>[ST.string, ST.message],
|
||||
<ST>[ST.placeholderExpr, ST.message],
|
||||
<ST>[ST.pluralExpr, ST.message],
|
||||
<ST>[ST.selectExpr, ST.message],
|
||||
<ST>[ST.empty],
|
||||
],
|
||||
ST.placeholderExpr: <List<ST>>[
|
||||
<ST>[ST.openBrace, ST.identifier, ST.closeBrace],
|
||||
],
|
||||
ST.pluralExpr: <List<ST>>[
|
||||
<ST>[ST.openBrace, ST.identifier, ST.comma, ST.plural, ST.comma, ST.pluralParts, ST.closeBrace],
|
||||
],
|
||||
ST.pluralParts: <List<ST>>[
|
||||
<ST>[ST.pluralPart, ST.pluralParts],
|
||||
<ST>[ST.empty],
|
||||
],
|
||||
ST.pluralPart: <List<ST>>[
|
||||
<ST>[ST.identifier, ST.openBrace, ST.message, ST.closeBrace],
|
||||
<ST>[ST.equalSign, ST.number, ST.openBrace, ST.message, ST.closeBrace],
|
||||
<ST>[ST.other, ST.openBrace, ST.message, ST.closeBrace],
|
||||
],
|
||||
ST.selectExpr: <List<ST>>[
|
||||
<ST>[ST.openBrace, ST.identifier, ST.comma, ST.select, ST.comma, ST.selectParts, ST.closeBrace],
|
||||
<ST>[ST.other, ST.openBrace, ST.message, ST.closeBrace],
|
||||
],
|
||||
ST.selectParts: <List<ST>>[
|
||||
<ST>[ST.selectPart, ST.selectParts],
|
||||
<ST>[ST.empty],
|
||||
],
|
||||
ST.selectPart: <List<ST>>[
|
||||
<ST>[ST.identifier, ST.openBrace, ST.message, ST.closeBrace],
|
||||
<ST>[ST.other, ST.openBrace, ST.message, ST.closeBrace],
|
||||
],
|
||||
};
|
||||
|
||||
class Node {
|
||||
Node(this.type, this.positionInMessage, { this.expectedSymbolCount = 0, this.value, List<Node>? children }): children = children ?? <Node>[];
|
||||
|
||||
// Token constructors.
|
||||
Node.openBrace(this.positionInMessage): type = ST.openBrace, value = '{';
|
||||
Node.closeBrace(this.positionInMessage): type = ST.closeBrace, value = '}';
|
||||
Node.brace(this.positionInMessage, String this.value) {
|
||||
if (value == '{') {
|
||||
type = ST.openBrace;
|
||||
} else if (value == '}') {
|
||||
type = ST.closeBrace;
|
||||
} else {
|
||||
// We should never arrive here.
|
||||
throw L10nException('Provided value $value is not a brace.');
|
||||
}
|
||||
}
|
||||
Node.equalSign(this.positionInMessage): type = ST.equalSign, value = '=';
|
||||
Node.comma(this.positionInMessage): type = ST.comma, value = ',';
|
||||
Node.string(this.positionInMessage, String this.value): type = ST.string;
|
||||
Node.number(this.positionInMessage, String this.value): type = ST.number;
|
||||
Node.identifier(this.positionInMessage, String this.value): type = ST.identifier;
|
||||
Node.pluralKeyword(this.positionInMessage): type = ST.plural, value = 'plural';
|
||||
Node.selectKeyword(this.positionInMessage): type = ST.select, value = 'select';
|
||||
Node.otherKeyword(this.positionInMessage): type = ST.other, value = 'other';
|
||||
Node.empty(this.positionInMessage): type = ST.empty, value = '';
|
||||
|
||||
String? value;
|
||||
late ST type;
|
||||
List<Node> children = <Node>[];
|
||||
int positionInMessage;
|
||||
int expectedSymbolCount = 0;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return _toStringHelper(0);
|
||||
}
|
||||
|
||||
String _toStringHelper(int indentLevel) {
|
||||
final String indent = List<String>.filled(indentLevel, ' ').join();
|
||||
if (children.isEmpty) {
|
||||
return '''
|
||||
${indent}Node($type, $positionInMessage${value == null ? '' : ", value: '$value'"})''';
|
||||
}
|
||||
final String childrenString = children.map((Node child) => child._toStringHelper(indentLevel + 1)).join(',\n');
|
||||
return '''
|
||||
${indent}Node($type, $positionInMessage${value == null ? '' : ", value: '$value'"}, children: <Node>[
|
||||
$childrenString,
|
||||
$indent])''';
|
||||
}
|
||||
|
||||
// Only used for testing. We don't compare expectedSymbolCount because
|
||||
// it is an auxiliary member used during the parse function but doesn't
|
||||
// have meaning after calling compress.
|
||||
@override
|
||||
// ignore: avoid_equals_and_hash_code_on_mutable_classes, hash_and_equals
|
||||
bool operator==(covariant Node other) {
|
||||
if(value != other.value
|
||||
|| type != other.type
|
||||
|| positionInMessage != other.positionInMessage
|
||||
|| children.length != other.children.length
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
if (children[i] != other.children[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool get isFull {
|
||||
return children.length >= expectedSymbolCount;
|
||||
}
|
||||
}
|
||||
|
||||
RegExp unescapedString = RegExp(r'[^{}]+');
|
||||
RegExp brace = RegExp(r'{|}');
|
||||
|
||||
RegExp whitespace = RegExp(r'\s+');
|
||||
RegExp pluralKeyword = RegExp(r'plural');
|
||||
RegExp selectKeyword = RegExp(r'select');
|
||||
RegExp otherKeyword = RegExp(r'other');
|
||||
RegExp numeric = RegExp(r'[0-9]+');
|
||||
RegExp alphanumeric = RegExp(r'[a-zA-Z0-9]+');
|
||||
RegExp comma = RegExp(r',');
|
||||
RegExp equalSign = RegExp(r'=');
|
||||
|
||||
// List of token matchers ordered by precedence
|
||||
Map<ST, RegExp> matchers = <ST, RegExp>{
|
||||
ST.empty: whitespace,
|
||||
ST.plural: pluralKeyword,
|
||||
ST.select: selectKeyword,
|
||||
ST.other: otherKeyword,
|
||||
ST.number: numeric,
|
||||
ST.comma: comma,
|
||||
ST.equalSign: equalSign,
|
||||
ST.identifier: alphanumeric,
|
||||
};
|
||||
|
||||
class Parser {
|
||||
Parser(this.messageId, this.filename, this.messageString);
|
||||
|
||||
final String messageId;
|
||||
final String messageString;
|
||||
final String filename;
|
||||
|
||||
static String indentForError(int position) {
|
||||
return '${List<String>.filled(position, ' ').join()}^';
|
||||
}
|
||||
|
||||
// Lexes the message into a list of typed tokens. General idea is that
|
||||
// every instance of "{" and "}" toggles the isString boolean and every
|
||||
// instance of "'" toggles the isEscaped boolean (and treats a double
|
||||
// single quote "''" as a single quote "'"). When !isString and !isEscaped
|
||||
// delimit tokens by whitespace and special characters.
|
||||
List<Node> lexIntoTokens() {
|
||||
final List<Node> tokens = <Node>[];
|
||||
bool isString = true;
|
||||
// Index specifying where to match from
|
||||
int startIndex = 0;
|
||||
|
||||
// At every iteration, we should be able to match a new token until we
|
||||
// reach the end of the string. If for some reason we don't match a
|
||||
// token in any iteration of the loop, throw an error.
|
||||
while (startIndex < messageString.length) {
|
||||
Match? match;
|
||||
if (isString) {
|
||||
// TODO(thkim1011): Uncomment this when we add escaping as an option.
|
||||
// See https://github.com/flutter/flutter/issues/113455.
|
||||
// match = escapedString.matchAsPrefix(message, startIndex);
|
||||
// if (match != null) {
|
||||
// final String string = match.group(0)!;
|
||||
// tokens.add(Node.string(startIndex, string == "''" ? "'" : string.substring(1, string.length - 1)));
|
||||
// startIndex = match.end;
|
||||
// continue;
|
||||
// }
|
||||
match = unescapedString.matchAsPrefix(messageString, startIndex);
|
||||
if (match != null) {
|
||||
tokens.add(Node.string(startIndex, match.group(0)!));
|
||||
startIndex = match.end;
|
||||
continue;
|
||||
}
|
||||
match = brace.matchAsPrefix(messageString, startIndex);
|
||||
if (match != null) {
|
||||
tokens.add(Node.brace(startIndex, match.group(0)!));
|
||||
isString = false;
|
||||
startIndex = match.end;
|
||||
continue;
|
||||
}
|
||||
// Theoretically, we only reach this point because of unmatched single quotes because
|
||||
// 1. If it begins with single quotes, then we match the longest string contained in single quotes.
|
||||
// 2. If it begins with braces, then we match those braces.
|
||||
// 3. Else the first character is neither single quote or brace so it is matched by RegExp "unescapedString"
|
||||
throw L10nParserException(
|
||||
'ICU Lexing Error: Unmatched single quotes.',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
startIndex,
|
||||
);
|
||||
} else {
|
||||
RegExp matcher;
|
||||
ST? matchedType;
|
||||
|
||||
// Try to match tokens until we succeed
|
||||
for (matchedType in matchers.keys) {
|
||||
matcher = matchers[matchedType]!;
|
||||
match = matcher.matchAsPrefix(messageString, startIndex);
|
||||
if (match != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match == null) {
|
||||
match = brace.matchAsPrefix(messageString, startIndex);
|
||||
if (match != null) {
|
||||
tokens.add(Node.brace(startIndex, match.group(0)!));
|
||||
isString = true;
|
||||
startIndex = match.end;
|
||||
continue;
|
||||
}
|
||||
// This should only happen when there are special characters we are unable to match.
|
||||
throw L10nParserException(
|
||||
'ICU Lexing Error: Unexpected character.',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
startIndex
|
||||
);
|
||||
} else if (matchedType == ST.empty) {
|
||||
// Do not add whitespace as a token.
|
||||
startIndex = match.end;
|
||||
continue;
|
||||
} else {
|
||||
tokens.add(Node(matchedType!, startIndex, value: match.group(0)));
|
||||
startIndex = match.end;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Node parseIntoTree() {
|
||||
final List<Node> tokens = lexIntoTokens();
|
||||
final List<ST> parsingStack = <ST>[ST.message];
|
||||
final Node syntaxTree = Node(ST.empty, 0, expectedSymbolCount: 1);
|
||||
final List<Node> treeTraversalStack = <Node>[syntaxTree];
|
||||
|
||||
// Helper function for parsing and constructing tree.
|
||||
void parseAndConstructNode(ST nonterminal, int ruleIndex) {
|
||||
final Node parent = treeTraversalStack.last;
|
||||
final List<ST> grammarRule = grammar[nonterminal]![ruleIndex];
|
||||
|
||||
// When we run out of tokens, just use -1 to represent the last index.
|
||||
final int positionInMessage = tokens.isNotEmpty ? tokens.first.positionInMessage : -1;
|
||||
final Node node = Node(nonterminal, positionInMessage, expectedSymbolCount: grammarRule.length);
|
||||
parsingStack.addAll(grammarRule.reversed);
|
||||
|
||||
// For tree construction, add nodes to the parent until the parent has all
|
||||
// all the children it is expecting.
|
||||
parent.children.add(node);
|
||||
if (parent.isFull) {
|
||||
treeTraversalStack.removeLast();
|
||||
}
|
||||
treeTraversalStack.add(node);
|
||||
}
|
||||
|
||||
while (parsingStack.isNotEmpty) {
|
||||
final ST symbol = parsingStack.removeLast();
|
||||
|
||||
// Figure out which production rule to use.
|
||||
switch(symbol) {
|
||||
case ST.message:
|
||||
if (tokens.isEmpty) {
|
||||
parseAndConstructNode(ST.message, 4);
|
||||
} else if (tokens[0].type == ST.closeBrace) {
|
||||
parseAndConstructNode(ST.message, 4);
|
||||
} else if (tokens[0].type == ST.string) {
|
||||
parseAndConstructNode(ST.message, 0);
|
||||
} else if (tokens[0].type == ST.openBrace) {
|
||||
if (3 < tokens.length && tokens[3].type == ST.plural) {
|
||||
parseAndConstructNode(ST.message, 2);
|
||||
} else if (3 < tokens.length && tokens[3].type == ST.select) {
|
||||
parseAndConstructNode(ST.message, 3);
|
||||
} else {
|
||||
parseAndConstructNode(ST.message, 1);
|
||||
}
|
||||
} else {
|
||||
// Theoretically, we can never get here.
|
||||
throw L10nException('ICU Syntax Error.');
|
||||
}
|
||||
break;
|
||||
case ST.placeholderExpr:
|
||||
parseAndConstructNode(ST.placeholderExpr, 0);
|
||||
break;
|
||||
case ST.pluralExpr:
|
||||
parseAndConstructNode(ST.pluralExpr, 0);
|
||||
break;
|
||||
case ST.pluralParts:
|
||||
if (tokens.isNotEmpty && (
|
||||
tokens[0].type == ST.identifier ||
|
||||
tokens[0].type == ST.other ||
|
||||
tokens[0].type == ST.equalSign
|
||||
)
|
||||
) {
|
||||
parseAndConstructNode(ST.pluralParts, 0);
|
||||
} else {
|
||||
parseAndConstructNode(ST.pluralParts, 1);
|
||||
}
|
||||
break;
|
||||
case ST.pluralPart:
|
||||
if (tokens.isNotEmpty && tokens[0].type == ST.identifier) {
|
||||
parseAndConstructNode(ST.pluralPart, 0);
|
||||
} else if (tokens.isNotEmpty && tokens[0].type == ST.equalSign) {
|
||||
parseAndConstructNode(ST.pluralPart, 1);
|
||||
} else if (tokens.isNotEmpty && tokens[0].type == ST.other) {
|
||||
parseAndConstructNode(ST.pluralPart, 2);
|
||||
} else {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Plural parts must be of the form "identifier { message }" or "= number { message }"',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
tokens[0].positionInMessage,
|
||||
);
|
||||
}
|
||||
break;
|
||||
case ST.selectExpr:
|
||||
parseAndConstructNode(ST.selectExpr, 0);
|
||||
break;
|
||||
case ST.selectParts:
|
||||
if (tokens.isNotEmpty && (
|
||||
tokens[0].type == ST.identifier ||
|
||||
tokens[0].type == ST.other
|
||||
)) {
|
||||
parseAndConstructNode(ST.selectParts, 0);
|
||||
} else {
|
||||
parseAndConstructNode(ST.selectParts, 1);
|
||||
}
|
||||
break;
|
||||
case ST.selectPart:
|
||||
if (tokens.isNotEmpty && tokens[0].type == ST.identifier) {
|
||||
parseAndConstructNode(ST.selectPart, 0);
|
||||
} else if (tokens.isNotEmpty && tokens[0].type == ST.other) {
|
||||
parseAndConstructNode(ST.selectPart, 1);
|
||||
} else {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Select parts must be of the form "identifier { message }"',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
tokens[0].positionInMessage
|
||||
);
|
||||
}
|
||||
break;
|
||||
// At this point, we are only handling terminal symbols.
|
||||
// ignore: no_default_cases
|
||||
default:
|
||||
final Node parent = treeTraversalStack.last;
|
||||
// If we match a terminal symbol, then remove it from tokens and
|
||||
// add it to the tree.
|
||||
if (symbol == ST.empty) {
|
||||
parent.children.add(Node.empty(-1));
|
||||
} else if (tokens.isEmpty) {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Expected "${terminalTypeToString[symbol]}" but found no tokens.',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
messageString.length + 1,
|
||||
);
|
||||
} else if (symbol == tokens[0].type) {
|
||||
final Node token = tokens.removeAt(0);
|
||||
parent.children.add(token);
|
||||
} else {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Expected "${terminalTypeToString[symbol]}" but found "${tokens[0].value}".',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
tokens[0].positionInMessage,
|
||||
);
|
||||
}
|
||||
|
||||
if (parent.isFull) {
|
||||
treeTraversalStack.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return syntaxTree.children[0];
|
||||
}
|
||||
|
||||
final Map<ST, String> terminalTypeToString = <ST, String>{
|
||||
ST.openBrace: '{',
|
||||
ST.closeBrace: '}',
|
||||
ST.comma: ',',
|
||||
ST.empty: '',
|
||||
ST.identifier: 'identifier',
|
||||
ST.number: 'number',
|
||||
ST.plural: 'plural',
|
||||
ST.select: 'select',
|
||||
ST.equalSign: '=',
|
||||
ST.other: 'other',
|
||||
};
|
||||
|
||||
// Compress the syntax tree. Note that after
|
||||
// parse(lex(message)), the individual parts (ST.string, ST.placeholderExpr,
|
||||
// ST.pluralExpr, and ST.selectExpr) are structured as a linked list See diagram
|
||||
// below. This
|
||||
// function compresses these parts into a single children array (and does this
|
||||
// for ST.pluralParts and ST.selectParts as well). Then it checks extra syntax
|
||||
// rules. Essentially, it converts
|
||||
//
|
||||
// Message
|
||||
// / \
|
||||
// PluralExpr Message
|
||||
// / \
|
||||
// String Message
|
||||
// / \
|
||||
// SelectExpr ...
|
||||
//
|
||||
// to
|
||||
//
|
||||
// Message
|
||||
// / | \
|
||||
// PluralExpr String SelectExpr ...
|
||||
//
|
||||
// Keep in mind that this modifies the tree in place and the values of
|
||||
// expectedSymbolCount and isFull is no longer useful after this operation.
|
||||
Node compress(Node syntaxTree) {
|
||||
Node node = syntaxTree;
|
||||
final List<Node> children = <Node>[];
|
||||
switch (syntaxTree.type) {
|
||||
case ST.message:
|
||||
case ST.pluralParts:
|
||||
case ST.selectParts:
|
||||
while (node.children.length == 2) {
|
||||
children.add(node.children[0]);
|
||||
compress(node.children[0]);
|
||||
node = node.children[1];
|
||||
}
|
||||
syntaxTree.children = children;
|
||||
break;
|
||||
// ignore: no_default_cases
|
||||
default:
|
||||
node.children.forEach(compress);
|
||||
}
|
||||
return syntaxTree;
|
||||
}
|
||||
|
||||
// Takes in a compressed syntax tree and checks extra rules on
|
||||
// plural parts and select parts.
|
||||
void checkExtraRules(Node syntaxTree) {
|
||||
final List<Node> children = syntaxTree.children;
|
||||
switch(syntaxTree.type) {
|
||||
case ST.pluralParts:
|
||||
// Must have an "other" case.
|
||||
if (children.every((Node node) => node.children[0].type != ST.other)) {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Plural expressions must have an "other" case.',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
syntaxTree.positionInMessage
|
||||
);
|
||||
}
|
||||
// Identifier must be one of "zero", "one", "two", "few", "many".
|
||||
for (final Node node in children) {
|
||||
final Node pluralPartFirstToken = node.children[0];
|
||||
const List<String> validIdentifiers = <String>['zero', 'one', 'two', 'few', 'many'];
|
||||
if (pluralPartFirstToken.type == ST.identifier && !validIdentifiers.contains(pluralPartFirstToken.value)) {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Plural expressions case must be one of "zero", "one", "two", "few", "many", or "other".',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
node.positionInMessage,
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST.selectParts:
|
||||
if (children.every((Node node) => node.children[0].type != ST.other)) {
|
||||
throw L10nParserException(
|
||||
'ICU Syntax Error: Select expressions must have an "other" case.',
|
||||
filename,
|
||||
messageId,
|
||||
messageString,
|
||||
syntaxTree.positionInMessage,
|
||||
);
|
||||
}
|
||||
break;
|
||||
// ignore: no_default_cases
|
||||
default:
|
||||
break;
|
||||
}
|
||||
children.forEach(checkExtraRules);
|
||||
}
|
||||
|
||||
Node parse() {
|
||||
final Node syntaxTree = compress(parseIntoTree());
|
||||
checkExtraRules(syntaxTree);
|
||||
return syntaxTree;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user