-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
[Merged by Bors] - Rework RegExp struct to include bitflags field #1837
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4601f0
Rework RegExp struct to include bitflags field
aaronmunsters 1d81ed3
Merge branch 'boa-dev:main' into rework_regexp_struct
aaronmunsters 2500e8d
Move bitflags struct to lexer
aaronmunsters b04df5f
Moved characters into bytes
aaronmunsters 1b64b2f
Implement FromStr trait for RegExpFlags
aaronmunsters 325cb85
Update code to align with format
aaronmunsters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ use bitflags::bitflags; | |
use boa_interner::{Interner, Sym}; | ||
use std::{ | ||
io::{self, ErrorKind, Read}, | ||
str, | ||
str::{self, FromStr}, | ||
}; | ||
|
||
/// Regex literal lexing. | ||
|
@@ -133,7 +133,7 @@ impl<R> Tokenizer<R> for RegexLiteral { | |
bitflags! { | ||
/// Flags of a regular expression. | ||
#[derive(Default)] | ||
struct RegExpFlags: u8 { | ||
pub struct RegExpFlags: u8 { | ||
const GLOBAL = 0b0000_0001; | ||
const IGNORE_CASE = 0b0000_0010; | ||
const MULTILINE = 0b0000_0100; | ||
|
@@ -143,33 +143,40 @@ bitflags! { | |
} | ||
} | ||
|
||
fn parse_regex_flags(s: &str, start: Position, interner: &mut Interner) -> Result<Sym, Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this function together with the structure, and change it so that this implements the |
||
let mut flags = RegExpFlags::default(); | ||
for c in s.bytes() { | ||
let new_flag = match c { | ||
b'g' => RegExpFlags::GLOBAL, | ||
b'i' => RegExpFlags::IGNORE_CASE, | ||
b'm' => RegExpFlags::MULTILINE, | ||
b's' => RegExpFlags::DOT_ALL, | ||
b'u' => RegExpFlags::UNICODE, | ||
b'y' => RegExpFlags::STICKY, | ||
_ => { | ||
return Err(Error::syntax( | ||
format!("invalid regular expression flag {}", char::from(c)), | ||
start, | ||
)) | ||
} | ||
}; | ||
impl FromStr for RegExpFlags { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut flags = Self::default(); | ||
for c in s.bytes() { | ||
let new_flag = match c { | ||
b'g' => Self::GLOBAL, | ||
b'i' => Self::IGNORE_CASE, | ||
b'm' => Self::MULTILINE, | ||
b's' => Self::DOT_ALL, | ||
b'u' => Self::UNICODE, | ||
b'y' => Self::STICKY, | ||
_ => return Err(format!("invalid regular expression flag {}", char::from(c))), | ||
}; | ||
|
||
if flags.contains(new_flag) { | ||
return Err(Error::syntax( | ||
format!("repeated regular expression flag {}", char::from(c)), | ||
start, | ||
)); | ||
if flags.contains(new_flag) { | ||
return Err(format!( | ||
"repeated regular expression flag {}", | ||
char::from(c) | ||
)); | ||
} | ||
flags.insert(new_flag); | ||
} | ||
flags.insert(new_flag); | ||
|
||
Ok(flags) | ||
} | ||
} | ||
|
||
fn parse_regex_flags(s: &str, start: Position, interner: &mut Interner) -> Result<Sym, Error> { | ||
match RegExpFlags::from_str(s) { | ||
Err(message) => Err(Error::Syntax(message.into(), start)), | ||
Ok(flags) => Ok(interner.get_or_intern(flags.to_string())), | ||
} | ||
Ok(interner.get_or_intern(flags.to_string())) | ||
} | ||
|
||
impl ToString for RegExpFlags { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would implement
FromStr
forRegexFlags
, and use the version ofparse_regex_flags()
in the lexer, which iterates through bytes instead of characters, and should therefore be more efficient. Would also reduce duplicate code.