Skip to content

Commit

Permalink
feat: advance in the implementation of expression parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Jul 6, 2024
1 parent a3fff40 commit 5c8604e
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 27 deletions.
8 changes: 8 additions & 0 deletions parser-ng/src/parser/cst/syntax_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub enum SyntaxKind {
// Punctuation
AMPERSAND,
ASTERISK,
CARET,
COLON,
COMMA,
BACKSLASH,
Expand Down Expand Up @@ -98,7 +99,13 @@ pub enum SyntaxKind {
PATTERNS_BLK,
PATTERN_MODS,
PATTERN_MOD,
RANGE,
REGEXP,
TERM,
EXPR,
INDEXING_EXPR,
PRIMARY_EXPR,
FUNC_CALL_EXPR,
META_DEF,
META_BLK,
SOURCE_FILE,
Expand Down Expand Up @@ -186,6 +193,7 @@ impl From<&Token> for SyntaxKind {
Token::AMPERSAND(_) => SyntaxKind::AMPERSAND,
Token::ASTERISK(_) => SyntaxKind::ASTERISK,
Token::BACKSLASH(_) => SyntaxKind::BACKSLASH,
Token::CARET(_) => SyntaxKind::CARET,
Token::COLON(_) => SyntaxKind::COLON,
Token::COMMA(_) => SyntaxKind::COMMA,
Token::DOT(_) => SyntaxKind::DOT,
Expand Down
83 changes: 77 additions & 6 deletions parser-ng/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,25 +1134,96 @@ impl<'src> InternalParser<'src> {
///
/// ```text
/// BOOLEAN_TERM := (
/// TRUE_KW |
/// FALSE_KW
/// `true` |
/// `false` |
/// `not` BOOLEAN_TERM |
/// `defined` BOOLEAN_TERM |
/// `(` BOOLEAN_EXPR `)`
/// )
/// ``
fn boolean_term(&mut self) -> &mut Self {
self.begin(SyntaxKind::BOOLEAN_TERM)
.begin_alt()
.alt(|p| p.expect(t!(TRUE_KW)))
.alt(|p| p.expect(t!(FALSE_KW)))
.alt(|p| {
p.expect(t!(PATTERN_IDENT))
.if_found(t!(AT_KW), |p| {
p.expect(t!(AT_KW)).then(|p| p.expr())
})
.if_found(t!(IN_KW), |p| {
p.expect(t!(IN_KW)).then(|p| p.range())
})
})
.alt(|p| p.expect(t!(TRUE_KW | FALSE_KW)))
.alt(|p| p.expect(t!(NOT_KW)).then(|p| p.boolean_term()))
.alt(|p| p.expect(t!(DEFINED_KW)).then(|p| p.boolean_term()))
.alt(|p| {
p.expect(t!(L_PAREN))
.then(|p| p.boolean_expr())
.expect(t!(R_PAREN))
})
.end_alt()
.end()
}

fn expr(&mut self) -> &mut Self {
todo!()
self.begin(SyntaxKind::EXPR)
.then(|p| p.term())
.zero_or_more(|p| {
p.expect(t!(PLUS
| HYPEN
| ASTERISK
| BACKSLASH
| PERCENT
| SHL
| SHR
| AMPERSAND
| PIPE
| TILDE
| DOT))
.then(|p| p.term())
})
.end()
}

fn term(&mut self) -> &mut Self {
todo!()
self.begin(SyntaxKind::TERM)
.begin_alt()
.alt(|p| p.indexing_expr())
.alt(|p| p.func_call_expr())
.alt(|p| p.primary_expr())
.end_alt()
.end()
}

/// Parses a range.
///
/// ```text
/// RANGE := `(` EXPR `.` `.` EXPR `)`
/// ``
fn range(&mut self) -> &mut Self {
self.begin(SyntaxKind::RANGE)
.expect(t!(L_PAREN))
.then(|p| p.expr())
.expect(t!(DOT))
.expect(t!(DOT))
.then(|p| p.expr())
.expect(t!(R_PAREN))
.end()
}

fn indexing_expr(&mut self) -> &mut Self {
// TODO
self
}

fn func_call_expr(&mut self) -> &mut Self {
// TODO
self
}

fn primary_expr(&mut self) -> &mut Self {
// TODO
self
}
}

Expand Down
4 changes: 4 additions & 0 deletions parser-ng/src/parser/tests/testdata/basic-4.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rule test {
condition:
true and not false or (false or true)
}
2 changes: 1 addition & 1 deletion parser-ng/src/parser/tests/testdata/hex-patterns-1.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ rule test {
strings:
$a = { 00 11 AA ?? B? ?2 ~02 11223344 }
condition:
true
$a
}
16 changes: 8 additions & 8 deletions parser-ng/src/parser/tests/testdata/hex-patterns-1.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SOURCE_FILE@0..87
RULE_DECL@0..87
SOURCE_FILE@0..85
RULE_DECL@0..85
[email protected] "rule"
[email protected] " "
[email protected] "test"
Expand Down Expand Up @@ -43,13 +43,13 @@ [email protected]
[email protected] "}"
[email protected] "\n"
[email protected] " "
CONDITION_BLK@67..85
CONDITION_BLK@67..83
[email protected] "condition"
[email protected] ":"
[email protected] "\n"
[email protected] "\t "
BOOLEAN_EXPR@81..85
BOOLEAN_TERM@81..85
TRUE_KW@81..85 "true"
NEWLINE@85..86 "\n"
R_BRACE@86..87 "}"
BOOLEAN_EXPR@81..83
BOOLEAN_TERM@81..83
PATTERN_IDENT@81..83 "$a"
NEWLINE@83..84 "\n"
R_BRACE@84..85 "}"
2 changes: 1 addition & 1 deletion parser-ng/src/parser/tests/testdata/hex-patterns-2.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ rule test {
strings:
$a = { 11 ( 22 | 33 ) 44 }
condition:
true
$a
}
16 changes: 8 additions & 8 deletions parser-ng/src/parser/tests/testdata/hex-patterns-2.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SOURCE_FILE@0..74
RULE_DECL@0..74
SOURCE_FILE@0..72
RULE_DECL@0..72
[email protected] "rule"
[email protected] " "
[email protected] "test"
Expand Down Expand Up @@ -41,13 +41,13 @@ [email protected]
[email protected] "}"
[email protected] "\n"
[email protected] " "
CONDITION_BLK@54..72
CONDITION_BLK@54..70
[email protected] "condition"
[email protected] ":"
[email protected] "\n"
[email protected] "\t "
BOOLEAN_EXPR@68..72
BOOLEAN_TERM@68..72
TRUE_KW@68..72 "true"
NEWLINE@72..73 "\n"
R_BRACE@73..74 "}"
BOOLEAN_EXPR@68..70
BOOLEAN_TERM@68..70
PATTERN_IDENT@68..70 "$a"
NEWLINE@70..71 "\n"
R_BRACE@71..72 "}"
3 changes: 3 additions & 0 deletions parser-ng/src/tokenizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ enum NormalToken<'src> {
Percent,
#[token("|")]
Pipe,
#[token("^")]
Caret,
#[token("~")]
Tilde,

Expand Down Expand Up @@ -545,6 +547,7 @@ fn convert_normal_token(token: NormalToken, span: Span) -> Token {
NormalToken::Ampersand => Token::AMPERSAND(span),
NormalToken::Asterisk => Token::ASTERISK(span),
NormalToken::Backslash => Token::BACKSLASH(span),
NormalToken::Caret => Token::CARET(span),
NormalToken::Colon => Token::COLON(span),
NormalToken::Dot => Token::DOT(span),
NormalToken::Equal => Token::EQUAL(span),
Expand Down
9 changes: 6 additions & 3 deletions parser-ng/src/tokenizer/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub enum Token {
AMPERSAND(Span),
ASTERISK(Span),
BACKSLASH(Span),
CARET(Span),
COLON(Span),
COMMA(Span),
DOT(Span),
Expand Down Expand Up @@ -173,19 +174,20 @@ impl Token {
| Token::GE(span)

// Punctuation.
| Token::PLUS(span)
| Token::AMPERSAND(span)
| Token::ASTERISK(span)
| Token::BACKSLASH(span)
| Token::AMPERSAND(span)
| Token::CARET(span)
| Token::COLON(span)
| Token::COMMA(span)
| Token::DOT(span)
| Token::EQUAL(span)
| Token::HYPEN(span)
| Token::PERCENT(span)
| Token::PIPE(span)
| Token::PLUS(span)
| Token::TILDE(span)

| Token::EQUAL(span)
| Token::L_BRACE(span)
| Token::R_BRACE(span)
| Token::L_BRACKET(span)
Expand Down Expand Up @@ -272,6 +274,7 @@ impl Token {
Token::AMPERSAND(_) => "&",
Token::ASTERISK(_) => "`*`",
Token::BACKSLASH(_) => "`\\`",
Token::CARET(_) => "`^`",
Token::COLON(_) => "`:`",
Token::COMMA(_) => "`,`",
Token::DOT(_) => "`.`",
Expand Down

0 comments on commit 5c8604e

Please sign in to comment.