Skip to content

Conversation

thkim1011
Copy link
Contributor

@thkim1011 thkim1011 commented Sep 26, 2022

Description

This PR implements the new parser for ICU message format. The current implementation involves doing a lot of hacky RegEx string replacements (i.e. replace placeholders to use a different syntax, RegEx match plurals and selects) and making assumptions about the messages (e.g. the original implementation assumes that messages may have at most one plural). This has a lot of limitations which include

  1. Not able to recursively use plural and select syntax.
  2. Incorrect behavior when placeholders, plurals, selects are mixed.
  3. Cannot return a syntax error if the RegEx is not matched.
  4. Hard to verify correctness of code.

We provide a new approach. The high level idea is to divide up the whole process into three steps:

  1. Lexing the message into tokens. This is done via using RegEx to determine what the next token is. See message_parser.dart:Parser.lexIntoTokens.
  2. Parsing the tokens into a syntax tree according to a grammar. This is done using the LL parsing algorithm. See message_parser.dart:Parser.parseIntoTree.
  3. Code generation via tree traversal through the syntax tree. This is done using code templates in gen_l10n_templates.dart and implemented in gen_l10n.dart:LocalizationsGenerator._generateMethod.

Another feature of separating the parsing and code generation process is better error messages. For example, if there is an unexpected token, then we should see the error message

ICU Syntax Error: Expected "identifier" but found ",".
{ , plural , = }
  ^

Design Doc

Link to design doc.

Testing

  1. Created a new message_parser_test.dart file which includes tests for up to parsing.
  2. Ensured that all existing code generation tests in generate_localizations_test.dart succeed.
  3. Added a nested plural/select test case to the E2E test in gen_l10n_test.dart.

Follow up work

There's a little more work left, which is a bit outside the scope of this PR. Essentially, the placeholder type inference for placeholders for plurals and selects were done by assuming that there is a unique count placeholder per message. Since we no longer make this assumption, we need to refactor the Message class to include all messages, parse all of these messages at the same time, then attempt to infer the correct type for each placeholder. This PR doesn't modify the old placeholder type inference logic, so it may not work properly if there are multiple plurals.

We're also setting aside escaping for a separate PR but that's lower priority than above.

Related Issue

Fixes #86906. Fixes #113495.

Future Work

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Sep 26, 2022
@thkim1011 thkim1011 changed the title [WIP] ICU Message Syntax Parser ICU Message Syntax Parser Sep 29, 2022
@thkim1011 thkim1011 marked this pull request as ready for review September 29, 2022 19:14
@thkim1011 thkim1011 mentioned this pull request Oct 14, 2022
@thkim1011 thkim1011 added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 1, 2022
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 1, 2022
@auto-submit
Copy link
Contributor

auto-submit bot commented Nov 1, 2022

auto label is removed for flutter/flutter, pr: 112390, due to - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

@thkim1011 thkim1011 added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 2, 2022
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 2, 2022
@auto-submit
Copy link
Contributor

auto-submit bot commented Nov 2, 2022

auto label is removed for flutter/flutter, pr: 112390, due to - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label.

