Relax syntax for gen-l10n (#130736)

To preserve backward compatibility with the old parser which would
ignore syntax errors, this PR introduces a way to treat the special
characters `{` and `}` in the following way:
1. If we encounter a `{` which searching for a string token and this `{`
is not followed by a valid placeholder, then we treat the `{` as a
string and continue lexing for strings.
2. If we encounter a `}` while not within some expression (i.e.
placeholders, arguments, plurals, or selects), then we treat the `}` as
a string and continue lexing for strings.

This makes it so that
```
"helloWorld": "{ } { placeholder }",
"@@helloWorld": {
  "placeholders": {
    "placeholder" {}
  }
}
```
treats the `{ }` as a string while `{ placeholder } ` is treated as a
placeholder.

Fixes https://github.com/flutter/flutter/issues/122404.
This commit is contained in:
Tae Hyung Kim
2023-07-18 13:59:48 -07:00
committed by GitHub
parent bb798e2a84
commit c6b93b2db7
7 changed files with 143 additions and 6 deletions

View File

@@ -95,6 +95,7 @@ void main() {
bool useEscaping = false,
bool areResourceAttributeRequired = false,
bool suppressWarnings = false,
bool relaxSyntax = false,
void Function(Directory)? setup,
}
) {
@@ -126,6 +127,7 @@ void main() {
useEscaping: useEscaping,
areResourceAttributesRequired: areResourceAttributeRequired,
suppressWarnings: suppressWarnings,
useRelaxedSyntax: relaxSyntax,
)
..loadResources()
..writeOutputFiles(isFromYaml: isFromYaml);
@@ -1475,6 +1477,22 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
expect(getGeneratedFileContent(locale: 'en'), contains('String helloWorld(Object name) {'));
expect(getGeneratedFileContent(locale: 'es'), contains('String helloWorld(Object name) {'));
});
testWithoutContext('braces are ignored as special characters if placeholder does not exist', () {
setupLocalizations(<String, String>{
'en': '''
{
"helloWorld": "Hello {name}",
"@@helloWorld": {
"placeholders": {
"names": {}
}
}
}'''
}, relaxSyntax: true);
final String content = getGeneratedFileContent(locale: 'en');
expect(content, contains("String get helloWorld => 'Hello {name}'"));
});
});
group('DateTime tests', () {

View File

@@ -293,6 +293,54 @@ void main() {
)));
});
testWithoutContext('relaxed lexer', () {
final List<Node> tokens1 = Parser(
'string',
'app_en.arb',
'{ }',
placeholders: <String>[],
).lexIntoTokens();
expect(tokens1, equals(<Node>[
Node(ST.string, 0, value: '{'),
Node(ST.string, 1, value: ' '),
Node(ST.string, 2, value: '}')
]));
final List<Node> tokens2 = Parser(
'string',
'app_en.arb',
'{ notAPlaceholder }',
placeholders: <String>['isAPlaceholder'],
).lexIntoTokens();
expect(tokens2, equals(<Node>[
Node(ST.string, 0, value: '{'),
Node(ST.string, 1, value: ' notAPlaceholder '),
Node(ST.string, 18, value: '}')
]));
final List<Node> tokens3 = Parser(
'string',
'app_en.arb',
'{ isAPlaceholder }',
placeholders: <String>['isAPlaceholder'],
).lexIntoTokens();
expect(tokens3, equals(<Node>[
Node(ST.openBrace, 0, value: '{'),
Node(ST.identifier, 2, value: 'isAPlaceholder'),
Node(ST.closeBrace, 17, value: '}')
]));
});
testWithoutContext('relaxed lexer complex', () {
const String message = '{ notPlaceholder } {count,plural, =0{Hello} =1{Hello World} =2{Hello two worlds} few{Hello {count} worlds} many{Hello all {count} worlds} other{Hello other {count} worlds}}';
final List<Node> tokens = Parser(
'string',
'app_en.arb',
message,
placeholders: <String>['count'],
).lexIntoTokens();
expect(tokens[0].type, equals(ST.string));
});
testWithoutContext('parser basic', () {
expect(Parser('helloWorld', 'app_en.arb', 'Hello {name}').parse(), equals(