gitql-parser 0.42.0

GitQL parser
Documentation
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
414
415
416
417
418
use gitql_ast::expression::Expr;
use gitql_ast::expression::IntervalExpr;
use gitql_ast::Interval;

use crate::diagnostic::Diagnostic;
use crate::parser::consume_conditional_token_or_errors;
use crate::parser::consume_token_or_error;
use crate::token::SourceLocation;
use crate::token::Token;
use crate::token::TokenKind;

const INTERVAL_MAX_VALUE: i64 = 170_000_000;

pub(crate) fn parse_interval_expression(
    tokens: &[Token],
    position: &mut usize,
) -> Result<Box<dyn Expr>, Box<Diagnostic>> {
    consume_token_or_error(
        tokens,
        position,
        TokenKind::Interval,
        "Expect 'Interval' Keyword",
    )?;

    let interval_value_token = consume_conditional_token_or_errors(
        tokens,
        position,
        |t| matches!(t.kind, TokenKind::String(_)),
        "Expect String after 'Interval' Keyword as interval value",
    )?;

    let interval = parse_interval_literal(
        &interval_value_token.to_string(),
        interval_value_token.location,
    )?;

    Ok(Box::new(IntervalExpr::new(interval)))
}

/// Parse string into Interval expression.
///
/// [@] <quantity> <unit> [quantity unit...] <direction>
///
/// quantity: is a number (possibly signed)
/// unit: is
///     microsecond, millisecond, second,
///     minute, hour, day, week, month, year,
///     decade, century, millennium, or abbreviations or plurals of these units
/// direction: can be ago or empty
/// The at sign (@) is optional noise
///
/// Ref: https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT
fn parse_interval_literal(
    interval_str: &str,
    location: SourceLocation,
) -> Result<Interval, Box<Diagnostic>> {
    let tokens = interval_str.split_whitespace().collect::<Vec<&str>>();
    if tokens.is_empty() {
        return Err(Diagnostic::error("Interval value can't be empty")
            .add_help("Please check the documentation for help")
            .with_location(location)
            .as_boxed());
    }

    let mut position = 0;
    let mut interval = Interval::default();

    // Date part
    let mut has_millenniums = false;
    let mut has_centuries = false;
    let mut has_decades = false;
    let mut has_years = false;
    let mut has_months = false;
    let mut has_week = false;
    let mut has_days = false;

    // Time part
    let mut has_any_time_part = false;
    let mut has_hours: bool = false;
    let mut has_minutes = false;
    let mut has_seconds = false;

    let mut has_direction_ago = false;

    while position < tokens.len() {
        let token = tokens[position].trim();
        if token.is_empty() {
            position += 1;
            continue;
        }

        if token == "ago" {
            if has_direction_ago {
                return Err(
                    Diagnostic::error("Interval can't contains more than one `ago`")
                        .add_help("Please keep at most only one `ago` direction")
                        .with_location(location)
                        .as_boxed(),
                );
            }

            has_direction_ago = true;
            position += 1;
            continue;
        }

        // Parse Millienniums, Centuries, Decades, Years, Weeks, Months and Days
        if let Ok(value) = token.parse::<i64>() {
            // Consume value
            position += 1;

            if position >= tokens.len() {
                return Err(Diagnostic::error(&format!(
                    "Missing interval unit after value {value}",
                ))
                .with_location(location)
                .as_boxed());
            }

            // Parse the unit
            let mut maybe_unit = tokens[position];
            let unit_lower = &maybe_unit.to_lowercase();
            maybe_unit = unit_lower.as_str();

            if matches!(maybe_unit, "millennium" | "millenniums") {
                check_interval_value_and_unit(&mut has_millenniums, value, maybe_unit, &location)?;
                interval.years += value * 1000;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "century" | "centuries") {
                check_interval_value_and_unit(&mut has_centuries, value, maybe_unit, &location)?;
                interval.years += value * 100;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "decade" | "decades") {
                check_interval_value_and_unit(&mut has_decades, value, maybe_unit, &location)?;
                interval.years += value * 10;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "y" | "year" | "years") {
                check_interval_value_and_unit(&mut has_years, value, maybe_unit, &location)?;
                interval.years += value;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "m" | "mon" | "mons" | "months") {
                check_interval_value_and_unit(&mut has_months, value, maybe_unit, &location)?;
                interval.months += value;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "w" | "week" | "weeks") {
                check_interval_value_and_unit(&mut has_week, value, maybe_unit, &location)?;
                interval.days += value * 7;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "d" | "day" | "days") {
                check_interval_value_and_unit(&mut has_days, value, maybe_unit, &location)?;
                interval.days += value;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "h" | "hour" | "hours") {
                check_interval_value_and_unit(&mut has_hours, value, maybe_unit, &location)?;
                has_any_time_part = true;
                interval.hours += value;
                position += 1;
                continue;
            }

            if matches!(maybe_unit, "minute" | "minutes") {
                check_interval_value_and_unit(&mut has_minutes, value, maybe_unit, &location)?;
                has_any_time_part = true;
                interval.minutes += value;
                position += 1;
                continue;
            }
        }

        // Parse Seconds
        if let Ok(value) = token.parse::<f64>() {
            // Consume value
            position += 1;

            if position >= tokens.len() {
                return Err(Diagnostic::error(&format!(
                    "Missing interval unit after value {value}",
                ))
                .with_location(location)
                .as_boxed());
            }

            // Parse the unit
            let mut maybe_unit = tokens[position];
            let unit_lower = &maybe_unit.to_lowercase();
            maybe_unit = unit_lower.as_str();

            if matches!(maybe_unit, "second" | "seconds") {
                check_interval_value_and_unit(
                    &mut has_seconds,
                    value as i64,
                    maybe_unit,
                    &location,
                )?;
                has_any_time_part = true;
                interval.seconds += value;
                position += 1;
                continue;
            }

            return Err(Diagnostic::error(&format!(
                "Invalid input syntax for interval unit `{maybe_unit}`",
            ))
            .add_help(
                "Interval date unit can be `[year | years | mon | mons | months | day or days]`",
            )
            .with_location(location)
            .as_boxed());
        }

        // Parse the optional time part without explicit unit markings (Seconds, Minutes or Hours)
        if token.contains(':') {
            if has_any_time_part {
                return Err(
                    Diagnostic::error("You can't have time value twice in same interval")
                        .with_location(location)
                        .as_boxed(),
                );
            }

            let time_parts: Vec<&str> = token.split(':').collect();
            if !matches!(time_parts.len(), 2 | 3) {
                return Err(Diagnostic::error("Invalid input syntax for type interval")
                    .with_location(location)
                    .as_boxed());
            }

            match time_parts[0].parse::<i64>() {
                Ok(hours) => {
                    check_interval_value_and_unit(&mut has_hours, hours, time_parts[0], &location)?;
                    interval.hours = hours;
                }
                Err(_) => {
                    return Err(Diagnostic::error("Invalid input syntax for type interval")
                        .with_location(location)
                        .as_boxed());
                }
            }

            match time_parts[1].parse::<i64>() {
                Ok(minutes) => {
                    check_interval_value_and_unit(
                        &mut has_minutes,
                        minutes,
                        time_parts[1],
                        &location,
                    )?;
                    interval.minutes = minutes;
                }
                Err(_) => {
                    return Err(Diagnostic::error("Invalid input syntax for type interval")
                        .with_location(location)
                        .as_boxed());
                }
            }

            if time_parts.len() == 3 {
                match time_parts[2].parse::<f64>() {
                    Ok(seconds) => {
                        check_interval_value_and_unit(
                            &mut has_seconds,
                            seconds as i64,
                            time_parts[2],
                            &location,
                        )?;
                        interval.seconds = seconds;
                    }
                    Err(_) => {
                        return Err(Diagnostic::error("Invalid input syntax for type interval")
                            .with_location(location)
                            .as_boxed());
                    }
                }
            }

            // Consume time token
            position += 1;
            continue;
        }

        return Err(Diagnostic::error("Invalid input syntax for type interval")
            .add_note("Expect numeric value before each unit in interval value")
            .with_location(location)
            .as_boxed());
    }

    // ago directon negates all the fields
    if has_direction_ago {
        interval = interval.mul(-1).unwrap_or(interval);
    }

    Ok(interval)
}

