1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::config::ConfigOption;
use crate::linter::{SyntaxRule, SyntaxRuleResult};
use sv_parser::{
    unwrap_locate, unwrap_node, GenerateBlock, Locate, NodeEvent, RefNode, SyntaxTree,
};

#[derive(Default)]
pub struct GenerateIfWithLabel;

impl SyntaxRule for GenerateIfWithLabel {
    fn check(
        &mut self,
        syntax_tree: &SyntaxTree,
        event: &NodeEvent,
        option: &ConfigOption,
    ) -> SyntaxRuleResult {
        let node = match event {
            NodeEvent::Enter(x) => x,
            NodeEvent::Leave(_) => {
                return SyntaxRuleResult::Pass;
            }
        };
        match node {
            RefNode::IfGenerateConstruct(x) => {
                let (_, _, ref a, ref b) = x.nodes;

                let id: Option<&Locate> = match unwrap_node!(*x, GenerateBlockIdentifier) {
                    Some(RefNode::GenerateBlockIdentifier(id_)) => {
                        unwrap_locate!(id_)
                    }
                    _ => None,
                };

                match a {
                    GenerateBlock::Multiple(x) => {
                        let (_, _, ref a, _, _, _) = x.nodes;

                        let is_prefixed: bool = match &id {
                            Some(x) => syntax_tree
                                .get_str(*x)
                                .unwrap()
                                .starts_with(&option.prefix_label),
                            _ => false,
                        };

                        if a.is_some() && is_prefixed {
                            match b {
                                Some((e, x)) => match x {
                                    GenerateBlock::Multiple(x) => {
                                        let (_, _, ref a, _, _, _) = x.nodes;

                                        let else_locate = unwrap_locate!(e).unwrap();

                                        match a {
                                            Some((_, id_)) => {
                                                let id: Option<&Locate> = unwrap_locate!(id_);
                                                let is_prefixed: bool = match &id {
                                                    Some(x) => syntax_tree
                                                        .get_str(*x)
                                                        .unwrap()
                                                        .starts_with(&option.prefix_label),
                                                    _ => false,
                                                };

                                                if is_prefixed {
                                                    SyntaxRuleResult::Pass
                                                } else {
                                                    // failed because a label of 'else' doesn't have prefix
                                                    SyntaxRuleResult::FailLocate(*else_locate)
                                                }
                                            }
                                            _ => {
                                                // failed because a label of 'else' is not found
                                                SyntaxRuleResult::FailLocate(*else_locate)
                                            }
                                        }
                                    }
                                    _ => {
                                        if is_prefixed {
                                            SyntaxRuleResult::Pass
                                        } else {
                                            // failed because a label of 'if' doesn't have prefix
                                            SyntaxRuleResult::Fail
                                        }
                                    }
                                },
                                // there is no 'else' to have a label
                                None => SyntaxRuleResult::Pass,
                            }
                        } else {
                            // failed because a label of 'if' is not found
                            // OR the label doesn't have prefix
                            SyntaxRuleResult::Fail
                        }
                    }
                    // failed because 'begin' of 'if' is not found
                    _ => SyntaxRuleResult::Fail,
                }
            }
            _ => SyntaxRuleResult::Pass,
        }
    }

    fn name(&self) -> String {
        String::from("generate_if_with_label")
    }

    fn hint(&self, option: &ConfigOption) -> String {
        String::from(format!(
            "Use a label with prefix \"{}\" on conditional generate block.",
            &option.prefix_label
        ))
    }

    fn reason(&self) -> String {
        String::from("Unnamed generate blocks imply unintuitive hierarchical paths.")
    }
}