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
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use serde::Serialize;
use std::mem;
use string_interner::symbol::SymbolU32;

#[derive(Debug, Clone, Serialize)]
pub enum Node {
    Terminal(String),
    RegexString(String),
    Nonterminal(String),
    Multiple(Vec<Node>),
    RegexExt(Box<Node>, RegexExtKind),
    Symbol(Box<Node>, SymbolKind, Box<Node>),
    Group(Box<Node>),
    EarlyEndRegexString(String),
    Substrings(String),
}

impl Drop for Node {
    fn drop(&mut self) {
        let mut stack = vec![];
        match self {
            Node::Terminal(_) | Node::RegexString(_) | Node::Nonterminal(_) => {}
            Node::Multiple(nodes) => {
                while let Some(node) = nodes.pop() {
                    stack.push(node);
                }
            }
            Node::RegexExt(node, _) => {
                let node = mem::replace(node.as_mut(), Node::Terminal(String::new()));
                stack.push(node);
            }
            Node::Symbol(lhs, _, rhs) => {
                let lhs = mem::replace(lhs.as_mut(), Node::Terminal(String::new()));
                let rhs = mem::replace(rhs.as_mut(), Node::Terminal(String::new()));
                stack.push(lhs);
                stack.push(rhs);
            }
            Node::Group(node) => {
                let node = mem::replace(node.as_mut(), Node::Terminal(String::new()));
                stack.push(node);
            }
            Node::EarlyEndRegexString(_) => {}
            Node::Substrings(_) => {}
        };
        while let Some(mut node) = stack.pop() {
            match &mut node {
                Node::Multiple(nodes) => {
                    while let Some(node) = nodes.pop() {
                        stack.push(node);
                    }
                }
                Node::RegexExt(node, _) => {
                    let node = mem::replace(node.as_mut(), Node::Terminal(String::new()));
                    stack.push(node);
                }
                Node::Symbol(lhs, _, rhs) => {
                    let lhs = mem::replace(lhs.as_mut(), Node::Terminal(String::new()));
                    let rhs = mem::replace(rhs.as_mut(), Node::Terminal(String::new()));
                    stack.push(lhs);
                    stack.push(rhs);
                }
                Node::Group(node) => {
                    let node = mem::replace(node.as_mut(), Node::Terminal(String::new()));
                    stack.push(node);
                }
                _ => {}
            }
        }
    }
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone)]
pub enum NodeWithID {
    Terminal(SymbolU32),
    RegexString(SymbolU32),
    Nonterminal(SymbolU32),
    Multiple(Vec<NodeWithID>),
    RegexExt(Box<NodeWithID>, RegexExtKind),
    Symbol(Box<NodeWithID>, SymbolKind, Box<NodeWithID>),
    Group(Box<NodeWithID>),
    EarlyEndRegexString(SymbolU32),
    Substrings(SymbolU32),
    Unknown,
}

impl Drop for NodeWithID {
    fn drop(&mut self) {
        let mut stack = vec![];
        match self {
            NodeWithID::Terminal(_) | NodeWithID::RegexString(_) | NodeWithID::Nonterminal(_) => {}
            NodeWithID::Multiple(nodes) => {
                while let Some(node) = nodes.pop() {
                    stack.push(node);
                }
            }
            NodeWithID::RegexExt(node, _) => {
                let node = mem::replace(node.as_mut(), NodeWithID::Unknown);
                stack.push(node);
            }
            NodeWithID::Symbol(lhs, _, rhs) => {
                let lhs = mem::replace(lhs.as_mut(), NodeWithID::Unknown);
                let rhs = mem::replace(rhs.as_mut(), NodeWithID::Unknown);
                stack.push(lhs);
                stack.push(rhs);
            }
            NodeWithID::Group(node) => {
                let node = mem::replace(node.as_mut(), NodeWithID::Unknown);
                stack.push(node);
            }
            NodeWithID::EarlyEndRegexString(_) => {}
            NodeWithID::Substrings(_) => {}
            NodeWithID::Unknown => {}
        };
        while let Some(mut node) = stack.pop() {
            match &mut node {
                NodeWithID::Multiple(nodes) => {
                    while let Some(node) = nodes.pop() {
                        stack.push(node);
                    }
                }
                NodeWithID::RegexExt(node, _) => {
                    let node = mem::replace(node.as_mut(), NodeWithID::Unknown);
                    stack.push(node);
                }
                NodeWithID::Symbol(lhs, _, rhs) => {
                    let lhs = mem::replace(lhs.as_mut(), NodeWithID::Unknown);
                    let rhs = mem::replace(rhs.as_mut(), NodeWithID::Unknown);
                    stack.push(lhs);
                    stack.push(rhs);
                }
                NodeWithID::Group(node) => {
                    let node = mem::replace(node.as_mut(), NodeWithID::Unknown);
                    stack.push(node);
                }
                _ => {}
            }
        }
    }
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone)]
pub(crate) enum NoNestingNode {
    Unknown,
    Terminal(SymbolU32),
    RegexString(SymbolU32),
    Nonterminal(SymbolU32),
    Concatenations(Vec<NoNestingNode>),
    Alternations(Vec<NoNestingNode>),
    EarlyEndRegexString(SymbolU32),
    Substrings(SymbolU32),
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub enum OperatorFlattenedNode {
    Terminal(SymbolU32),
    RegexString(SymbolU32),
    Nonterminal(SymbolU32),
    EarlyEndRegexString(SymbolU32),
    Substrings(SymbolU32),
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Rhs {
    pub alternations: Vec<Alternation>,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct Alternation {
    pub concatenations: Vec<OperatorFlattenedNode>,
}
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq, Hash)]
pub enum RegexExtKind {
    Repeat0,
    Repeat1,
    Optional,
}

#[derive(Debug, Clone, Serialize, Copy)]
pub enum SymbolKind {
    Concatenation,
    Alternation,
}