Skip to content

Commit ca9a4a2

Browse files
committed
Only pass parser settings when checking function parameters.
Passing the whole parser allows to access too many things, like the scheme and potentially other things we might add in the future. It also makes writing unit tests more cumbersome since we now have to build a scheme first just to get a dummy parser.
1 parent 8e10e4b commit ca9a4a2

File tree

8 files changed

+76
-29
lines changed

8 files changed

+76
-29
lines changed

engine/src/ast/field_expr.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,10 @@ impl<'s> Expr<'s> for ComparisonExpr<'s> {
645645
mod tests {
646646
use super::*;
647647
use crate::{
648-
ast::function_expr::{FunctionCallArgExpr, FunctionCallExpr},
649-
ast::logical_expr::LogicalExpr,
648+
ast::{
649+
function_expr::{FunctionCallArgExpr, FunctionCallExpr},
650+
logical_expr::LogicalExpr,
651+
},
650652
execution_context::ExecutionContext,
651653
functions::{
652654
FunctionArgKind, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
@@ -658,7 +660,7 @@ mod tests {
658660
rhs_types::{IpRange, RegexFormat},
659661
scheme::{FieldIndex, IndexAccessError, Scheme},
660662
types::ExpectedType,
661-
BytesFormat,
663+
BytesFormat, ParserSettings,
662664
};
663665
use cidr::IpCidr;
664666
use std::sync::LazyLock;
@@ -715,7 +717,7 @@ mod tests {
715717
impl FunctionDefinition for FilterFunction {
716718
fn check_param(
717719
&self,
718-
_: &FilterParser<'_>,
720+
_: &ParserSettings,
719721
params: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
720722
next_param: &FunctionParam<'_>,
721723
_: Option<&mut FunctionDefinitionContext>,

engine/src/ast/function_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<'s> FunctionCallExpr<'s> {
418418

419419
definition
420420
.check_param(
421-
parser,
421+
parser.settings(),
422422
&mut args.iter().map(|arg| arg.into()),
423423
&next_param,
424424
ctx.as_mut(),

engine/src/ast/parse.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,56 @@ impl Display for ParseError<'_> {
9696
}
9797
}
9898

99+
/// Parser settings.
100+
#[derive(Clone, Debug, PartialEq, Eq)]
101+
pub struct ParserSettings {
102+
/// Approximate size of the cache used by the DFA of a regex.
103+
/// Default: 10MB
104+
pub regex_dfa_size_limit: usize,
105+
/// Approximate size limit of the compiled regular expression.
106+
/// Default: 2MB
107+
pub regex_compiled_size_limit: usize,
108+
/// Maximum number of star metacharacters allowed in a wildcard.
109+
/// Default: unlimited
110+
pub wildcard_star_limit: usize,
111+
}
112+
113+
impl Default for ParserSettings {
114+
#[inline]
115+
fn default() -> Self {
116+
Self {
117+
// Default value extracted from the regex crate.
118+
regex_compiled_size_limit: 10 * (1 << 20),
119+
// Default value extracted from the regex crate.
120+
regex_dfa_size_limit: 2 * (1 << 20),
121+
wildcard_star_limit: usize::MAX,
122+
}
123+
}
124+
}
125+
99126
/// A structure used to drive parsing of an expression into a [`FilterAst`].
100127
#[derive(Clone, Debug, PartialEq, Eq)]
101128
pub struct FilterParser<'s> {
102129
pub(crate) scheme: &'s Scheme,
103-
pub(crate) regex_dfa_size_limit: usize,
104-
pub(crate) regex_compiled_size_limit: usize,
105-
pub(crate) wildcard_star_limit: usize,
130+
pub(crate) settings: ParserSettings,
106131
}
107132

108133
impl<'s> FilterParser<'s> {
109-
/// Creates a new parser with default configuration.
134+
/// Creates a new parser with default settings.
110135
#[inline]
111136
pub fn new(scheme: &'s Scheme) -> Self {
112137
Self {
113138
scheme,
114-
// Default value extracted from the regex crate.
115-
regex_compiled_size_limit: 10 * (1 << 20),
116-
// Default value extracted from the regex crate.
117-
regex_dfa_size_limit: 2 * (1 << 20),
118-
wildcard_star_limit: usize::MAX,
139+
settings: ParserSettings::default(),
119140
}
120141
}
121142

143+
/// Creates a new parser with the specified settings.
144+
#[inline]
145+
pub fn with_settings(scheme: &'s Scheme, settings: ParserSettings) -> Self {
146+
Self { scheme, settings }
147+
}
148+
122149
/// Returns the [`Scheme`](struct@Scheme) for which this parser has been constructor for.
123150
#[inline]
124151
pub fn scheme(&self) -> &'s Scheme {
@@ -143,39 +170,45 @@ impl<'s> FilterParser<'s> {
143170
complete(self.lex_as(input.trim())).map_err(|err| ParseError::new(input, err))
144171
}
145172

173+
/// Retrieve parser settings.
174+
#[inline]
175+
pub fn settings(&self) -> &ParserSettings {
176+
&self.settings
177+
}
178+
146179
/// Set the approximate size limit of the compiled regular expression.
147180
#[inline]
148181
pub fn regex_set_compiled_size_limit(&mut self, regex_compiled_size_limit: usize) {
149-
self.regex_compiled_size_limit = regex_compiled_size_limit;
182+
self.settings.regex_compiled_size_limit = regex_compiled_size_limit;
150183
}
151184

152185
/// Get the approximate size limit of the compiled regular expression.
153186
#[inline]
154187
pub fn regex_get_compiled_size_limit(&self) -> usize {
155-
self.regex_compiled_size_limit
188+
self.settings.regex_compiled_size_limit
156189
}
157190

158191
/// Set the approximate size of the cache used by the DFA of a regex.
159192
#[inline]
160193
pub fn regex_set_dfa_size_limit(&mut self, regex_dfa_size_limit: usize) {
161-
self.regex_dfa_size_limit = regex_dfa_size_limit;
194+
self.settings.regex_dfa_size_limit = regex_dfa_size_limit;
162195
}
163196

164197
/// Get the approximate size of the cache used by the DFA of a regex.
165198
#[inline]
166199
pub fn regex_get_dfa_size_limit(&self) -> usize {
167-
self.regex_dfa_size_limit
200+
self.settings.regex_dfa_size_limit
168201
}
169202

170203
/// Set the maximum number of star metacharacters allowed in a wildcard.
171204
#[inline]
172205
pub fn wildcard_set_star_limit(&mut self, wildcard_star_limit: usize) {
173-
self.wildcard_star_limit = wildcard_star_limit;
206+
self.settings.wildcard_star_limit = wildcard_star_limit;
174207
}
175208

176209
/// Get the maximum number of star metacharacters allowed in a wildcard.
177210
#[inline]
178211
pub fn wildcard_get_star_limit(&self) -> usize {
179-
self.wildcard_star_limit
212+
self.settings.wildcard_star_limit
180213
}
181214
}

engine/src/functions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
filter::CompiledValueResult,
33
types::{ExpectedType, ExpectedTypeList, GetType, LhsValue, RhsValue, Type, TypeMismatchError},
4-
FilterParser,
4+
ParserSettings,
55
};
66
use std::any::Any;
77
use std::convert::TryFrom;
@@ -371,7 +371,7 @@ pub trait FunctionDefinition: Debug + Send + Sync {
371371
/// correct. Return the expected the parameter definition.
372372
fn check_param(
373373
&self,
374-
parser: &FilterParser<'_>,
374+
settings: &ParserSettings,
375375
params: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
376376
next_param: &FunctionParam<'_>,
377377
ctx: Option<&mut FunctionDefinitionContext>,
@@ -460,7 +460,7 @@ pub struct SimpleFunctionDefinition {
460460
impl FunctionDefinition for SimpleFunctionDefinition {
461461
fn check_param(
462462
&self,
463-
_parser: &FilterParser<'_>,
463+
_settings: &ParserSettings,
464464
params: &mut dyn ExactSizeIterator<Item = FunctionParam<'_>>,
465465
next_param: &FunctionParam<'_>,
466466
_: Option<&mut FunctionDefinitionContext>,

engine/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub use self::{
8484
function_expr::{FunctionCallArgExpr, FunctionCallExpr},
8585
index_expr::IndexExpr,
8686
logical_expr::{LogicalExpr, LogicalOp, ParenthesizedExpr, UnaryOp},
87-
parse::{FilterParser, ParseError},
87+
parse::{FilterParser, ParseError, ParserSettings},
8888
visitor::{Visitor, VisitorMut},
8989
Expr, FilterAst, FilterValueAst, ValueExpr,
9090
},

engine/src/rhs_types/regex/imp_real.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ impl Regex {
1818
) -> Result<Self, Error> {
1919
::regex::bytes::RegexBuilder::new(pattern)
2020
.unicode(false)
21-
.size_limit(parser.regex_compiled_size_limit)
22-
.dfa_size_limit(parser.regex_dfa_size_limit)
21+
.size_limit(parser.settings.regex_compiled_size_limit)
22+
.dfa_size_limit(parser.settings.regex_dfa_size_limit)
2323
.build()
2424
.map(|r| Regex {
2525
compiled_regex: r,

engine/src/rhs_types/wildcard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<const STRICT: bool> Serialize for Wildcard<STRICT> {
126126
impl<'i, 's, const STRICT: bool> LexWith<'i, &FilterParser<'s>> for Wildcard<STRICT> {
127127
fn lex_with(input: &'i str, parser: &FilterParser<'s>) -> LexResult<'i, Wildcard<STRICT>> {
128128
lex_quoted_or_raw_string(input).and_then(|(pattern, rest)| {
129-
match Wildcard::new(pattern, parser.wildcard_star_limit) {
129+
match Wildcard::new(pattern, parser.settings.wildcard_star_limit) {
130130
Ok(wildcard) => Ok((wildcard, rest)),
131131
Err(err) => Err((LexErrorKind::ParseWildcard(err), input)),
132132
}

engine/src/scheme.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{
2-
ast::parse::{FilterParser, ParseError},
3-
ast::{FilterAst, FilterValueAst},
2+
ast::{
3+
parse::{FilterParser, ParseError, ParserSettings},
4+
FilterAst, FilterValueAst,
5+
},
46
functions::FunctionDefinition,
57
lex::{expect, span, take_while, Lex, LexErrorKind, LexResult, LexWith},
68
list_matcher::ListDefinition,
@@ -536,6 +538,16 @@ impl<'s> Scheme {
536538
})
537539
}
538540

541+
/// Creates a new parser with default settings.
542+
pub fn parser(&self) -> FilterParser<'_> {
543+
FilterParser::new(self)
544+
}
545+
546+
/// Creates a new parser with the specified settings.
547+
pub fn parser_with_settings(&self, settings: ParserSettings) -> FilterParser<'_> {
548+
FilterParser::with_settings(self, settings)
549+
}
550+
539551
/// Parses a filter expression into an AST form.
540552
pub fn parse<'i>(&'s self, input: &'i str) -> Result<FilterAst<'s>, ParseError<'i>> {
541553
FilterParser::new(self).parse(input)

0 commit comments

Comments
 (0)