Skip to content

Commit

Permalink
binary not and some logic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Mar 25, 2024
1 parent a673471 commit 4690cfd
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 30 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn test_parse_text() {
// Path to the .in.zip file.
let path = entry.into_path();
let display_path = path.display();
println!("{:?}", display_path);

let input = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Failed to read input file {:?}", display_path));
Expand All @@ -359,10 +360,8 @@ fn test_parse_text() {
.map(|error| format!("{:?}", error))
.collect::<Vec<_>>()
.join("\n");
print!("{:?}", display_path);
assert_eq!(actual_errors, expected_errors);
} else {
print!("{:?}", display_path);
assert!(ast_struct.errors().is_empty(), "Unexpected errors: {:?}", ast_struct.errors());
}
}
Expand Down
75 changes: 47 additions & 28 deletions src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ fn boolean_term(p: &mut Parser) -> Option<CompletedMarker> {
for_expr(p);
}
_ => {
let mut primary_expr_len = primary_expr_length(p, 0);
let mut parentheses_count = 0;
let mut primary_expr_len = primary_expr_length(p, 0, &mut parentheses_count);
if parentheses_count != 0 {
primary_expr_len = 0;
}

// If there is percatage sign after primary expression we need to bump one
// more token to check for "of" keyword
Expand Down Expand Up @@ -541,8 +545,8 @@ fn term(p: &mut Parser) -> Option<CompletedMarker> {
while p.at(T![,]) {
p.bump(T![,]);
expr(p, None, 1);
p.expect(T![')']);
}
p.expect(T![')']);
}
m.complete(p, FUNCTION_CALL_EXPR)
}
Expand Down Expand Up @@ -582,6 +586,10 @@ fn primary_expr(p: &mut Parser) -> Option<CompletedMarker> {
p.bump(T![-]);
term(p);
}
T![~] => {
p.bump(T![~]);
term(p);
}
T!['('] => {
p.bump(T!['(']);
expr(p, None, 1);
Expand Down Expand Up @@ -766,28 +774,25 @@ fn variable_wildcard(p: &mut Parser) {
m.complete(p, VARIABLE_WILDCARD);
}

fn primary_expr_length(p: &mut Parser, mut len: usize) -> usize {
len += match p.current() {
fn primary_expr_length(p: &mut Parser, len: usize, parentheses_count: &mut i32) -> usize {
match p.nth(len) {
T![float_lit]
| T![int_lit]
| T![string_lit]
| T![identifier]
| T![filesize]
| T![entrypoint] => 1,
| T![entrypoint] => len + 1,
T![/] => regex_pattern_length(p, len),
T![-] => term_length(p, len),
T!['('] => expr_length(p, len),
_ => 0,
};
len
T![-] => term_length(p, len + 1, parentheses_count),
T!['('] => expr_length(p, len, parentheses_count),
_ => len,
}
}

fn regex_pattern_length(p: &mut Parser, mut len: usize) -> usize {
// Check if the pattern starts with `/` and ends with `/`
if p.nth(len) == T![/] && p.nth(len + 1) == REGEX_LIT && p.nth(len + 2) == T![/] {
len += 3;
} else {
return 0;
}

// Check for regex specific modifiers
Expand All @@ -798,20 +803,40 @@ fn regex_pattern_length(p: &mut Parser, mut len: usize) -> usize {
len
}

fn term_length(p: &mut Parser, mut len: usize) -> usize {
len += primary_expr_length(p, len);
len
fn term_length(p: &mut Parser, mut len: usize, parentheses_count: &mut i32) -> usize {
len = primary_expr_length(p, len, parentheses_count);

match p.nth(len) {
T!['['] => {
len += 1;
len = expr_length(p, len, parentheses_count);
len + 1
}
T!['('] => {
len += 1;
if p.nth(len) == T![')'] {
len + 1
} else {
len = expr_length(p, len, parentheses_count);
while p.nth(len) == T![,] {
len += 1;
len = expr_length(p, len, parentheses_count);
}
len + 1
}
}
_ => len,
}
}

fn expr_length(p: &mut Parser, mut len: usize) -> usize {
fn expr_length(p: &mut Parser, mut len: usize, parentheses_count: &mut i32) -> usize {
// Check if the expression starts with `(`
if p.nth(len) == T!['('] {
len += 1;
} else {
return 0;
*parentheses_count += 1;
}

len = term_length(p, len);
len = term_length(p, len, parentheses_count);

while p.nth(len) == T![|]
|| p.nth(len) == T![^]
Expand All @@ -826,20 +851,14 @@ fn expr_length(p: &mut Parser, mut len: usize) -> usize {
|| p.nth(len) == T![.]
{
len += 1;
len += expr_length(p, len);

len = expr_length(p, len, parentheses_count);
}

// Check if the expression ends with `)`
len += 1;
if p.nth(len) == T![')'] {
len += 1;
} else {
return 0;
}

// Ensure that the expression is not empty
if len == 2 {
return 0;
*parentheses_count -= 1;
}

len
Expand Down
3 changes: 3 additions & 0 deletions src/syntax/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,9 @@ impl PrimaryExpr {
pub fn term(&self) -> Option<Term> {
support::child(&self.syntax)
}
pub fn tilde_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![~])
}
pub fn l_paren_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T!['('])
}
Expand Down
7 changes: 7 additions & 0 deletions tests/test42.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule test
{
strings:
$a = "foo"
condition:
-1 or $a
}
7 changes: 7 additions & 0 deletions tests/test43.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rule test
{
strings:
$a = "foo"
condition:
~1 or $a
}
1 change: 1 addition & 0 deletions yara.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ PrimaryExpr =
| 'entrypoint'
| RegexPattern
| '-' Term
| '~' Term
| '(' Expr ')'

IndexingExpr =
Expand Down

0 comments on commit 4690cfd

Please sign in to comment.