rusty-promql-parser 0.2.1

A Prometheus PromQL parser written in Rust
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
419
420
421
422
423
424
//! Number literal parser for PromQL.
//!
//! This module handles parsing of all numeric formats supported by PromQL:
//!
//! - **Integers**: `42`, `0`, `123`
//! - **Floats**: `3.14`, `.5`, `5.`
//! - **Hexadecimal**: `0x1F`, `0X2A`
//! - **Octal**: `0755` (legacy), `0o755` (modern)
//! - **Scientific notation**: `1e10`, `2.5E-3`
//! - **Special values**: `Inf`, `+Inf`, `-Inf`, `NaN` (case-insensitive)
//! - **Signed numbers**: `+42`, `-3.14`
//!
//! # Examples
//!
//! ```rust
//! use rusty_promql_parser::lexer::number::number;
//!
//! // Decimal
//! let (_, n) = number("42").unwrap();
//! assert_eq!(n, 42.0);
//!
//! // Float
//! let (_, n) = number("3.14").unwrap();
//! assert!((n - 3.14).abs() < f64::EPSILON);
//!
//! // Hexadecimal
//! let (_, n) = number("0xFF").unwrap();
//! assert_eq!(n, 255.0);
//!
//! // Scientific notation
//! let (_, n) = number("1e-3").unwrap();
//! assert!((n - 0.001).abs() < f64::EPSILON);
//!
//! // Special values
//! let (_, n) = number("Inf").unwrap();
//! assert!(n.is_infinite());
//! ```

use nom::{
    IResult, Parser,
    branch::alt,
    bytes::complete::{tag, tag_no_case, take_while1},
    character::complete::{char, one_of},
    combinator::{map, map_res, opt, recognize, value},
    sequence::{pair, preceded},
};

/// Parse a PromQL number literal and return its f64 value.
///
/// This parser handles all PromQL number formats:
/// - Decimal integers and floats
/// - Hexadecimal (0x/0X prefix)
/// - Octal (leading 0)
/// - Scientific notation (e/E)
/// - Special values (Inf, NaN)
pub fn number(input: &str) -> IResult<&str, f64> {
    alt((special_float, signed_number)).parse(input)
}

/// Check if the next character is alphanumeric or underscore (identifier continuation)
fn is_ident_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

/// Parse special float values: Inf, +Inf, -Inf, NaN (case-insensitive)
/// These must not be followed by alphanumeric characters (to avoid matching "info" as "inf")
fn special_float(input: &str) -> IResult<&str, f64> {
    let (rest, val) = alt((
        value(f64::INFINITY, preceded(opt(char('+')), tag_no_case("Inf"))),
        value(f64::NEG_INFINITY, preceded(char('-'), tag_no_case("Inf"))),
        value(f64::NAN, preceded(opt(one_of("+-")), tag_no_case("NaN"))),
    ))
    .parse(input)?;

    // Ensure not followed by alphanumeric/underscore (would make it an identifier like "info")
    if rest.chars().next().is_some_and(is_ident_char) {
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    }

    Ok((rest, val))
}

/// Parse a signed number (with optional +/- prefix)
fn signed_number(input: &str) -> IResult<&str, f64> {
    map(
        pair(opt(one_of("+-")), unsigned_number),
        |(sign, value)| match sign {
            Some('-') => -value,
            _ => value,
        },
    )
    .parse(input)
}

/// Parse an unsigned number (hex, octal, or decimal)
fn unsigned_number(input: &str) -> IResult<&str, f64> {
    alt((
        hexadecimal,
        // Modern octal with 0o/0O prefix
        octal_prefixed,
        // Legacy octal must come before decimal since both start with digits
        // But we need to be careful: "0.5" should parse as decimal, not octal
        octal_legacy,
        decimal_float,
    ))
    .parse(input)
}

/// Parse a hexadecimal number: 0x1F, 0X2A
fn hexadecimal(input: &str) -> IResult<&str, f64> {
    map_res(
        preceded(
            alt((tag("0x"), tag("0X"))),
            take_while1(|c: char| c.is_ascii_hexdigit()),
        ),
        |digits: &str| i64::from_str_radix(digits, 16).map(|v| v as f64),
    )
    .parse(input)
}

/// Parse a modern octal number with prefix: 0o755, 0O755
fn octal_prefixed(input: &str) -> IResult<&str, f64> {
    map_res(
        preceded(
            alt((tag("0o"), tag("0O"))),
            take_while1(|c: char| matches!(c, '0'..='7')),
        ),
        |digits: &str| i64::from_str_radix(digits, 8).map(|v| v as f64),
    )
    .parse(input)
}

