Skip to content

C++: Small locations fix for regex PR - #33

Open
geoffw0 wants to merge 2 commits into
jketema:jketema/ecma-regexfrom
geoffw0:ecma-regex
Open

C++: Small locations fix for regex PR#33
geoffw0 wants to merge 2 commits into
jketema:jketema/ecma-regexfrom
geoffw0:ecma-regex

Conversation

@geoffw0

@geoffw0 geoffw0 commented Aug 27, 2026

Copy link
Copy Markdown

PR onto github#22300 .

I was a bit skeptical that the second case in regexpContentOffset() can safely assume a StringLiteral.getValueText necessarily always contains ". A quick real world investigation confirmed this isn't always true - in particular, string literals produced by macro stringification don't necessarily contain a " in their value text.

This PR adds a few test cases and addresses the gap in locations. Note that the new locations are not character accurate - but they do at least have a location on the correct line of code, sufficient for a user to see what is being referred to (doing better might be tricky and has debatable priority).


I think we should also consider moving most of the logic from regexpContentOffset() into a method StringLiteral.getContentOffset(), overridden in RawStringLiteral for the additional logic of that situation. I haven't done this here.

Comment on lines +225 to +229
or
// No opening '"'.
not re instanceof RawStringLiteral and
not exists(int i | vt.charAt(i) = "\"") and
result = 0

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Don't we have the same problem for raw string literals, but then for (?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If it's possible to stringify into raw string literals, yes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

... in fact right now no, due to the way you define RawStringLiteral it must contain a (. But it's fragile to depend on that.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It would be less fragile if we defined getContentOffset() in StringLiteral and RawStringLiteral. That way the definition that depends on ( being present is close to the constructor that ensures it, so it seems less fragile as a future developer working on RawStringLiteral ought to be aware of both.

How do you feel about the direction of providing the functionality of regexpContentOffset in StringLiteral?

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.

2 participants