Skip to content

Commit

Permalink
fix strings and conditions redefinition and grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Jan 29, 2024
1 parent 22eedaf commit 9623d2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,28 @@ pub(crate) fn block_expr(p: &mut Parser) {
}

pub(super) fn rule_body(p: &mut Parser) {
let mut has_strings = false;
let mut has_condition = false;
while !p.at(EOF) && !p.at(RBRACE) {
match p.current() {
// add metadata later
STRINGS => strings(p),
CONDITION => condition(p),
STRINGS => {
if has_strings {
p.error("only one strings block is allowed");
}
if has_condition {
p.error("strings block must come before condition block");
}
strings(p);
has_strings = true;
}
CONDITION => {
if has_condition {
p.error("only one condition block is allowed");
}
condition(p);
has_condition = true;
}
_ => {
p.err_and_bump("expected strings or condition");
}
Expand Down
1 change: 0 additions & 1 deletion src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ impl<'t> Parser<'t> {

pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) {
if self.at_ts(recovery) {
println!("recovery: {:?}", self.current());
self.error(message);
return;
}
Expand Down
7 changes: 4 additions & 3 deletions yara_subset.grammar
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
SOURCE -> RULE | eps.
RULE -> rule identifier lbrace RULEBODY rbrace.
RULEBODY -> STRINGS RULEBODY | CONDITION RULEBODY | eps.
RULEBODY -> STRINGS CONDITION | CONDITION .
STRINGS -> string colon STRINGSBODY.
CONDITION -> condition colon CONDITIONBODY.
STRINGSBODY -> variable assign string STRINGSBODY | eps.
CONDITIONBODY -> LITERAL CONDITIONBODY | OPERATOR CONDITIONBODY | eps.
CONDITIONBODY -> LITERAL EXPRESSION | OPERATOR EXPRESSION .
EXPRESSION -> LITERAL EXPRESSION | OPERATOR EXPRESSION | eps.
LITERAL -> variable | BOOLEAN.
BOOLEAN -> true | false.
OPERATOR -> and | or | not.

// https://smlweb.cpsc.ucalgary.ca/vital-stats.php?grammar=SOURCE+-%3E+RULE+%7C+eps.%0D%0ARULE+-%3E+rule+identifier+lbrace+RULEBODY+rbrace.%0D%0ARULEBODY+-%3E+STRINGS+%7C+CONDITION+%7C+eps.%0D%0ASTRINGS+-%3E+string+colon+STRINGSBODY.%0D%0ACONDITION+-%3E+condition+colon+CONDITIONBODY.%0D%0ASTRINGSBODY+-%3E+variable+assign+string+STRINGSBODY+%7C+eps.%0D%0ACONDITIONBODY+-%3E+LITERAL+EXPRESSION+%7C+OPERATOR+EXPRESSION+%7C+eps.%0D%0ALITERAL+-%3E+variable+%7C+BOOLEAN.%0D%0ABOOLEAN+-%3E+true+%7C+false.%0D%0AOPERATOR+-%3E+and+%7C+or+%7C+not.
// https://smlweb.cpsc.ucalgary.ca/vital-stats.php?grammar=SOURCE+-%3E+RULE+%7C+eps.%0D%0ARULE+-%3E+rule+identifier+lbrace+RULEBODY+rbrace.%0D%0ARULEBODY+-%3E+STRINGS+CONDITION+%7C+CONDITION+.%0D%0ASTRINGS+-%3E+string+colon+STRINGSBODY.%0D%0ACONDITION+-%3E+condition+colon+CONDITIONBODY.%0D%0ASTRINGSBODY+-%3E+variable+assign+string+STRINGSBODY+%7C+eps.%0D%0ACONDITIONBODY+-%3E+LITERAL+EXPRESSION+%7C+OPERATOR+EXPRESSION+.%0D%0AEXPRESSION+-%3E+LITERAL+EXPRESSION+%7C+OPERATOR+EXPRESSION+%7C+eps.%0D%0ALITERAL+-%3E+variable+%7C+BOOLEAN.%0D%0ABOOLEAN+-%3E+true+%7C+false.%0D%0AOPERATOR+-%3E+and+%7C+or+%7C+not.

0 comments on commit 9623d2b

Please sign in to comment.