@thkim1011 thkim1011 added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 3, 2022
@thkim1011 thkim1011 merged commit cef4c2a into flutter:master Nov 5, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 6, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Nov 6, 2022
auto-submit bot pushed a commit to flutter/plugins that referenced this pull request Nov 6, 2022
* cef4c2a ICU Message Syntax Parser (flutter/flutter#112390)

* 07d3a64 0886c7d0b Narrow the scope of the Windows Android arm hack (flutter/engine#37125) (flutter/flutter#114751)

* cf0cd06 Mark new_gallery__transition_perf as non-flaky (flutter/flutter#114694)

* 5bc50c1 Fix macOS migration nothing-to-upgrade test (flutter/flutter#114703)

* b1509ed 36cfa9e68 Apply the Windows friendly path solution in remaining spots (flutter/engine#37344) (flutter/flutter#114759)

* b45e628 b8218eacc Roll Fuchsia Mac SDK from Ua8Jtf8Zka9uxIVdl... to sNXsQVxntMX8f42LE... (flutter/engine#37352) (flutter/flutter#114760)

* 5ea5a53 6f3018a72 Roll Skia from a34882309d04 to c3c31be8729b (1 revision) (flutter/engine#37358) (flutter/flutter#114765)
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Nov 7, 2022
* cef4c2a ICU Message Syntax Parser (flutter/flutter#112390)

* 07d3a64 0886c7d0b Narrow the scope of the Windows Android arm hack (flutter/engine#37125) (flutter/flutter#114751)

* cf0cd06 Mark new_gallery__transition_perf as non-flaky (flutter/flutter#114694)

* 5bc50c1 Fix macOS migration nothing-to-upgrade test (flutter/flutter#114703)

* b1509ed 36cfa9e68 Apply the Windows friendly path solution in remaining spots (flutter/engine#37344) (flutter/flutter#114759)

* b45e628 b8218eacc Roll Fuchsia Mac SDK from Ua8Jtf8Zka9uxIVdl... to sNXsQVxntMX8f42LE... (flutter/engine#37352) (flutter/flutter#114760)

* 5ea5a53 6f3018a72 Roll Skia from a34882309d04 to c3c31be8729b (1 revision) (flutter/engine#37358) (flutter/flutter#114765)
dependentPlaceholders.addAll(pluralPartHelper.dependentPlaceholders);
} else {
logger.printWarning('''
The plural part specified below is overrided by a later plural part.
Copy link

@florian-obernberger florian-obernberger Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "overrided" is a typo? Shouldn't it be "overridden"?

IVLIVS-III pushed a commit to IVLIVS-III/flutter_plugins_fork that referenced this pull request Nov 11, 2022
* cef4c2a ICU Message Syntax Parser (flutter/flutter#112390)

* 07d3a64 0886c7d0b Narrow the scope of the Windows Android arm hack (flutter/engine#37125) (flutter/flutter#114751)

* cf0cd06 Mark new_gallery__transition_perf as non-flaky (flutter/flutter#114694)

* 5bc50c1 Fix macOS migration nothing-to-upgrade test (flutter/flutter#114703)

* b1509ed 36cfa9e68 Apply the Windows friendly path solution in remaining spots (flutter/engine#37344) (flutter/flutter#114759)

* b45e628 b8218eacc Roll Fuchsia Mac SDK from Ua8Jtf8Zka9uxIVdl... to sNXsQVxntMX8f42LE... (flutter/engine#37352) (flutter/flutter#114760)

* 5ea5a53 6f3018a72 Roll Skia from a34882309d04 to c3c31be8729b (1 revision) (flutter/engine#37358) (flutter/flutter#114765)
adam-harwood pushed a commit to adam-harwood/flutter_plugins that referenced this pull request Nov 21, 2022
* cef4c2a ICU Message Syntax Parser (flutter/flutter#112390)

* 07d3a64 0886c7d0b Narrow the scope of the Windows Android arm hack (flutter/engine#37125) (flutter/flutter#114751)

* cf0cd06 Mark new_gallery__transition_perf as non-flaky (flutter/flutter#114694)

* 5bc50c1 Fix macOS migration nothing-to-upgrade test (flutter/flutter#114703)

* b1509ed 36cfa9e68 Apply the Windows friendly path solution in remaining spots (flutter/engine#37344) (flutter/flutter#114759)

* b45e628 b8218eacc Roll Fuchsia Mac SDK from Ua8Jtf8Zka9uxIVdl... to sNXsQVxntMX8f42LE... (flutter/engine#37352) (flutter/flutter#114760)

* 5ea5a53 6f3018a72 Roll Skia from a34882309d04 to c3c31be8729b (1 revision) (flutter/engine#37358) (flutter/flutter#114765)
@edeuss
Copy link

edeuss commented Nov 21, 2022

Any idea when this will be brought to stable or beta? We are currently running into some issues that this pull fixes for us.

@christopherfujino
Copy link
Contributor

Any idea when this will be brought to stable or beta? We are currently running into some issues that this pull fixes for us.

I believe it's already in beta: https://github.com/flutter/flutter/tree/beta/packages/flutter_tools/lib/src/localizations

jonahwilliams pushed a commit to jonahwilliams/flutter that referenced this pull request Nov 28, 2022
shogohida pushed a commit to shogohida/flutter that referenced this pull request Dec 7, 2022
* 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
gspencergoog pushed a commit to gspencergoog/flutter that referenced this pull request Jan 19, 2023
* 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
mauricioluz pushed a commit to mauricioluz/plugins that referenced this pull request Jan 26, 2023
* cef4c2a ICU Message Syntax Parser (flutter/flutter#112390)

* 07d3a64 0886c7d0b Narrow the scope of the Windows Android arm hack (flutter/engine#37125) (flutter/flutter#114751)

* cf0cd06 Mark new_gallery__transition_perf as non-flaky (flutter/flutter#114694)

* 5bc50c1 Fix macOS migration nothing-to-upgrade test (flutter/flutter#114703)

* b1509ed 36cfa9e68 Apply the Windows friendly path solution in remaining spots (flutter/engine#37344) (flutter/flutter#114759)

* b45e628 b8218eacc Roll Fuchsia Mac SDK from Ua8Jtf8Zka9uxIVdl... to sNXsQVxntMX8f42LE... (flutter/engine#37352) (flutter/flutter#114760)

* 5ea5a53 6f3018a72 Roll Skia from a34882309d04 to c3c31be8729b (1 revision) (flutter/engine#37358) (flutter/flutter#114765)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flutter Localization - Can't use datetime placeholder in select placeholder Add support for multiple/nested plurals and selects in gen_l10n tool
5 participants