Allow select cases to be numbers (#116625)

This commit is contained in:
Tae Hyung Kim 2023-01-10 01:18:03 -08:00 committed by GitHub
parent ad7322ddbf
commit 583a8122bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -70,6 +70,7 @@ Map<ST, List<List<ST>>> grammar = <ST, List<List<ST>>>{
],
ST.selectPart: <List<ST>>[
<ST>[ST.identifier, ST.openBrace, ST.message, ST.closeBrace],
<ST>[ST.number, ST.openBrace, ST.message, ST.closeBrace],
<ST>[ST.other, ST.openBrace, ST.message, ST.closeBrace],
],
};
@ -408,6 +409,7 @@ class Parser {
case ST.selectParts:
if (tokens.isNotEmpty && (
tokens[0].type == ST.identifier ||
tokens[0].type == ST.number ||
tokens[0].type == ST.other
)) {
parseAndConstructNode(ST.selectParts, 0);
@ -418,8 +420,10 @@ class Parser {
case ST.selectPart:
if (tokens.isNotEmpty && tokens[0].type == ST.identifier) {
parseAndConstructNode(ST.selectPart, 0);
} else if (tokens.isNotEmpty && tokens[0].type == ST.other) {
} else if (tokens.isNotEmpty && tokens[0].type == ST.number) {
parseAndConstructNode(ST.selectPart, 1);
} else if (tokens.isNotEmpty && tokens[0].type == ST.other) {
parseAndConstructNode(ST.selectPart, 2);
} else {
throw L10nParserException(
'ICU Syntax Error: Select parts must be of the form "identifier { message }"',
@ -581,6 +585,10 @@ class Parser {
checkExtraRules(syntaxTree);
return syntaxTree;
} on L10nParserException catch (error) {
// For debugging purposes.
if (logger == null) {
rethrow;
}
logger?.printError(error.toString());
return Node(ST.empty, 0, value: '');
}

View File

@ -503,4 +503,15 @@ void main() {
contains(expectedError3),
)));
});
testWithoutContext('parser allows select cases with numbers', () {
final Node node = Parser('numberSelect', 'app_en.arb', '{ count, select, 0{none} 100{perfect} other{required!} }').parse();
final Node selectExpr = node.children[0];
final Node selectParts = selectExpr.children[5];
final Node selectPart = selectParts.children[0];
expect(selectPart.children[0].value, equals('0'));
expect(selectPart.children[1].value, equals('{'));
expect(selectPart.children[2].type, equals(ST.message));
expect(selectPart.children[3].value, equals('}'));
});
}