Skip to content

Commit

Permalink
add better variable support
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Jan 23, 2024
1 parent 01838ff commit 967b615
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions example.yar
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ rule test
$a = "foo"
$b = "bar"
condition:
$a and
$b
$a or
$b and true
}
23 changes: 16 additions & 7 deletions src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,34 @@ fn condition(p: &mut Parser) {
m.complete(p, CONDITION);
}

const VARIABLE_RECOVERY_SET: TokenSet = TokenSet::new(&[VARIABLE]);

pub(super) fn strings_body(p: &mut Parser) {
// add support for meta also
while !p.at(EOF) && !p.at(STRINGS) && !p.at(CONDITION) && !p.at(RBRACE) {
assert!(p.at(VARIABLE));
let m = p.start();
p.bump(VARIABLE);
if p.at(VARIABLE) {
let m = p.start();
p.bump(VARIABLE);
m.complete(p, VARIABLE);
} else {
p.err_recover("expected a variable", VARIABLE_RECOVERY_SET);
}
p.expect(ASSIGN);
// so far only strings are supported, later add match for hex strings and regex
string(p);
m.complete(p, VARIABLE);
m.complete(p, VARIABLE_STMT);
}
}

// do the same for hex and regex strings
// add support for hex and regex strings later on
fn string(p: &mut Parser) {
assert!(p.at(STRING));
let m = p.start();
p.bump(STRING);
// add plain string modifiers
match p.current() {
STRING => p.bump(STRING),
_ => p.err_and_bump("expected a string"),
}
// add string modifiers
m.complete(p, STRING);
}

Expand Down
13 changes: 10 additions & 3 deletions src/parser/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
Some(m.complete(p, LITERAL))
}

const EXPR_RECOVERY_SET: TokenSet = TokenSet::new(&[VARIABLE, TRUE, FALSE]);

// add support for while/for loops, if/else statements, etc.
pub(super) fn atom_expr(p: &mut Parser) -> Option<CompletedMarker> {
if let Some(m) = literal(p) {
Some(m)
} else {
todo!("add support for other atoms")
return Some(m);
}

let _done = match p.current() {
_ => {
p.err_recover("expected expression", EXPR_RECOVERY_SET);
return None;
}
};
}
1 change: 1 addition & 0 deletions src/parser/syntaxkind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum SyntaxKind {
LITERAL,
EXPRESSION,
EXPRESSION_STMT,
VARIABLE_STMT,
__LAST,
}

Expand Down

0 comments on commit 967b615

Please sign in to comment.