Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs when parsing regex #458

Merged
merged 4 commits into from
Oct 6, 2021
Merged

Fix bugs when parsing regex #458

merged 4 commits into from
Oct 6, 2021

Conversation

TwitchBronBron
Copy link
Member

Resolves #455

@TwitchBronBron TwitchBronBron marked this pull request as ready for review October 5, 2021 13:59
/**
* The tokens that might preceed a regex literal
*/
export const PreceedingRegexTypes = new Set([
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the full list of tokens that can come before a regex literal. If it's not in this list, then a regex literal will not be tokenized even if we encounter a forward slash. I find it safer to create an allowlist rather than a denylist, as it's easier to test, and we won't accidentally break functionality when we forgot to put something in the denylist, whereas only new adopters of regexp literals would encounter issues when something is missing from the allowlist.

//consume all flag-like chars (let the parser validate the actual values)
while (/[a-z]/i.exec(this.peek())) {
//preceeded by an allowed token, or if there are no previous tokens (i.e. this is the first token in the file).
if (PreceedingRegexTypes.has(previousKind) || !previousKind) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the core of the change. Check for only an approved token preceeding the opening slash.

@TwitchBronBron TwitchBronBron marked this pull request as draft October 5, 2021 15:56
@TwitchBronBron TwitchBronBron marked this pull request as ready for review October 5, 2021 17:14
@markwpearce markwpearce self-requested a review October 5, 2021 19:18
@markwpearce
Copy link
Collaborator

I believe the RegexLiteral check is being done in Brightscript (.BRS) mode? If it is, I don't think it should be

@TwitchBronBron
Copy link
Member Author

Oops! You're right. Good catch.

@TwitchBronBron
Copy link
Member Author

@markwpearce I addressed your concern in 57cca5d

@TwitchBronBron TwitchBronBron merged commit 1b05843 into master Oct 6, 2021
@TwitchBronBron TwitchBronBron deleted the bugfix/regex-literal branch October 6, 2021 19:51
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.

Regex literal syntax bug
2 participants