From a3446a72b6fb43570c73dcd0ab627d37912a571a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mu=C3=B1oz=20Tous?= Date: Fri, 6 Sep 2024 02:34:41 +0200 Subject: [PATCH] refactor(biome_parser): use enumflags2 for `TokenFlag ` and remove `bitflags` (#3802) --- Cargo.lock | 4 +- Cargo.toml | 1 - crates/biome_js_analyze/Cargo.toml | 1 - crates/biome_js_parser/Cargo.toml | 1 - crates/biome_parser/Cargo.toml | 2 +- crates/biome_parser/src/lexer.rs | 59 +++++++++++++++++++++++------- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01fa58177044..7c3eebcc8e84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -713,7 +713,6 @@ dependencies = [ "biome_suppression", "biome_test_utils", "biome_unicode_table", - "bitflags 2.6.0", "bitvec", "enumflags2", "insta", @@ -776,7 +775,6 @@ dependencies = [ "biome_parser", "biome_rowan", "biome_unicode_table", - "bitflags 2.6.0", "drop_bomb", "enumflags2", "expect-test", @@ -968,8 +966,8 @@ dependencies = [ "biome_diagnostics", "biome_rowan", "biome_unicode_table", - "bitflags 2.6.0", "drop_bomb", + "enumflags2", "unicode-bom", ] diff --git a/Cargo.toml b/Cargo.toml index b379647aa473..9753791b143c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/biome_js_analyze/Cargo.toml b/crates/biome_js_analyze/Cargo.toml index c740783f0ad4..064caedee302 100644 --- a/crates/biome_js_analyze/Cargo.toml +++ b/crates/biome_js_analyze/Cargo.toml @@ -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 } diff --git a/crates/biome_js_parser/Cargo.toml b/crates/biome_js_parser/Cargo.toml index 83c890506ff2..00b5283e61ad 100644 --- a/crates/biome_js_parser/Cargo.toml +++ b/crates/biome_js_parser/Cargo.toml @@ -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 } diff --git a/crates/biome_parser/Cargo.toml b/crates/biome_parser/Cargo.toml index e06bfb9108b0..769db15fc9b3 100644 --- a/crates/biome_parser/Cargo.toml +++ b/crates/biome_parser/Cargo.toml @@ -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] diff --git a/crates/biome_parser/src/lexer.rs b/crates/biome_parser/src/lexer.rs index 5e262fbcc6fb..4546b88b56c1 100644 --- a/crates/biome_parser/src/lexer.rs +++ b/crates/biome_parser/src/lexer.rs @@ -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. @@ -760,24 +761,56 @@ impl LexerCheckpoint { } } -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); + 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) -> bool { + self.0.contains(other.into().0) + } + + pub fn set(&mut self, other: impl Into, 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; + } +}