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

refactor(biome_parser): use enumflags2 for TokenFlag and remove bitflags #3802

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ tests_macros = { path = "./crates/tests_macros" }

# Crates needed in the workspace
anyhow = "1.0.86"
bitflags = "2.6.0"
bpaf = { version = "0.9.12", features = ["derive"] }
countme = "3.0.1"
crossbeam = "0.8.4"
Expand Down
1 change: 0 additions & 1 deletion crates/biome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ biome_rowan = { workspace = true }
biome_string_case = { workspace = true }
biome_suppression = { workspace = true }
biome_unicode_table = { workspace = true }
bitflags = { workspace = true }
bitvec = "1.0.1"
enumflags2 = { workspace = true }
natord = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion crates/biome_js_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ biome_js_syntax = { workspace = true }
biome_parser = { workspace = true }
biome_rowan = { workspace = true }
biome_unicode_table = { workspace = true }
bitflags = { workspace = true }
drop_bomb = "0.1.5"
enumflags2 = { workspace = true }
indexmap = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ biome_console = { workspace = true }
biome_diagnostics = { workspace = true }
biome_rowan = { workspace = true }
biome_unicode_table = { workspace = true }
bitflags = { workspace = true }
drop_bomb = "0.1.5"
enumflags2 = { workspace = true }
unicode-bom = { workspace = true }

[lints]
Expand Down
59 changes: 46 additions & 13 deletions crates/biome_parser/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::diagnostic::ParseDiagnostic;
use biome_rowan::{SyntaxKind, TextRange, TextSize};
use biome_unicode_table::{lookup_byte, Dispatch::WHS};
use bitflags::bitflags;
use enumflags2::{bitflags, make_bitflags, BitFlags};
use std::collections::VecDeque;
use std::iter::FusedIterator;
use std::ops::{BitOr, BitOrAssign};
use unicode_bom::Bom;

/// `Lexer` trait defines the necessary methods a lexer must implement.
Expand Down Expand Up @@ -760,24 +761,56 @@ impl<Kind: SyntaxKind> LexerCheckpoint<Kind> {
}
}

bitflags! {
/// Flags for a lexed token.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct TokenFlags: u8 {
/// Indicates that there has been a line break between the last non-trivia token
const PRECEDING_LINE_BREAK = 1 << 0;

/// Indicates that an identifier contains an unicode escape sequence
const UNICODE_ESCAPE = 1 << 1;
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[bitflags]
#[repr(u8)]
enum TokenFlag {
PrecedingLineBreak = 1 << 0,
UnicodeEscape = 1 << 1,
}

/// Flags for a lexed token.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct TokenFlags(BitFlags<TokenFlag>);

impl TokenFlags {
pub const fn has_preceding_line_break(&self) -> bool {
/// Indicates that there has been a line break between the last non-trivia token
pub const PRECEDING_LINE_BREAK: Self = Self(make_bitflags!(TokenFlag::{PrecedingLineBreak}));

/// Indicates that an identifier contains an unicode escape sequence
pub const UNICODE_ESCAPE: Self = Self(make_bitflags!(TokenFlag::{UnicodeEscape}));

pub const fn empty() -> Self {
Self(BitFlags::EMPTY)
}

pub fn contains(&self, other: impl Into<TokenFlags>) -> bool {
self.0.contains(other.into().0)
}

pub fn set(&mut self, other: impl Into<TokenFlags>, cond: bool) {
self.0.set(other.into().0, cond)
}

pub fn has_preceding_line_break(&self) -> bool {
self.contains(TokenFlags::PRECEDING_LINE_BREAK)
}

pub const fn has_unicode_escape(&self) -> bool {
pub fn has_unicode_escape(&self) -> bool {
self.contains(TokenFlags::UNICODE_ESCAPE)
}
}

impl BitOr for TokenFlags {
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
TokenFlags(self.0 | rhs.0)
}
}

impl BitOrAssign for TokenFlags {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
Loading