fn check_interval_value_and_unit(
    is_used_twice: &mut bool,
    interval_value: i64,
    unit_name: &str,
    location: &SourceLocation,
) -> Result<(), Box<Diagnostic>> {
    if !*is_used_twice {
        *is_used_twice = true;
        return Ok(());
    }

    if !(-INTERVAL_MAX_VALUE..=INTERVAL_MAX_VALUE).contains(&interval_value) {
        return Err(Diagnostic::error(&format!(
            "Interval value for unit `{unit_name}` is out of the range",
        ))
        .add_help("Interval value must be in range from -170_000_000 to 170_000_000")
        .with_location(*location)
        .as_boxed());
    }

    Err(Diagnostic::error(&format!(
        "Can't use the same interval unit `{unit_name}` twice",
    ))
    .with_location(*location)
    .as_boxed())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn valid_hours() {
        let inputs = ["1 h", "1 hour", "1 hours", "1:00:00"];
        for input in inputs {
            let parse_result = parse_interval_literal(input, SourceLocation::default());
            assert!(parse_result.is_ok());

            if let Ok(interval) = parse_result {
                assert_eq!(interval.hours, 1);
            }
        }
    }

    #[test]
    fn valid_weeks() {
        let inputs = [
            "2 w",
            "2 week",
            "2 weeks",
            "1 week 7 day",
            "1 w 7 d",
            "1 weeks 7 days",
        ];

        for input in inputs {
            let parse_result = parse_interval_literal(input, SourceLocation::default());
            assert!(parse_result.is_ok());

            if let Ok(interval) = parse_result {
                assert_eq!(interval.days, 14);
            }
        }
    }

    #[test]
    fn valid_seconds() {
        let inputs = ["10.1 second"];

        for input in inputs {
            let parse_result = parse_interval_literal(input, SourceLocation::default());
            assert!(parse_result.is_ok());

            if let Ok(interval) = parse_result {
                assert_eq!(interval.seconds, 10.1);
            }
        }
    }

    #[test]
    fn ago_direction() {
        let parse_result = parse_interval_literal("1 y 1 m 1 w 1 d", SourceLocation::default());
        assert!(parse_result.is_ok());

        let parse_result_with_ago =
            parse_interval_literal("1 y 1 m 1 w 1 d ago", SourceLocation::default());
        assert!(parse_result_with_ago.is_ok());

        assert_eq!(
            parse_result.ok().unwrap().mul(-1).unwrap(),
            parse_result_with_ago.ok().unwrap()
        );
    }

    #[test]
    fn invalid_time() {
        let inputs = ["1 h 1:00:00", "1 h 1 h"];
        for input in inputs {
            let parse_result = parse_interval_literal(input, SourceLocation::default());
            assert!(parse_result.is_err());
        }
    }
}