Skip to content

Commit

Permalink
feat: more improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Jul 5, 2024
1 parent b370d2e commit 81878ca
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 7 deletions.
29 changes: 22 additions & 7 deletions parser-ng/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ impl<'src> InternalParser<'src> {
/// Notice how the error is now more localized.
fn recover_and_sync(&mut self, recovery_set: &TokenSet) -> &mut Self {
self.recover();
/*if let Some(t) = self.peek_non_ws() {
if recovery_set.contains(t) {
return self;
}
}*/
self.sync(recovery_set);
self
}
Expand Down Expand Up @@ -631,7 +636,7 @@ impl<'src> InternalParser<'src> {
}

/// Applies `parser` exactly one time.
fn one<P>(&mut self, parser: P) -> &mut Self
fn then<P>(&mut self, parser: P) -> &mut Self
where
P: Fn(&mut Self) -> &mut Self,
{
Expand Down Expand Up @@ -802,10 +807,12 @@ impl<'src> InternalParser<'src> {
.if_found(t!(COLON), |p| p.rule_tags())
.recover_and_sync(t!(L_BRACE))
.expect(t!(L_BRACE))
.recover_and_sync(t!(META_KW | STRINGS_KW | CONDITION_KW))
.if_found(t!(META_KW), |p| p.meta_blk())
.recover_and_sync(t!(STRINGS_KW | CONDITION_KW))
.if_found(t!(STRINGS_KW), |p| p.patterns_blk())
.recover_and_sync(t!(CONDITION_KW)) // todo: remove
.one(|p| p.condition_blk())
.recover_and_sync(t!(CONDITION_KW))
.then(|p| p.condition_blk())
.expect(t!(R_BRACE))
.end()
}
Expand Down Expand Up @@ -846,6 +853,14 @@ impl<'src> InternalParser<'src> {
.expect(t!(META_KW))
.expect(t!(COLON))
.one_or_more(|p| p.meta_def())
/*.then(|p| {
while matches!(p.peek_non_ws(), Some(IDENT(_))) {
p.trivia();
p.meta_def();
p.recover_and_sync(t!(IDENT | STRINGS_KW | CONDITION_KW));
}
p
})*/
.end()
}

Expand Down Expand Up @@ -923,15 +938,15 @@ impl<'src> InternalParser<'src> {
self.begin(SyntaxKind::CONDITION_BLK)
.expect(t!(CONDITION_KW))
.expect(t!(COLON))
.one(|p| p.boolean_expr())
.then(|p| p.boolean_expr())
.end()
}

fn hex_pattern(&mut self) -> &mut Self {
self.begin(SyntaxKind::HEX_PATTERN)
.expect(t!(L_BRACE))
.enter_hex_pattern_mode()
.one(|p| p.hex_tokens())
.then(|p| p.hex_tokens())
.expect(t!(R_BRACE))
.end()
}
Expand All @@ -955,9 +970,9 @@ impl<'src> InternalParser<'src> {
/// ``
fn boolean_expr(&mut self) -> &mut Self {
self.begin(SyntaxKind::BOOLEAN_EXPR)
.one(|p| p.boolean_term())
.then(|p| p.boolean_term())
.zero_or_more(|p| {
p.expect(t!(AND_KW | OR_KW)).one(|p| p.boolean_term())
p.expect(t!(AND_KW | OR_KW)).then(|p| p.boolean_term())
})
.end()
}
Expand Down
6 changes: 6 additions & 0 deletions parser-ng/src/parser/tests/testdata/basic-error-1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rule test {{
meta:
foo = 1
condition:
true
}
9 changes: 9 additions & 0 deletions parser-ng/src/parser/tests/testdata/meta-error-5.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rule test {
meta:
foo = bar
bar = "bar"
strings:
$a = "foo"
condition:
true
}
56 changes: 56 additions & 0 deletions parser-ng/src/parser/tests/testdata/meta-error-5.out
10 changes: 10 additions & 0 deletions parser-ng/src/parser/tests/testdata/meta-error-6.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rule test {
meta:
foo =
strings
bar = "bar"
strings:
$a = "foo"
condition:
true
}
57 changes: 57 additions & 0 deletions parser-ng/src/parser/tests/testdata/meta-error-6.out

0 comments on commit 81878ca

Please sign in to comment.