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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::{vec, char, fmt, str};
use std::collections::BTreeSet;
use std::iter::Peekable;
use self::Regex::*;
use dfa::Normalize;

/// A regular expression over the alphabet `T`.
#[derive(PartialOrd, Ord, PartialEq, Eq, Debug, Clone)]
pub enum Regex<T> {
    /// The null set. This never matches anything.
    Null,
    /// The empty string (matches exactly "").
    Empty,
    /// Matches any single character except the listed ones.
    Except(Vec<T>),
    /// Alternation (also known as disjunction). Matches any of the contained
    /// characters, as well as any string matched by a contained regex.
    Alt(Vec<T>, Vec<Regex<T>>),
    /// Conjunction. Matches iff all contained regexes match.
    And(Vec<Regex<T>>),
    /// Negation. Matches iff the contained regex does not match.
    Not(Box<Regex<T>>),
    /// Concatenation. Matches iff the contained regexes match in sequence.
    Cat(Vec<Regex<T>>),
    /// Kleene closure. Matches zero or more repetitions of the contained regex.
    Kleene(Box<Regex<T>>),
}

struct Puller<A, B, Fun: FnMut(A) -> Result<Vec<A>, B>, Iter: Iterator> {
    s: Iter,
    f: Fun,
    cur: Vec<vec::IntoIter<A>>,
}

trait Pull<A>: Iterator + Sized {
    fn pull<B, Fun: FnMut(A) -> Result<Vec<A>, B>>(self, f: Fun) -> Puller<A, B, Fun, Self>;
}

impl<A, It: Iterator<Item=A>> Pull<A> for It {
    fn pull<B, Fun: FnMut(A) -> Result<Vec<A>, B>>(self, f: Fun) -> Puller<A, B, Fun, Self> {
        Puller {
            s: self,
            f: f,
            cur: Vec::new()
        }
    }
}

impl<A, B, Fun: FnMut(A) -> Result<Vec<A>, B>, Iter: Iterator<Item=A>> Iterator for Puller<A, B, Fun, Iter> {
    type Item = B;
    fn next(&mut self) -> Option<B> {
        let mut el = None;
        while let Some(mut it) = self.cur.pop() {
            if let Some(y) = it.next() {
                el = Some(y);
                self.cur.push(it);
                break;
            }
        }
        if el.is_none() {
            el = self.s.next();
        }
        match el {
            Some(val) => {
                match (self.f)(val) {
                    Ok(v) => {
                        self.cur.push(v.into_iter());
                        self.next()
                    }
                    Err(it) => {
                        Some(it)
                    }
                }
            }
            None => None
        }
    }
}

impl<T: Ord> Normalize for Regex<T> {
    fn normalize(self) -> Self {
        let not_null = Not(Box::new(Null)); // FIXME: allocation here ;_;
        match self {
            Null => Null,
            Empty => Empty,
            Except(a) => Except(a.into_iter().collect::<BTreeSet<_>>().into_iter().collect()),
            Alt(a, xs) => {
                let mut chars = BTreeSet::new();
                for c in a.into_iter() {
                    chars.insert(c);
                }
                let mut xs: BTreeSet<_> = xs.into_iter().map(Normalize::normalize).pull(|x| match x {
                    Alt(cs, v) => {
                        for c in cs.into_iter() {
                            chars.insert(c);
                        }
                        Ok(v)
                    }
                    x => Err(x)
                }).collect();

                if xs.contains(&not_null) {
                    return not_null;
                }
                xs.remove(&Null);

                let chars: Vec<_> = chars.into_iter().collect();
                let mut xs: Vec<_> = xs.into_iter().collect();

                match (chars.len(), xs.len()) {
                    (0, 0) => Null,
                    (0, 1) => xs.pop().unwrap(),
                    _      => Alt(chars, xs)
                }
            }
            And(xs) => {
                let mut xs: BTreeSet<_> = xs.into_iter().map(Normalize::normalize).pull(|x| match x {
                    And(v) => Ok(v),
                    x => Err(x)
                }).collect();
                if xs.contains(&Null) {
                    return Null;
                }
                xs.remove(&not_null);
                let mut xs: Vec<_> = xs.into_iter().collect();
                match xs.len() {
                    0 => not_null,
                    1 => xs.pop().unwrap(),
                    _ => And(xs)
                }
            }
            Not(x) => {
                match x.normalize() {
                    Not(y) => *y,
                    y => Not(Box::new(y))
                }
            }
            Cat(xs) => {
                let mut killed = false;
                let mut xs: Vec<_> = xs.into_iter().map(Normalize::normalize).pull(|x| match x {
                    Cat(v) => Ok(v),
                    x => Err(x)
                }).filter(|x| match *x {
                    Null => {
                        killed = true;
                        false
                    }
                    Empty => false,
                    _ => true
                }).collect();
                if killed {
                    return Null;
                }
                match xs.len() {
                    0 => Empty,
                    1 => xs.pop().unwrap(),
                    _ => Cat(xs)
                }
            }
            Kleene(x) => {
                match x.normalize() {
                    Kleene(y) => Kleene(y),
                    Null => Empty,
                    Empty => Empty,
                    Except(ref chs) if chs.len() == 0 => not_null,
                    y => Kleene(Box::new(y)),
                }
            }
        }
    }
}

