Truly independent web browser
LibRegex: Move the compiler core into a no_std crate
Andreas Klinge4d093a25612
diff --git a/Cargo.lock b/Cargo.lockindex a121c6f4653..939db76437e 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2003,6 +2003,7 @@ dependencies = ["libjs_jit","libjs_parser","libjs_runtime_macros",+ "libregex_compiler","libregex_rust","libunicode_rust","memchr",@@ -2028,6 +2029,14 @@ version = "0.2.16"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"+[[package]]+name = "libregex_compiler"+version = "0.1.0"+dependencies = [+ "ak",+ "libunicode_rust",+]+[[package]]name = "libregex_rust"version = "0.1.0"@@ -2035,6 +2044,7 @@ dependencies = ["ak","cbindgen","ladybird_allocator",+ "libregex_compiler","libunicode_rust","memchr","rustc-demangle",@@ -2072,6 +2082,7 @@ dependencies = ["encoding_rs","getrandom 0.4.2","ladybird_allocator",+ "libregex_compiler","libregex_rust","libunicode_rust","rustc-demangle",diff --git a/Cargo.toml b/Cargo.tomlindex c470fd63316..0184686b2ce 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -16,6 +16,7 @@ members = ["Libraries/LibJS/Rust","Libraries/LibJS/Rust/embedding_headers","Libraries/LibJS/Rust/macros",+ "Libraries/LibRegex/Compiler/Rust","Libraries/LibRegex/Rust","Libraries/LibTextCodec/Rust","Libraries/LibUnicode/Rust",diff --git a/Libraries/LibJS/Rust/Cargo.toml b/Libraries/LibJS/Rust/Cargo.tomlindex 724d137dd2f..d1adf564a8b 100644--- a/Libraries/LibJS/Rust/Cargo.toml+++ b/Libraries/LibJS/Rust/Cargo.toml@@ -30,6 +30,7 @@ libjs_jit = { path = "../JIT/Rust", optional = true }libjs_parser = { path = "../Parser/Rust" }libjs_runtime_macros = { path = "macros" }memchr = "2"+libregex_compiler = { path = "../../LibRegex/Compiler/Rust" }libregex_rust = { path = "../../LibRegex/Rust", default-features = false }libunicode_rust = { path = "../../LibUnicode/Rust", default-features = false }num-bigint = "0.5"diff --git a/Libraries/LibJS/Rust/src/frontend_host.rs b/Libraries/LibJS/Rust/src/frontend_host.rsindex 999e8002643..e293e40631a 100644--- a/Libraries/LibJS/Rust/src/frontend_host.rs+++ b/Libraries/LibJS/Rust/src/frontend_host.rs@@ -6,8 +6,8 @@//! The functions the Rust frontend (Libraries/LibJS/Rust) calls on the runtime that embeds it, through host.rs.-use libregex_rust::ast::Flags;-use libregex_rust::parser::Error as ParseError;+use libregex_compiler::ast::Flags;+use libregex_compiler::parser::Error as ParseError;use libregex_rust::regex::Regex;use num_bigint::BigInt;diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_regex.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_regex.rsindex 8c4d712fe45..5ffbbad8b28 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_regex.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_regex.rs@@ -10,7 +10,7 @@use core::cell::RefCell;use ak::Utf16FlyString;-use libregex_rust::ast::Flags;+use libregex_compiler::ast::Flags;use libregex_rust::regex::Regex;use libregex_rust::vm::VmResult;diff --git a/Libraries/LibRegex/Compiler/Rust/Cargo.toml b/Libraries/LibRegex/Compiler/Rust/Cargo.tomlnew file mode 100644index 00000000000..af5eb5cdcf9--- /dev/null+++ b/Libraries/LibRegex/Compiler/Rust/Cargo.toml@@ -0,0 +1,16 @@+[package]+name = "libregex_compiler"+version = "0.1.0"+edition = "2024"++[lib]+crate-type = ["rlib"]++# After changing dependencies, regenerate the Flatpak sources:+# python3 Meta/CMake/flatpak/generate-cargo-sources.py+[dependencies]+ak = { path = "../../../../AK/Rust" }+libunicode_rust = { path = "../../../LibUnicode/Rust", default-features = false }++[lints]+workspace = truediff --git a/Libraries/LibRegex/Rust/src/ast.rs b/Libraries/LibRegex/Compiler/Rust/src/ast.rssimilarity index 100%rename from Libraries/LibRegex/Rust/src/ast.rsrename to Libraries/LibRegex/Compiler/Rust/src/ast.rsdiff --git a/Libraries/LibRegex/Rust/src/compiler.rs b/Libraries/LibRegex/Compiler/Rust/src/compiler.rssimilarity index 99%rename from Libraries/LibRegex/Rust/src/compiler.rsrename to Libraries/LibRegex/Compiler/Rust/src/compiler.rsindex 143a9308ade..7b1bec1c55e 100644--- a/Libraries/LibRegex/Rust/src/compiler.rs+++ b/Libraries/LibRegex/Compiler/Rust/src/compiler.rs@@ -1598,7 +1598,7 @@ impl<'z> Compiler<'z> {return;}self.emit_split_chain(&filtered, |s, operand| {- s.compile_class_set_operand_at_length(operand, length)+ s.compile_class_set_operand_at_length(operand, length);});}ClassSetExpression::Intersection(operands) => {diff --git a/Libraries/LibRegex/Compiler/Rust/src/lib.rs b/Libraries/LibRegex/Compiler/Rust/src/lib.rsnew file mode 100644index 00000000000..e78bc7df2fa--- /dev/null+++ b/Libraries/LibRegex/Compiler/Rust/src/lib.rs@@ -0,0 +1,22 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! The core of the LibRegex compiler: the parser, the syntax tree, the compiler to a program and the literal hints.+//!+//! A compile has all of its work data in one zone (see `ak::Zone`), which the code that starts the compile hands it.+//! What the compile keeps of its results is copied out of the zone by that code.++// NB: The Rust code of a compile makes no block in the heap: all its work data is in its zone. Without `std` (and+// without `alloc`), rustc rejects code of this crate that would make one. The LibUnicode helper that gives the+// data of a string property (`get_string_property_data()`) still makes temporary C++ vectors.+#![cfg_attr(not(test), no_std)]++pub mod ast;+pub mod compiler;+pub mod literal_hints;+pub mod parser;+mod stack;+pub mod zone_program;diff --git a/Libraries/LibRegex/Rust/src/literal_hints.rs b/Libraries/LibRegex/Compiler/Rust/src/literal_hints.rssimilarity index 100%rename from Libraries/LibRegex/Rust/src/literal_hints.rsrename to Libraries/LibRegex/Compiler/Rust/src/literal_hints.rsdiff --git a/Libraries/LibRegex/Rust/src/parser.rs b/Libraries/LibRegex/Compiler/Rust/src/parser.rssimilarity index 99%rename from Libraries/LibRegex/Rust/src/parser.rsrename to Libraries/LibRegex/Compiler/Rust/src/parser.rsindex 0176135aaab..87bf9920da4 100644--- a/Libraries/LibRegex/Rust/src/parser.rs+++ b/Libraries/LibRegex/Compiler/Rust/src/parser.rs@@ -455,20 +455,17 @@ impl<'z> Parser<'z> {let start = self.pos;self.pos += 1; // skip '{'- let min = match self.try_parse_decimal()? {- Some(n) => n,- None => {- // Not a valid quantifier -- rewind.- self.pos = start;- if self.unicode_aware() {- return Err(Error::InvalidQuantifier);- }- return Ok(None);+ let Some(min) = self.try_parse_decimal() else {+ // Not a valid quantifier -- rewind.+ self.pos = start;+ if self.unicode_aware() {+ return Err(Error::InvalidQuantifier);}+ return Ok(None);};let max = if self.eat(',') {- self.try_parse_decimal()?+ self.try_parse_decimal()} else {Some(min) // {n} means exactly n};@@ -494,11 +491,11 @@ impl<'z> Parser<'z> {}/// Try to parse a decimal integer. Returns:- /// - `Ok(None)` if no digits were found- /// - `Ok(Some(n))` if a valid u32 was parsed+ /// - `None` if no digits were found+ /// - `Some(n)` if a valid u32 was parsed////// Clamp braced quantifier bounds to 2^31 - 1 to match browser behavior.- fn try_parse_decimal(&mut self) -> Result<Option<u32>, Error<'z>> {+ fn try_parse_decimal(&mut self) -> Option<u32> {let start = self.pos;let mut value: u32 = 0;while let Some(ch) = self.peek() {@@ -510,7 +507,7 @@ impl<'z> Parser<'z> {break;}}- if self.pos == start { Ok(None) } else { Ok(Some(value)) }+ if self.pos == start { None } else { Some(value) }}// ---------------------------------------------------------------@@ -1154,7 +1151,7 @@ impl<'z> Parser<'z> {loop {match self.peek() {- Some(':') | Some(')') | None => break,+ Some(':' | ')') | None => break,Some('-') if !removing => {self.advance();removing = true;diff --git a/Libraries/LibRegex/Rust/src/stack.rs b/Libraries/LibRegex/Compiler/Rust/src/stack.rssimilarity index 100%rename from Libraries/LibRegex/Rust/src/stack.rsrename to Libraries/LibRegex/Compiler/Rust/src/stack.rsdiff --git a/Libraries/LibRegex/Rust/src/zone_program.rs b/Libraries/LibRegex/Compiler/Rust/src/zone_program.rssimilarity index 93%rename from Libraries/LibRegex/Rust/src/zone_program.rsrename to Libraries/LibRegex/Compiler/Rust/src/zone_program.rsindex 15d5fccc396..1d7071e3c01 100644--- a/Libraries/LibRegex/Rust/src/zone_program.rs+++ b/Libraries/LibRegex/Compiler/Rust/src/zone_program.rs@@ -6,9 +6,9 @@//! The program that the compiler makes of a pattern, in the zone of the compile.//!-//! It has the instructions of the program that the VM runs (see `bytecode::Program`), but its lists and names are-//! slices and strings of the zone, so it is plain data with the lifetime `'z` of the zone. What a compile keeps is a-//! copy of it at its final size, outside of the zone.+//! It has the instructions of the program that the VM runs (see `libregex_rust::bytecode::Program`), but its lists+//! and names are slices and strings of the zone, so it is plain data with the lifetime `'z` of the zone. What a compile+//! keeps is a copy of it at its final size, outside of the zone.use crate::ast::BuiltinCharacterClass;use crate::ast::NamedGroup;@@ -40,7 +40,7 @@ pub struct ZoneProgram<'z> {pub named_groups: &'z [NamedGroup<'z>],}-/// A single bytecode instruction, as `bytecode::Instruction` has it, with the variable data in the zone.+/// A single bytecode instruction, as `libregex_rust::bytecode::Instruction` has it, with the variable data in the zone./// <https://tc39.es/ecma262/#sec-pattern-semantics>#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum ZoneInstruction<'z> {@@ -156,7 +156,7 @@ pub enum ZoneInstruction<'z> {},}-/// A simple character matcher for optimized greedy/lazy loops, as `bytecode::SimpleMatch` has it.+/// A simple character matcher for optimized greedy/lazy loops, as `libregex_rust::bytecode::SimpleMatch` has it.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum ZoneSimpleMatch<'z> {/// Any character (`.`), with dot_all flag.diff --git a/Libraries/LibRegex/Rust/Cargo.toml b/Libraries/LibRegex/Rust/Cargo.tomlindex 5d4025cb4b5..ceb941a4a73 100644--- a/Libraries/LibRegex/Rust/Cargo.toml+++ b/Libraries/LibRegex/Rust/Cargo.toml@@ -11,6 +11,7 @@ crate-type = ["staticlib", "rlib"][dependencies]ak = { path = "../../../AK/Rust" }ladybird_allocator = { path = "../../RustAllocator", optional = true }+libregex_compiler = { path = "../Compiler/Rust" }libunicode_rust = { path = "../../LibUnicode/Rust", default-features = false }memchr = "2"rustc-demangle = "0.1"diff --git a/Libraries/LibRegex/Rust/src/bytecode.rs b/Libraries/LibRegex/Rust/src/bytecode.rsindex 87c839260ba..05c74794b49 100644--- a/Libraries/LibRegex/Rust/src/bytecode.rs+++ b/Libraries/LibRegex/Rust/src/bytecode.rs@@ -267,7 +267,7 @@ pub struct CharRange {}/// Re-export for use by the compiler and VM.-pub use crate::ast::BuiltinCharacterClass;+pub use libregex_compiler::ast::BuiltinCharacterClass;impl Default for Program {fn default() -> Self {diff --git a/Libraries/LibRegex/Rust/src/copy_out.rs b/Libraries/LibRegex/Rust/src/copy_out.rsindex c13286e7394..ca8fc3b6c1c 100644--- a/Libraries/LibRegex/Rust/src/copy_out.rs+++ b/Libraries/LibRegex/Rust/src/copy_out.rs@@ -7,7 +7,14 @@//! The copy of what a regex keeps of its compile, out of the zone of the compile and at its final size: the program//! that the VM runs and the literals that let a search skip the VM.-use crate::ast::UnicodeProperty;+use libregex_compiler::ast::UnicodeProperty;+use libregex_compiler::compiler;+use libregex_compiler::literal_hints::RequiredLiteral;+use libregex_compiler::zone_program::CodePointRange;+use libregex_compiler::zone_program::ZoneInstruction;+use libregex_compiler::zone_program::ZoneProgram;+use libregex_compiler::zone_program::ZoneSimpleMatch;+use crate::bytecode::CharRange;use crate::bytecode::CharRanges;use crate::bytecode::Instruction;@@ -15,13 +22,7 @@ use crate::bytecode::NamedGroupEntry;use crate::bytecode::Program;use crate::bytecode::SimpleMatch;use crate::bytecode::UnicodePropertyData;-use crate::compiler;-use crate::literal_hints::RequiredLiteral;use crate::vm;-use crate::zone_program::CodePointRange;-use crate::zone_program::ZoneInstruction;-use crate::zone_program::ZoneProgram;-use crate::zone_program::ZoneSimpleMatch;pub(crate) fn program(program: &ZoneProgram<'_>) -> Program {let mut instructions = Vec::with_capacity(program.instructions.len());diff --git a/Libraries/LibRegex/Rust/src/ffi.rs b/Libraries/LibRegex/Rust/src/ffi.rsindex 0cb6f498daf..fb54e2421b5 100644--- a/Libraries/LibRegex/Rust/src/ffi.rs+++ b/Libraries/LibRegex/Rust/src/ffi.rs@@ -8,7 +8,8 @@////// Provides a C-compatible API for use from C++ (LibJS, LibURL)./// All functions use `extern "C"` linkage and opaque pointer types.-use crate::ast::Flags;+use libregex_compiler::ast::Flags;+use crate::regex::Regex;/// Opaque regex handle.diff --git a/Libraries/LibRegex/Rust/src/lib.rs b/Libraries/LibRegex/Rust/src/lib.rsindex 0d71512682a..c16ab1d5a08 100644--- a/Libraries/LibRegex/Rust/src/lib.rs+++ b/Libraries/LibRegex/Rust/src/lib.rs@@ -13,15 +13,9 @@ mod rust_demangle;#[path = "../../../RustPanic.rs"]mod rust_panic;-pub mod ast;pub mod bytecode;-pub mod compiler;mod copy_out;pub mod ffi;-pub mod literal_hints;-pub mod parser;pub mod regex;-mod stack;pub mod vm;mod work_zone;-pub mod zone_program;diff --git a/Libraries/LibRegex/Rust/src/regex.rs b/Libraries/LibRegex/Rust/src/regex.rsindex e616be61715..6db3662b98f 100644--- a/Libraries/LibRegex/Rust/src/regex.rs+++ b/Libraries/LibRegex/Rust/src/regex.rs@@ -7,16 +7,16 @@/// High-level regex API.////// This is the main entry point for using the regex engine.-use crate::ast::Flags;use crate::bytecode::NamedGroupEntry;-use crate::compiler;use crate::copy_out;-use crate::literal_hints;-use crate::literal_hints::is_ascii_word_code_unit;-use crate::parser;use crate::vm;use crate::work_zone::with_work_zone;use ak::Zone;+use libregex_compiler::ast::Flags;+use libregex_compiler::compiler;+use libregex_compiler::literal_hints;+use libregex_compiler::literal_hints::is_ascii_word_code_unit;+use libregex_compiler::parser;use std::cell::RefCell;/// A compiled regular expression.diff --git a/Libraries/LibURL/Rust/Cargo.toml b/Libraries/LibURL/Rust/Cargo.tomlindex 35b7d99cf2b..2549ab72e8f 100644--- a/Libraries/LibURL/Rust/Cargo.toml+++ b/Libraries/LibURL/Rust/Cargo.toml@@ -11,6 +11,7 @@ ak = { path = "../../../AK/Rust" }encoding_rs = "0.8.44"getrandom = "0.4"ladybird_allocator = { path = "../../RustAllocator", optional = true }+libregex_compiler = { path = "../../LibRegex/Compiler/Rust" }libregex_rust = { path = "../../LibRegex/Rust" }libunicode_rust = { path = "../../LibUnicode/Rust", features = ["idna"] }rustc-demangle = "0.1"diff --git a/Libraries/LibURL/Rust/src/pattern/component.rs b/Libraries/LibURL/Rust/src/pattern/component.rsindex b19b1e4c91f..88df8576618 100644--- a/Libraries/LibURL/Rust/src/pattern/component.rs+++ b/Libraries/LibURL/Rust/src/pattern/component.rs@@ -8,7 +8,7 @@ use std::collections::BTreeMap;use std::fmt;use std::rc::Rc;-use libregex_rust::ast::Flags;+use libregex_compiler::ast::Flags;use libregex_rust::regex::Regex;use libregex_rust::vm::VmResult;