chrono_english/
parser.rs

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
use errors::*;
use scanlex::{Scanner, Token};
use types::*;

// when we parse dates, there's often a bit of time parsed..
#[derive(Clone, Copy, Debug)]
enum TimeKind {
    Formal,
    Informal,
    AmPm(bool),
    Unknown,
}

pub struct DateParser<'a> {
    s: Scanner<'a>,
    direct: Direction,
    maybe_time: Option<(u32, TimeKind)>,
    pub american: bool, // 9/11, not 20/03
}

impl<'a> DateParser<'a> {
    pub fn new(text: &'a str) -> DateParser<'a> {
        DateParser {
            s: Scanner::new(text).no_float(),
            direct: Direction::Here,
            maybe_time: None,
            american: false,
        }
    }

    pub fn american_date(mut self) -> DateParser<'a> {
        self.american = true;
        self
    }

    fn iso_date(&mut self, y: u32) -> DateResult<DateSpec> {
        let month = self.s.get_int::<u32>()?;
        self.s.get_ch_matching(&['-'])?;
        let day = self.s.get_int::<u32>()?;
        Ok(DateSpec::absolute(y, month, day))
    }

    fn informal_date(&mut self, day_or_month: u32) -> DateResult<DateSpec> {
        let month_or_day = self.s.get_int::<u32>()?;
        let (d, m) = if self.american {
            (month_or_day, day_or_month)
        } else {
            (day_or_month, month_or_day)
        };
        Ok(if self.s.peek() == '/' {
            self.s.get();
            let y = self.s.get_int::<u32>()?;
            let y = if y < 100 {
                // pivot (1940, 2040)
                if y > 40 {
                    1900 + y
                } else {
                    2000 + y
                }
            } else {
                y
            };
            DateSpec::absolute(y, m, d)
        } else {
            DateSpec::FromName(ByName::from_day_month(d, m, self.direct))
        })
    }

    fn parse_date(&mut self) -> DateResult<Option<DateSpec>> {
        let mut t = self.s.next().or_err("empty date string")?;

        let sign = if t.is_char() && t.as_char().unwrap() == '-' {
            true
        } else {
            false
        };
        if sign {
            t = self.s.next().or_err("nothing after '-'")?;
        }
        if let Some(name) = t.as_iden() {
            let shortcut = match name {
                "now" => Some(0),
                "today" => Some(0),
                "yesterday" => Some(-1),
                "tomorrow" => Some(1),
                _ => None,
            };
            if let Some(skip) = shortcut {
                return Ok(Some(DateSpec::skip(time_unit("day").unwrap(), skip)));
            } else
            // maybe next or last?
            if let Some(d) = Direction::from_name(&name) {
                self.direct = d;
            }
        }
        if self.direct != Direction::Here {
            t = self.s.next().or_err("nothing after last/next")?;
        }
        Ok(match t {
            Token::Iden(ref name) => {
                let name = name.to_lowercase();
                // maybe weekday or month name?
                if let Some(by_name) = ByName::from_name(&name, self.direct) {
                    // however, MONTH _might_ be followed by DAY, YEAR
                    if let Some(month) = by_name.as_month() {
                        let t = self.s.get();
                        if t.is_integer() {
                            let day = t.to_int_result::<u32>()?;
                            return Ok(Some(if self.s.peek() == ',' {
                                self.s.get_char()?; // eat ','
                                let year = self.s.get_int::<u32>()?;
                                DateSpec::absolute(year, month, day)
                            } else {
                                // MONTH DAY is like DAY MONTH (tho no time!)
                                DateSpec::from_day_month(day, month, self.direct)
                            }));
                        }
                    }
                    Some(DateSpec::FromName(by_name))
                } else {
                    return date_result("expected week day or month name");
                }
            }
            Token::Int(_) => {
                let n = t.to_int_result::<u32>()?;
                let t = self.s.get();
                if t.finished() {
                    // must be a year...
                    return Ok(Some(DateSpec::absolute(n, 1, 1)));
                }
                match t {
                    Token::Iden(ref name) => {
                        let day = n;
                        let name = name.to_lowercase();
                        if let Some(month) = month_name(&name) {
                            if let Ok(year) = self.s.get_int::<u32>() {
                                // 4 July 2017
                                Some(DateSpec::absolute(year, month, day))
                            } else {
                                // 4 July
                                Some(DateSpec::from_day_month(day, month, self.direct))
                            }
                        } else if let Some(u) = time_unit(&name) {
                            // '2 days'
                            let mut n = n as i32;
                            if sign {
                                n = -n;
                            } else {
                                let t = self.s.get();
                                let got_ago = if let Some(name) = t.as_iden() {
                                    if name == "ago" {
                                        n = -n;
                                        true
                                    } else {
                                        return date_result("only expected 'ago'");
                                    }
                                } else {
                                    false
                                };
                                if !got_ago {
                                    if let Some(h) = t.to_integer() {
                                        self.maybe_time = Some((h as u32, TimeKind::Unknown));
                                    }
                                }
                            }
                            Some(DateSpec::skip(u, n))
                        } else if name == "am" || name == "pm" {
                            self.maybe_time = Some((n, TimeKind::AmPm(name == "pm")));
                            None
                        } else {
                            return date_result("expected month or time unit");
                        }
                    }
                    Token::Char(ch) => match ch {
                        '-' => Some(self.iso_date(n)?),
                        '/' => Some(self.informal_date(n)?),
                        ':' | '.' => {
                            let kind = if ch == ':' {
                                TimeKind::Formal
                            } else {
                                TimeKind::Informal
                            };
                            self.maybe_time = Some((n, kind));
                            None
                        }
                        _ => return date_result(&format!("unexpected char {:?}", ch)),
                    },
                    _ => return date_result(&format!("unexpected token {:?}", t)),
                }
            }
            _ => return date_result(&format!("not expected token {:?}", t)),
        })
    }

