Skip to content

ICU-23509 Update the UnicodeSet parser to match the grammar that has the right precedence (no functional change) - #4174

Merged
eggrobin merged 1 commit into
unicode-org:mainfrom
eggrobin:precedence
Sep 11, 2026
Merged

eggrobin merged 1 commit into
unicode-org:mainfrom
eggrobin:precedence

Conversation

@eggrobin

@eggrobin eggrobin commented Sep 9, 2026

Copy link
Copy Markdown
Member

The existing parser did not actually do what UTS61 said, so going back to a grammar that has the desired precedence (equal between union, intersection, and difference, instead of difference and intersection having higher precedence) has no effect.

How did the parser fail to implement the bad grammar?

See

void UnicodeSet::parseRestriction(Lexer &lexer,
UnicodeString &rebuiltPat,
uint32_t options,
UnicodeSet &(UnicodeSet::*caseClosure)(int32_t attribute),
int32_t depth,
UErrorCode &ec) {
// Parse a https://www.unicode.org/reports/tr61/#Restriction:
// Restriction ::= UnicodeSet
// | Intersection
// | Difference
// Intersection ::= Restriction & UnicodeSet
// Difference ::= Restriction - UnicodeSet
Instead of
UnicodeSet leftHandSide;
leftHandSide.parseUnicodeSet(lexer, rebuiltPat, options, caseClosure, depth + 1, ec);
addAll(leftHandSide);
and then, in a loop,
UnicodeSet rightHandSide;
rightHandSide.parseUnicodeSet(lexer, rebuiltPat, options, caseClosure, depth + 1, ec);
U_UNICODESET_RETURN_IF_ERROR(ec);
retainAll(rightHandSide);
or
UnicodeSet rightHandSide;
rightHandSide.parseUnicodeSet(lexer, rebuiltPat, options, caseClosure, depth + 1, ec);
U_UNICODESET_RETURN_IF_ERROR(ec);
removeAll(rightHandSide);
a parser actually implementing what the grammar said would done leftHandSide.retainAll/removeAll, and leftHandSide.addAll only at the end of the Restriction.

Checklist

  • Required: Issue filed: ICU-23509
  • Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable
  • Approver: Feel free to merge on my behalf

@eggrobin

eggrobin commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

@markusicu I have no idea what ticket to use here (ICU-23297 would fit nicely but went into 78, all the others listed in #4030 seem irrelevant to the basic grammar).

@eggrobin

eggrobin commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

I guess the ticket question will also arise for the UnicodeSet documentation PR, and this fits the same theme (this is just internal documentation, I am changing comments and renaming internal identifiers).

Comment thread icu4c/source/common/uniset_props.cpp Outdated
Comment thread icu4c/source/common/uniset_props.cpp
Comment thread icu4c/source/common/uniset_props.cpp Outdated
@markusicu markusicu self-assigned this Sep 9, 2026
Comment thread icu4c/source/common/uniset_props.cpp Outdated
Comment thread icu4j/main/core/src/main/java/com/ibm/icu/text/UnicodeSet.java Outdated
Comment thread icu4c/source/common/uniset_props.cpp Outdated
@markusicu

Copy link
Copy Markdown
Member

once you have a ticket, i could excavate my rubber stamp

@eggrobin eggrobin changed the title Update the UnicodeSet parser to match the grammar that has the right precedence (no functional change) ICU-23509 Update the UnicodeSet parser to match the grammar that has the right precedence (no functional change) Sep 10, 2026
@eggrobin

Copy link
Copy Markdown
Member Author

I filed ICU-23509.

Comment thread icu4c/source/common/uniset_props.cpp Outdated
@eggrobin
eggrobin marked this pull request as ready for review September 10, 2026 17:09
@eggrobin

Copy link
Copy Markdown
Member Author

So... we follow the alternate grammar which is easier to parse

I think in order to make this easier to follow, I should make the parseMeow functions where Meow actually is represents an expression static, returning a set. (If I had done that, my parser from last year would have correctly parsed the grammar that gave - and & higher precedence, this would probably have broken everything, and I would have noticed the bug in the grammar a year earlier.)

Then the weird functions that parse a non-expression can remain mutating member functions, and the distinction will be much more obvious. This might also make the code more concise, as a lot of the usages of expression-parsing functions are UnicodeSet blah; blah.parseMeow().

But I will do that in a separate PR, so that this one only changes comments and renames stuff but preserves the logic.

@eggrobin
eggrobin requested a review from markusicu September 10, 2026 17:14
@eggrobin

eggrobin commented Sep 11, 2026

Copy link
Copy Markdown
Member Author

(This will get a couple more changes, Roozbeh spotted a mistake in the grammar. The comments will actually get simpler, for a change.)

@eggrobin

Copy link
Copy Markdown
Member Author

Should match the grammar in https://github.com/unicode-org/unicode-reports/pull/330 now.

@markusicu

Copy link
Copy Markdown
Member

... I should make the parseMeow functions where Meow actually is represents an expression static, returning a set. ...

But I will do that in a separate PR, so that this one only changes comments and renames stuff but preserves the logic.

sg -- please create a ticket

markusicu
markusicu previously approved these changes Sep 11, 2026

@markusicu markusicu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

rubber stamp

// those to this set, and we end up with the UnicodeSet of the Union following by any right
// hand sides. In the LL grammar from the note, this is:
// those to this object, and we end up with the UnicodeSet of the Union following by any
// right hand sides. In the LL grammar from the note, this is:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

optional: please avoid dot space space...

@markusicu

Copy link
Copy Markdown
Member

oh, spotless wants to wrap another line -- whose overflow then wants to be merged into the following stuff

@jira-pull-request-webhook

Copy link
Copy Markdown

Hooray! The files in the branch are the same across the force-push. 😃

~ Your Friendly Jira-GitHub PR Checker Bot

@eggrobin

Copy link
Copy Markdown
Member Author

sg -- please create a ticket

I might reuse ICU-23257.

@markusicu

markusicu commented Sep 11, 2026

Copy link
Copy Markdown
Member

sg -- please create a ticket

I might reuse ICU-23257.

sgtm

@markusicu

Copy link
Copy Markdown
Member

all checks have passed, ready to merge (approver not encouraged to do so)

@eggrobin
eggrobin merged commit a172968 into unicode-org:main Sep 11, 2026
107 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants