Skip to content

Commit

Permalink
Remove regex compilation from hotpath
Browse files Browse the repository at this point in the history
  • Loading branch information
rozukke committed Aug 9, 2024
1 parent f73e690 commit cf75e4c
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use regex::Regex;

use crate::lexer::cursor::Cursor;
Expand Down Expand Up @@ -43,6 +44,25 @@ pub fn tokenize(input: &str) -> impl Iterator<Item = Token> + '_ {
}
})
}
lazy_static! {
// Order is important since some patterns are subpatterns of others.
// Do NOT rearrange without a good hard think.
static ref PATTERNS: Vec<(TokenKind, Regex)> = vec![
(TokenKind::Junk, Regex::new(r"^[,\s]+").unwrap()),
(
TokenKind::Lit(LiteralKind::Hex),
Regex::new(r"^(0x|x)[0-9a-fA-F]+\b").unwrap(),
),
(
TokenKind::Lit(LiteralKind::Dec),
Regex::new(r"^#[0-9]+\b").unwrap(),
),
(TokenKind::Reg, Regex::new(r"^[rR][0-8]\b").unwrap()),
(TokenKind::Ident, Regex::new(r"^[a-zA-Z_]\w*\b").unwrap()),
(TokenKind::Comment, Regex::new(r"^;[^\n]*").unwrap()),
(TokenKind::Direc, Regex::new(r"^\.[a-zA-Z_]*\b").unwrap()),
];
}

impl Cursor<'_> {
pub fn advance_token(&mut self) -> Token {
Expand All @@ -53,26 +73,10 @@ impl Cursor<'_> {
};
}

let patterns = vec![
(TokenKind::Junk, Regex::new(r"^[,\s]+").unwrap()),
(
TokenKind::Lit(LiteralKind::Hex),
Regex::new(r"^(0x|x)[0-9a-fA-F]+\b").unwrap(),
),
(
TokenKind::Lit(LiteralKind::Dec),
Regex::new(r"^#[0-9]+\b").unwrap(),
),
(TokenKind::Ident, Regex::new(r"^[a-zA-Z_]\w*\b").unwrap()),
(TokenKind::Reg, Regex::new(r"^[rR][0-8]\b").unwrap()),
(TokenKind::Comment, Regex::new(r"^;[^\n]*").unwrap()),
(TokenKind::Direc, Regex::new(r"^\.[a-zA-Z_]*\b").unwrap()),
];

for (kind, re) in patterns {
for (kind, re) in PATTERNS.iter() {
if let Some(tok) = re.find(self.at_curr_pt()) {
let token = Token {
kind,
kind: *kind,
span: Span::new(Idx(self.curr_pt() as u32), tok.len() as u16),
};
self.advance(tok.len());
Expand Down

0 comments on commit cf75e4c

Please sign in to comment.