/*
Char : NORMAL
     : '\' CHAR
Chars :
      : Char Chars
Atom : Char
     : '(' Alt ')'
     : '[' Chars ']'
Kleene : Atom
       : Kleene '*'
       : Kleene '+'
       : Kleene '?'
Cat : Kleene
    : Kleene Cat
Not : Cat
    : '~' Not
And : Not
    : Not '&' And
Alt : And
    : And '|' Alt
*/
#[derive(Copy, Clone, Debug)]
pub enum ParseError {
    UnexpectedEof(&'static str),
    UnexpectedChar(&'static str, char),
    BadRange(&'static str, char, char),
}
impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ParseError::UnexpectedEof(s) => write!(f, "{}", s),
            ParseError::UnexpectedChar(s, c) => write!(f, "{}: `{}`", s, c),
            ParseError::BadRange(s, c, d) => write!(f, "{}: `{}-{}`", s, c, d),
        }
    }
}
struct Parser<I: Iterator<Item=char>> {
    it: Peekable<I>,
}
type Res<T> = Result<T, ParseError>;
impl<I: Iterator<Item=char>> Parser<I> {
    fn char(&mut self) -> Res<char> {
        match self.it.next() {
            Some('\\') => {
                match self.it.next() {
                    Some('r') => Ok('\r'),
                    Some('n') => Ok('\n'),
                    Some('t') => Ok('\t'),
                    Some(c) => Ok(c),
                    None => Err(ParseError::UnexpectedEof("unfollowed '\\'"))
                }
            }
            Some(c) => {
                Ok(c)
            }
            None => panic!("char not nullable")
        }
    }
    fn char_first(c: char) -> bool {
        !['~','|','&','[',']','(',')','*','+','~','.','?'].contains(&c)
    }
    fn char_group(c: char) -> bool {
        c != ']'
    }
    fn chars(&mut self) -> Res<Vec<char>> {
        let mut v = Vec::new();
        loop {
            match self.it.peek() {
                Some(&c) if Parser::<I>::char_group(c) => {
                    let c = try!(self.char());
                    if let Some(&'-') = self.it.peek() {
                        self.it.next();
                        if let None = self.it.peek() {
                            return Err(ParseError::UnexpectedEof("unterminated range"));
                        }
                        let d = try!(self.char());
                        // FIXME: This should be an inclusive range.
                        for x in (c as u64)..(d as u64 + 1) {
                            if let Some(x) = char::from_u32(x as u32) {
                                v.push(x);
                            } else {
                                return Err(ParseError::BadRange("range contains bad codepoints", c, d));
                            }
                        }
                    } else {
                        v.push(c);
                    }
                }
                _ => {
                    break;
                }
            }
        }
        Ok(v)
    }
    fn atom(&mut self) -> Res<Regex<char>> {
        match self.it.peek() {
            Some(&'(') => {
                self.it.next();
                let r = try!(self.alt());
                match self.it.next() {
                    Some(')') => Ok(r),
                    Some(c) => Err(ParseError::UnexpectedChar("unexpected character", c)),
                    None => Err(ParseError::UnexpectedEof("unmatched '('")),
                }
            }
            Some(&'[') => {
                self.it.next();
                let except = if let Some(&'^') = self.it.peek() {
                    self.it.next();
                    true
                } else {
                    false
                };
                let r = try!(self.chars());
                match self.it.next() {
                    Some(']') => Ok(if except {
                        Except(r)
                    } else {
                        Alt(r, Vec::new())
                    }),
                    Some(c) => Err(ParseError::UnexpectedChar("bad character for character class", c)),
                    None => Err(ParseError::UnexpectedEof("unmatched '['")),
                }
            }
            Some(&'.') => {
                self.it.next();
                Ok(Except(Vec::new()))
            }
            Some(_) => Ok(Alt(vec![try!(self.char())], Vec::new())),
            None => panic!("atom not nullable"),
        }
    }
    fn atom_first(c: char) -> bool {
        c == '(' || c == '[' || c == '.' || Parser::<I>::char_first(c)
    }
    fn kleene(&mut self) -> Res<Regex<char>> {
        let mut r = try!(self.atom());
        loop {
            match self.it.peek() {
                Some(&'*') => {
                    self.it.next();
                    r = Kleene(Box::new(r))
                }
                Some(&'+') => {
                    self.it.next();
                    r = Cat(vec![r.clone(), Kleene(Box::new(r))])
                }
                Some(&'?') => {
                    self.it.next();
                    r = Alt(vec![], vec![Empty, r])
                }
                _ => break,
            }
        }
        Ok(r)
    }
    fn kleene_first(c: char) -> bool {
        Parser::<I>::atom_first(c)
    }
    fn cat(&mut self) -> Res<Regex<char>> {
        let mut r = Vec::new();
        loop {
            match self.it.peek() {
                Some(&c) if Parser::<I>::kleene_first(c) => r.push(try!(self.kleene())),
                _ => break,
            }
        }
        Ok(Cat(r))
    }
    fn not(&mut self) -> Res<Regex<char>> {
        match self.it.peek() {
            Some(&'~') => {
                self.it.next();
                Ok(Not(Box::new(try!(self.not()))))
            }
            _ => self.cat()
        }
    }
    fn and(&mut self) -> Res<Regex<char>> {
        let mut r = vec![try!(self.not())];
        loop {
            match self.it.peek() {
                Some(&'&') => {
                    self.it.next();
                    r.push(try!(self.not()));
                }
                _ => break,
            }
        }
        Ok(And(r))
    }
    fn alt(&mut self) -> Res<Regex<char>> {
        let mut r = vec![try!(self.and())];
        loop {
            match self.it.peek() {
                Some(&'|') => {
                    self.it.next();
                    r.push(try!(self.and()));
                }
                _ => break,
            }
        }
        Ok(Alt(Vec::new(),r))
    }
    fn parse(it: I) -> Res<Regex<char>> {
        let mut parser = Parser { it: it.peekable() };
        let r = try!(parser.alt());
        if let Some(c) = parser.it.next() {
            Err(ParseError::UnexpectedChar("bad character in regex", c))
        } else {
            Ok(r)
        }
    }
}

impl str::FromStr for Regex<char> {
    type Err = ParseError;
    /// Parse a string as a regular expression.
    fn from_str(s: &str) -> Result<Regex<char>, ParseError> {
        Parser::parse(s.chars())
    }
}

impl<T> Regex<T> {
    /// Tests whether a regular expression is nullable, i.e. whether it matches
    /// the empty string.
    pub fn nullable(&self) -> bool {
        // FIXME: This could be inefficient. Try to cache it in the data structure
        match *self {
            Null => false,
            Empty => true,
            Except(_) => false,
            Alt(_, ref xs) => xs.iter().any(Regex::nullable),
            And(ref xs) => xs.iter().all(Regex::nullable),
            Not(ref x) => !x.nullable(),
            Cat(ref xs) => xs.iter().all(Regex::nullable),
            Kleene(_) => true,
        }
    }
}