/// Parse a legacy octal number: 0755
/// Only matches if it starts with 0 followed by octal digits (0-7)
/// and doesn't look like a decimal float (no dot or e/E following)
fn octal_legacy(input: &str) -> IResult<&str, f64> {
    // Must start with 0, followed by at least one octal digit
    // We need to be careful not to consume "0" alone or "0.5" or "0e5"
    let (remaining, _) = char('0')(input)?;

    // Check if there's at least one more octal digit
    if remaining.is_empty() {
        // Just "0" - let decimal handle it
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    }

    let next_char = remaining.chars().next().unwrap();

    // If next char is not an octal digit, let decimal handle it
    if !matches!(next_char, '0'..='7') {
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Parse the rest as octal
    let (remaining, octal_digits) = take_while1(|c: char| matches!(c, '0'..='7'))(remaining)?;

    // Make sure this isn't actually a decimal number (no dot or exponent)
    if let Some(c) = remaining.chars().next()
        && (c == '.' || c == 'e' || c == 'E')
    {
        // This is a decimal number, not octal
        return Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Parse the octal value (including the initial 0)
    let full_octal = format!("0{}", octal_digits);
    match i64::from_str_radix(&full_octal, 8) {
        Ok(v) => Ok((remaining, v as f64)),
        Err(_) => Err(nom::Err::Error(nom::error::Error::new(
            input,
            nom::error::ErrorKind::MapRes,
        ))),
    }
}

/// Parse a decimal number (integer or float, with optional scientific notation)
fn decimal_float(input: &str) -> IResult<&str, f64> {
    map_res(recognize(decimal_float_inner), |s: &str| s.parse::<f64>()).parse(input)
}

/// Inner recognizer for decimal floats - captures the string representation
fn decimal_float_inner(input: &str) -> IResult<&str, &str> {
    alt((
        // Case 1: .42 or .42e10
        recognize((char('.'), decimal_digits, opt(exponent))),
        // Case 2: 42e10 or 42.42e10 (exponent required)
        recognize((
            decimal_digits,
            opt(pair(char('.'), opt(decimal_digits))),
            exponent,
        )),
        // Case 3: 42. or 42.42 (no exponent)
        recognize((decimal_digits, char('.'), opt(decimal_digits))),
        // Case 4: plain integer
        recognize(decimal_digits),
    ))
    .parse(input)
}

/// Parse decimal digits (one or more)
fn decimal_digits(input: &str) -> IResult<&str, &str> {
    take_while1(|c: char| c.is_ascii_digit())(input)
}

/// Parse the exponent part: e10, E-3, e+5
fn exponent(input: &str) -> IResult<&str, &str> {
    recognize((one_of("eE"), opt(one_of("+-")), decimal_digits)).parse(input)
}

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

    /// Helper to test that a number parses to the expected value
    fn assert_number(input: &str, expected: f64) {
        let result = number(input);
        match result {
            Ok((remaining, value)) => {
                assert!(
                    remaining.is_empty(),
                    "Parser did not consume entire input '{}', remaining: '{}'",
                    input,
                    remaining
                );
                if expected.is_nan() {
                    assert!(
                        value.is_nan(),
                        "Expected NaN for input '{}', got {}",
                        input,
                        value
                    );
                } else {
                    assert!(
                        (value - expected).abs() < f64::EPSILON || value == expected,
                        "For input '{}', expected {}, got {}",
                        input,
                        expected,
                        value
                    );
                }
            }
            Err(e) => panic!("Failed to parse '{}': {:?}", input, e),
        }
    }

    /// Helper to test that a number parses to the expected value with remaining input
    fn assert_number_partial(input: &str, expected: f64, expected_remaining: &str) {
        let result = number(input);
        match result {
            Ok((remaining, value)) => {
                assert_eq!(
                    remaining, expected_remaining,
                    "For input '{}', expected remaining '{}', got '{}'",
                    input, expected_remaining, remaining
                );
                if expected.is_nan() {
                    assert!(
                        value.is_nan(),
                        "Expected NaN for input '{}', got {}",
                        input,
                        value
                    );
                } else {
                    assert!(
                        (value - expected).abs() < f64::EPSILON || value == expected,
                        "For input '{}', expected {}, got {}",
                        input,
                        expected,
                        value
                    );
                }
            }
            Err(e) => panic!("Failed to parse '{}': {:?}", input, e),
        }
    }

    /// Helper to test that input fails to parse as a number
    fn assert_not_number(input: &str) {
        let result = number(input);
        assert!(
            result.is_err(),
            "Expected '{}' to fail parsing, but got {:?}",
            input,
            result
        );
    }

    // Basic integers
    #[test]
    fn test_integer() {
        assert_number("1", 1.0);
        assert_number("0", 0.0);
        assert_number("42", 42.0);
        assert_number("123", 123.0);
    }

    // Floats
    #[test]
    fn test_float() {
        assert_number(".5", 0.5);
        assert_number("5.", 5.0);
        assert_number("123.4567", 123.4567);
        assert_number("4.23", 4.23);
        assert_number(".3", 0.3);
    }

    // Scientific notation
    #[test]
    fn test_scientific() {
        assert_number("5e-3", 0.005);
        assert_number("5e3", 5000.0);
        assert_number("5e+3", 5000.0);
        assert_number("1e10", 1e10);
        assert_number("2.5E-3", 0.0025);
        assert_number("1e1", 10.0);
        assert_number("1e-1", 0.1);
        assert_number("1.0e1", 10.0);
        assert_number("1e01", 10.0);
        assert_number("1E01", 10.0);
        assert_number("1.e2", 100.0);
    }

    // Hexadecimal
    #[test]
    fn test_hex() {
        assert_number("0xc", 12.0);
        assert_number("0x123", 291.0);
        assert_number("0X2A", 42.0);
        assert_number("0x1F", 31.0);
        assert_number("0xA", 10.0);
    }

    // Octal - both legacy (leading 0) and modern (0o prefix)
    #[test]
    fn test_octal() {
        // Legacy octal with leading zero (from Prometheus parse_test.go)
        assert_number("0755", 493.0); // 7*64 + 5*8 + 5 = 493
        assert_number("0644", 420.0); // 6*64 + 4*8 + 4 = 420
        assert_number("07", 7.0);
        assert_number("010", 8.0); // 1*8 + 0 = 8
        assert_number("0777", 511.0); // 7*64 + 7*8 + 7 = 511
    }

    #[test]
    fn test_octal_prefixed() {
        // Modern octal with 0o/0O prefix (Go 1.13+ strconv.ParseInt)
        assert_number("0o0", 0.0);
        assert_number("0O0", 0.0);
        assert_number("0o7", 7.0);
        assert_number("0o10", 8.0);
        assert_number("0o755", 493.0);
        assert_number("0o777", 511.0);
        assert_number("0O755", 493.0);
    }

    #[test]
    fn test_octal_signed() {
        // Signed octal (from Prometheus parse_test.go: -0755 = -493)
        assert_number("-0755", -493.0);
        assert_number("+0755", 493.0);
        assert_number("-0o755", -493.0);
        assert_number("+0o755", 493.0);
    }

    // Signed numbers
    #[test]
    fn test_signed() {
        assert_number("-0755", -493.0);
        assert_number("-1", -1.0);
        assert_number("+1", 1.0);
        assert_number("-1e1", -10.0);
        assert_number("-1e-1", -0.1);
        assert_number("+5.5e-3", 0.0055);
    }

    // Special float values
    #[test]
    fn test_special_floats() {
        assert_number("NaN", f64::NAN);
        assert_number("nAN", f64::NAN);
        assert_number("Inf", f64::INFINITY);
        assert_number("iNf", f64::INFINITY);
        assert_number("+Inf", f64::INFINITY);
        assert_number("-Inf", f64::NEG_INFINITY);
    }

    // Numbers followed by other content (partial parsing)
    #[test]
    fn test_partial_parse() {
        assert_number_partial("NaN 123", f64::NAN, " 123");
        assert_number_partial("123abc", 123.0, "abc");
    }

    // Invalid numbers - these should NOT parse as identifiers
    #[test]
    fn test_not_numbers() {
        // These start like numbers but aren't valid
        assert_not_number("."); // Just a dot
        assert_not_number(""); // Empty
    }

    // Edge cases that should parse correctly
    #[test]
    fn test_edge_cases() {
        // 0 alone should parse as 0, not octal
        assert_number("0", 0.0);
        // 0.5 should parse as decimal, not octal
        assert_number("0.5", 0.5);
        // 0e5 should parse as decimal with exponent
        assert_number("0e5", 0.0);
    }
}