use crate::config::ConfigOption;
use crate::linter::{check_regex, SyntaxRule, SyntaxRuleResult};
use regex::Regex;
use sv_parser::{unwrap_node, NodeEvent, RefNode, SyntaxTree, PortDirection};
#[derive(Default)]
pub struct ReForbiddenPortInput {
re: Option<Regex>,
under_input_declaration: bool,
under_ansi_port_declaration: bool,
previous_port_direction_input: bool,
}
impl SyntaxRule for ReForbiddenPortInput {
fn check(
&mut self,
syntax_tree: &SyntaxTree,
event: &NodeEvent,
option: &ConfigOption,
) -> SyntaxRuleResult {
if self.re.is_none() {
self.re = Some(Regex::new(&option.re_forbidden_port_input).unwrap());
}
let node = match event {
NodeEvent::Enter(x) => {
match x {
RefNode::InputDeclaration(_) => {
self.under_input_declaration = true;
}
RefNode::AnsiPortDeclaration(_) => {
self.under_ansi_port_declaration = true;
}
RefNode::PortDirection(PortDirection::Input(_)) => {
self.previous_port_direction_input = true;
}
RefNode::ModuleAnsiHeader(_) |
RefNode::PortDirection(PortDirection::Inout(_)) |
RefNode::PortDirection(PortDirection::Output(_)) |
RefNode::PortDirection(PortDirection::Ref(_)) => {
self.previous_port_direction_input = false;
}
_ => ()
}
x
}
NodeEvent::Leave(x) => {
match x {
RefNode::InputDeclaration(_) => {
self.under_input_declaration = false;
}
RefNode::AnsiPortDeclaration(_) => {
self.under_ansi_port_declaration = false;
}
_ => ()
}
return SyntaxRuleResult::Pass;
}
};
let c: bool = self.under_input_declaration ||
(self.under_ansi_port_declaration && self.previous_port_direction_input);
match (c, node) {
(true, RefNode::PortIdentifier(x)) => {
check_regex(false, unwrap_node!(*x, Identifier),
&syntax_tree, &self.re.as_ref().unwrap())
}
_ => SyntaxRuleResult::Pass
}
}
fn name(&self) -> String {
String::from("re_forbidden_port_input")
}
fn hint(&self, option: &ConfigOption) -> String {
String::from(format!(
"Use a port identifier not matching regex `{}`.",
&option.re_forbidden_port_input
))
}
fn reason(&self) -> String {
String::from("Identifiers must conform to the naming scheme.")
}
}