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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::Any;
use crate::MatchingResult;
use crate::RangeContainsMax;
use crate::{ActualItems, Controls, Expected, ExpectedItems, Machine, Quantity};
use std::fmt;

impl Default for Machine {
    fn default() -> Self {
        Machine {
            actual_index: 0,
            expected_index: 0,
            is_final: false,
            matched_length_in_repeat: 0,
        }
    }
}

impl Machine {
    pub fn matching<T>(
        &mut self,
        actual_items: &ActualItems<T>,
        expected_items: &ExpectedItems<T>,
    ) -> bool
    where
        T: std::cmp::PartialEq + std::cmp::PartialOrd,
    {
        for (i, act) in actual_items.get_items().iter().enumerate() {
            self.actual_index = i;
            if self.actual_index + 1 == actual_items.len() {
                self.is_final = true;
            }

            // TODO expected_index カーソルを勧めるのはあとで。

            if let Some(mut exp) = expected_items.get(self.expected_index) {
                match self.matching2(act, &mut exp) {
                    MatchingResult::Matched => {
                        // println!("(trace.30) マッチしたという判断。ループ続行。");

                        self.expected_index += 1;
                        self.matched_length_in_repeat = 0; // reset.

                    }
                    MatchingResult::NotMatch => {
                        // println!("(trace.35) マッチしていないという判断。");

                        return false;
                    }
                    MatchingResult::Ongoing => {
                        // println!("(trace.38) マッチ中という判断。ループ続行。");

                    }
                }
            } else {
                // マッチしていないという判断。

                // println!("(trace.44) マッチしていないという判断。");

                return false;
            }
        }

        // 失敗していなければ成功という判断。

        // println!("(trace.51) 失敗していなければ成功という判断。");

        return true;
    }

    pub fn matching2<T>(&mut self, act: &T, exp: &Controls<T>) -> MatchingResult
    where
        T: std::cmp::PartialEq + std::cmp::PartialOrd,
    {
        match exp {
            Controls::Once(exp) => match exp {
                Quantity::Any(any) => self.matching4_any(act, any),
                Quantity::One(exp) => self.matching4_one(act, exp),
            },
            Controls::Repeat(rep) => {
                if rep.is_cutoff(self.matched_length_in_repeat) {
                    //  || self.is_final

                    match self.matching3_quantity(act, &rep.quantity) {
                        MatchingResult::NotMatch => {
                            // println!("(trace.85) rep={}", rep);

                            return MatchingResult::NotMatch;
                        }
                        MatchingResult::Matched | MatchingResult::Ongoing => {
                            self.matched_length_in_repeat += 1;
                            if rep.is_cutoff(self.matched_length_in_repeat) {
                                /*
                                // println!(
                                    "(trace.93) Cutoff. 上限までマッチしたので切上げ。 rep={}",
                                    rep
                                );
                                */
                                return MatchingResult::Matched;
                            } else if rep.is_success(self.matched_length_in_repeat) {
                                // println!("(trace.87) rep={}", rep);

                                return MatchingResult::Matched;
                            } else {
                                // println!("(trace.90) fail. rep={}", rep);

                                return MatchingResult::NotMatch;
                            }
                        }
                    }
                } else {
                    match self.matching3_quantity(act, &rep.quantity) {
                        MatchingResult::NotMatch => {
                            if rep.is_cutoff(self.matched_length_in_repeat) {
                                /*
                                // println!(
                                    "(trace.93) Cutoff. 上限までマッチしたので切上げ。 rep={}",
                                    rep
                                );
                                */
                                return MatchingResult::Matched;
                            } else if rep.is_success(self.matched_length_in_repeat) {
                                /*
                                // println!(
                                    "(trace.104) マッチしなくなったところで再判定。 rep={}",
                                    rep
                                );
                                // */
                                return MatchingResult::Matched;
                            } else {
                                // println!("(trace.107) fail. rep={}", rep);

                                return MatchingResult::NotMatch;
                            }
                        }
                        MatchingResult::Matched => {
                            self.matched_length_in_repeat += 1;
                            // println!("(trace.112) マッチ中なので続行。 rep={}", rep);

                            return MatchingResult::Ongoing;
                        }
                        MatchingResult::Ongoing => {
                            self.matched_length_in_repeat += 1;
                            // println!("(trace.115) rep={}", rep);

                            return MatchingResult::Ongoing;
                        }
                    }
                }
            }
        }
    }

    pub fn matching3_quantity<T>(&mut self, act: &T, exp: &Quantity<T>) -> MatchingResult
    where
        T: std::cmp::PartialEq + std::cmp::PartialOrd,
    {
        match exp {
            Quantity::Any(any) => self.matching4_any(act, any),
            Quantity::One(exp) => self.matching4_one(act, exp),
        }
    }

    fn matching4_any<T>(&mut self, act: &T, any: &Any<T>) -> MatchingResult
    where
        T: std::cmp::PartialEq + std::cmp::PartialOrd,
    {
        for exp in &any.items {
            match exp {
                Expected::Exact(exa) => {
                    if *exa == *act {
                        // println!("(trace.138) matching_any/matched.");

                        return MatchingResult::Matched;
                    }
                }
                Expected::RangeContainsMax(rng) => {
                    match self.matching5_range_contains_max(act, rng) {
                        MatchingResult::Matched => {
                            // println!("(trace.138) matching_any/rng/matched.");

                            return MatchingResult::Matched;
                        }
                        MatchingResult::Ongoing => {
                            // println!("(trace.138) matching_any/rng/ongoing.");

                            return MatchingResult::Ongoing;
                        }
                        MatchingResult::NotMatch => {
                            // 続行。

                            // println!("(trace.138) matching_any/rng/notmatch.");

                        }
                    }
                }
            }
        }
        // println!("(trace.67) Anyでぜんぶ不一致。");

        return MatchingResult::NotMatch;
    }
    fn matching4_one<T>(&mut self, act: &T, exp: &Expected<T>) -> MatchingResult
    where
        T: std::cmp::PartialEq + std::cmp::PartialOrd,
    {
        match exp {
            Expected::Exact(exa) => {
                if *exa == *act {
                    // println!("(trace.72)");

                    MatchingResult::Matched
                } else {
                    // println!("(trace.75)");

                    MatchingResult::NotMatch
                }
            }
            Expected::RangeContainsMax(rng) => {
                return self.matching5_range_contains_max(act, rng);
            }
        }
    }
    fn matching5_range_contains_max<T>(
        &mut self,
        act: &T,
        rng: &RangeContainsMax<T>,
    ) -> MatchingResult
    where
        T: std::cmp::PartialEq + std::cmp::PartialOrd,
    {
        if let Some(min) = &rng.min {
            if *act < *min {
                return MatchingResult::NotMatch;
            }
        }
        if let Some(max) = &rng.max {
            if *max < *act {
                return MatchingResult::NotMatch;
            }
        }
        return MatchingResult::Matched;
    }
}

impl fmt::Display for Machine {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf = String::new();
        buf.push_str(&format!("actual_index={} ", self.actual_index));
        buf.push_str(&format!("expected_index={} ", self.expected_index));
        buf.push_str(&format!(
            "matched_length_in_repeat={} ",
            self.matched_length_in_repeat
        ));
        write!(f, "{}", buf)
    }
}
impl fmt::Debug for Machine {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf = String::new();
        buf.push_str(&format!("actual_index={} ", self.actual_index));
        buf.push_str(&format!("expected_index={:?} ", self.expected_index));
        buf.push_str(&format!(
            "matched_length_in_repeat={} ",
            self.matched_length_in_repeat
        ));
        write!(f, "{}", buf)
    }
}