svlint/opt/rustwide/workdir/src/syntaxrules/
re_required_program.rs1use crate::config::ConfigOption;
2use crate::linter::{check_regex, SyntaxRule, SyntaxRuleResult};
3use regex::Regex;
4use sv_parser::{unwrap_node, NodeEvent, RefNode, SyntaxTree};
5
6#[derive(Default)]
7pub struct ReRequiredProgram {
8 re: Option<Regex>,
9}
10
11impl SyntaxRule for ReRequiredProgram {
12 fn check(
13 &mut self,
14 syntax_tree: &SyntaxTree,
15 event: &NodeEvent,
16 option: &ConfigOption,
17 ) -> SyntaxRuleResult {
18 if self.re.is_none() {
19 self.re = Some(Regex::new(&option.re_required_program).unwrap());
20 }
21
22 let node = match event {
23 NodeEvent::Enter(x) => x,
24 NodeEvent::Leave(_) => {
25 return SyntaxRuleResult::Pass;
26 }
27 };
28
29 match node {
30 RefNode::ProgramDeclaration(x) => {
31 check_regex(true, unwrap_node!(*x, ProgramIdentifier),
32 &syntax_tree, &self.re.as_ref().unwrap())
33 }
34 _ => SyntaxRuleResult::Pass,
35 }
36 }
37
38 fn name(&self) -> String {
39 String::from("re_required_program")
40 }
41
42 fn hint(&self, option: &ConfigOption) -> String {
43 String::from(format!(
44 "Use a program identifier matching regex `{}`.",
45 &option.re_required_program
46 ))
47 }
48
49 fn reason(&self) -> String {
50 String::from("Identifiers must conform to the naming scheme.")
51 }
52}