    fn formal_time(&mut self, hour: u32) -> DateResult<TimeSpec> {
        let min = self.s.get_int::<u32>()?;
        // minute may be followed by [:secs][am|pm]
        let mut tnext = None;
        let sec = if let Some(t) = self.s.next() {
            if let Some(ch) = t.as_char() {
                if ch != ':' {
                    return date_result("expecting ':'");
                }
                self.s.get_int::<u32>()?
            } else {
                tnext = Some(t);
                0
            }
        } else {
            0
        };
        // we found seconds, look ahead
        if tnext.is_none() {
            tnext = self.s.next();
        }
        let micros = if let Some(Some('.')) = tnext.as_ref().map(|t| t.as_char()) {
            let frac = self.s.grab_while(char::is_numeric);
            if frac.is_empty() {
                return date_result("expected fractional second after '.'");
            }
            let frac = "0.".to_owned() + &frac;
            let micros_f = frac.parse::<f64>().unwrap() * 1.0e6;
            tnext = self.s.next();
            micros_f as u32
        } else {
            0
        };
        if tnext.is_none() {
            Ok(TimeSpec::new(hour, min, sec, micros))
        } else {
            let tok = tnext.as_ref().unwrap();
            if let Some(ch) = tok.as_char() {
                let expecting_offset = match ch {
                    '+' | '-' => true,
                    _ => return date_result("expected +/- before timezone"),
                };
                let offset = if expecting_offset {
                    let h = self.s.get_int::<u32>()?;
                    let (h, m) = if self.s.peek() == ':' {
                        // 02:00
                        self.s.nextch();
                        (h, self.s.get_int::<u32>()?)
                    } else {
                        // 0030 ....
                        let hh = h;
                        let h = hh / 100;
                        let m = hh % 100;
                        (h, m)
                    };
                    let res = 60 * (m + 60 * h);
                    (res as i64) * if ch == '-' { -1 } else { 1 }
                } else {
                    0
                };
                Ok(TimeSpec::new_with_offset(hour, min, sec, offset, micros))
            } else if let Some(id) = tok.as_iden() {
                if id == "Z" {
                    Ok(TimeSpec::new_with_offset(hour, min, sec, 0, micros))
                } else {
                    // am or pm
                    let hour = DateParser::am_pm(&id, hour)?;
                    Ok(TimeSpec::new(hour, min, sec, micros))
                }
            } else {
                Ok(TimeSpec::new(hour, min, sec, micros))
            }
        }
    }

    fn informal_time(&mut self, hour: u32) -> DateResult<TimeSpec> {
        let min = self.s.get_int::<u32>()?;
        let hour = if let Some(t) = self.s.next() {
            let name = t.to_iden_result()?;
            DateParser::am_pm(&name, hour)?
        } else {
            hour
        };
        Ok(TimeSpec::new(hour, min, 0, 0))
    }

    fn am_pm(name: &str, mut hour: u32) -> DateResult<u32> {
        if name == "pm" {
            hour += 12;
        } else if name != "am" {
            return date_result("expected am or pm");
        }
        Ok(hour)
    }

    fn hour_time(name: &str, hour: u32) -> DateResult<TimeSpec> {
        Ok(TimeSpec::new(DateParser::am_pm(name, hour)?, 0, 0, 0))
    }

    fn parse_time(&mut self) -> DateResult<Option<TimeSpec>> {
        // here the date parser looked ahead and saw an hour followed by some separator
        if let Some(hour_sep) = self.maybe_time {
            // didn't see a separator, so look...
            let (h, mut kind) = hour_sep;
            if let TimeKind::Unknown = kind {
                kind = match self.s.get_char()? {
                    ':' => TimeKind::Formal,
                    '.' => TimeKind::Informal,
                    ch => return date_result(&format!("expected : or ., not {}", ch)),
                };
            }
            Ok(Some(match kind {
                TimeKind::Formal => self.formal_time(h)?,
                TimeKind::Informal => self.informal_time(h)?,
                TimeKind::AmPm(is_pm) => DateParser::hour_time(if is_pm { "pm" } else { "am" }, h)?,
                TimeKind::Unknown => unreachable!(),
            }))
        } else {
            // no lookahead...
            if self.s.peek() == 'T' {
                self.s.nextch();
            }
            let t = self.s.get();
            if t.finished() {
                return Ok(None);
            }

            let hour = t.to_int_result::<u32>()?;
            Ok(Some(match self.s.get() {
                Token::Char(ch) => match ch {
                    ':' => self.formal_time(hour)?,
                    '.' => self.informal_time(hour)?,
                    ch => return date_result(&format!("unexpected char {:?}", ch)),
                },
                Token::Iden(name) => DateParser::hour_time(&name, hour)?,
                t => return date_result(&format!("unexpected token {:?}", t)),
            }))
        }
    }

    pub fn parse(&mut self) -> DateResult<DateTimeSpec> {
        let date = self.parse_date()?;
        let time = self.parse_time()?;
        Ok(DateTimeSpec {
            date: date,
            time: time,
        })
    }
}