Skip to content

Commit

Permalink
Add warning for empty rule
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2a-42 committed Dec 31, 2024
1 parent fded3d8 commit 2e84d74
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/frontend/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub const UNUSED_RULE: &str = "W001";
pub const UNUSED_TOKEN: &str = "W002";
pub const UNUSED_OPEN_NODE: &str = "W003";
pub const REDUNDANT_ELISION: &str = "W004";
pub const EMPTY_RULE: &str = "W005";

pub trait LanguageErrors {
fn invalid_binding_pos(span: &Span) -> Self;
Expand Down Expand Up @@ -67,6 +68,7 @@ pub trait LanguageErrors {
fn redundant_elision(span: &Span) -> Self;
fn expected_rule(span: &Span) -> Self;
fn missing_node_name(span: &Span) -> Self;
fn empty_rule(span: &Span) -> Self;
}

impl LanguageErrors for Diagnostic {
Expand Down Expand Up @@ -359,4 +361,11 @@ impl LanguageErrors for Diagnostic {
.with_message("missing node name")
.with_labels(vec![Label::primary((), span.clone())])
}

fn empty_rule(span: &Span) -> Self {
Diagnostic::warning()
.with_code(EMPTY_RULE)
.with_message("empty rule")
.with_labels(vec![Label::primary((), span.clone())])
}
}
7 changes: 5 additions & 2 deletions src/frontend/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,11 @@ impl<'a> GeneralCheck<'a> {
diags: &mut Vec<Diagnostic>,
sema: &mut SemanticData<'a>,
) {
rule.regex(cst)
.map(|regex| self.check_regex(cst, rule, regex, diags, sema, false, false));
if let Some(regex) = rule.regex(cst) {
self.check_regex(cst, rule, regex, diags, sema, false, false);
} else {
diags.push(Diagnostic::empty_rule(&rule.span(cst)));
}
self.check_recursive(cst, sema, rule, diags);
if let Some(regex) = rule.regex(cst) {
let mut open = HashSet::new();
Expand Down
13 changes: 13 additions & 0 deletions tests/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ fn lowercase_token() {

assert_eq!(lines.next().unwrap(), "tests/frontend/lowercase_token.llw:1:7: error[E007]: token name starts with lower case letter");
assert_eq!(lines.next().unwrap(), "tests/frontend/lowercase_token.llw:1:9: error[E007]: token name starts with lower case letter");
assert_eq!(lines.next().unwrap(), "tests/frontend/lowercase_token.llw:4:1: warning[W005]: empty rule");
assert_eq!(lines.next(), None);
}

Expand Down Expand Up @@ -239,6 +240,10 @@ fn redefinition() {
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:14:1: error[E005]: redefinition of rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:1:9: error[E005]: redefinition of token");
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:1:13: error[E005]: redefinition of token");
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:6:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:9:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:12:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/redefinition.llw:14:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "error[E008]: missing start rule");
assert_eq!(lines.next(), None);
}
Expand Down Expand Up @@ -309,6 +314,9 @@ fn unused_element() {
let diags = gen_diags("tests/frontend/unused_element.llw");
let mut lines = diags.lines();

assert_eq!(lines.next().unwrap(), "tests/frontend/unused_element.llw:13:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/unused_element.llw:20:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/unused_element.llw:23:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/unused_element.llw:9:1: warning[W001]: unused rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/unused_element.llw:13:1: warning[W001]: unused rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/unused_element.llw:23:1: warning[W001]: unused rule");
Expand All @@ -335,5 +343,10 @@ fn uppercase_rule() {

assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:6:1: error[E006]: rule name starts with upper case letter");
assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:9:1: error[E006]: rule name starts with upper case letter");
assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:3:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:6:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:9:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:12:1: warning[W005]: empty rule");
assert_eq!(lines.next().unwrap(), "tests/frontend/uppercase_rule.llw:15:1: warning[W005]: empty rule");
assert_eq!(lines.next(), None);
}

0 comments on commit 2e84d74

Please sign in to comment.