feat: use ere (compile-time regex compilation)

This commit is contained in:
2026-02-07 17:00:06 +08:00
parent 216930027d
commit e8a28a6d2f
4 changed files with 55 additions and 20 deletions

41
Cargo.lock generated
View File

@@ -916,6 +916,40 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "ere"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced253a70c1159bac921dc672edbb5530c35fbca23a7f1b9f03641f39e4172f1"
dependencies = [
"ere-core",
"ere-macros",
]
[[package]]
name = "ere-core"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6639aa08d01ec59ec0509e9b79b4e17b953131b053d1a9a6b449150e58c9706d"
dependencies = [
"proc-macro2",
"quote",
"syn",
"thiserror 2.0.17",
"utf8-ranges",
]
[[package]]
name = "ere-macros"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9a41368e568caa1ee19d8dfa943d7f2f3ddabc0d9c0965cc37d76e0146c6b89"
dependencies = [
"ere-core",
"quote",
"syn",
]
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.13" version = "0.3.13"
@@ -1912,6 +1946,7 @@ dependencies = [
"deno_error", "deno_error",
"derive_more", "derive_more",
"dirs", "dirs",
"ere",
"flate2", "flate2",
"hashbrown 0.16.1", "hashbrown 0.16.1",
"hex", "hex",
@@ -3550,6 +3585,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "utf8-ranges"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcfc827f90e53a02eaef5e535ee14266c1d569214c6aa70133a624d8a3164ba"
[[package]] [[package]]
name = "utf8_iter" name = "utf8_iter"
version = "1.0.4" version = "1.0.4"

View File

@@ -62,6 +62,7 @@ rnix = "0.12"
rowan = "0.15" rowan = "0.15"
nix-js-macros = { path = "../nix-js-macros" } nix-js-macros = { path = "../nix-js-macros" }
ere = "0.2.4"
[dev-dependencies] [dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] } criterion = { version = "0.5", features = ["html_reports"] }

View File

@@ -1,7 +1,6 @@
use anyhow::Result; use anyhow::Result;
use nix_js::context::Context; use nix_js::context::Context;
use nix_js::error::Source; use nix_js::error::Source;
use regex::Regex;
use rustyline::DefaultEditor; use rustyline::DefaultEditor;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
@@ -10,7 +9,7 @@ fn main() -> Result<()> {
let mut rl = DefaultEditor::new()?; let mut rl = DefaultEditor::new()?;
let mut context = Context::new()?; let mut context = Context::new()?;
let re = Regex::new(r"^\s*([a-zA-Z_][a-zA-Z0-9_'-]*)\s*=(.*)$").unwrap(); const RE: ere::Regex<3> = ere::compile_regex!("^[ \t]*([a-zA-Z_][a-zA-Z0-9_'-]*)[ \t]*(.*)$");
loop { loop {
let readline = rl.readline("nix-js-repl> "); let readline = rl.readline("nix-js-repl> ");
match readline { match readline {
@@ -19,18 +18,16 @@ fn main() -> Result<()> {
continue; continue;
} }
let _ = rl.add_history_entry(line.as_str()); let _ = rl.add_history_entry(line.as_str());
if let Some(_caps) = re.captures(&line) { if let Some([Some(_), Some(_ident), Some(_expr)]) = RE.exec(&line) {
eprintln!("Error: binding not implemented yet"); eprintln!("Error: binding not implemented yet");
continue; continue;
/* let ident = caps.get(1).unwrap().as_str(); // if expr.is_empty() {
let expr = caps.get(2).unwrap().as_str().trim(); // eprintln!("Error: missing expression after '='");
if expr.is_empty() { // continue;
eprintln!("Error: missing expression after '='"); // }
continue; // if let Err(err) = context.add_binding(ident, expr) {
} // eprintln!("Error: {}", err);
if let Err(err) = context.add_binding(ident, expr) { // }
eprintln!("Error: {}", err);
} */
} else { } else {
let src = Source::new_repl(line)?; let src = Source::new_repl(line)?;
match context.eval_shallow(src) { match context.eval_shallow(src) {

View File

@@ -5,10 +5,8 @@ use core::ops::Deref;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::sync::LazyLock;
use derive_more::{Constructor, IsVariant, Unwrap}; use derive_more::{Constructor, IsVariant, Unwrap};
use regex::Regex;
/// Represents a Nix symbol, which is used as a key in attribute sets. /// Represents a Nix symbol, which is used as a key in attribute sets.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Constructor)] #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Constructor)]
@@ -23,10 +21,10 @@ impl<T: Into<String>> From<T> for Symbol {
/// Formats a string slice as a Nix symbol, quoting it if necessary. /// Formats a string slice as a Nix symbol, quoting it if necessary.
pub fn format_symbol<'a>(sym: impl Into<Cow<'a, str>>) -> Cow<'a, str> { pub fn format_symbol<'a>(sym: impl Into<Cow<'a, str>>) -> Cow<'a, str> {
let sym = sym.into(); let sym = sym.into();
if REGEX.is_match(&sym) { if Symbol::NORMAL_REGEX.test(&sym) {
sym sym
} else { } else {
Cow::Owned(format!(r#""{sym}""#)) Cow::Owned(escape_quote_string(&sym))
} }
} }
@@ -40,13 +38,11 @@ impl Display for Symbol {
} }
} }
static REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_'-]*$").expect("hardcoded regex is always valid")
});
impl Symbol { impl Symbol {
const NORMAL_REGEX: ere::Regex<1> = ere::compile_regex!("^[a-zA-Z_][a-zA-Z0-9_'-]*$");
/// Checks if the symbol is a "normal" identifier that doesn't require quotes. /// Checks if the symbol is a "normal" identifier that doesn't require quotes.
fn normal(&self) -> bool { fn normal(&self) -> bool {
REGEX.is_match(self) Self::NORMAL_REGEX.test(self)
} }
} }