pag_lexer/
lib.rs

1// Copyright (c) 2023 Paguroidea Developers
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9#![feature(let_chains)]
10#![feature(array_chunks)]
11#![feature(portable_simd)]
12
13pub mod congruence;
14pub mod derivative;
15pub mod intervals;
16pub mod lookahead;
17pub mod normalization;
18pub mod regex_tree;
19pub mod vector;
20
21#[cfg(test)]
22mod tests {
23    use crate::congruence::approximate_congruence_class;
24    use crate::derivative::derivative;
25    use crate::lookahead::LoopOptimizer;
26    use crate::normalization::normalize;
27    use crate::regex_tree::*;
28    use crate::vector::Vector;
29    use std::rc::Rc;
30    use RegexTree::*;
31
32    #[test]
33    fn it_prints_basic() {
34        let a = Rc::new(RegexTree::single(b'a'));
35        let b = Rc::new(RegexTree::single(b'b'));
36        let ab = Rc::new(Concat(a, b));
37        let alt = Rc::new(Union(ab.clone(), ab));
38        println!("{}", alt);
39        let derivative = derivative(alt, b'a');
40        println!("{}", derivative);
41        let normalized = normalize(Rc::new(derivative));
42        println!("{}", normalized);
43        println!("{:?}", approximate_congruence_class(&normalized));
44    }
45
46    #[test]
47    fn renormalize_tests() {
48        // concat
49        let a = Rc::new(RegexTree::single(b'a'));
50        let b = Rc::new(RegexTree::single(b'b'));
51        let concat = Rc::new(Concat(a.clone(), b));
52        let normalized = normalize(concat.clone());
53        assert!(Rc::ptr_eq(&concat, &normalized));
54        // kleene closure
55        let kleene = Rc::new(KleeneClosure(a));
56        let normalized = normalize(kleene.clone());
57        assert!(Rc::ptr_eq(&kleene, &normalized));
58    }
59
60    #[test]
61    fn beautify_mangle_tests() {
62        // generate huge test for me
63        let a = Rc::new(RegexTree::single(b'a'));
64        let b = Rc::new(RegexTree::single(b'b'));
65        let c = Rc::new(RegexTree::single(b'c'));
66        let d = Rc::new(RegexTree::single(b'd'));
67        let ba = Rc::new(Concat(b, a.clone()));
68        let a_or_ba = Rc::new(Union(a, ba));
69        let a_or_ba_or_c = Rc::new(Union(a_or_ba, c));
70        let a_or_ba_or_c_con_d = Rc::new(KleeneClosure(Rc::new(Concat(a_or_ba_or_c, d))));
71        let normalized = normalize(a_or_ba_or_c_con_d);
72        let congruence = approximate_congruence_class(&normalized);
73        println!("{:?}", congruence);
74        let vectorized = Vector::new([normalized]);
75        let mut optimizer = LoopOptimizer::new();
76        println!(
77            "{}",
78            vectorized.generate_dfa("polo".to_string(), &mut optimizer)
79        );
80    }
81
82    #[test]
83    fn approximate_congruence_class_test() {
84        let a = Rc::new(RegexTree::single(b'a'));
85        let b = Rc::new(RegexTree::single(b'b'));
86        let c = Rc::new(RegexTree::single(b'c'));
87        let ba = Rc::new(Concat(b, a.clone()));
88        let a_or_ba = Rc::new(Union(a, ba));
89        let a_or_ba_or_c = Rc::new(KleeneClosure(Rc::new(Union(a_or_ba, c))));
90        println!("{}", a_or_ba_or_c);
91        let normalized = normalize(a_or_ba_or_c);
92        println!("{}", normalized);
93        let congruence = approximate_congruence_class(&normalized);
94        println!("{:?}", congruence);
95        println!();
96        let vectorized = Vector::new([normalized]);
97        let mut optimizer = LoopOptimizer::new();
98        println!(
99            "{}",
100            vectorized.generate_dfa("test".to_string(), &mut optimizer)
101        );
102    }
103}