1use std::{
2 collections::BTreeSet as Set,
3 u32,
4};
5use ::segment_map::{
6 Segment,
7 segment_map,
8};
9use finite_automata::{
10 Enfa,
11 Nfa,
12 Dfa,
13 Subsume,
14 states_contains_from,
15 states_contains_all_from,
16};
17use crate::{
18 StateGenerator,
19 SimpleStateGenerator,
20};
21
22#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
23pub enum Expression {
24 SymbolSet { segments: Vec<Segment<u32>> },
25 NegatedSymbolSet { segments: Vec<Segment<u32>> },
26 Alternation { expressions: Vec<Expression> },
27 Concatenation { expressions: Vec<Expression> },
28 Repetition { expression: Box<Expression>, min: Option<u32>, max: Option<u32> },
29}
30
31impl Expression {
32 pub fn as_enfa<S: Clone + Ord, G: StateGenerator<State = S>>(&self, states: &mut G) -> Enfa<S, u32> {
33 match self {
34 Expression::SymbolSet { segments } => {
35 let mut sym = Enfa::new(states.next_initial());
36 let sym_final_index = sym.states_insert(states.next_final());
37 if segments.len() > 0 {
38 for segment in segments {
39 sym.transitions_insert((sym.initial_index(), segment.clone(), sym_final_index));
40 }
41 } else {
42 sym.transitions_insert((sym.initial_index(), Segment::empty(), sym_final_index));
43 }
44 sym.set_final(sym_final_index);
45 sym
46 },
47 Expression::NegatedSymbolSet { segments } => {
48 let mut neg = Enfa::new(states.next_initial());
49 let neg_final_index = neg.states_insert(states.next_final());
50 let mut negated_segments = segment_map![Segment::all() => true];
51 for segment in segments {
52 negated_segments.update(&segment, |_| Some(false));
53 }
54 for (negated_segment, is_negated) in negated_segments {
55 if is_negated {
56 neg.transitions_insert((neg.initial_index(), negated_segment, neg_final_index));
57 }
58 }
59 neg.set_final(neg_final_index);
60 neg
61 },
62 Expression::Alternation { expressions } => {
63 let mut alt = Enfa::new(states.next_initial());
64 let alt_final_index = alt.states_insert(states.next_final());
65 for expression in expressions {
66 let fa = expression.as_enfa(states.disable_final());
67 alt.subsume(&fa);
68 let fa_initial_index = states_contains_from(&alt, &fa, fa.initial_index()).expect("state does not exist");
69 alt.transitions_insert((alt.initial_index(), Segment::empty(), fa_initial_index));
70 for fa_final_index in fa.final_indices() {
71 let fa_final_index = states_contains_from(&alt, &fa, fa_final_index).expect("state does not exist");
72 alt.transitions_insert((fa_final_index, Segment::empty(), alt_final_index));
73 }
74 }
75 alt.set_final(alt_final_index);
76 alt
77 },
78 Expression::Concatenation { expressions } => {
79 let mut con = Enfa::new(states.next_initial());
80 let con_final_index = con.states_insert(states.next_final());
81 let mut prev_fa_final_indices = set![con.initial_index()];
82 for expression in expressions {
83 let fa = expression.as_enfa(states.disable_final());
84 con.subsume(&fa);
85 let fa_initial_index = states_contains_from(&con, &fa, fa.initial_index()).expect("state does not exist");
86 for prev_fa_final_index in prev_fa_final_indices {
87 con.transitions_insert((prev_fa_final_index, Segment::empty(), fa_initial_index));
88 }
89 prev_fa_final_indices = states_contains_all_from(&con, &fa, fa.final_indices()).expect("not all states exist").collect();
90 }
91 for prev_fa_final_index in prev_fa_final_indices {
92 con.transitions_insert((prev_fa_final_index, Segment::empty(), con_final_index));
93 }
94 con.set_final(con_final_index);
95 con
96 },
97 Expression::Repetition { expression, min, max } => {
98 let mut rep = Enfa::new(states.next_initial());
99 let rep_final_index = rep.states_insert(states.next_final());
100 let mut prev_fa_final_indices = set![rep.initial_index()];
101 let mut count = 0;
102 while if let Some(min) = min { count < *min } else { false } {
103 let fa = expression.as_enfa(states.disable_final());
104 rep.subsume(&fa);
105 let fa_initial_index = states_contains_from(&rep, &fa, fa.initial_index()).expect("state does not exist");
106 for prev_fa_final_index in prev_fa_final_indices {
107 rep.transitions_insert((prev_fa_final_index, Segment::empty(), fa_initial_index));
108 }
109 prev_fa_final_indices = states_contains_all_from(&rep, &fa, fa.final_indices()).expect("not all states exist").collect();
110 count += 1;
111 }
112 if let Some(max) = max {
113 while count < *max {
114 let fa = expression.as_enfa(states.disable_final());
115 rep.subsume(&fa);
116 let fa_initial_index = states_contains_from(&rep, &fa, fa.initial_index()).expect("state does not exist");
117 for prev_fa_final_index in prev_fa_final_indices {
118 rep.transitions_insert((prev_fa_final_index, Segment::empty(), fa_initial_index));
119 rep.transitions_insert((prev_fa_final_index, Segment::empty(), rep_final_index));
120 }
121 prev_fa_final_indices = states_contains_all_from(&rep, &fa, fa.final_indices()).expect("not all states exist").collect();
122 count += 1;
123 }
124 for prev_fa_final_index in prev_fa_final_indices {
125 rep.transitions_insert((prev_fa_final_index, Segment::empty(), rep_final_index));
126 }
127 } else {
128 let fa = expression.as_enfa(states.disable_final());
129 rep.subsume(&fa);
130 let fa_initial_index = states_contains_from(&rep, &fa, fa.initial_index()).expect("state does not exist");
131 for prev_fa_final_index in prev_fa_final_indices {
132 rep.transitions_insert((prev_fa_final_index, Segment::empty(), fa_initial_index));
133 rep.transitions_insert((prev_fa_final_index, Segment::empty(), rep_final_index));
134 }
135 for fa_final_index in fa.final_indices() {
136 let fa_final_index = states_contains_from(&rep, &fa, fa_final_index).expect("state does not exist");
137 rep.transitions_insert((fa_final_index, Segment::empty(), rep_final_index));
138 rep.transitions_insert((fa_final_index, Segment::empty(), fa_initial_index));
139 }
140 }
141 rep.set_final(rep_final_index);
142 rep
143 },
144 }
145 }
146}
147
148impl From<Re> for Expression {
149 fn from(re: Re) -> Expression {
150 re.into_expression()
151 }
152}
153
154impl From<&Expression> for Enfa<u32, u32> {
155 fn from(expression: &Expression) -> Enfa<u32, u32> {
156 expression.as_enfa(&mut SimpleStateGenerator::new())
157 }
158}
159
160impl From<&Expression> for Nfa<Set<u32>, u32> {
161 fn from(expression: &Expression) -> Nfa<Set<u32>, u32> {
162 Nfa::from(&Enfa::from(expression))
163 }
164}
165
166impl From<&Expression> for Dfa<Set<u32>, u32> {
167 fn from(expression: &Expression) -> Dfa<Set<u32>, u32> {
168 Dfa::from(&Enfa::from(expression))
169 }
170}
171
172#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
173pub struct Re {
174 expression: Expression,
175 dfa: Dfa<Set<u32>, u32>
176}
177
178impl Re {
179 pub fn new(expression: Expression) -> Re {
180 Re { dfa: Dfa::from(&expression), expression }
181 }
182
183 pub fn is_match(&self, text: &str) -> bool {
184 let mut source_index = self.dfa.initial_index();
185 for character in text.chars() {
186 if let Some(transition_index) = self.dfa.transitions_contains_outgoing((source_index, &character.into())) {
187 let (_, _, target_index) = self.dfa.transitions_index(transition_index);
188 source_index = target_index;
189 } else { return false; }
190 }
191 self.dfa.is_final(source_index)
192 }
193
194 pub fn into_expression(self) -> Expression {
195 self.expression
196 }
197
198 pub fn as_expression(&self) -> &Expression {
199 &self.expression
200 }
201}
202
203impl From<Expression> for Re {
204 fn from(expression: Expression) -> Re {
205 Re::new(expression)
206 }
207}
208
209impl From<&Re> for Enfa<u32, u32> {
210 fn from(re: &Re) -> Enfa<u32, u32> {
211 Enfa::from(re.as_expression())
212 }
213}
214
215impl From<&Re> for Nfa<Set<u32>, u32> {
216 fn from(re: &Re) -> Nfa<Set<u32>, u32> {
217 Nfa::from(&Enfa::from(re))
218 }
219}
220
221impl From<&Re> for Dfa<Set<u32>, u32> {
222 fn from(re: &Re) -> Dfa<Set<u32>, u32> {
223 Dfa::from(&Enfa::from(re))
224 }
225}
226
227#[macro_export]
228macro_rules! sym {
229 ($($x:expr),*) => {{
230 #[allow(unused_mut)]
231 let mut temp_vec = Vec::new();
232 $(temp_vec.push($x);)*
233 $crate::Expression::SymbolSet { segments: temp_vec }
234 }}
235}
236
237#[macro_export]
238macro_rules! neg {
239 ($($x:expr),*) => {{
240 #[allow(unused_mut)]
241 let mut temp_vec = Vec::new();
242 $(temp_vec.push($x);)*
243 $crate::Expression::NegatedSymbolSet { segments: temp_vec }
244 }}
245}
246
247#[macro_export]
248macro_rules! alt {
249 ($($x:expr),*) => {{
250 #[allow(unused_mut)]
251 let mut temp_vec = Vec::new();
252 $(temp_vec.push($x);)*
253 $crate::Expression::Alternation { expressions: temp_vec }
254 }}
255}
256
257#[macro_export]
258macro_rules! con {
259 ($($x:expr),*) => {{
260 #[allow(unused_mut)]
261 let mut temp_vec = Vec::new();
262 $(temp_vec.push($x);)*
263 $crate::Expression::Concatenation { expressions: temp_vec }
264 }}
265}
266
267#[macro_export]
268macro_rules! rep {
269 ($x:expr, $y:expr, $z:expr) => {{
270 $crate::Expression::Repetition { expression: Box::new($x), min: $y, max: $z }
271 }}
272}
273
274#[macro_export]
275macro_rules! ast { ($x:expr) => {{
277 $crate::Expression::Repetition { expression: Box::new($x), min: None, max: None }
278 }}
279}
280
281#[macro_export]
282macro_rules! plu { ($x:expr) => {{
284 $crate::Expression::Repetition { expression: Box::new($x), min: Some(1), max: None }
285 }}
286}
287
288#[macro_export]
289macro_rules! que { ($x:expr) => {{
291 $crate::Expression::Repetition { expression: Box::new($x), min: None, max: Some(1) }
292 }}
293}
294
295#[macro_export]
296macro_rules! sgl { ($x:expr) => {{
298 segment_map::Segment::singleton(u32::from($x))
299 }}
300}
301
302#[macro_export]
303macro_rules! rng { ($x:expr, $y:expr) => {{
305 segment_map::Segment::closed(u32::from($x), u32::from($y))
306 }}
307}
308
309#[macro_export]
310macro_rules! all {
311 () => {{
312 segment_map::Segment::all()
313 }}
314}
315
316#[cfg(test)]
317mod tests {
318 use std::{
319 collections::BTreeSet as Set,
320 fmt::Debug,
321 u32,
322 };
323 use segment_map::Segment;
324 use finite_automata::{
325 Enfa,
326 Dfa,
327 };
328 use crate::Re;
329
330 #[test]
331 fn test_enfa_epsilon() {
332 let expected = ExpectedEnfa {
333 initial: 0,
334 transitions: set![
335 (0, Segment::empty(), 1)
336 ],
337 finals: set![1]
338 };
339 let actual = Enfa::from(&sym![]);
341 assert_enfa_eq(expected, actual);
342 }
343
344 #[test]
345 fn test_enfa_symbol() {
346 let expected = ExpectedEnfa {
347 initial: 0,
348 transitions: set![
349 (0, Segment::singleton(u32::from('A')), 1)
350 ],
351 finals: set![1]
352 };
353 let actual = Enfa::from(&sym![sgl!('A')]);
355 assert_enfa_eq(expected, actual);
356 }
357
358 #[test]
359 fn test_enfa_negated_symbol() {
360 let expected = ExpectedEnfa {
361 initial: 0,
362 transitions: set![
363 (0, Segment::less_than(u32::from('A')), 1),
364 (0, Segment::greater_than(u32::from('A')), 1)
365 ],
366 finals: set![1]
367 };
368 let actual = Enfa::from(&neg![sgl!('A')]);
370 assert_enfa_eq(expected, actual);
371 }
372
373 #[test]
374 fn test_enfa_symbol_set() {
375 let expected = ExpectedEnfa {
376 initial: 0,
377 transitions: set![
378 (0, Segment::closed(u32::from('A'), u32::from('Z')), 1),
379 (0, Segment::closed(u32::from('a'), u32::from('z')), 1)
380 ],
381 finals: set![1]
382 };
383 let actual = Enfa::from(&sym![rng!('A', 'Z'), rng!('a', 'z')]);
385 assert_enfa_eq(expected, actual);
386 }
387
388 #[test]
389 fn test_enfa_negated_symbol_set() {
390 let expected = ExpectedEnfa {
391 initial: 0,
392 transitions: set![
393 (0, Segment::less_than(u32::from('A')), 1),
394 (0, Segment::open(u32::from('Z'), u32::from('a')), 1),
395 (0, Segment::greater_than(u32::from('z')), 1)
396 ],
397 finals: set![1]
398 };
399 let actual = Enfa::from(&neg![rng!('A', 'Z'), rng!('a', 'z')]);
401 assert_enfa_eq(expected, actual);
402 }
403
404 #[test]
405 fn test_enfa_alternation() {
406 let expected = ExpectedEnfa {
407 initial: 0,
408 transitions: set![
409 (0, Segment::empty(), 2),
410 (0, Segment::empty(), 4),
411 (2, Segment::singleton(u32::from('A')), 3),
412 (3, Segment::empty(), 1),
413 (4, Segment::singleton(u32::from('B')), 5),
414 (5, Segment::empty(), 1)
415 ],
416 finals: set![1]
417 };
418 let actual = Enfa::from(&alt![sym![sgl!('A')], sym![sgl!('B')]]);
420 assert_enfa_eq(expected, actual);
421 }
422
423 #[test]
424 fn test_enfa_concatenation() {
425 let expected = ExpectedEnfa {
426 initial: 0,
427 transitions: set![
428 (0, Segment::empty(), 2),
429 (2, Segment::singleton(u32::from('A')), 3),
430 (3, Segment::empty(), 4),
431 (4, Segment::singleton(u32::from('B')), 5),
432 (5, Segment::empty(), 1)
433 ],
434 finals: set![1]
435 };
436 let actual = Enfa::from(&con![sym![sgl!('A')], sym![sgl!('B')]]);
438 assert_enfa_eq(expected, actual);
439 }
440
441 #[test]
442 fn test_enfa_repetition() {
443 let expected = ExpectedEnfa {
444 initial: 0,
445 transitions: set![
446 (0, Segment::empty(), 2),
447 (0, Segment::empty(), 1),
448 (2, Segment::singleton(u32::from('A')), 3),
449 (3, Segment::empty(), 1),
450 (3, Segment::empty(), 2)
451 ],
452 finals: set![1]
453 };
454 let actual = Enfa::from(&ast!(sym![sgl!('A')]));
456 assert_enfa_eq(expected, actual);
457 }
458
459 #[test]
460 fn test_dfa_epsilon() {
461 let expected = ExpectedDfa {
462 initial: set![0, 1],
463 transitions: set![],
464 finals: set![set![0, 1]]
465 };
466 let actual = Dfa::from(&sym![]);
468 assert_dfa_eq(expected, actual);
469 }
470
471 #[test]
472 fn test_dfa_symbol() {
473 let expected = ExpectedDfa {
474 initial: set![0],
475 transitions: set![
476 (set![0], Segment::singleton(u32::from('A')), set![1])
477 ],
478 finals: set![set![1]]
479 };
480 let actual = Dfa::from(&sym![sgl!('A')]);
482 assert_dfa_eq(expected, actual);
483 }
484
485 #[test]
486 fn test_dfa_negated_symbol() {
487 let expected = ExpectedDfa {
488 initial: set![0],
489 transitions: set![
490 (set![0], Segment::less_than(u32::from('A')), set![1]),
491 (set![0], Segment::greater_than(u32::from('A')), set![1])
492 ],
493 finals: set![set![1]]
494 };
495 let actual = Dfa::from(&neg![sgl!('A')]);
497 assert_dfa_eq(expected, actual);
498 }
499
500 #[test]
501 fn test_dfa_symbol_set() {
502 let expected = ExpectedDfa {
503 initial: set![0],
504 transitions: set![
505 (set![0], Segment::closed(u32::from('A'), u32::from('Z')), set![1]),
506 (set![0], Segment::closed(u32::from('a'), u32::from('z')), set![1])
507 ],
508 finals: set![set![1]]
509 };
510 let actual = Dfa::from(&sym![rng!('A', 'Z'), rng!('a', 'z')]);
512 assert_dfa_eq(expected, actual);
513 }
514
515 #[test]
516 fn test_dfa_negated_symbol_set() {
517 let expected = ExpectedDfa {
518 initial: set![0],
519 transitions: set![
520 (set![0], Segment::less_than(u32::from('A')), set![1]),
521 (set![0], Segment::open(u32::from('Z'), u32::from('a')), set![1]),
522 (set![0], Segment::greater_than(u32::from('z')), set![1])
523 ],
524 finals: set![set![1]]
525 };
526 let actual = Dfa::from(&neg![rng!('A', 'Z'), rng!('a', 'z')]);
528 assert_dfa_eq(expected, actual);
529 }
530
531 #[test]
532 fn test_dfa_alternation() {
533 let expected = ExpectedDfa {
534 initial: set![0, 2, 4],
535 transitions: set![
536 (set![0, 2, 4], Segment::singleton(u32::from('A')), set![1, 3]),
537 (set![0, 2, 4], Segment::singleton(u32::from('B')), set![1, 5])
538 ],
539 finals: set![set![1, 3], set![1, 5]]
540 };
541 let actual = Dfa::from(&alt![sym![sgl!('A')], sym![sgl!('B')]]);
543 assert_dfa_eq(expected, actual);
544 }
545
546 #[test]
547 fn test_dfa_concatenation() {
548 let expected = ExpectedDfa {
549 initial: set![0, 2],
550 transitions: set![
551 (set![0, 2], Segment::singleton(u32::from('A')), set![3, 4]),
552 (set![3, 4], Segment::singleton(u32::from('B')), set![1, 5])
553 ],
554 finals: set![set![1, 5]]
555 };
556 let actual = Dfa::from(&con![sym![sgl!('A')], sym![sgl!('B')]]);
558 assert_dfa_eq(expected, actual);
559 }
560
561 #[test]
562 fn test_dfa_repetition() {
563 let expected = ExpectedDfa {
564 initial: set![0, 1, 2],
565 transitions: set![
566 (set![0, 1, 2], Segment::singleton(u32::from('A')), set![1, 2, 3]),
567 (set![1, 2, 3], Segment::singleton(u32::from('A')), set![1, 2, 3])
568 ],
569 finals: set![set![0, 1, 2], set![1, 2, 3]]
570 };
571 let actual = Dfa::from(&ast!(sym![sgl!('A')]));
573 assert_dfa_eq(expected, actual);
574 }
575
576 #[test]
577 fn test_match_epsilon_1() {
578 let re = Re::new(sym![]);
580 assert!(re.is_match(""));
581 }
582
583 #[test]
584 fn test_match_epsilon_2() {
585 let re = Re::new(sym![]);
587 assert!(!re.is_match("A"));
588 }
589
590 #[test]
591 fn test_match_symbol_1() {
592 let re = Re::new(sym![sgl!('A')]);
594 assert!(re.is_match("A"));
595 }
596
597 #[test]
598 fn test_match_symbol_2() {
599 let re = Re::new(sym![sgl!('A')]);
601 assert!(!re.is_match("B"));
602 }
603
604 #[test]
605 fn test_match_negated_symbol_1() {
606 let re = Re::new(neg![sgl!('A')]);
608 assert!(re.is_match("B"));
609 }
610
611 #[test]
612 fn test_match_negated_symbol_2() {
613 let re = Re::new(neg![sgl!('A')]);
615 assert!(!re.is_match("A"));
616 }
617
618 #[test]
619 fn test_match_symbol_set_1() {
620 let re = Re::new(sym![rng!('A', 'Z'), rng!('a', 'z')]);
622 assert!(re.is_match("D"));
623 }
624
625 #[test]
626 fn test_match_symbol_set_2() {
627 let re = Re::new(sym![rng!('A', 'Z'), rng!('a', 'z')]);
629 assert!(!re.is_match("_"));
630 }
631
632 #[test]
633 fn test_match_negated_symbol_set_1() {
634 let re = Re::new(neg![rng!('A', 'Z'), rng!('a', 'z')]);
636 assert!(re.is_match("_"));
637 }
638
639 #[test]
640 fn test_match_negated_symbol_set_2() {
641 let re = Re::new(neg![rng!('A', 'Z'), rng!('a', 'z')]);
643 assert!(!re.is_match("D"));
644 }
645
646 #[test]
647 fn test_match_alternation_1() {
648 let re = Re::new(alt![sym![sgl!('A')], sym![sgl!('B')]]);
650 assert!(re.is_match("A"));
651 }
652
653 #[test]
654 fn test_match_alternation_2() {
655 let re = Re::new(alt![sym![sgl!('A')], sym![sgl!('B')]]);
657 assert!(re.is_match("B"));
658 }
659
660 #[test]
661 fn test_match_alternation_3() {
662 let re = Re::new(alt![sym![sgl!('A')], sym![sgl!('B')]]);
664 assert!(!re.is_match("C"));
665 }
666
667 #[test]
668 fn test_match_concatenation_1() {
669 let re = Re::new(con![sym![sgl!('A')], sym![sgl!('B')]]);
671 assert!(re.is_match("AB"));
672 }
673
674 #[test]
675 fn test_match_concatenation_2() {
676 let re = Re::new(con![sym![sgl!('A')], sym![sgl!('B')]]);
678 assert!(!re.is_match("AA"));
679 }
680
681 #[test]
682 fn test_match_repetition_1() {
683 let re = Re::new(ast!(sym![sgl!('A')]));
685 assert!(re.is_match("AAAAAAAAA"));
686 }
687
688 #[test]
689 fn test_match_repetition_2() {
690 let re = Re::new(ast!(sym![sgl!('A')]));
692 assert!(!re.is_match("AAAABAAAA"));
693 }
694
695 #[test]
696 fn test_match_repetition_3() {
697 let re = Re::new(ast!(sym![sgl!('A')]));
699 assert!(re.is_match(""));
700 }
701
702 #[test]
703 fn test_match_repetition_4() {
704 let re = Re::new(plu!(sym![sgl!('A')]));
706 assert!(re.is_match("AAAAAAAAAA"));
707 }
708
709 #[test]
710 fn test_match_repetition_5() {
711 let re = Re::new(plu!(sym![sgl!('A')]));
713 assert!(!re.is_match("AAAABAAAAA"));
714 }
715
716 #[test]
717 fn test_match_repetition_6() {
718 let re = Re::new(plu!(sym![sgl!('A')]));
720 assert!(!re.is_match(""));
721 }
722
723 #[test]
724 fn test_match_repetition_7() {
725 let re = Re::new(que!(sym![sgl!('A')]));
727 assert!(re.is_match("A"));
728 }
729
730 #[test]
731 fn test_match_repetition_8() {
732 let re = Re::new(que!(sym![sgl!('A')]));
734 assert!(!re.is_match("B"));
735 }
736
737 #[test]
738 fn test_match_repetition_9() {
739 let re = Re::new(que!(sym![sgl!('A')]));
741 assert!(re.is_match(""));
742 }
743
744 #[test]
745 fn test_match_repetition_10() {
746 let re = Re::new(rep!(sym![sgl!('A')], None, Some(3)));
748 assert!(re.is_match(""));
749 }
750
751 #[test]
752 fn test_match_repetition_11() {
753 let re = Re::new(rep!(sym![sgl!('A')], None, Some(3)));
755 assert!(re.is_match("AA"));
756 }
757
758 #[test]
759 fn test_match_repetition_12() {
760 let re = Re::new(rep!(sym![sgl!('A')], None, Some(3)));
762 assert!(re.is_match("AAA"));
763 }
764
765 #[test]
766 fn test_match_repetition_13() {
767 let re = Re::new(rep!(sym![sgl!('A')], None, Some(3)));
769 assert!(!re.is_match("AAAA"));
770 }
771
772 #[test]
773 fn test_match_repetition_14() {
774 let re = Re::new(rep!(sym![sgl!('A')], Some(3), None));
776 assert!(!re.is_match(""));
777 }
778
779 #[test]
780 fn test_match_repetition_15() {
781 let re = Re::new(rep!(sym![sgl!('A')], Some(3), None));
783 assert!(!re.is_match("AA"));
784 }
785
786 #[test]
787 fn test_match_repetition_16() {
788 let re = Re::new(rep!(sym![sgl!('A')], Some(3), None));
790 assert!(re.is_match("AAA"));
791 }
792
793 #[test]
794 fn test_match_repetition_17() {
795 let re = Re::new(rep!(sym![sgl!('A')], Some(3), None));
797 assert!(re.is_match("AAAA"));
798 }
799
800 #[test]
801 fn test_match_repetition_18() {
802 let re = Re::new(rep!(sym![sgl!('A')], Some(3), Some(3)));
804 assert!(!re.is_match(""));
805 }
806
807 #[test]
808 fn test_match_repetition_19() {
809 let re = Re::new(rep!(sym![sgl!('A')], Some(3), Some(3)));
811 assert!(!re.is_match("AA"));
812 }
813
814 #[test]
815 fn test_match_repetition_20() {
816 let re = Re::new(rep!(sym![sgl!('A')], Some(3), Some(3)));
818 assert!(re.is_match("AAA"));
819 }
820
821 #[test]
822 fn test_match_repetition_21() {
823 let re = Re::new(rep!(sym![sgl!('A')], Some(3), Some(3)));
825 assert!(!re.is_match("AAAA"));
826 }
827
828 #[test]
829 fn test_match_repetition_22() {
830 let re = Re::new(rep!(sym![sgl!('A')], Some(2), Some(3)));
832 assert!(!re.is_match(""));
833 }
834
835 #[test]
836 fn test_match_repetition_23() {
837 let re = Re::new(rep!(sym![sgl!('A')], Some(2), Some(3)));
839 assert!(re.is_match("AA"));
840 }
841
842 #[test]
843 fn test_match_repetition_24() {
844 let re = Re::new(rep!(sym![sgl!('A')], Some(2), Some(3)));
846 assert!(re.is_match("AAA"));
847 }
848
849 #[test]
850 fn test_match_repetition_25() {
851 let re = Re::new(rep!(sym![sgl!('A')], Some(2), Some(3)));
853 assert!(!re.is_match("AAAA"));
854 }
855
856 #[test]
857 fn test_match_complex_1() {
858 let re = Re::new(con![ast![alt![sym![sgl!('A')], sym![sgl!('B')]]], alt![con![sym![sgl!('A')], sym![sgl!('A')], sym![sgl!('A')]], con![sym![sgl!('B')], sym![sgl!('B')], sym![sgl!('B')]]], ast![alt![sym![sgl!('A')], sym![sgl!('B')]]]]);
860 assert!(re.is_match("ABBBA"));
861 }
862
863 #[test]
864 fn test_match_complex_2() {
865 let re = Re::new(con![ast![alt![sym![sgl!('A')], sym![sgl!('B')]]], alt![con![sym![sgl!('A')], sym![sgl!('A')], sym![sgl!('A')]], con![sym![sgl!('B')], sym![sgl!('B')], sym![sgl!('B')]]], ast![alt![sym![sgl!('A')], sym![sgl!('B')]]]]);
867 assert!(!re.is_match("ABBA"));
868 }
869
870 #[test]
871 fn test_match_complex_3() {
872 let re = Re::new(con![ast![alt![sym![sgl!('A')], sym![sgl!('B')]]], alt![con![sym![sgl!('A')], sym![sgl!('A')], sym![sgl!('A')]], con![sym![sgl!('B')], sym![sgl!('B')], sym![sgl!('B')]]], ast![alt![sym![sgl!('A')], sym![sgl!('B')]]]]);
874 assert!(re.is_match("ABBAAA"));
875 }
876
877 struct ExpectedEnfa<S, T> {
878 initial: S,
879 transitions: Set<(S, Segment<T>, S)>,
880 finals: Set<S>,
881 }
882
883 fn assert_enfa_eq<S: Clone + Debug + Ord, T: Clone + Debug + Ord>(expected: ExpectedEnfa<S, T>, actual: Enfa<S, T>) {
884 assert_eq!(expected.initial, actual.states_index(actual.initial_index()).clone());
885 assert_eq!(expected.transitions, actual.transitions_slice(actual.transition_indices()).map(|(source, transition, target)| (actual.states_index(source).clone(), transition.clone(), actual.states_index(target).clone())).collect());
886 assert_eq!(expected.finals, actual.states_slice(actual.final_indices()).cloned().collect());
887 }
888
889 struct ExpectedDfa<S, T> {
890 initial: S,
891 transitions: Set<(S, Segment<T>, S)>,
892 finals: Set<S>,
893 }
894
895 fn assert_dfa_eq<S: Clone + Debug + Ord, T: Clone + Debug + Ord>(expected: ExpectedDfa<Set<S>, T>, actual: Dfa<Set<S>, T>) {
896 assert_eq!(expected.initial, actual.states_index(actual.initial_index()).clone());
897 assert_eq!(expected.transitions, actual.transitions_slice(actual.transition_indices()).map(|(source, transition, target)| (actual.states_index(source).clone(), transition.clone(), actual.states_index(target).clone())).collect());
898 assert_eq!(expected.finals, actual.states_slice(actual.final_indices()).cloned().collect());
899 }
900}