Skip to main content

tp_lexer/
lib.rs

1//! A tiny no_std filter expression parser and evaluator for Linux tracepoint–like filters.
2//!
3//! Features:
4//! - Numeric comparisons: `== != < <= > >=` (supports bit mask form: `field & 0x10 == 0x10`).
5//! - String/byte comparisons: `== != ~` (`~` performs glob matching supporting `* ? [..]` and class negation `[!a]` / `[^a]`).
6//! - Unquoted barewords allowed (`comm != bash`); quote when spaces or special characters are present (`"C Program"`).
7//! - Logical operators with precedence: parentheses > comparison > `&&` > `||` (short‑circuit evaluation).
8//! - Tri‑state runtime: missing field => `Unknown`; top‑level `Unknown` is treated as `true` (fail‑open), `Unknown` is true‑neutral for `&&` and false‑neutral for `||`.
9//! - Compile‑time validation of field existence and type.
10//! - Signed decimal / hex integer literals such as `-1`, `0x10`, and `-(0x10)`.
11//! - `u64` / `u128` / `i128` / `isize` / `usize` fields, plus their `NonZero*` variants, are
12//!   supported, but evaluation still uses an internal `i64`; out-of-range values are truncated
13//!   with Rust's integer cast semantics before comparison.
14//! - no_std + alloc friendly (uses `BTreeMap` in examples; can evaluate directly over a raw byte buffer via `BufContext`).
15//!
16//! Schema & Field Types:
17//! A schema is defined via the `schema!` macro listing `(name, FieldType, offset, length)` tuples. Integer types implement `FieldClassifier` exposing a `FIELD_TYPE` constant. Bytes fields use `FieldType::Bytes`.
18//!
19//! Example (map based):
20//! ```rust
21//! extern crate alloc;
22//! use alloc::collections::BTreeMap;
23//! use tp_lexer::{schema, FieldType, compile_with_schema, FieldClassifier};
24//!
25//! let schema = schema!(
26//!     "sig"  => (u32::FIELD_TYPE, 0, 4),
27//!     "comm" => (FieldType::Bytes, 4, 16),
28//! );
29//! let compiled = compile_with_schema("sig >= 10 && comm != bash", schema).unwrap();
30//! let mut ctx = BTreeMap::from([
31//!     ("sig".to_string(), "12".to_string()),
32//!     ("comm".to_string(), "sh".to_string()),
33//! ]);
34//! assert!(compiled.evaluate(&ctx));
35//! ```
36//!
37//! Example (raw buffer):
38//! ```rust
39//! use tp_lexer::{schema, FieldType, compile_with_schema, BufContext, FieldClassifier};
40//! // Layout: [ sig: u32 (4 bytes) | comm: 12 bytes ASCII | padding ]
41//! let schema = schema!(
42//!     "sig"  => (u32::FIELD_TYPE, 0, 4),
43//!     "comm" => (FieldType::Bytes, 4, 12),
44//! );
45//! let mut buf = [0u8; 32];
46//! // sig = 12
47//! buf[0..4].copy_from_slice(&12u32.to_le_bytes());
48//! // comm = "sh" zero padded
49//! buf[4..6].copy_from_slice(b"sh");
50//! let ctx = BufContext::new(&buf, &schema);
51//! let compiled = compile_with_schema("sig >= 10 && comm != bash", schema).unwrap();
52//! assert!(compiled.evaluate(&ctx));
53//! ```
54//!
55//! Tri‑state rationale: treating top‑level `Unknown` as `true` prevents inadvertent data loss when a new field is not yet populated. If strict behavior is desired you can wrap your filter: `(field != value) && defined(field)` by introducing an explicit presence flag in your schema.
56//!
57//! Limitations / Non‑Goals:
58//! - No arithmetic besides bit masking.
59//! - No unary operators or regex; glob only.
60//! - Evaluation uses an internal `i64` representation. `u64`/`u128`/`i128`/`isize`/`usize`
61//!   fields are accepted, but values outside the `i64` range are truncated using Rust's `as i64`
62//!   integer cast semantics before comparison.
63//! - Evaluation is left‑associative for chains of `&&` / `||` (standard short‑circuit).
64//!
65//! Safety Notes:
66//! - Buffer offsets and lengths are trusted; ensure schema matches actual event layout.
67//! - Integer extraction uses user‑provided `ToI64` implementations; undefined behavior inside those implementations is out of scope.
68//!
69//! See README for a more thorough integration guide.
70#![deny(missing_docs)]
71#![no_std]
72
73extern crate alloc;
74use alloc::{
75    boxed::Box,
76    collections::BTreeMap,
77    format,
78    string::{String, ToString},
79};
80use core::{fmt::Debug, num::IntErrorKind};
81mod internal;
82
83/// A span in the input expression, used for diagnostics.
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct Span {
86    /// Zero-based start byte offset (inclusive).
87    pub start: usize,
88    end: usize,
89}
90
91/// An error produced while lexing or parsing a filter expression.
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct ParseError {
94    /// The offending span in the original input.
95    pub span: Span,
96    /// A human-readable error message.
97    pub message: String,
98}
99
100impl ParseError {
101    fn new<S: Into<String>>(start: usize, end: usize, msg: S) -> Self {
102        Self {
103            span: Span { start, end },
104            message: msg.into(),
105        }
106    }
107}
108
109fn parse_i64_literal_text(input: &str) -> Result<i64, &'static str> {
110    let s = input.trim();
111    if s.is_empty() {
112        return Err("integer value is empty");
113    }
114    let (negative, rest) = match s.as_bytes()[0] {
115        b'-' => (true, &s[1..]),
116        b'+' => (false, &s[1..]),
117        _ => (false, s),
118    };
119    if rest.is_empty() {
120        return Err("integer literal missing digits");
121    }
122    if let Some(hex) = rest.strip_prefix("0x").or_else(|| rest.strip_prefix("0X")) {
123        if hex.is_empty() {
124            return Err("hex literal missing digits");
125        }
126        let value = i64::from_str_radix(hex, 16).map_err(|err| match err.kind() {
127            IntErrorKind::PosOverflow | IntErrorKind::NegOverflow => "integer literal overflow",
128            _ => "invalid integer literal",
129        })?;
130        if negative {
131            value.checked_neg().ok_or("integer literal overflow")
132        } else {
133            Ok(value)
134        }
135    } else {
136        let value = rest.parse::<i64>().map_err(|err| match err.kind() {
137            IntErrorKind::PosOverflow | IntErrorKind::NegOverflow => "integer literal overflow",
138            IntErrorKind::Empty => "integer literal missing digits",
139            _ => "invalid integer literal",
140        })?;
141        if negative {
142            value.checked_neg().ok_or("integer literal overflow")
143        } else {
144            Ok(value)
145        }
146    }
147}
148
149/// An error produced while evaluating a compiled expression.
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct EvalError {
152    /// The field being accessed when the error occurred.
153    pub field: String,
154    /// A human-readable error message.
155    pub message: String,
156}
157
158impl EvalError {
159    fn new<S: Into<String>>(field: &str, message: S) -> Self {
160        Self {
161            field: field.to_string(),
162            message: message.into(),
163        }
164    }
165}
166
167#[derive(Debug, Clone, PartialEq, Eq)]
168enum TokenKind {
169    Ident(String),
170    Int(i64),
171    String(String),
172    Bareword(String),
173    AndAnd, // &&
174    OrOr,   // ||
175    Amp,    // &
176    EqEq,   // ==
177    NotEq,  // !=
178    Lt,     // <
179    Le,     // <=
180    Gt,     // >
181    Ge,     // >=
182    Tilde,  // ~
183    LParen, // (
184    RParen, // )
185    Eof,
186}
187
188#[derive(Debug, Clone, PartialEq, Eq)]
189struct Token {
190    kind: TokenKind,
191    span: Span,
192}
193
194struct Lexer<'a> {
195    input: &'a str,
196    bytes: &'a [u8],
197    pos: usize,
198    len: usize,
199}
200
201impl<'a> Lexer<'a> {
202    fn new(input: &'a str) -> Self {
203        Self {
204            input,
205            bytes: input.as_bytes(),
206            pos: 0,
207            len: input.len(),
208        }
209    }
210
211    fn peek(&self) -> Option<u8> {
212        if self.pos < self.len {
213            Some(self.bytes[self.pos])
214        } else {
215            None
216        }
217    }
218
219    fn peek2(&self) -> Option<u8> {
220        if self.pos + 1 < self.len {
221            Some(self.bytes[self.pos + 1])
222        } else {
223            None
224        }
225    }
226
227    fn bump(&mut self) -> Option<u8> {
228        if self.pos < self.len {
229            let b = self.bytes[self.pos];
230            self.pos += 1;
231            Some(b)
232        } else {
233            None
234        }
235    }
236
237    fn skip_ws(&mut self) {
238        while let Some(b) = self.peek() {
239            if b.is_ascii_whitespace() {
240                self.pos += 1;
241            } else {
242                break;
243            }
244        }
245    }
246
247    fn lex_number(&mut self, start: usize) -> Result<Token, ParseError> {
248        // Support 0x... hex and decimal
249        if self.peek() == Some(b'0') && matches!(self.peek2(), Some(b'x') | Some(b'X')) {
250            self.bump(); // 0
251            self.bump(); // x
252            let from = self.pos;
253            while let Some(b) = self.peek() {
254                if (b as char).is_ascii_hexdigit() {
255                    self.pos += 1;
256                } else {
257                    break;
258                }
259            }
260            if self.pos == from {
261                return Err(ParseError::new(
262                    start,
263                    self.pos,
264                    "hex literal missing digits",
265                ));
266            }
267            let s = &self.input[from..self.pos];
268            let v = i64::from_str_radix(s, 16)
269                .map_err(|_| ParseError::new(start, self.pos, "hex literal overflow"))?;
270            Ok(Token {
271                kind: TokenKind::Int(v),
272                span: Span {
273                    start,
274                    end: self.pos,
275                },
276            })
277        } else {
278            let from = self.pos;
279            while let Some(b) = self.peek() {
280                if (b as char).is_ascii_digit() {
281                    self.pos += 1;
282                } else {
283                    break;
284                }
285            }
286            let s = &self.input[from..self.pos];
287            let v: i64 = s
288                .parse()
289                .map_err(|_| ParseError::new(start, self.pos, "decimal literal overflow"))?;
290            Ok(Token {
291                kind: TokenKind::Int(v),
292                span: Span {
293                    start,
294                    end: self.pos,
295                },
296            })
297        }
298    }
299
300    fn lex_ident(&mut self, start: usize) -> Token {
301        let from = self.pos - 1; // already consumed first char
302        while let Some(b) = self.peek() {
303            if (b as char).is_ascii_alphanumeric() || b == b'_' {
304                self.pos += 1;
305            } else {
306                break;
307            }
308        }
309        let s = &self.input[from..self.pos];
310        Token {
311            kind: TokenKind::Ident(s.to_string()),
312            span: Span {
313                start,
314                end: self.pos,
315            },
316        }
317    }
318
319    fn lex_string(&mut self, start: usize, quote: u8) -> Result<Token, ParseError> {
320        let mut out = String::new();
321        while let Some(b) = self.bump() {
322            if b == quote {
323                return Ok(Token {
324                    kind: TokenKind::String(out),
325                    span: Span {
326                        start,
327                        end: self.pos,
328                    },
329                });
330            } else if b == b'\\' {
331                match self.bump() {
332                    Some(b'\\') => out.push('\\'),
333                    Some(b'\"') => out.push('"'),
334                    Some(b'\'') => out.push('\''),
335                    Some(b'n') => out.push('\n'),
336                    Some(b't') => out.push('\t'),
337                    Some(c) => out.push(c as char),
338                    None => {
339                        return Err(ParseError::new(start, self.pos, "incomplete string escape"));
340                    }
341                }
342            } else {
343                out.push(b as char);
344            }
345        }
346        Err(ParseError::new(start, self.pos, "unterminated string"))
347    }
348
349    fn is_op_char(b: u8) -> bool {
350        matches!(
351            b,
352            b'&' | b'|' | b'=' | b'!' | b'<' | b'>' | b'~' | b'(' | b')'
353        )
354    }
355
356    fn lex_bareword(&mut self, start: usize) -> Token {
357        let from = self.pos - 1; // consumed first
358        // bareword continues until whitespace or operator or paren
359        while let Some(b) = self.peek() {
360            if b.is_ascii_whitespace() || Self::is_op_char(b) {
361                break;
362            }
363            self.pos += 1;
364        }
365        let s = &self.input[from..self.pos];
366        Token {
367            kind: TokenKind::Bareword(s.to_string()),
368            span: Span {
369                start,
370                end: self.pos,
371            },
372        }
373    }
374
375    fn next_token(&mut self) -> Result<Token, ParseError> {
376        self.skip_ws();
377        let start = self.pos;
378        if self.pos >= self.len {
379            return Ok(Token {
380                kind: TokenKind::Eof,
381                span: Span { start, end: start },
382            });
383        }
384        let b = self.bump().unwrap();
385        match b {
386            b'(' => Ok(Token {
387                kind: TokenKind::LParen,
388                span: Span {
389                    start,
390                    end: self.pos,
391                },
392            }),
393            b')' => Ok(Token {
394                kind: TokenKind::RParen,
395                span: Span {
396                    start,
397                    end: self.pos,
398                },
399            }),
400            b'&' => {
401                if self.peek() == Some(b'&') {
402                    self.bump();
403                    Ok(Token {
404                        kind: TokenKind::AndAnd,
405                        span: Span {
406                            start,
407                            end: self.pos,
408                        },
409                    })
410                } else {
411                    Ok(Token {
412                        kind: TokenKind::Amp,
413                        span: Span {
414                            start,
415                            end: self.pos,
416                        },
417                    })
418                }
419            }
420            b'|' => {
421                if self.peek() == Some(b'|') {
422                    self.bump();
423                    Ok(Token {
424                        kind: TokenKind::OrOr,
425                        span: Span {
426                            start,
427                            end: self.pos,
428                        },
429                    })
430                } else {
431                    Err(ParseError::new(
432                        start,
433                        self.pos,
434                        "single '|' is invalid; did you mean '||'?",
435                    ))
436                }
437            }
438            b'=' => {
439                if self.peek() == Some(b'=') {
440                    self.bump();
441                    Ok(Token {
442                        kind: TokenKind::EqEq,
443                        span: Span {
444                            start,
445                            end: self.pos,
446                        },
447                    })
448                } else {
449                    Err(ParseError::new(
450                        start,
451                        self.pos,
452                        "single '=' is invalid; did you mean '=='?",
453                    ))
454                }
455            }
456            b'!' => {
457                if self.peek() == Some(b'=') {
458                    self.bump();
459                    Ok(Token {
460                        kind: TokenKind::NotEq,
461                        span: Span {
462                            start,
463                            end: self.pos,
464                        },
465                    })
466                } else {
467                    Err(ParseError::new(
468                        start,
469                        self.pos,
470                        "'!' is only supported as '!='",
471                    ))
472                }
473            }
474            b'<' => {
475                if self.peek() == Some(b'=') {
476                    self.bump();
477                    Ok(Token {
478                        kind: TokenKind::Le,
479                        span: Span {
480                            start,
481                            end: self.pos,
482                        },
483                    })
484                } else {
485                    Ok(Token {
486                        kind: TokenKind::Lt,
487                        span: Span {
488                            start,
489                            end: self.pos,
490                        },
491                    })
492                }
493            }
494            b'>' => {
495                if self.peek() == Some(b'=') {
496                    self.bump();
497                    Ok(Token {
498                        kind: TokenKind::Ge,
499                        span: Span {
500                            start,
501                            end: self.pos,
502                        },
503                    })
504                } else {
505                    Ok(Token {
506                        kind: TokenKind::Gt,
507                        span: Span {
508                            start,
509                            end: self.pos,
510                        },
511                    })
512                }
513            }
514            b'~' => Ok(Token {
515                kind: TokenKind::Tilde,
516                span: Span {
517                    start,
518                    end: self.pos,
519                },
520            }),
521            b'"' | b'\'' => self.lex_string(start, b),
522            b'0'..=b'9' => {
523                // rewind pos by one because lex_number expects current pos at the first char already considered
524                self.pos -= 1;
525                self.lex_number(start)
526            }
527            b'a'..=b'z' | b'A'..=b'Z' | b'_' => {
528                // identifier starts with letter or '_'
529                Ok(self.lex_ident(start))
530            }
531            other => {
532                // treat as start of bareword, if it's not whitespace and not operator
533                if other.is_ascii_whitespace() || Self::is_op_char(other) {
534                    Err(ParseError::new(
535                        start,
536                        self.pos,
537                        format!("unrecognized character: '{}'", other as char),
538                    ))
539                } else {
540                    Ok(self.lex_bareword(start))
541                }
542            }
543        }
544    }
545}
546
547#[derive(Debug, Clone, PartialEq, Eq)]
548enum NumOp {
549    Eq,
550    Ne,
551    Lt,
552    Le,
553    Gt,
554    Ge,
555}
556
557#[derive(Debug, Clone, PartialEq, Eq)]
558enum StrOp {
559    Eq,
560    Ne,
561    Glob,
562}
563
564#[derive(Debug, Clone, PartialEq, Eq)]
565enum Expr {
566    Or(Box<Expr>, Box<Expr>),
567    And(Box<Expr>, Box<Expr>),
568    NumCmp {
569        field: String,
570        field_span: Span,
571        mask: Option<i64>,
572        op: NumOp,
573        rhs: i64,
574    },
575    StrCmp {
576        field: String,
577        field_span: Span,
578        op: StrOp,
579        pat: String,
580    },
581    Group(Box<Expr>),
582}
583
584struct Parser<'a> {
585    lexer: Lexer<'a>,
586    lookahead: Token,
587}
588
589impl<'a> Parser<'a> {
590    fn new(input: &'a str) -> Result<Self, ParseError> {
591        let mut lex = Lexer::new(input);
592        let first = lex.next_token()?;
593        Ok(Self {
594            lexer: lex,
595            lookahead: first,
596        })
597    }
598
599    fn bump(&mut self) -> Result<Token, ParseError> {
600        let span = self.lookahead.span;
601        let cur = core::mem::replace(
602            &mut self.lookahead,
603            Token {
604                kind: TokenKind::Eof,
605                span,
606            },
607        );
608        self.lookahead = self.lexer.next_token()?;
609        Ok(cur)
610    }
611
612    fn expect(&mut self, kind: &TokenKind) -> Result<Token, ParseError> {
613        if &self.lookahead.kind == kind {
614            self.bump()
615        } else {
616            Err(ParseError::new(
617                self.lookahead.span.start,
618                self.lookahead.span.end,
619                format!("expected {:?}, found {:?}", kind, self.lookahead.kind),
620            ))
621        }
622    }
623
624    fn parse(&mut self) -> Result<Expr, ParseError> {
625        let expr = self.parse_or()?;
626        match self.lookahead.kind {
627            TokenKind::Eof => Ok(expr),
628            _ => Err(ParseError::new(
629                self.lookahead.span.start,
630                self.lookahead.span.end,
631                "extraneous input",
632            )),
633        }
634    }
635
636    fn parse_or(&mut self) -> Result<Expr, ParseError> {
637        let mut left = self.parse_and()?;
638        while matches!(self.lookahead.kind, TokenKind::OrOr) {
639            self.bump()?; // ||
640            let right = self.parse_and()?;
641            left = Expr::Or(Box::new(left), Box::new(right));
642        }
643        Ok(left)
644    }
645
646    fn parse_and(&mut self) -> Result<Expr, ParseError> {
647        let mut left = self.parse_cmp()?;
648        while matches!(self.lookahead.kind, TokenKind::AndAnd) {
649            self.bump()?; // &&
650            let right = self.parse_cmp()?;
651            left = Expr::And(Box::new(left), Box::new(right));
652        }
653        Ok(left)
654    }
655
656    fn parse_cmp(&mut self) -> Result<Expr, ParseError> {
657        match &self.lookahead.kind {
658            TokenKind::LParen => {
659                self.bump()?; // (
660                let e = self.parse_or()?;
661                self.expect(&TokenKind::RParen)?;
662                Ok(Expr::Group(Box::new(e)))
663            }
664            TokenKind::Ident(_) => self.parse_field_cmp(),
665            _ => Err(ParseError::new(
666                self.lookahead.span.start,
667                self.lookahead.span.end,
668                "expected '(' or field name",
669            )),
670        }
671    }
672
673    fn parse_field_cmp(&mut self) -> Result<Expr, ParseError> {
674        let name_tok = self.bump()?; // ident
675        let field_span = name_tok.span;
676        let field = match name_tok.kind {
677            TokenKind::Ident(s) => s,
678            _ => unreachable!(),
679        };
680
681        // Case 1: numeric with optional '& INT'
682        if matches!(self.lookahead.kind, TokenKind::Amp) {
683            self.bump()?; // &
684            let mask_val = self.parse_signed_int()?;
685            let op_tok = self.bump()?; // numeric op
686            let op = match op_tok.kind {
687                TokenKind::EqEq => NumOp::Eq,
688                TokenKind::NotEq => NumOp::Ne,
689                TokenKind::Lt => NumOp::Lt,
690                TokenKind::Le => NumOp::Le,
691                TokenKind::Gt => NumOp::Gt,
692                TokenKind::Ge => NumOp::Ge,
693                _ => {
694                    return Err(ParseError::new(
695                        op_tok.span.start,
696                        op_tok.span.end,
697                        "numeric comparison operator required",
698                    ));
699                }
700            };
701            let rhs = self.parse_signed_int()?;
702            return Ok(Expr::NumCmp {
703                field,
704                field_span,
705                mask: Some(mask_val),
706                op,
707                rhs,
708            });
709        }
710
711        // Case 2: if next token is one of <, <=, >, >= -> numeric
712        if matches!(
713            self.lookahead.kind,
714            TokenKind::Lt | TokenKind::Le | TokenKind::Gt | TokenKind::Ge
715        ) {
716            let op_tok = self.bump()?;
717            let op = match op_tok.kind {
718                TokenKind::Lt => NumOp::Lt,
719                TokenKind::Le => NumOp::Le,
720                TokenKind::Gt => NumOp::Gt,
721                TokenKind::Ge => NumOp::Ge,
722                _ => unreachable!(),
723            };
724            let rhs = self.parse_signed_int()?;
725            return Ok(Expr::NumCmp {
726                field,
727                field_span,
728                mask: None,
729                op,
730                rhs,
731            });
732        }
733
734        // Case 3: '==' / '!=' / '~' -> could be string (or numeric for ==/!=). Decide by RHS token.
735        match self.lookahead.kind.clone() {
736            TokenKind::Tilde => {
737                self.bump()?; // ~
738                let pat = self.parse_pattern_literal()?;
739                Ok(Expr::StrCmp {
740                    field,
741                    field_span,
742                    op: StrOp::Glob,
743                    pat,
744                })
745            }
746            TokenKind::EqEq | TokenKind::NotEq => {
747                let op_tok = self.bump()?;
748                // if RHS is INT -> numeric; else -> string literal/pattern
749                if self.starts_numeric_literal() {
750                    let rhs = self.parse_signed_int()?;
751                    let op = match op_tok.kind {
752                        TokenKind::EqEq => NumOp::Eq,
753                        TokenKind::NotEq => NumOp::Ne,
754                        _ => unreachable!(),
755                    };
756                    Ok(Expr::NumCmp {
757                        field,
758                        field_span,
759                        mask: None,
760                        op,
761                        rhs,
762                    })
763                } else {
764                    let pat = self.parse_pattern_literal()?;
765                    let op = match op_tok.kind {
766                        TokenKind::EqEq => StrOp::Eq,
767                        TokenKind::NotEq => StrOp::Ne,
768                        _ => unreachable!(),
769                    };
770                    Ok(Expr::StrCmp {
771                        field,
772                        field_span,
773                        op,
774                        pat,
775                    })
776                }
777            }
778            _ => Err(ParseError::new(
779                self.lookahead.span.start,
780                self.lookahead.span.end,
781                "comparison operator required",
782            )),
783        }
784    }
785
786    fn parse_pattern_literal(&mut self) -> Result<String, ParseError> {
787        // Accept STRING, BAREWORD, or IDENT possibly followed by BAREWORD chunks (to capture bash*)
788        let mut s = match self.lookahead.kind.clone() {
789            TokenKind::String(st) => {
790                self.bump()?;
791                st
792            }
793            TokenKind::Bareword(st) => {
794                self.bump()?;
795                st
796            }
797            TokenKind::Ident(st) => {
798                self.bump()?;
799                st
800            }
801            _ => {
802                return Err(ParseError::new(
803                    self.lookahead.span.start,
804                    self.lookahead.span.end,
805                    "string literal required (quotes optional)",
806                ));
807            }
808        };
809        // Merge trailing bareword chunks if any
810        while let TokenKind::Bareword(st) = self.lookahead.kind.clone() {
811            self.bump()?;
812            s.push_str(&st);
813        }
814        Ok(s)
815    }
816
817    fn starts_numeric_literal(&self) -> bool {
818        match &self.lookahead.kind {
819            TokenKind::Int(_) => true,
820            TokenKind::Bareword(st) => st == "-" || st == "+" || parse_i64_literal_text(st).is_ok(),
821            TokenKind::LParen => true,
822            _ => false,
823        }
824    }
825
826    fn parse_signed_int(&mut self) -> Result<i64, ParseError> {
827        match self.lookahead.kind.clone() {
828            TokenKind::Int(v) => {
829                self.bump()?;
830                Ok(v)
831            }
832            TokenKind::Bareword(st) => {
833                let span = self.lookahead.span;
834                if st == "-" {
835                    self.bump()?;
836                    let value = self.parse_signed_int()?;
837                    value.checked_neg().ok_or_else(|| {
838                        ParseError::new(span.start, span.end, "integer literal overflow")
839                    })
840                } else if st == "+" {
841                    self.bump()?;
842                    self.parse_signed_int()
843                } else {
844                    match parse_i64_literal_text(&st) {
845                        Ok(v) => {
846                            self.bump()?;
847                            Ok(v)
848                        }
849                        Err(message) => Err(ParseError::new(span.start, span.end, message)),
850                    }
851                }
852            }
853            TokenKind::LParen => {
854                let start = self.bump()?.span.start;
855                let value = self.parse_signed_int()?;
856                let end = self.expect(&TokenKind::RParen)?.span.end;
857                let _ = (start, end);
858                Ok(value)
859            }
860            _ => Err(ParseError::new(
861                self.lookahead.span.start,
862                self.lookahead.span.end,
863                "numeric literal required",
864            )),
865        }
866    }
867}
868
869/// A trait to convert byte slices to i64 values.
870pub trait ToI64: Send + Sync {
871    /// Converts the given byte slice to an i64.
872    fn to_i64(&self, bytes: &[u8], offset: usize) -> Result<i64, &'static str>;
873
874    /// Returns the exact byte width expected by this converter when known.
875    fn byte_len(&self) -> Option<usize> {
876        None
877    }
878}
879
880fn to_str_bytes(buf: &[u8], offset: usize, len: usize) -> Result<&[u8], &'static str> {
881    if offset + len <= buf.len() {
882        let end = buf[offset..offset + len]
883            .iter()
884            .position(|&b| b == 0) // find the first null byte
885            .map(|p| p + offset)
886            .unwrap_or(offset + len);
887        Ok(&buf[offset..end])
888    } else {
889        Err("buffer too small for field")
890    }
891}
892
893/// The type of a field in the schema.
894#[derive(Clone, Copy)]
895#[allow(missing_docs)]
896pub enum FieldType {
897    Integer(&'static dyn ToI64),
898    Bytes,
899    Unsupported,
900}
901
902impl Debug for FieldType {
903    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
904        match self {
905            FieldType::Integer(_) => write!(f, "Integer"),
906            FieldType::Bytes => write!(f, "Bytes"),
907            FieldType::Unsupported => write!(f, "Unsupported"),
908        }
909    }
910}
911
912/// A trait to classify fields at compile time.
913pub trait FieldClassifier {
914    /// The field type.
915    const FIELD_TYPE: FieldType;
916}
917
918/// Declares the available fields and their types for compile-time validation.
919///
920/// Define a schema with the `schema!` macro: each entry supplies the field name,
921/// its `FieldType`, and the (offset, length) describing where the data resides
922/// in a raw event buffer. Integer types implement `FieldClassifier` so you can
923/// write `u32::FIELD_TYPE` directly. Unknown fields referenced in an expression
924/// produce a compile-time `ParseError`.
925#[derive(Default, Clone, Copy)]
926pub struct Schema {
927    /// The list of fields and their types.
928    fields: &'static [(&'static str, FieldType, usize, usize)],
929}
930
931impl Debug for Schema {
932    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
933        let mut fmt = f.debug_struct("Schema");
934        for (n, t, offset, len) in self.fields.iter() {
935            fmt.field(
936                n,
937                &format_args!("type: {:?}, offset: {}, length: {}", t, offset, len),
938            );
939        }
940        fmt.finish()
941    }
942}
943
944impl Schema {
945    /// Creates a new schema from the given field list.
946    pub const fn new(fields: &'static [(&'static str, FieldType, usize, usize)]) -> Self {
947        Self { fields }
948    }
949
950    /// Looks up the type, offset, and length of a field by name.
951    pub fn get(&self, name: &str) -> Option<(FieldType, usize, usize)> {
952        for (n, t, offset, len) in self.fields.iter() {
953            if *n == name {
954                return Some((*t, *offset, *len));
955            }
956        }
957        None
958    }
959
960    /// Looks up the type of a field by name.
961    pub fn get_type(&self, name: &str) -> Option<FieldType> {
962        self.get(name).map(|(t, _, _)| t)
963    }
964}
965
966/// Helper macro to define a schema inline.
967#[macro_export]
968macro_rules! schema {
969    ($( $name:expr => ($ftype:expr, $offset:expr, $len:expr) ),* $(,)? ) => {
970        {
971            let fields: &'static [(&'static str, $crate::FieldType, usize, usize)] = &[
972                $(
973                    ($name, $ftype, $offset, $len),
974                )*
975            ];
976            $crate::Schema::new(fields)
977        }
978
979    };
980}
981
982/// Runtime context abstraction used during expression evaluation.
983///
984/// Two common implementations are provided:
985/// - `BTreeMap<String,String>` (alloc based, convenient for testing)
986/// - `BufContext` for zero-copy access over a raw byte buffer + schema
987///
988/// Custom contexts can implement this trait to bridge existing telemetry/event
989/// representations without copying. Return `Ok(None)` for missing fields to
990/// trigger the tri-state `Unknown` logic, and `Err(EvalError)` for malformed
991/// runtime data.
992pub trait Context {
993    /// Returns an integer value for the given field, or `Ok(None)` if missing.
994    fn get_integer(&self, name: &str) -> Result<Option<i64>, EvalError>;
995    /// Returns a byte slice value for the given field, or `Ok(None)` if missing.
996    fn get_str_bytes(&self, name: &str) -> Result<Option<&[u8]>, EvalError>;
997}
998
999impl Context for BTreeMap<String, String> {
1000    fn get_integer(&self, name: &str) -> Result<Option<i64>, EvalError> {
1001        match self.get(name) {
1002            None => Ok(None),
1003            Some(value) => parse_i64_literal_text(value).map(Some).map_err(|message| {
1004                let message = if message == "integer literal overflow" {
1005                    "integer literal overflow"
1006                } else {
1007                    "invalid integer literal"
1008                };
1009                EvalError::new(name, message)
1010            }),
1011        }
1012    }
1013    fn get_str_bytes(&self, name: &str) -> Result<Option<&[u8]>, EvalError> {
1014        Ok(self.get(name).map(|s| s.as_bytes()))
1015    }
1016}
1017
1018/// Runtime context for evaluating against a byte buffer and schema.
1019pub struct BufContext<'a> {
1020    buf: &'a [u8],
1021    schema: &'a Schema,
1022}
1023
1024impl<'a> BufContext<'a> {
1025    /// Creates a new buffer context with the given buffer and schema.
1026    pub fn new(buf: &'a [u8], schema: &'a Schema) -> Self {
1027        Self { buf, schema }
1028    }
1029}
1030
1031impl<'a> Context for BufContext<'a> {
1032    fn get_integer(&self, name: &str) -> Result<Option<i64>, EvalError> {
1033        let Some((field_type, offset, len)) = self.schema.get(name) else {
1034            return Ok(None);
1035        };
1036        match field_type {
1037            FieldType::Integer(to_i64_fn) => {
1038                if let Some(expected_len) = to_i64_fn.byte_len()
1039                    && len != expected_len
1040                {
1041                    return Err(EvalError::new(
1042                        name,
1043                        format!(
1044                            "schema length {} does not match integer width {}",
1045                            len, expected_len
1046                        ),
1047                    ));
1048                }
1049                to_i64_fn
1050                    .to_i64(self.buf, offset)
1051                    .map(Some)
1052                    .map_err(|message| EvalError::new(name, message))
1053            }
1054            _ => Ok(None),
1055        }
1056    }
1057
1058    fn get_str_bytes(&self, name: &str) -> Result<Option<&[u8]>, EvalError> {
1059        let Some((field_type, offset, len)) = self.schema.get(name) else {
1060            return Ok(None);
1061        };
1062        match field_type {
1063            FieldType::Bytes => {
1064                let bytes = to_str_bytes(self.buf, offset, len)
1065                    .map_err(|message| EvalError::new(name, message))?;
1066                Ok(Some(bytes))
1067            }
1068            _ => Ok(None),
1069        }
1070    }
1071}
1072
1073// --------------- Glob matcher ----------------
1074fn glob_match_bytes(p: &[u8], t: &[u8]) -> bool {
1075    glob_match_impl(p, 0, t, 0)
1076}
1077
1078fn glob_match_impl(p: &[u8], pi: usize, t: &[u8], ti: usize) -> bool {
1079    let mut pi = pi;
1080    let mut ti = ti;
1081    while pi < p.len() {
1082        match p[pi] {
1083            b'?' => {
1084                if ti >= t.len() {
1085                    return false;
1086                }
1087                pi += 1;
1088                ti += 1;
1089            }
1090            b'*' => {
1091                // collapse consecutive *
1092                while pi < p.len() && p[pi] == b'*' {
1093                    pi += 1;
1094                }
1095                if pi == p.len() {
1096                    return true;
1097                } // trailing * matches rest
1098                // try to match the rest at any position
1099                let mut k = ti;
1100                while k <= t.len() {
1101                    if glob_match_impl(p, pi, t, k) {
1102                        return true;
1103                    }
1104                    if k == t.len() {
1105                        break;
1106                    }
1107                    k += 1;
1108                }
1109                return false;
1110            }
1111            b'[' => {
1112                if ti >= t.len() {
1113                    return false;
1114                }
1115                let (ok, new_pi) = match_class(p, pi + 1, t[ti]);
1116                if !ok {
1117                    return false;
1118                }
1119                pi = new_pi; // new_pi points after ']'
1120                ti += 1;
1121            }
1122            c => {
1123                if ti >= t.len() || t[ti] != c {
1124                    return false;
1125                }
1126                pi += 1;
1127                ti += 1;
1128            }
1129        }
1130    }
1131    ti == t.len()
1132}
1133
1134fn match_class(p: &[u8], mut pi: usize, ch: u8) -> (bool, usize) {
1135    let mut matched = false;
1136    let negated = if pi < p.len() && (p[pi] == b'^' || p[pi] == b'!') {
1137        pi += 1;
1138        true
1139    } else {
1140        false
1141    };
1142    let _start_pi = pi;
1143    let mut prev: Option<u8> = None;
1144    while pi < p.len() && p[pi] != b']' {
1145        let c = p[pi];
1146        if c == b'-'
1147            && let Some(start_c) = prev
1148        {
1149            pi += 1; // consume '-'
1150            if pi >= p.len() {
1151                return (false, pi);
1152            }
1153            let end_c = p[pi];
1154            if ch >= start_c && ch <= end_c {
1155                matched = true;
1156            }
1157            prev = None;
1158            pi += 1;
1159            continue;
1160        }
1161        if ch == c {
1162            matched = true;
1163        }
1164        prev = Some(c);
1165        pi += 1;
1166    }
1167    if pi >= p.len() || p[pi] != b']' {
1168        // Unclosed [
1169        return (false, p.len());
1170    }
1171    pi += 1; // skip ']'
1172    let res = if negated { !matched } else { matched };
1173    (res, pi)
1174}
1175
1176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1177enum Tri {
1178    True,
1179    False,
1180    Unknown,
1181}
1182
1183impl Tri {
1184    fn and(self, other: Tri) -> Tri {
1185        match (self, other) {
1186            (Tri::False, _) | (_, Tri::False) => Tri::False,
1187            (Tri::Unknown, x) | (x, Tri::Unknown) => x,
1188            (Tri::True, Tri::True) => Tri::True,
1189        }
1190    }
1191    fn or(self, other: Tri) -> Tri {
1192        match (self, other) {
1193            (Tri::True, _) | (_, Tri::True) => Tri::True,
1194            (Tri::Unknown, x) | (x, Tri::Unknown) => x,
1195            (Tri::False, Tri::False) => Tri::False,
1196        }
1197    }
1198}
1199
1200/// A compiled expression ready to be evaluated against runtime data.
1201#[derive(Debug, Clone)]
1202pub struct Compiled {
1203    expr: Expr,
1204    schema: Schema,
1205}
1206
1207impl Compiled {
1208    /// Evaluates the expression against the provided context.
1209    ///
1210    /// Tri-state semantics: on `Unknown` the top-level result is treated as `true`.
1211    ///
1212    /// Runtime data errors are treated as `false`. Use [`Self::try_evaluate`] to
1213    /// retrieve the underlying [`EvalError`] instead.
1214    pub fn evaluate<C: Context>(&self, ctx: &C) -> bool {
1215        self.try_evaluate(ctx).unwrap_or(false)
1216    }
1217
1218    /// Evaluates the expression against the provided context and reports
1219    /// runtime data errors instead of collapsing them to `false`.
1220    pub fn try_evaluate<C: Context>(&self, ctx: &C) -> Result<bool, EvalError> {
1221        let tri = eval_expr(&self.expr, &self.schema, ctx)?;
1222        match tri {
1223            Tri::True => Ok(true),
1224            Tri::False => Ok(false),
1225            Tri::Unknown => Ok(true), // treat unknown as true at top level
1226        }
1227    }
1228}
1229
1230fn eval_expr<C: Context>(e: &Expr, schema: &Schema, ctx: &C) -> Result<Tri, EvalError> {
1231    match e {
1232        Expr::Group(inner) => eval_expr(inner, schema, ctx),
1233        Expr::Or(l, r) => {
1234            let lv = eval_expr(l, schema, ctx)?;
1235            if lv == Tri::True {
1236                return Ok(Tri::True);
1237            }
1238            let rv = eval_expr(r, schema, ctx)?;
1239            Ok(lv.or(rv))
1240        }
1241        Expr::And(l, r) => {
1242            let lv = eval_expr(l, schema, ctx)?;
1243            if lv == Tri::False {
1244                return Ok(Tri::False);
1245            }
1246            let rv = eval_expr(r, schema, ctx)?;
1247            Ok(lv.and(rv))
1248        }
1249        Expr::NumCmp {
1250            field,
1251            mask,
1252            op,
1253            rhs,
1254            ..
1255        } => {
1256            match schema.get_type(field) {
1257                Some(FieldType::Integer(_)) => match ctx.get_integer(field)? {
1258                    None => Ok(Tri::Unknown),
1259                    Some(mut v) => {
1260                        if let Some(m) = mask {
1261                            v &= *m;
1262                        }
1263                        let res = match op {
1264                            NumOp::Eq => v == *rhs,
1265                            NumOp::Ne => v != *rhs,
1266                            NumOp::Lt => v < *rhs,
1267                            NumOp::Le => v <= *rhs,
1268                            NumOp::Gt => v > *rhs,
1269                            NumOp::Ge => v >= *rhs,
1270                        };
1271                        if res { Ok(Tri::True) } else { Ok(Tri::False) }
1272                    }
1273                },
1274                // Type mismatch: The expression expects an integer field, but the schema says it's a string.
1275                Some(FieldType::Bytes) => Ok(Tri::False),
1276                Some(FieldType::Unsupported) => Ok(Tri::False),
1277                None => unreachable!(),
1278            }
1279        }
1280        Expr::StrCmp { field, op, pat, .. } => {
1281            match schema.get_type(field) {
1282                Some(FieldType::Bytes) => match ctx.get_str_bytes(field)? {
1283                    None => Ok(Tri::Unknown),
1284                    Some(v) => {
1285                        let res = match op {
1286                            StrOp::Eq => v == pat.as_bytes(),
1287                            StrOp::Ne => v != pat.as_bytes(),
1288                            StrOp::Glob => glob_match_bytes(pat.as_bytes(), v),
1289                        };
1290                        if res { Ok(Tri::True) } else { Ok(Tri::False) }
1291                    }
1292                },
1293                // Type mismatch: The expression expects a string field, but the schema says it's an integer.
1294                Some(FieldType::Integer(_)) => Ok(Tri::False),
1295                Some(FieldType::Unsupported) => Ok(Tri::False),
1296                None => unreachable!(),
1297            }
1298        }
1299    }
1300}
1301
1302/// Parses and validates a filter expression against the given `Schema`.
1303///
1304/// Returns a `Compiled` expression on success, or a `ParseError` if lexing,
1305/// parsing, or schema validation fails.
1306pub fn compile_with_schema(input: &str, schema: Schema) -> Result<Compiled, ParseError> {
1307    let mut p = Parser::new(input)?;
1308    let expr = p.parse()?;
1309    // validate identifiers against schema & type/operator compatibility
1310    validate(&expr, &schema)?;
1311    Ok(Compiled { expr, schema })
1312}
1313
1314fn validate(expr: &Expr, schema: &Schema) -> Result<(), ParseError> {
1315    match expr {
1316        Expr::Or(l, r) | Expr::And(l, r) => {
1317            validate(l, schema)?;
1318            validate(r, schema)
1319        }
1320        Expr::Group(inner) => validate(inner, schema),
1321        Expr::NumCmp {
1322            field, field_span, ..
1323        } => match schema.get(field) {
1324            None => Err(ParseError::new(
1325                field_span.start,
1326                field_span.end,
1327                format!("unknown field: {}", field),
1328            )),
1329            Some((FieldType::Bytes, _, _)) => Err(ParseError::new(
1330                field_span.start,
1331                field_span.end,
1332                format!("field '{}' is not numeric", field),
1333            )),
1334            Some((FieldType::Integer(to_i64_fn), _, len)) => {
1335                if let Some(expected_len) = to_i64_fn.byte_len()
1336                    && len != expected_len
1337                {
1338                    Err(ParseError::new(
1339                        field_span.start,
1340                        field_span.end,
1341                        format!(
1342                            "field '{}' length {} does not match integer width {}",
1343                            field, len, expected_len
1344                        ),
1345                    ))
1346                } else {
1347                    Ok(())
1348                }
1349            }
1350            Some((FieldType::Unsupported, _, _)) => Err(ParseError::new(
1351                field_span.start,
1352                field_span.end,
1353                format!("field '{}' has unsupported type", field),
1354            )),
1355        },
1356        Expr::StrCmp {
1357            field, field_span, ..
1358        } => match schema.get(field) {
1359            None => Err(ParseError::new(
1360                field_span.start,
1361                field_span.end,
1362                format!("unknown field: {}", field),
1363            )),
1364            Some((FieldType::Integer(_), _, _)) => Err(ParseError::new(
1365                field_span.start,
1366                field_span.end,
1367                format!("field '{}' is not string", field),
1368            )),
1369            Some((FieldType::Bytes, _, _)) => Ok(()),
1370            Some((FieldType::Unsupported, _, _)) => Err(ParseError::new(
1371                field_span.start,
1372                field_span.end,
1373                format!("field '{}' has unsupported type", field),
1374            )),
1375        },
1376    }
1377}
1378
1379#[cfg(test)]
1380mod tests {
1381    use core::num::{NonZeroI128, NonZeroIsize, NonZeroU64, NonZeroU128, NonZeroUsize};
1382
1383    use super::*;
1384
1385    fn schema_sig_comm_flags_user() -> Schema {
1386        schema! {
1387            "sig" => (u32::FIELD_TYPE, 0, 4),
1388            "comm" => (FieldType::Bytes, 4, 16),
1389            "flags" => (i32::FIELD_TYPE, 20, 4),
1390            "user" => (FieldType::Bytes, 24, 16),
1391        }
1392    }
1393
1394    #[test]
1395    fn test_basic_parse_and_eval() {
1396        let schema = schema_sig_comm_flags_user();
1397        let compiled = compile_with_schema(
1398            "((sig >= 10 && sig < 15) || sig == 17) && comm != bash",
1399            schema,
1400        )
1401        .expect("compile");
1402
1403        // matching example
1404        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1405        ctx.insert("sig".into(), "12".into());
1406        ctx.insert("comm".into(), "sh".into());
1407        assert!(compiled.evaluate(&ctx));
1408
1409        // non-matching due to comm
1410        ctx.insert("comm".into(), "bash".into());
1411        assert!(!compiled.evaluate(&ctx));
1412
1413        // matching by sig == 17
1414        ctx.insert("sig".into(), "17".into());
1415        ctx.insert("comm".into(), "bash5".into()); // comm != bash still true
1416        assert!(compiled.evaluate(&ctx));
1417    }
1418
1419    #[test]
1420    fn test_hex_and_mask() {
1421        let schema = schema_sig_comm_flags_user();
1422        let compiled = compile_with_schema("flags & 0x10 == 0x10", schema).expect("compile");
1423        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1424        ctx.insert("flags".into(), format!("{}", 0x30));
1425        assert!(compiled.evaluate(&ctx));
1426        ctx.insert("flags".into(), format!("{}", 0x20));
1427        assert!(!compiled.evaluate(&ctx));
1428    }
1429
1430    #[test]
1431    fn test_map_context_accepts_hex_integer_values() {
1432        let schema = schema!(
1433            "flags" => (u32::FIELD_TYPE, 0, 4),
1434        );
1435        let compiled = compile_with_schema("flags == 0x20", schema).expect("compile");
1436        let mut ctx = BTreeMap::new();
1437        ctx.insert("flags".into(), "0x20".into());
1438        assert!(compiled.evaluate(&ctx));
1439    }
1440
1441    #[test]
1442    fn test_map_context_invalid_integer_fails_closed() {
1443        let schema = schema!(
1444            "flags" => (u32::FIELD_TYPE, 0, 4),
1445        );
1446        let compiled = compile_with_schema("flags == 16", schema).expect("compile");
1447        let mut ctx = BTreeMap::new();
1448        ctx.insert("flags".into(), "xyz".into());
1449        assert!(!compiled.evaluate(&ctx));
1450        let err = compiled.try_evaluate(&ctx).unwrap_err();
1451        assert_eq!(err.field, "flags");
1452        assert!(err.message.contains("invalid integer literal"));
1453    }
1454
1455    #[test]
1456    fn test_negative_decimal_literal() {
1457        let schema = schema!(
1458            "v" => (i64::FIELD_TYPE, 0, 8),
1459        );
1460        let compiled = compile_with_schema("v == -1", schema).expect("compile");
1461        let buf = (-1i64).to_ne_bytes();
1462        let ctx = BufContext::new(&buf, &schema);
1463        assert!(compiled.evaluate(&ctx));
1464    }
1465
1466    #[test]
1467    fn test_unary_minus_with_parenthesized_hex_literal() {
1468        let schema = schema!(
1469            "v" => (i64::FIELD_TYPE, 0, 8),
1470        );
1471        let compiled = compile_with_schema("v == -(0x10)", schema).expect("compile");
1472        let buf = (-16i64).to_ne_bytes();
1473        let ctx = BufContext::new(&buf, &schema);
1474        assert!(compiled.evaluate(&ctx));
1475    }
1476
1477    #[test]
1478    fn test_glob_match_primitives() {
1479        assert!(super::glob_match_bytes(b"bash*", b"bash"));
1480        assert!(super::glob_match_bytes(b"bash*", b"bash123"));
1481        assert!(super::glob_match_bytes(b"b?sh", b"bash"));
1482        assert!(super::glob_match_bytes(b"b[ae]sh", b"bash"));
1483        assert!(!super::glob_match_bytes(b"b[!a]sh", b"bash"));
1484        assert!(!super::glob_match_bytes(b"b[^a]sh", b"bash"));
1485    }
1486
1487    #[test]
1488    fn test_string_glob_expr() {
1489        let schema = schema_sig_comm_flags_user();
1490        let compiled = compile_with_schema("comm ~ bash*", schema).expect("compile");
1491        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1492        ctx.insert("comm".into(), "bashXYZ".into());
1493        assert!(compiled.evaluate(&ctx));
1494        ctx.insert("comm".into(), "sh".into());
1495        assert!(!compiled.evaluate(&ctx));
1496    }
1497
1498    #[test]
1499    fn test_string_equality_literal_star() {
1500        let schema = schema_sig_comm_flags_user();
1501        let compiled = compile_with_schema("comm == bash*", schema).expect("compile");
1502        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1503        ctx.insert("comm".into(), "bash*".into());
1504        assert!(compiled.evaluate(&ctx));
1505        ctx.insert("comm".into(), "bashXYZ".into());
1506        assert!(!compiled.evaluate(&ctx)); // equality, not glob
1507    }
1508
1509    #[test]
1510    fn test_string_with_quotes_and_spaces() {
1511        let schema = schema_sig_comm_flags_user();
1512        let compiled = compile_with_schema("comm == \"C Program\"", schema).expect("compile");
1513        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1514        ctx.insert("comm".into(), "C Program".into());
1515        assert!(compiled.evaluate(&ctx));
1516    }
1517
1518    #[test]
1519    fn test_runtime_missing_field_unknown_semantics() {
1520        // 'user' is in schema but not provided in ctx
1521        let schema = schema_sig_comm_flags_user();
1522        let compiled1 = compile_with_schema("user == root", schema).expect("compile");
1523        let compiled2 = compile_with_schema("user == root || sig == 17", schema).expect("compile");
1524        let compiled3 = compile_with_schema("user == root && sig == 17", schema).expect("compile");
1525
1526        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1527        // Only sig is provided later when needed
1528        assert!(compiled1.evaluate(&ctx)); // Unknown at top level => true
1529
1530        ctx.insert("sig".into(), "17".into());
1531        assert!(compiled2.evaluate(&ctx)); // Unknown || True => True
1532        assert!(compiled3.evaluate(&ctx)); // Unknown && True => True (true-neutral)
1533    }
1534
1535    #[test]
1536    fn test_precedence_and_grouping() {
1537        let schema = schema_sig_comm_flags_user();
1538        let c1 =
1539            compile_with_schema("sig == 1 || sig == 2 && comm == bash", schema).expect("compile");
1540        let c2 =
1541            compile_with_schema("(sig == 1 || sig == 2) && comm == bash", schema).expect("compile");
1542        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1543        ctx.insert("sig".into(), "2".into());
1544        ctx.insert("comm".into(), "sh".into());
1545        assert!(!c1.evaluate(&ctx));
1546        assert!(!c2.evaluate(&ctx));
1547        ctx.insert("comm".into(), "bash".into());
1548        assert!(c2.evaluate(&ctx));
1549        assert!(c1.evaluate(&ctx));
1550    }
1551
1552    #[test]
1553    fn test_type_mismatch_compile_error() {
1554        let schema = schema_sig_comm_flags_user();
1555        let err = compile_with_schema("sig == \"17\"", schema).unwrap_err();
1556        assert!(err.message.contains("not string"));
1557    }
1558
1559    #[test]
1560    fn test_leading_trailing_whitespace() {
1561        let schema = schema_sig_comm_flags_user();
1562        let c = compile_with_schema("  comm   !=   bash   ", schema).expect("compile");
1563        let mut ctx: BTreeMap<String, String> = BTreeMap::new();
1564        ctx.insert("comm".into(), "zsh".into());
1565        assert!(c.evaluate(&ctx));
1566        ctx.insert("comm".into(), "bash".into());
1567        assert!(!c.evaluate(&ctx));
1568    }
1569
1570    #[test]
1571    fn test_numeric_eq_ne() {
1572        let schema = schema!(
1573            "sig" => (u32::FIELD_TYPE, 0, 4),
1574        );
1575        let eq = compile_with_schema("sig == 10", schema).expect("compile eq");
1576        let ne = compile_with_schema("sig != 10", schema).expect("compile ne");
1577        let mut ctx = BTreeMap::new();
1578        ctx.insert("sig".into(), "10".into());
1579        assert!(eq.evaluate(&ctx));
1580        assert!(!ne.evaluate(&ctx));
1581        ctx.insert("sig".into(), "11".into());
1582        assert!(!eq.evaluate(&ctx));
1583        assert!(ne.evaluate(&ctx));
1584    }
1585
1586    #[test]
1587    fn test_numeric_lt_le_gt_ge() {
1588        let schema = schema!(
1589            "v" => (u32::FIELD_TYPE, 0, 4),
1590        );
1591        let lt = compile_with_schema("v < 5", schema).unwrap();
1592        let le = compile_with_schema("v <= 5", schema).unwrap();
1593        let gt = compile_with_schema("v > 5", schema).unwrap();
1594        let ge = compile_with_schema("v >= 5", schema).unwrap();
1595        let mut ctx = BTreeMap::new();
1596        ctx.insert("v".into(), "5".into());
1597        assert!(!lt.evaluate(&ctx));
1598        assert!(le.evaluate(&ctx));
1599        assert!(!gt.evaluate(&ctx));
1600        assert!(ge.evaluate(&ctx));
1601        ctx.insert("v".into(), "4".into());
1602        assert!(lt.evaluate(&ctx));
1603        assert!(le.evaluate(&ctx));
1604        assert!(!gt.evaluate(&ctx));
1605        assert!(!ge.evaluate(&ctx));
1606        ctx.insert("v".into(), "6".into());
1607        assert!(!lt.evaluate(&ctx));
1608        assert!(!le.evaluate(&ctx));
1609        assert!(gt.evaluate(&ctx));
1610        assert!(ge.evaluate(&ctx));
1611    }
1612
1613    #[test]
1614    fn test_numeric_mask_ne() {
1615        let schema = schema!(
1616            "flags" => (i32::FIELD_TYPE, 0, 4),
1617        );
1618        let cmp = compile_with_schema("flags & 0x10 != 0x10", schema).unwrap();
1619        let mut ctx = BTreeMap::new();
1620        ctx.insert("flags".into(), format!("{}", 0x30)); // 0x30 & 0x10 == 0x10 => expression should be false
1621        assert!(!cmp.evaluate(&ctx));
1622        ctx.insert("flags".into(), format!("{}", 0x20)); // 0x20 & 0x10 == 0x00 => != 0x10 => true
1623        assert!(cmp.evaluate(&ctx));
1624    }
1625
1626    #[test]
1627    fn test_string_eq_ne_ops() {
1628        let schema = schema!(
1629            "comm" => (FieldType::Bytes, 0, 16),
1630        );
1631        let eq = compile_with_schema("comm == bash", schema).unwrap();
1632        let ne = compile_with_schema("comm != bash", schema).unwrap();
1633        let mut ctx = BTreeMap::new();
1634        ctx.insert("comm".into(), "bash".into());
1635        assert!(eq.evaluate(&ctx));
1636        assert!(!ne.evaluate(&ctx));
1637        ctx.insert("comm".into(), "zsh".into());
1638        assert!(!eq.evaluate(&ctx));
1639        assert!(ne.evaluate(&ctx));
1640    }
1641
1642    #[test]
1643    fn test_compile_error_string_field_numeric_ops() {
1644        let schema = schema!(
1645            "comm" => (FieldType::Bytes, 0, 16),
1646        );
1647        let err = compile_with_schema("comm < 5", schema).unwrap_err();
1648        assert!(err.message.contains("not numeric"));
1649    }
1650
1651    #[test]
1652    fn test_compile_error_numeric_field_string_glob() {
1653        let schema = schema!(
1654            "sig" => (u32::FIELD_TYPE, 0, 4),
1655        );
1656        let err = compile_with_schema("sig ~ bash*", schema).unwrap_err();
1657        assert!(err.message.contains("not string"));
1658    }
1659
1660    #[test]
1661    fn test_u64_values_use_i64_cast_semantics() {
1662        let schema = schema!(
1663            "v" => (u64::FIELD_TYPE, 0, 8),
1664        );
1665        let compiled = compile_with_schema("v == 42", schema).expect("compile");
1666        let buf = 42u64.to_ne_bytes();
1667        let ctx = BufContext::new(&buf, &schema);
1668        assert!(compiled.evaluate(&ctx));
1669    }
1670
1671    #[test]
1672    fn test_u128_values_truncate_to_i64() {
1673        let schema = schema!(
1674            "v" => (u128::FIELD_TYPE, 0, 16),
1675        );
1676        let compiled = compile_with_schema("v == 7", schema).expect("compile");
1677        let buf = ((1u128 << 80) | 7).to_ne_bytes();
1678        let ctx = BufContext::new(&buf, &schema);
1679        assert!(compiled.evaluate(&ctx));
1680    }
1681
1682    #[test]
1683    fn test_i128_values_truncate_to_i64() {
1684        let schema = schema!(
1685            "v" => (i128::FIELD_TYPE, 0, 16),
1686        );
1687        let compiled = compile_with_schema("v == 5", schema).expect("compile");
1688        let buf = ((1i128 << 80) | 5).to_ne_bytes();
1689        let ctx = BufContext::new(&buf, &schema);
1690        assert!(compiled.evaluate(&ctx));
1691    }
1692
1693    #[test]
1694    fn test_nonzero_u128_values_truncate_to_i64() {
1695        let schema = schema!(
1696            "v" => (NonZeroU128::FIELD_TYPE, 0, 16),
1697        );
1698        let compiled = compile_with_schema("v == 9", schema).expect("compile");
1699        let value = NonZeroU128::new((1u128 << 96) | 9).unwrap();
1700        let buf = value.get().to_ne_bytes();
1701        let ctx = BufContext::new(&buf, &schema);
1702        assert!(compiled.evaluate(&ctx));
1703    }
1704
1705    #[test]
1706    fn test_nonzero_i128_values_truncate_to_i64() {
1707        let schema = schema!(
1708            "v" => (NonZeroI128::FIELD_TYPE, 0, 16),
1709        );
1710        let compiled = compile_with_schema("v == 11", schema).expect("compile");
1711        let value = NonZeroI128::new((1i128 << 96) | 11).unwrap();
1712        let buf = value.get().to_ne_bytes();
1713        let ctx = BufContext::new(&buf, &schema);
1714        assert!(compiled.evaluate(&ctx));
1715    }
1716
1717    #[test]
1718    fn test_usize_values_use_i64_cast_semantics() {
1719        let schema = schema!(
1720            "v" => (usize::FIELD_TYPE, 0, core::mem::size_of::<usize>()),
1721        );
1722        let expr = format!("v == {}", 42usize as i64);
1723        let compiled = compile_with_schema(&expr, schema).expect("compile");
1724        let buf = 42usize.to_ne_bytes();
1725        let ctx = BufContext::new(&buf, &schema);
1726        assert!(compiled.evaluate(&ctx));
1727    }
1728
1729    #[test]
1730    fn test_nonzero_usize_values_are_supported() {
1731        let schema = schema!(
1732            "v" => (NonZeroUsize::FIELD_TYPE, 0, core::mem::size_of::<usize>()),
1733        );
1734        let expr = format!("v == {}", 17usize as i64);
1735        let compiled = compile_with_schema(&expr, schema).expect("compile");
1736        let value = NonZeroUsize::new(17usize).unwrap();
1737        let buf = value.get().to_ne_bytes();
1738        let ctx = BufContext::new(&buf, &schema);
1739        assert!(compiled.evaluate(&ctx));
1740    }
1741
1742    #[test]
1743    fn test_isize_values_are_supported() {
1744        let schema = schema!(
1745            "v" => (isize::FIELD_TYPE, 0, core::mem::size_of::<isize>()),
1746        );
1747        let expr = format!("v == {}", 42isize as i64);
1748        let compiled = compile_with_schema(&expr, schema).expect("compile");
1749        let buf = 42isize.to_ne_bytes();
1750        let ctx = BufContext::new(&buf, &schema);
1751        assert!(compiled.evaluate(&ctx));
1752    }
1753
1754    #[test]
1755    fn test_nonzero_isize_values_are_supported() {
1756        let schema = schema!(
1757            "v" => (NonZeroIsize::FIELD_TYPE, 0, core::mem::size_of::<isize>()),
1758        );
1759        let expr = format!("v == {}", 19isize as i64);
1760        let compiled = compile_with_schema(&expr, schema).expect("compile");
1761        let value = NonZeroIsize::new(19isize).unwrap();
1762        let buf = value.get().to_ne_bytes();
1763        let ctx = BufContext::new(&buf, &schema);
1764        assert!(compiled.evaluate(&ctx));
1765    }
1766
1767    #[test]
1768    fn test_nonzero_u64_values_are_supported() {
1769        let schema = schema!(
1770            "v" => (NonZeroU64::FIELD_TYPE, 0, 8),
1771        );
1772        let compiled = compile_with_schema("v == 23", schema).expect("compile");
1773        let value = NonZeroU64::new(23u64).unwrap();
1774        let buf = value.get().to_ne_bytes();
1775        let ctx = BufContext::new(&buf, &schema);
1776        assert!(compiled.evaluate(&ctx));
1777    }
1778
1779    #[test]
1780    fn test_compile_error_unsupported_f32_field() {
1781        let schema = schema!(
1782            "v" => (f32::FIELD_TYPE, 0, 4),
1783        );
1784        let err = compile_with_schema("v == 1", schema).unwrap_err();
1785        assert!(err.message.contains("unsupported type"));
1786    }
1787
1788    #[test]
1789    fn test_compile_error_integer_length_mismatch() {
1790        let schema = schema!(
1791            "v" => (u32::FIELD_TYPE, 0, 1),
1792        );
1793        let err = compile_with_schema("v == 1", schema).unwrap_err();
1794        assert!(
1795            err.message
1796                .contains("length 1 does not match integer width 4")
1797        );
1798    }
1799}