Skip to content

Commit

Permalink
refactor(lexer): make handle_byte a method of Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 6, 2025
1 parent 76a5e21 commit 4e5df89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
20 changes: 11 additions & 9 deletions crates/oxc_parser/src/lexer/byte_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use crate::diagnostics;

use super::{Kind, Lexer};

/// Handle next byte of source.
///
/// # SAFETY
///
/// * Lexer must not be at end of file.
/// * `byte` must be next byte of source code, corresponding to current position of `lexer.source`.
/// * Only `BYTE_HANDLERS` for ASCII characters may use the `ascii_byte_handler!()` macro.
pub(super) unsafe fn handle_byte(byte: u8, lexer: &mut Lexer) -> Kind {
BYTE_HANDLERS[byte as usize](lexer)
impl Lexer<'_> {
/// Handle next byte of source.
///
/// # SAFETY
///
/// * Lexer must not be at end of file.
/// * `byte` must be next byte of source code, corresponding to current position of `lexer.source`.
/// * Only `BYTE_HANDLERS` for ASCII characters may use the `ascii_byte_handler!()` macro.
pub(super) unsafe fn handle_byte(&mut self, byte: u8) -> Kind {
BYTE_HANDLERS[byte as usize](self)
}
}

type ByteHandler = unsafe fn(&mut Lexer<'_>) -> Kind;
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub use kind::Kind;
pub use number::{parse_big_int, parse_float, parse_int};
pub use token::Token;

use byte_handlers::handle_byte;
use source::{Source, SourcePosition};
use trivia_builder::TriviaBuilder;

Expand Down Expand Up @@ -315,7 +314,7 @@ impl<'a> Lexer<'a> {
};

// SAFETY: `byte` is byte value at current position in source
let kind = unsafe { handle_byte(byte, self) };
let kind = unsafe { self.handle_byte(byte) };
if kind != Kind::Skip {
return kind;
}
Expand Down

0 comments on commit 4e5df89

Please sign in to comment.