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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! The actual implementation of Regex pattern parsing
use super::error::{HelpMsg, SyntaxError, SyntaxErrorKind};
use super::{Ast, AstNode, AstNodeId, AstNodeKind, Span};
use std::mem;
/// Helper macro for defining patterns that match a set of characters
macro_rules! char_pat {
(macro_rules! $name:ident = $($chars:literal),+ $(,)?) => {
macro_rules! $name {
() => { $($chars)|+ }
}
}
}
pub fn parse(pattern: &str) -> Result<Ast, SyntaxError> {
// This function implements a fairly simple shift-reduce parser. Throughout parsing, we add
// completed AST nodes into the backing vector (`nodes`), and we return from inside the loop
// once we reach the end of the input — i.e. the final "reduce" action just returns.
// A "partial" node.
#[derive(Debug)]
enum PartialNode {
// A completed node
Node(AstNodeId),
// A list of concatenated nodes
Concat(Vec<AstNodeId>),
// A parenthesized sub-expression. The full parsing for such a group would be:
Group {
start_span: Span,
},
// // A run of literal values in the regex. The actual contents are determined by the span
Literal {
span: Span,
},
// A list of alternatives (i.e. <EXPR> "|" <EXPR> ...)
Alternate {
span: Span,
alts: Vec<Option<AstNodeId>>,
trailing_pipe: bool,
},
}
use PartialNode::*;
// Completed AST nodes
let mut nodes: Vec<AstNode> = Vec::new();
// Note: the stack is always non-empty.
let mut stack: Vec<PartialNode> = vec![];
// An iterator over characters *and their span*
let mut chars = char_ranges(pattern).peekable();
let reserved_chars = &['|', '(', ')'];
let is_special_char = |c| reserved_chars.contains(&c);
char_pat!(macro_rules! ends_concat = '|', ')');
char_pat!(macro_rules! ends_alt = ')');
enum Consume {
Yes,
No,
}
loop {
let consume = match (chars.peek(), stack.as_mut_slice()) {
// ----- Success & end-Errors -----
// Success condition: If we get to the end of the input with a single completed
// node, then we finished our AST
(None, [Node(id)]) => return Ok(Ast { root: *id, nodes }),
// The string was empty. We shouldn't be given empty strings.
(None, []) => {
return Err(SyntaxError {
span: Span { start: 0, end: 0 },
msg: SyntaxErrorKind::EmptyPattern,
help: &[],
})
}
// Unclosed parenthesis
(None, [.., Group { start_span }] | [.., Group { start_span }, Node(_)]) => {
return Err(SyntaxError {
span: *start_span,
msg: SyntaxErrorKind::UnclosedDelim { name: "parenthesis" },
help: &[HelpMsg::EscapeToMatchLiteral {
name: "open parenthesis",
escaped: r"\(",
}],
})
}
// ----- Literal -----
// shift: Literal + * -> Literal (special case for efficiency)
(Some((sp, c)), [.., Literal { span }]) if !is_special_char(*c) => {
*span = span.join(*sp);
Consume::Yes
}
// shift: * -> Literal
(Some((span, c)), _) if !is_special_char(*c) => {
stack.push(Literal { span: *span });
Consume::Yes
}
// reduce: Literal -> Node
(_, [.., Literal { span }]) => {
let node = AstNode {
span: *span,
kind: AstNodeKind::Literal(&pattern[span.start..span.end]),
};
let id = AstNodeId(nodes.len());
nodes.push(node);
stack.pop();
stack.push(Node(id));
Consume::No
}
// ----- Concat -----
// reduce: Node + Node -> Concat
(_, [.., Node(fst_id), Node(snd_id)]) => {
let node = Concat(vec![*fst_id, *snd_id]);
let _ = (stack.pop(), stack.pop());
stack.push(node);
Consume::No
}
// reduce: Concat + Node -> Concat
(_, [.., Concat(n_ids), Node(id)]) => {
n_ids.push(*id);
stack.pop(); // Remove Node from the stack
Consume::No
}
// reduce: Concat -> Node
(None | Some((_, ends_concat!())), [.., Concat(n_ids)]) => {
let fst = n_ids.first().unwrap();
let lst = n_ids.last().unwrap();
let node = AstNode {
span: nodes[fst.0].span.join(nodes[lst.0].span),
kind: AstNodeKind::Concat(mem::take(n_ids)),
};
let id = AstNodeId(nodes.len());
nodes.push(node);
// Remove the existing Concat & replace it:
stack.pop();
stack.push(Node(id));
Consume::No
}
// ----- Alternate -----
// reduce: Alternate (empty end) + Node -> Alternate (nonempty end)
(_, [.., Alternate { span, alts, trailing_pipe }, Node(id)]) if *trailing_pipe => {
*span = span.join(nodes[id.0].span);
alts.push(Some(*id));
*trailing_pipe = false;
stack.pop();
Consume::No
}
// shift: Node + "|" -> Alternate
(Some((span, '|')), [.., Node(id)]) => {
let span = nodes[id.0].span.join(*span);
let alt = Alternate {
span,
alts: vec![Some(*id)],
trailing_pipe: true,
};
stack.pop();
stack.push(alt);
Consume::Yes
}
// shift: [group start] + "|" -> Alternate
(Some((span, '|')), [.., Group { .. }] | []) => {
let alt = Alternate {
span: *span,
alts: vec![None], // single alt for the empty start
trailing_pipe: true,
};
stack.push(alt);
Consume::Yes
}
// shift: Alternate (nonempty end) + "|" -> Alternate (empty end)
(Some((p, '|')), [.., Alternate { span, trailing_pipe, .. }]) if !*trailing_pipe => {
*span = span.join(*p);
*trailing_pipe = true;
Consume::Yes
}
// reduce: Alternate -> Node
(None | Some((_, ends_alt!())), [.., Alternate { span, alts, trailing_pipe }]) => {
if *trailing_pipe {
alts.push(None);
}
let node = AstNode {
span: *span,
kind: AstNodeKind::Alternate(mem::take(alts)),
};
let id = AstNodeId(nodes.len());
nodes.push(node);
stack.pop();
stack.push(Node(id));
Consume::No
}
// ----- Group -----
// shift: '(' -> Group
(Some((span, '(')), _) => {
stack.push(Group { start_span: *span });
Consume::Yes
}
// shift: Group + Node + ')' -> Node
(Some((span, ')')), [.., Group { start_span }, Node(id)]) => {
let node = AstNode {
span: start_span.join(*span),
kind: AstNodeKind::Group(Some(*id)),
};
let id = AstNodeId(nodes.len());
nodes.push(node);
// Remove the Group and Node
let _ = (stack.pop(), stack.pop());
stack.push(Node(id));
Consume::Yes
}
// shift: Group + ')' -> Node
(Some((span, ')')), [.., Group { start_span }]) => {
let node = AstNode {
span: start_span.join(*span),
kind: AstNodeKind::Group(None),
};
let id = AstNodeId(nodes.len());
nodes.push(node);
stack.pop();
stack.push(Node(id));
Consume::Yes
}
// ----- Errors -----
// * + ')' -> Error
(Some((span, ')')), _) => {
return Err(SyntaxError {
span: *span,
msg: SyntaxErrorKind::UnexpectedCloseDelim { name: "parenthesis" },
help: &[HelpMsg::EscapeToMatchLiteral {
name: "close parenthesis",
escaped: r"\)",
}],
})
}
// ----- Nothing else applies -----
#[allow(unused_variables)]
(next, _) => {
// // for debugging the parser, it can be useful to print the stack:
// println!("stack: {:?}", stack);
// println!("next: {:?}", next);
unreachable!();
}
};
if let Consume::Yes = consume {
chars.next();
}
}
}
fn char_ranges(string: &str) -> impl '_ + Iterator<Item = (Span, char)> {
use std::str::CharIndices;
struct Iter<'s> {
original_len: usize,
next: Option<(usize, char)>,
chars: CharIndices<'s>,
}
impl<'s> Iterator for Iter<'s> {
type Item = (Span, char);
fn next(&mut self) -> Option<Self::Item> {
let (start, c) = self.next?;
self.next = self.chars.next();
let end = self.next.map(|(i, _)| i).unwrap_or(self.original_len);
Some((Span { start, end }, c))
}
}
let mut chars = string.char_indices();
let next = chars.next();
Iter { original_len: string.len(), next, chars }
}