pub struct CompileError {
pub message: String,
pub expression: Option<ExpressionIndex>,
}
compiler
only.Expand description
Error details returned by the pattern compiler.
This is returned by the compile calls
(Database::compile()
and
Database::compile_multi()
) on
failure. The caller may inspect the values returned in this type to
determine the cause of failure.
Fields§
§message: String
A human-readable error message describing the error.
§Common Errors
Common errors generated during the compile process include:
-
Invalid parameter: An invalid argument was specified in the compile call.
-
Unrecognised flag: An unrecognised value was passed in the flags argument.
-
Pattern matches empty buffer: By default, Vectorscan only supports patterns that will always consume at least one byte of input. Patterns that do not have this property (such as
/(abc)?/
) will produce this error unless theFlags::ALLOWEMPTY
flag is supplied. Note that such patterns will produce a match for every byte when scanned. -
Embedded anchors not supported: Vectorscan only supports the use of anchor meta-characters (such as
^
and$
) in patterns where they could only match at the start or end of a buffer. A pattern containing an embedded anchor, such as/abc^def/
, can never match, as there is no way forabc
to precede the start of the data stream. -
Bounded repeat is too large: The pattern contains a repeated construct with very large finite bounds.
-
Unsupported component type: An unsupported PCRE construct was used in the pattern. Consider using
chimera
for full PCRE support. -
Unable to generate bytecode: This error indicates that Vectorscan was unable to compile a pattern that is syntactically valid. The most common cause is a pattern that is very long and complex or contains a large repeated subpattern.
-
Unable to allocate memory: The library was unable to allocate temporary storage used during compilation time.
-
Allocator returned misaligned memory: The memory allocator (either
libc::malloc()
or the allocator set withset_db_allocator()
) did not correctly return memory suitably aligned for the largest representable data type on this platform. -
Internal error: An unexpected error occurred: if this error is reported, please contact the Vectorscan team with a description of the situation.
expression: Option<ExpressionIndex>
The zero-based number of the expression that caused the error (if this
can be determined). For a database with a single expression, this value
will be 0
:
use vectorscan::{expression::*, error::*, matchers::*, flags::*};
let expr: Expression = "as(df".parse()?;
let index = match expr.compile(Flags::default(), Mode::BLOCK) {
Err(VectorscanCompileError::Compile(CompileError { expression, .. })) => expression,
_ => unreachable!(),
};
assert_eq!(index, Some(ExpressionIndex(0)));
Note that while this uses the same ExpressionIndex
type as in
Match
, the value is not
calculated from any ExprId
instances
provided to
ExpressionSet::with_ids()
,
but instead just from the expression’s index in the set:
use vectorscan::{expression::*, error::*, matchers::*, flags::*};
let e1: Expression = "aa".parse()?;
let e2: Expression = "as(df".parse()?;
let set = ExpressionSet::from_exprs([&e1, &e2]).with_ids([ExprId(2), ExprId(3)]);
let index = match set.compile(Mode::BLOCK) {
Err(VectorscanCompileError::Compile(CompileError { expression, .. })) => expression,
_ => unreachable!(),
};
assert_eq!(index, Some(ExpressionIndex(1)));
If the error is not specific to an expression, then this value will be
None
:
// Using vectorscan::alloc requires the "alloc" feature.
#[cfg(feature = "alloc")]
fn main() -> Result<(), vectorscan::error::VectorscanError> {
use vectorscan::{expression::*, error::*, flags::*, alloc::*};
use std::{alloc::{GlobalAlloc, Layout}, ptr};
// Create a broken allocator:
struct S;
unsafe impl GlobalAlloc for S {
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { ptr::null_mut() }
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
}
// Set it as the db compile allocator:
assert!(set_db_allocator(LayoutTracker::new(S.into())).unwrap().is_none());
let expr: Expression = "a".parse()?;
let CompileError { message, expression } = match expr.compile(Flags::default(), Mode::BLOCK) {
Err(VectorscanCompileError::Compile(err)) => err,
_ => unreachable!(),
};
assert_eq!(expression, None);
assert_eq!(&message, "Could not allocate memory for bytecode.");
Ok(())
}