Skip to content

Commit

Permalink
fix: minor error changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Apr 17, 2024
1 parent f501ca0 commit a0d14e3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
/// It should provide also token for whitespaces
/// as we want full fidelity and error resilience.;
use crate::syntax::{
syntax_error::SyntaxError, syntax_node::SyntaxNode, text_token_source::TextTokenSource,
text_tree_sink::TextTreeSink,
syntax_error::SyntaxError, text_token_source::TextTokenSource, text_tree_sink::TextTreeSink,
};

pub use crate::parser::SyntaxKind;
pub use crate::syntax::ast::*;
pub use crate::syntax::syntax_node::{SyntaxToken, YARALanguage};
pub use crate::syntax::syntax_node::{SyntaxNode, SyntaxToken, YARALanguage};
pub use crate::syntax::Parse;
pub use crate::syntax::SourceFile;
pub use rowan_test::{NodeOrToken, WalkEvent};

// use only for tests
#[cfg(test)]
use rowan_test::{NodeOrToken, WalkEvent};
#[cfg(test)]
use std::fs;
#[cfg(test)]
use std::io::Write;
Expand Down Expand Up @@ -269,11 +268,12 @@ fn api_walktrough() {

// There are some errors
assert!(!parse_struct.errors().is_empty());
assert!(parse_struct.errors().len() == 4);
assert!(parse_struct.errors()[0].to_string() == "expected a variable");
assert!(parse_struct.errors().len() == 6);
assert!(
parse_struct.errors()[1].to_string() == "expected meta, strings or condition keyword"
parse_struct.errors()[0].to_string()
== "expected a new pattern statement or pattern modifier"
);
assert!(parse_struct.errors()[3].to_string() == "invalid yara expression");

// We still have the AST and we can traverse it
let ast = parse_struct.tree();
Expand Down Expand Up @@ -308,13 +308,13 @@ fn api_walktrough() {
}
// We can also search a token that produced the error
// Even though it produces range, ParseErrors only supports text offsets
assert_eq!(parse_struct.errors()[1].range(), TextRange::new(173.into(), 173.into()));
assert_eq!(parse_struct.errors()[3].range(), TextRange::new(173.into(), 173.into()));

// But luckily we can obtain the token at the offset
// and from it we can get both its text and length
let tkn = ast
.syntax()
.token_at_offset(parse_struct.errors()[1].range().start())
.token_at_offset(parse_struct.errors()[3].range().start())
.right_biased()
.unwrap();

Expand Down
56 changes: 29 additions & 27 deletions src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const PATTERN_MODIFIERS_SET: TokenSet = TokenSet::new(&[
/// `rule_body` and `block_expr`.
pub(crate) fn block_expr(p: &mut Parser) {
if !p.at(T!['{']) {
p.error("expected a block expression");
p.error("expected a block expression or rule tags");
return;
}
let m = p.start();
Expand Down Expand Up @@ -66,11 +66,15 @@ pub(super) fn rule_body(p: &mut Parser) {
has_condition = true;
}
_ => {
// It did not contain strings or condition in valid form
// but we can still try to parse their body and throw an error for parent
// for now it just looks at next 2 tokens to differenciate between valid strings
// body or condition body. This should probably be adjusted later
p.err_and_bump("expected meta, strings or condition keyword");
if has_condition {
p.err_and_bump("invalid yara expression");
} else {
// It did not contain strings or condition in valid form
// but we can still try to parse their body and throw an error for parent
// for now it just looks at next 2 tokens to differenciate between valid strings
// body or condition body. This should probably be adjusted later
p.err_and_bump("expected meta, strings or condition keyword");
}
}
}
}
Expand Down Expand Up @@ -142,31 +146,29 @@ pub(super) fn strings_body(p: &mut Parser) {

if p.at(T![variable]) {
p.bump(T![variable]);
} else {
p.err_and_bump("expected a variable");
}
p.expect(T![=]);

// so far only strings are supported, later add match for hex strings and regex
let n = p.start();
match p.current() {
STRING_LIT => p.bump(STRING_LIT),
T!['{'] => hex_pattern(p),
T![/] => regex_pattern(p),
_ => {
p.err_and_bump("expected a valid string");
while !p.at(T!['}']) {
p.bump_any();
p.expect(T![=]);

// so far only strings are supported, later add match for hex strings and regex
let n = p.start();
match p.current() {
STRING_LIT => p.bump(STRING_LIT),
T!['{'] => hex_pattern(p),
T![/] => regex_pattern(p),
_ => {
p.err_and_bump("expected a valid string pattern");
}
p.bump_any();
}
}
if p.at_ts(PATTERN_MODIFIERS_SET) {
string_modifiers(p);
}
n.complete(p, PATTERN);
if p.at_ts(PATTERN_MODIFIERS_SET) {
string_modifiers(p);
}
n.complete(p, PATTERN);

m.complete(p, VARIABLE_STMT);
m.complete(p, VARIABLE_STMT);
} else {
m.abandon(p);
p.err_and_bump("expected a new pattern statement or pattern modifier");
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ pub(super) fn process_top_level(p: &mut Parser, stop_on_r_brace: bool) {
m.abandon(p);
match p.current() {
T!['{'] => {
error_block(p, "expected an item");
error_block(p, "expected an import statement, include statement or a rule");
}
T!['}'] if !stop_on_r_brace => {
let e = p.start();
p.error("unmatched }");
p.bump(T!['}']);
e.complete(p, ERROR);
}
EOF | T!['}'] => p.error("expected an item"),
_ => p.err_and_bump("expected an item"),
EOF | T!['}'] => p.error("invalid rule body"),
_ => p.err_and_bump("expected an import statement, include statement or a rule"),
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/test4.err
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SyntaxError("expected a variable", 98..98)
SyntaxError("expected a new pattern statement or pattern modifier", 98..98)
SyntaxError("expected a new pattern statement or pattern modifier", 100..100)
SyntaxError("expected a new pattern statement or pattern modifier", 102..102)
14 changes: 7 additions & 7 deletions tests/test4.out
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ [email protected]
[email protected] "strings"
[email protected] ":"
[email protected] "\n\t\t"
VARIABLE_STMT@98..107
ERROR@98..99
[email protected] "a"
[email protected] " "
ERROR@98..99
IDENTIFIER@98..99 "a"
[email protected] " "
[email protected]
[email protected] "="
[email protected] " "
PATTERN@102..107
[email protected] "\"foo\""
[email protected] " "
ERROR@102..107
[email protected] "\"foo\""
[email protected] "\n\t\t"
[email protected]
[email protected] "$b"
Expand Down
6 changes: 3 additions & 3 deletions tests/test6.err
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SyntaxError("expected meta, strings or condition keyword", 106..106)
SyntaxError("expected meta, strings or condition keyword", 114..114)
SyntaxError("expected meta, strings or condition keyword", 117..117)
SyntaxError("expected meta, strings or condition keyword", 119..119)
SyntaxError("expected meta, strings or condition keyword", 141..141)
SyntaxError("expected meta, strings or condition keyword", 147..147)
SyntaxError("expected meta, strings or condition keyword", 150..150)
SyntaxError("invalid yara expression", 141..141)
SyntaxError("invalid yara expression", 147..147)
SyntaxError("invalid yara expression", 150..150)
SyntaxError("Invalid character", 98..99)

0 comments on commit a0d14e3

Please sign in to comment.