Skip to content

Commit

Permalink
for expression
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Mar 25, 2024
1 parent ba84cfe commit 8ef9667
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub(crate) enum LogosToken {
In,
#[token("of")]
Of,
#[token("for")]
For,
#[token("all")]
All,
#[token("any")]
Expand Down Expand Up @@ -298,6 +300,7 @@ fn logos_tokenkind_to_syntaxkind(token: LogosToken) -> SyntaxKind {
LogosToken::At => SyntaxKind::AT_KW,
LogosToken::In => SyntaxKind::IN_KW,
LogosToken::Of => SyntaxKind::OF_KW,
LogosToken::For => SyntaxKind::FOR_KW,
LogosToken::All => SyntaxKind::ALL_KW,
LogosToken::Any => SyntaxKind::ANY_KW,
LogosToken::None => SyntaxKind::NONE_KW,
Expand Down
71 changes: 70 additions & 1 deletion src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ fn boolean_term(p: &mut Parser) -> Option<CompletedMarker> {
p.bump(T![defined]);
boolean_term(p);
}

T![for] => {
for_expr(p);
}
_ => {
let mut primary_expr_len = primary_expr_length(p, 0);

Expand Down Expand Up @@ -614,6 +616,29 @@ fn of_expr(p: &mut Parser) {
m.complete(p, OF_EXPR);
}

fn for_expr(p: &mut Parser) {
let m = p.start();
p.expect(T![for]);
quantifier(p);
if p.at(T![of]) {
p.bump(T![of]);
if p.at(T![them]) {
p.bump(T![them]);
} else {
pattern_ident_tupple(p);
}
} else {
ident_tuple(p);
p.expect(T![in]);
iterable(p);
}
p.expect(T![:]);
p.expect(T!['(']);
boolean_expr(p, None, 1);
p.expect(T![')']);
m.complete(p, FOR_EXPR);
}

fn quantifier(p: &mut Parser) {
let m = p.start();
match p.current() {
Expand All @@ -636,6 +661,34 @@ fn quantifier(p: &mut Parser) {
m.complete(p, QUANTIFIER);
}

fn iterable(p: &mut Parser) {
let m = p.start();
match p.current() {
T!['('] => {
let n = p.start();
p.bump(T!['(']);
expr(p, None, 1);
if p.at(T![..]) {
p.bump(T![..]);
expr(p, None, 1);
p.expect(T![')']);
n.complete(p, RANGE);
} else {
while p.at(T![,]) {
p.bump(T![,]);
expr(p, None, 1);
}
p.expect(T![')']);
n.complete(p, EXPR_TUPLE);
}
}
_ => {
expr(p, None, 1);
}
}
m.complete(p, ITERABLE);
}

fn boolean_expr_tuple(p: &mut Parser) {
let m = p.start();
p.expect(T!['(']);
Expand All @@ -660,6 +713,22 @@ fn pattern_ident_tupple(p: &mut Parser) {
m.complete(p, PATTERN_IDENT_TUPLE);
}

fn ident_tuple(p: &mut Parser) {
let m = p.start();
p.expect(T![identifier]);
m.complete(p, IDENTIFIER_NODE);
while p.at(T![,]) {
ident_list(p);
}
}

fn ident_list(p: &mut Parser) {
let m = p.start();
p.expect(T![,]);
p.expect(T![identifier]);
m.complete(p, IDENTIFIER_NODE);
}

fn variable_wildcard(p: &mut Parser) {
let m = p.start();
p.expect(T![variable]);
Expand Down
9 changes: 8 additions & 1 deletion src/parser/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub enum SyntaxKind {
ALL_KW,
ANY_KW,
NONE_KW,
FOR_KW,
STRING_LIT,
INT_LIT,
FLOAT_LIT,
Expand Down Expand Up @@ -124,10 +125,14 @@ pub enum SyntaxKind {
VARIABLE_ANCHOR,
RANGE,
OF_EXPR,
FOR_EXPR,
QUANTIFIER,
PATTERN_IDENT_TUPLE,
BOOLEAN_EXPR_TUPLE,
VARIABLE_WILDCARD,
EXPR_TUPLE,
IDENTIFIER_NODE,
ITERABLE,
#[doc(hidden)]
__LAST,
}
Expand Down Expand Up @@ -172,6 +177,7 @@ impl SyntaxKind {
| ALL_KW
| ANY_KW
| NONE_KW
| FOR_KW
)
}
pub fn is_punct(self) -> bool {
Expand Down Expand Up @@ -249,6 +255,7 @@ impl SyntaxKind {
"all" => ALL_KW,
"any" => ANY_KW,
"none" => NONE_KW,
"for" => FOR_KW,
_ => return None,
};
Some(kw)
Expand Down Expand Up @@ -283,5 +290,5 @@ impl SyntaxKind {
}
}
#[macro_export]
macro_rules ! T { [:] => { $ crate :: SyntaxKind :: COLON } ; ['('] => { $ crate :: SyntaxKind :: L_PAREN } ; [')'] => { $ crate :: SyntaxKind :: R_PAREN } ; ['{'] => { $ crate :: SyntaxKind :: L_BRACE } ; ['}'] => { $ crate :: SyntaxKind :: R_BRACE } ; ['['] => { $ crate :: SyntaxKind :: L_BRACKET } ; [']'] => { $ crate :: SyntaxKind :: R_BRACKET } ; [,] => { $ crate :: SyntaxKind :: COMMA } ; [=] => { $ crate :: SyntaxKind :: ASSIGN } ; [-] => { $ crate :: SyntaxKind :: HYPHEN } ; [?] => { $ crate :: SyntaxKind :: QUESTION_MARK } ; [~] => { $ crate :: SyntaxKind :: TILDE } ; [|] => { $ crate :: SyntaxKind :: PIPE } ; [/] => { $ crate :: SyntaxKind :: SLASH } ; [+] => { $ crate :: SyntaxKind :: PLUS } ; [*] => { $ crate :: SyntaxKind :: STAR } ; [%] => { $ crate :: SyntaxKind :: PERCENTAGE } ; [<<] => { $ crate :: SyntaxKind :: SHL } ; [>>] => { $ crate :: SyntaxKind :: SHR } ; [&] => { $ crate :: SyntaxKind :: AMPERSAND } ; [^] => { $ crate :: SyntaxKind :: CARET } ; [.] => { $ crate :: SyntaxKind :: DOT } ; [==] => { $ crate :: SyntaxKind :: EQ } ; [!=] => { $ crate :: SyntaxKind :: NE } ; [<] => { $ crate :: SyntaxKind :: LT } ; [<=] => { $ crate :: SyntaxKind :: LE } ; [>] => { $ crate :: SyntaxKind :: GT } ; [>=] => { $ crate :: SyntaxKind :: GE } ; [..] => { $ crate :: SyntaxKind :: DOTDOT } ; [and] => { $ crate :: SyntaxKind :: AND_KW } ; [or] => { $ crate :: SyntaxKind :: OR_KW } ; [not] => { $ crate :: SyntaxKind :: NOT_KW } ; [rule] => { $ crate :: SyntaxKind :: RULE_KW } ; [strings] => { $ crate :: SyntaxKind :: STRINGS_KW } ; [condition] => { $ crate :: SyntaxKind :: CONDITION_KW } ; [meta] => { $ crate :: SyntaxKind :: META_KW } ; [private] => { $ crate :: SyntaxKind :: PRIVATE_KW } ; [global] => { $ crate :: SyntaxKind :: GLOBAL_KW } ; [import] => { $ crate :: SyntaxKind :: IMPORT_KW } ; [include] => { $ crate :: SyntaxKind :: INCLUDE_KW } ; [ascii] => { $ crate :: SyntaxKind :: ASCII_KW } ; [wide] => { $ crate :: SyntaxKind :: WIDE_KW } ; [nocase] => { $ crate :: SyntaxKind :: NOCASE_KW } ; [fullword] => { $ crate :: SyntaxKind :: FULLWORD_KW } ; [xor] => { $ crate :: SyntaxKind :: XOR_KW } ; [base64] => { $ crate :: SyntaxKind :: BASE64_KW } ; [base64wide] => { $ crate :: SyntaxKind :: BASE64WIDE_KW } ; [contains] => { $ crate :: SyntaxKind :: CONTAINS_KW } ; [icontains] => { $ crate :: SyntaxKind :: ICONTAINS_KW } ; [startswith] => { $ crate :: SyntaxKind :: STARTSWITH_KW } ; [istartswith] => { $ crate :: SyntaxKind :: ISTARTSWITH_KW } ; [endswith] => { $ crate :: SyntaxKind :: ENDSWITH_KW } ; [iendswith] => { $ crate :: SyntaxKind :: IENDSWITH_KW } ; [iequals] => { $ crate :: SyntaxKind :: IEQUALS_KW } ; [matches] => { $ crate :: SyntaxKind :: MATCHES_KW } ; [defined] => { $ crate :: SyntaxKind :: DEFINED_KW } ; [filesize] => { $ crate :: SyntaxKind :: FILESIZE_KW } ; [entrypoint] => { $ crate :: SyntaxKind :: ENTRYPOINT_KW } ; [at] => { $ crate :: SyntaxKind :: AT_KW } ; [in] => { $ crate :: SyntaxKind :: IN_KW } ; [of] => { $ crate :: SyntaxKind :: OF_KW } ; [them] => { $ crate :: SyntaxKind :: THEM_KW } ; [all] => { $ crate :: SyntaxKind :: ALL_KW } ; [any] => { $ crate :: SyntaxKind :: ANY_KW } ; [none] => { $ crate :: SyntaxKind :: NONE_KW } ; [identifier] => { $ crate :: SyntaxKind :: IDENTIFIER } ; [variable] => { $ crate :: SyntaxKind :: VARIABLE } ; [string_lit] => { $ crate :: SyntaxKind :: STRING_LIT } ; [int_lit] => { $ crate :: SyntaxKind :: INT_LIT } ; [float_lit] => { $ crate :: SyntaxKind :: FLOAT_LIT } ; [bool_lit] => { $ crate :: SyntaxKind :: BOOL_LIT } ; [hex_lit] => { $ crate :: SyntaxKind :: HEX_LIT } ; [regex_lit] => { $ crate :: SyntaxKind :: REGEX_LIT } ; [dot_matches_all] => { $ crate :: SyntaxKind :: DOT_MATCHES_ALL } ; [case_insensitive] => { $ crate :: SyntaxKind :: CASE_INSENSITIVE } ; [backslash] => { $ crate :: SyntaxKind :: BACKSLASH } ; }
macro_rules ! T { [:] => { $ crate :: SyntaxKind :: COLON } ; ['('] => { $ crate :: SyntaxKind :: L_PAREN } ; [')'] => { $ crate :: SyntaxKind :: R_PAREN } ; ['{'] => { $ crate :: SyntaxKind :: L_BRACE } ; ['}'] => { $ crate :: SyntaxKind :: R_BRACE } ; ['['] => { $ crate :: SyntaxKind :: L_BRACKET } ; [']'] => { $ crate :: SyntaxKind :: R_BRACKET } ; [,] => { $ crate :: SyntaxKind :: COMMA } ; [=] => { $ crate :: SyntaxKind :: ASSIGN } ; [-] => { $ crate :: SyntaxKind :: HYPHEN } ; [?] => { $ crate :: SyntaxKind :: QUESTION_MARK } ; [~] => { $ crate :: SyntaxKind :: TILDE } ; [|] => { $ crate :: SyntaxKind :: PIPE } ; [/] => { $ crate :: SyntaxKind :: SLASH } ; [+] => { $ crate :: SyntaxKind :: PLUS } ; [*] => { $ crate :: SyntaxKind :: STAR } ; [%] => { $ crate :: SyntaxKind :: PERCENTAGE } ; [<<] => { $ crate :: SyntaxKind :: SHL } ; [>>] => { $ crate :: SyntaxKind :: SHR } ; [&] => { $ crate :: SyntaxKind :: AMPERSAND } ; [^] => { $ crate :: SyntaxKind :: CARET } ; [.] => { $ crate :: SyntaxKind :: DOT } ; [==] => { $ crate :: SyntaxKind :: EQ } ; [!=] => { $ crate :: SyntaxKind :: NE } ; [<] => { $ crate :: SyntaxKind :: LT } ; [<=] => { $ crate :: SyntaxKind :: LE } ; [>] => { $ crate :: SyntaxKind :: GT } ; [>=] => { $ crate :: SyntaxKind :: GE } ; [..] => { $ crate :: SyntaxKind :: DOTDOT } ; [and] => { $ crate :: SyntaxKind :: AND_KW } ; [or] => { $ crate :: SyntaxKind :: OR_KW } ; [not] => { $ crate :: SyntaxKind :: NOT_KW } ; [rule] => { $ crate :: SyntaxKind :: RULE_KW } ; [strings] => { $ crate :: SyntaxKind :: STRINGS_KW } ; [condition] => { $ crate :: SyntaxKind :: CONDITION_KW } ; [meta] => { $ crate :: SyntaxKind :: META_KW } ; [private] => { $ crate :: SyntaxKind :: PRIVATE_KW } ; [global] => { $ crate :: SyntaxKind :: GLOBAL_KW } ; [import] => { $ crate :: SyntaxKind :: IMPORT_KW } ; [include] => { $ crate :: SyntaxKind :: INCLUDE_KW } ; [ascii] => { $ crate :: SyntaxKind :: ASCII_KW } ; [wide] => { $ crate :: SyntaxKind :: WIDE_KW } ; [nocase] => { $ crate :: SyntaxKind :: NOCASE_KW } ; [fullword] => { $ crate :: SyntaxKind :: FULLWORD_KW } ; [xor] => { $ crate :: SyntaxKind :: XOR_KW } ; [base64] => { $ crate :: SyntaxKind :: BASE64_KW } ; [base64wide] => { $ crate :: SyntaxKind :: BASE64WIDE_KW } ; [contains] => { $ crate :: SyntaxKind :: CONTAINS_KW } ; [icontains] => { $ crate :: SyntaxKind :: ICONTAINS_KW } ; [startswith] => { $ crate :: SyntaxKind :: STARTSWITH_KW } ; [istartswith] => { $ crate :: SyntaxKind :: ISTARTSWITH_KW } ; [endswith] => { $ crate :: SyntaxKind :: ENDSWITH_KW } ; [iendswith] => { $ crate :: SyntaxKind :: IENDSWITH_KW } ; [iequals] => { $ crate :: SyntaxKind :: IEQUALS_KW } ; [matches] => { $ crate :: SyntaxKind :: MATCHES_KW } ; [defined] => { $ crate :: SyntaxKind :: DEFINED_KW } ; [filesize] => { $ crate :: SyntaxKind :: FILESIZE_KW } ; [entrypoint] => { $ crate :: SyntaxKind :: ENTRYPOINT_KW } ; [at] => { $ crate :: SyntaxKind :: AT_KW } ; [in] => { $ crate :: SyntaxKind :: IN_KW } ; [of] => { $ crate :: SyntaxKind :: OF_KW } ; [them] => { $ crate :: SyntaxKind :: THEM_KW } ; [all] => { $ crate :: SyntaxKind :: ALL_KW } ; [any] => { $ crate :: SyntaxKind :: ANY_KW } ; [none] => { $ crate :: SyntaxKind :: NONE_KW } ; [for] => { $ crate :: SyntaxKind :: FOR_KW } ; [identifier] => { $ crate :: SyntaxKind :: IDENTIFIER } ; [variable] => { $ crate :: SyntaxKind :: VARIABLE } ; [string_lit] => { $ crate :: SyntaxKind :: STRING_LIT } ; [int_lit] => { $ crate :: SyntaxKind :: INT_LIT } ; [float_lit] => { $ crate :: SyntaxKind :: FLOAT_LIT } ; [bool_lit] => { $ crate :: SyntaxKind :: BOOL_LIT } ; [hex_lit] => { $ crate :: SyntaxKind :: HEX_LIT } ; [regex_lit] => { $ crate :: SyntaxKind :: REGEX_LIT } ; [dot_matches_all] => { $ crate :: SyntaxKind :: DOT_MATCHES_ALL } ; [case_insensitive] => { $ crate :: SyntaxKind :: CASE_INSENSITIVE } ; [backslash] => { $ crate :: SyntaxKind :: BACKSLASH } ; }
pub use T;
Loading

0 comments on commit 8ef9667

Please sign in to comment.