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
use crate::config::ConfigOption;
use crate::linter::{SyntaxRule, SyntaxRuleResult};
use sv_parser::{unwrap_locate, NodeEvent, RefNode, StatementItem, StatementOrNull, SyntaxTree};

#[derive(Default)]
pub struct MultilineIfBegin;

impl SyntaxRule for MultilineIfBegin {
    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::ConditionalStatement(x) => {
                let (ref a, ref b, ref c, ref d, ref e, ref f) = x.nodes;

                // if statement
                let mut if_str = String::from("");
                syntax_tree.get_str(a).map(|x| if_str.push_str(x));
                syntax_tree.get_str(b).map(|x| if_str.push_str(x));
                syntax_tree.get_str(c).map(|x| if_str.push_str(x));
                syntax_tree.get_str_trim(d).map(|x| if_str.push_str(x));

                if let StatementOrNull::Statement(x) = d {
                    let (_, _, ref x) = x.nodes;
                    match x {
                        StatementItem::SeqBlock(_) => (),
                        _ => {
                            if if_str.contains('\n') {
                                return SyntaxRuleResult::Fail;
                            }
                        }
                    }
                }

                // else if statement
                for e in e {
                    let (ref a, ref b, ref c, ref d) = e;

                    let mut elsif_str = String::from("");
                    syntax_tree.get_str(a).map(|x| elsif_str.push_str(x));
                    syntax_tree.get_str(b).map(|x| elsif_str.push_str(x));
                    syntax_tree.get_str(c).map(|x| elsif_str.push_str(x));
                    syntax_tree.get_str_trim(d).map(|x| elsif_str.push_str(x));

                    if let StatementOrNull::Statement(x) = d {
                        let (_, _, ref x) = x.nodes;
                        match x {
                            StatementItem::SeqBlock(_) => (),
                            _ => {
                                if elsif_str.contains('\n') {
                                    let locate = unwrap_locate!(a).unwrap();
                                    return SyntaxRuleResult::FailLocate(*locate);
                                }
                            }
                        }
                    };
                }

                // else statement
                if let Some(f) = f {
                    let (ref a, ref b) = f;

                    let mut else_str = String::from("");
                    syntax_tree.get_str(a).map(|x| else_str.push_str(x));
                    syntax_tree.get_str_trim(b).map(|x| else_str.push_str(x));

                    if let StatementOrNull::Statement(x) = b {
                        let (_, _, ref x) = x.nodes;
                        match x {
                            StatementItem::SeqBlock(_) => (),
                            _ => {
                                if else_str.contains('\n') {
                                    let locate = unwrap_locate!(a).unwrap();
                                    return SyntaxRuleResult::FailLocate(*locate);
                                }
                            }
                        }
                    };
                }

                SyntaxRuleResult::Pass
            }
            _ => SyntaxRuleResult::Pass,
        }
    }

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

    fn hint(&self, _option: &ConfigOption) -> String {
        String::from("Add `begin`/`end` around multi-line `if` statement.")
    }

    fn reason(&self) -> String {
        String::from("Without `begin`/`end`, the conditional statement may be confusing.")
    }
}