Skip to content

Commit

Permalink
refacto: pass whole compiler to Scanner::new
Browse files Browse the repository at this point in the history
This avoids the "too many arguments" lint on Scanner::new, and will
scale better.
  • Loading branch information
vthib committed Sep 28, 2024
1 parent 261b11c commit b0acb42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
30 changes: 10 additions & 20 deletions boreal/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ use crate::{statistics, Scanner};
#[derive(Debug)]
pub struct Compiler {
/// List of compiled rules.
rules: Vec<rule::Rule>,
pub(crate) rules: Vec<rule::Rule>,

/// List of compiled, global rules.
global_rules: Vec<rule::Rule>,
pub(crate) global_rules: Vec<rule::Rule>,

/// List of compiled variables.
variables: Vec<variable::Variable>,
pub(crate) variables: Vec<variable::Variable>,

/// Number of variables used by global rules.
nb_global_rules_variables: usize,
Expand All @@ -46,7 +46,7 @@ pub struct Compiler {
/// This list always contains at least one namespace: the default one,
/// at index 0. Other namespaces are added when rules are added in the
/// non default namespace.
namespaces: Vec<Namespace>,
pub(crate) namespaces: Vec<Namespace>,

/// Map from the namespace name to its index in the `namespaces` list.
namespaces_indexes: HashMap<String, usize>,
Expand All @@ -57,15 +57,15 @@ pub struct Compiler {
available_modules: HashMap<&'static str, AvailableModule>,

/// List of imported modules, passed to the scanner.
imported_modules: Vec<Box<dyn crate::module::Module>>,
pub(crate) imported_modules: Vec<Box<dyn crate::module::Module>>,

/// Externally defined symbols.
external_symbols: Vec<external_symbol::ExternalSymbol>,
pub(crate) external_symbols: Vec<external_symbol::ExternalSymbol>,

/// Bytes intern pool.
///
/// This is used to reduce memory footprint and share byte strings.
bytes_pool: BytesPoolBuilder,
pub(crate) bytes_pool: BytesPoolBuilder,

/// Compilation parameters
params: CompilerParams,
Expand Down Expand Up @@ -496,17 +496,7 @@ impl Compiler {
/// Can fail if generating a set of all rules variables is not possible.
#[must_use]
pub fn into_scanner(self) -> Scanner {
let namespaces = self.namespaces.into_iter().map(|v| v.name).collect();

Scanner::new(
self.rules,
self.global_rules,
self.variables,
self.imported_modules,
self.external_symbols,
namespaces,
self.bytes_pool.into_pool(),
)
Scanner::new(self)
}
}

Expand All @@ -517,9 +507,9 @@ impl Compiler {
/// - new rules can reference already existing rules
/// - new rules can either import new modules, or directly use already imported modules
#[derive(Debug, Default)]
struct Namespace {
pub(crate) struct Namespace {
/// Name of the namespace, `None` if default.
name: Option<String>,
pub(crate) name: Option<String>,

/// Map of a rule name to its index in the `rules` vector in [`Compiler`].
///
Expand Down
38 changes: 17 additions & 21 deletions boreal/src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::evaluator::{self, evaluate_rule, EvalError};
use crate::memory::{FragmentedMemory, Memory, Region};
use crate::module::{Module, ModuleData, ModuleUserData};
use crate::timeout::TimeoutChecker;
use crate::{statistics, Metadata};
use crate::{statistics, Compiler, Metadata};

pub use crate::evaluator::variable::StringMatch;

Expand Down Expand Up @@ -101,15 +101,19 @@ pub struct Scanner {
}

impl Scanner {
pub(crate) fn new(
rules: Vec<Rule>,
global_rules: Vec<Rule>,
variables: Vec<Variable>,
modules: Vec<Box<dyn Module>>,
external_symbols: Vec<ExternalSymbol>,
namespaces: Vec<Option<String>>,
bytes_pool: BytesPool,
) -> Self {
pub(crate) fn new(compiler: Compiler) -> Self {
let Compiler {
rules,
global_rules,
variables,
namespaces,
imported_modules,
external_symbols,
bytes_pool,
..
} = compiler;
let namespaces = namespaces.into_iter().map(|v| v.name).collect();

let ac_scan = ac_scan::AcScan::new(&variables);

let mut external_symbols_values = Vec::new();
Expand All @@ -129,10 +133,10 @@ impl Scanner {
global_rules,
variables,
ac_scan,
modules,
modules: imported_modules,
external_symbols_map,
namespaces,
bytes_pool,
bytes_pool: bytes_pool.into_pool(),
}),
scan_params: ScanParams::default(),
external_symbols_values,
Expand Down Expand Up @@ -1507,15 +1511,7 @@ mod tests {

#[test]
fn test_types_traits() {
test_type_traits(Scanner::new(
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
Vec::new(),
BytesPool::default(),
));
test_type_traits(Scanner::new(Compiler::default()));
test_type_unwind_safe::<Scanner>();

test_type_traits_non_clonable(ScanResult {
Expand Down

0 comments on commit b0acb42

Please sign in to comment.