Skip to content

Commit

Permalink
feat: allow the use of the {,n} syntax in patterns (#41)
Browse files Browse the repository at this point in the history
The implementation of this feature for regexps used with the `match` operator is still pending.
  • Loading branch information
plusvic authored Sep 15, 2023
1 parent 32a7bd7 commit a6fa757
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
74 changes: 50 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ lazy_static = "1.4.0"
line-span = "0.1.3"
linkme = "0.3"
log = "0.4"
memchr = "2.5.0"
memchr = "2.6.3"
memx = "0.1.28"
num = "0.4.0"
pest = "2.5.5"
Expand All @@ -55,8 +55,8 @@ pretty_assertions = "1.3.0"
protobuf = "3.2.0"
protobuf-codegen = "3.2.0"
protobuf-parse = "3.2.0"
regex = "1.9.1"
regex-syntax = "0.7.4"
regex = { git = "https://github.com/plusvic/regex.git", rev="423493d" }
regex-syntax = { git = "https://github.com/plusvic/regex.git", rev="423493d" }
rustc-hash = "1.1.0"
smallvec = "1.10.0"
serde = "1.0.156"
Expand Down
1 change: 1 addition & 0 deletions yara-x/src/compiler/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ impl Rules {
/// If no regular expression with such [`RegexpId`] exists.
#[inline]
pub(crate) fn get_regexp(&self, regexp_id: RegexpId) -> Regex {
// TODO: when compiling regexps allow the use of `{,n}` syntax
let re = Regexp::new(self.regexp_pool.get(regexp_id).unwrap());
RegexBuilder::new(re.naked())
.case_insensitive(re.case_insensitive())
Expand Down
10 changes: 6 additions & 4 deletions yara-x/src/re/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ impl Parser {

/// Parses the regexp and returns its HIR.
pub fn parse(&self, regexp: &ast::Regexp) -> Result<Hir, Error> {
let ast = re::ast::parse::Parser::new().parse(regexp.src).map_err(
|err| Error::SyntaxError {
let mut parser =
re::ast::parse::ParserBuilder::new().empty_min_range(true).build();

let ast =
parser.parse(regexp.src).map_err(|err| Error::SyntaxError {
msg: err.kind().to_string(),
span: *err.span(),
},
)?;
})?;

let greedy = Validator::new().validate(&ast);

Expand Down
3 changes: 3 additions & 0 deletions yara-x/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ fn regexp_patterns_2() {
pattern_match!(r#"/ab{2,3}c/"#, b"abbbc", b"abbbc");
pattern_match!(r#"/ab{2,3}?c/"#, b"abbbc", b"abbbc");
pattern_match!(r#"/ab{0,1}?c/"#, b"abc", b"abc");
pattern_match!(r#"/ab{,1}?c/"#, b"abc", b"abc");
pattern_match!(r#"/a{0,1}bc/"#, b"bbc", b"bc");
pattern_match!(r#"/ab{0,}c/"#, b"ac", b"ac");
pattern_match!(r#"/ab{0,}c/"#, b"abc", b"abc");
Expand All @@ -935,7 +936,9 @@ fn regexp_patterns_2() {
pattern_false!(r#"/ab{1,}b/"#, b"ab");
pattern_match!(r#"/ab{1,1}c/"#, b"abc", b"abc");
pattern_match!(r#"/ab{0,3}c/"#, b"abbbc", b"abbbc");
pattern_match!(r#"/ab{,3}c/"#, b"abbbc", b"abbbc");
pattern_false!(r#"/ab{0,2}c/"#, b"abbbc");
pattern_false!(r#"/ab{,2}c/"#, b"abbbc");
pattern_false!(r#"/ab{4,5}c/"#, b"abbbc");
pattern_false!(r#"/ab{3}c/"#, b"abbbbc");
pattern_false!(r#"/ab{4}c/"#, b"abbbbbc");
Expand Down

0 comments on commit a6fa757

Please sign in to comment.