Expand description
Main expression parser for PromQL.
This module provides the top-level expr function that parses any valid
PromQL expression into an AST. It uses a Pratt parser (precedence climbing)
algorithm for correct handling of binary operator precedence and associativity.
§Expression Grammar (Simplified)
expr = unary_expr | binary_expr
binary_expr = expr binary_op expr
unary_expr = unary_op? postfix_expr
postfix_expr = primary_expr postfix*
postfix = subquery_range | matrix_range
primary_expr = number | string | selector | paren_expr | function_call | aggregation
paren_expr = "(" expr ")"§Operator Precedence (Lowest to Highest)
or- Set unionand,unless- Set intersection/difference==,!=,<,<=,>,>=- Comparison+,-- Addition/subtraction*,/,%,atan2- Multiplication/division^- Power (right-associative)
§Examples
use rusty_promql_parser::parser::expr::expr;
// Simple metric
let (rest, e) = expr("http_requests_total").unwrap();
assert!(rest.is_empty());
// Binary expression with correct precedence
let (rest, e) = expr("1 + 2 * 3").unwrap(); // Parses as: 1 + (2 * 3)
assert!(rest.is_empty());
// Complex aggregation
let (rest, e) = expr("sum(rate(http_requests[5m])) by (job)").unwrap();
assert!(rest.is_empty());Functions§
- expr
- Parse a PromQL expression