bytecode-filter 0.1.1

Fast bytecode-compiled filter engine for delimiter-separated records
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Compiler for filter expressions.
//!
//! Compiles an AST into bytecode that can be executed by the VM.

use std::collections::HashMap;

use regex::bytes::Regex;
use thiserror::Error;

use crate::opcode::Opcode;
use crate::parser::{Expr, ParseError, ParserConfig};
use crate::vm::CompiledFilter;

/// Compilation error types.
#[derive(Debug, Clone, Error)]
#[allow(missing_docs)]
pub enum CompileError {
    #[error("Parse error: {0}")]
    Parse(#[from] ParseError),

    #[error("Unknown field '{0}'. Available fields: {1}")]
    UnknownField(String, String),

    #[error("Invalid regex pattern '{pattern}': {error}")]
    InvalidRegex { pattern: String, error: String },

    #[error("Too many strings (max 65535)")]
    TooManyStrings,

    #[error("Too many regexes (max 65535)")]
    TooManyRegexes,

    #[error("Too many string sets (max 65535)")]
    TooManySets,
}

/// Compiler state during bytecode generation.
struct Compiler<'a> {
    config: &'a ParserConfig,
    bytecode: Vec<u8>,
    strings: Vec<Vec<u8>>,
    string_map: HashMap<Vec<u8>, u16>,
    regexes: Vec<Regex>,
    regex_map: HashMap<String, u16>,
    string_sets: Vec<Vec<u16>>,
}

impl<'a> Compiler<'a> {
    fn new(config: &'a ParserConfig) -> Self {
        Self {
            config,
            bytecode: Vec::new(),
            strings: Vec::new(),
            string_map: HashMap::new(),
            regexes: Vec::new(),
            regex_map: HashMap::new(),
            string_sets: Vec::new(),
        }
    }

    /// Intern a string and return its index.
    fn intern_string(&mut self, s: &str) -> Result<u16, CompileError> {
        let bytes = s.as_bytes().to_vec();
        if let Some(&idx) = self.string_map.get(&bytes) {
            return Ok(idx);
        }

        let idx = self.strings.len();
        if idx > u16::MAX as usize {
            return Err(CompileError::TooManyStrings);
        }

        self.string_map.insert(bytes.clone(), idx as u16);
        self.strings.push(bytes);
        Ok(idx as u16)
    }

    /// Intern a regex and return its index.
    fn intern_regex(&mut self, pattern: &str) -> Result<u16, CompileError> {
        if let Some(&idx) = self.regex_map.get(pattern) {
            return Ok(idx);
        }

        let regex = Regex::new(pattern).map_err(|e| CompileError::InvalidRegex {
            pattern: pattern.to_string(),
            error: e.to_string(),
        })?;

        let idx = self.regexes.len();
        if idx > u16::MAX as usize {
            return Err(CompileError::TooManyRegexes);
        }

        self.regex_map.insert(pattern.to_string(), idx as u16);
        self.regexes.push(regex);
        Ok(idx as u16)
    }

    /// Add a string set and return its index.
    fn add_string_set(&mut self, values: &[String]) -> Result<u16, CompileError> {
        let indices: Vec<u16> = values
            .iter()
            .map(|v| self.intern_string(v))
            .collect::<Result<_, _>>()?;

        let idx = self.string_sets.len();
        if idx > u16::MAX as usize {
            return Err(CompileError::TooManySets);
        }

        self.string_sets.push(indices);
        Ok(idx as u16)
    }

    /// Look up a field name and return its part index.
    fn lookup_field(&self, name: &str) -> Result<u8, CompileError> {
        // Try case-insensitive lookup
        let upper = name.to_uppercase();
        self.config
            .fields
            .get(&upper)
            .or_else(|| self.config.fields.get(name))
            .copied()
            .ok_or_else(|| {
                let available: Vec<_> = self.config.fields.keys().cloned().collect();
                CompileError::UnknownField(name.to_string(), available.join(", "))
            })
    }

    /// Emit a single byte.
    fn emit(&mut self, byte: u8) {
        self.bytecode.push(byte);
    }

    /// Emit a u16 in little-endian format.
    fn emit_u16(&mut self, value: u16) {
        self.bytecode.extend_from_slice(&value.to_le_bytes());
    }

    /// Emit an i16 in little-endian format.
    fn emit_i16(&mut self, value: i16) {
        self.bytecode.extend_from_slice(&value.to_le_bytes());
    }

    /// Current bytecode offset (for backpatching).
    fn offset(&self) -> usize {
        self.bytecode.len()
    }

    /// Backpatch an i16 at the given bytecode position.
    fn patch_i16(&mut self, pos: usize, value: i16) {
        let bytes = value.to_le_bytes();
        self.bytecode[pos] = bytes[0];
        self.bytecode[pos + 1] = bytes[1];
    }

    /// Compile an expression.
    fn compile_expr(&mut self, expr: &Expr) -> Result<(), CompileError> {
        match expr {
            Expr::Bool(true) => {
                self.emit(Opcode::PushTrue as u8);
            }
            Expr::Bool(false) => {
                self.emit(Opcode::PushFalse as u8);
            }
            Expr::Rand(n) => {
                self.emit(Opcode::Rand as u8);
                self.emit_u16(*n);
            }

            // Payload-wide operations
            Expr::Contains(s) => {
                let idx = self.intern_string(s)?;
                self.emit(Opcode::Contains as u8);
                self.emit_u16(idx);
            }
            Expr::StartsWith(s) => {
                let idx = self.intern_string(s)?;
                self.emit(Opcode::StartsWith as u8);
                self.emit_u16(idx);
            }
            Expr::EndsWith(s) => {
                let idx = self.intern_string(s)?;
                self.emit(Opcode::EndsWith as u8);
                self.emit_u16(idx);
            }
            Expr::Equals(s) => {
                let idx = self.intern_string(s)?;
                self.emit(Opcode::Equals as u8);
                self.emit_u16(idx);
            }
            Expr::Matches(pattern) => {
                let idx = self.intern_regex(pattern)?;
                self.emit(Opcode::Matches as u8);
                self.emit_u16(idx);
            }

            // Part-specific operations
            Expr::PartContains { part, value } => {
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartContains as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
            }
            Expr::PartIContains { part, value } => {
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartIContains as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
            }
            Expr::PartStartsWith { part, value } => {
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartStartsWith as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
            }
            Expr::PartEndsWith { part, value } => {
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartEndsWith as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
            }
            Expr::PartEquals { part, value } => {
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartEquals as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
            }
            Expr::PartIEquals { part, value } => {
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartIEquals as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
            }
            Expr::PartNotEquals { part, value } => {
                // Compile as NOT (PartEquals)
                let part_idx = self.lookup_field(part)?;
                let str_idx = self.intern_string(value)?;
                self.emit(Opcode::PartEquals as u8);
                self.emit(part_idx);
                self.emit_u16(str_idx);
                self.emit(Opcode::Not as u8);
            }
            Expr::PartMatches { part, pattern } => {
                let part_idx = self.lookup_field(part)?;
                let regex_idx = self.intern_regex(pattern)?;
                self.emit(Opcode::PartMatches as u8);
                self.emit(part_idx);
                self.emit_u16(regex_idx);
            }
            Expr::PartInSet { part, values } => {
                let part_idx = self.lookup_field(part)?;
                let set_idx = self.add_string_set(values)?;
                self.emit(Opcode::PartInSet as u8);
                self.emit(part_idx);
                self.emit_u16(set_idx);
            }
            Expr::PartIsEmpty { part } => {
                let part_idx = self.lookup_field(part)?;
                self.emit(Opcode::PartIsEmpty as u8);
                self.emit(part_idx);
            }
            Expr::PartNotEmpty { part } => {
                let part_idx = self.lookup_field(part)?;
                self.emit(Opcode::PartNotEmpty as u8);
                self.emit(part_idx);
            }

            // Header operations
            Expr::HeaderEquals {
                part,
                header,
                value,
            } => {
                let part_idx = self.lookup_field(part)?;
                let hdr_idx = self.intern_string(header)?;
                let val_idx = self.intern_string(value)?;
                self.emit(Opcode::HeaderEquals as u8);
                self.emit(part_idx);
                self.emit_u16(hdr_idx);
                self.emit_u16(val_idx);
            }
            Expr::HeaderIEquals {
                part,
                header,
                value,
            } => {
                let part_idx = self.lookup_field(part)?;
                let hdr_idx = self.intern_string(header)?;
                let val_idx = self.intern_string(value)?;
                self.emit(Opcode::HeaderIEquals as u8);
                self.emit(part_idx);
                self.emit_u16(hdr_idx);
                self.emit_u16(val_idx);
            }
            Expr::HeaderContains {
                part,
                header,
                value,
            } => {
                let part_idx = self.lookup_field(part)?;
                let hdr_idx = self.intern_string(header)?;
                let val_idx = self.intern_string(value)?;
                self.emit(Opcode::HeaderContains as u8);
                self.emit(part_idx);
                self.emit_u16(hdr_idx);
                self.emit_u16(val_idx);
            }
            Expr::HeaderExists { part, header } => {
                let part_idx = self.lookup_field(part)?;
                let hdr_idx = self.intern_string(header)?;
                self.emit(Opcode::HeaderExists as u8);
                self.emit(part_idx);
                self.emit_u16(hdr_idx);
            }

            // Boolean operations — short-circuit with jumps
            Expr::And(left, right) => {
                // Emit left operand
                self.compile_expr(left)?;
                // JumpIfFalse: if left is false, skip right (leave false on stack)
                let opcode_pos = self.offset();
                self.emit(Opcode::JumpIfFalse as u8);
                let patch_pos = self.offset();
                self.emit_i16(0); // placeholder
                // Emit right operand (its result becomes the AND result)
                self.compile_expr(right)?;
                // Backpatch: offset is relative to opcode position (VM does pc += offset)
                let jump_target = self.offset();
                let offset = (jump_target as isize - opcode_pos as isize) as i16;
                self.patch_i16(patch_pos, offset);
            }
            Expr::Or(left, right) => {
                // Emit left operand
                self.compile_expr(left)?;
                // JumpIfTrue: if left is true, skip right (leave true on stack)
                let opcode_pos = self.offset();
                self.emit(Opcode::JumpIfTrue as u8);
                let patch_pos = self.offset();
                self.emit_i16(0); // placeholder
                // Emit right operand (its result becomes the OR result)
                self.compile_expr(right)?;
                // Backpatch: offset is relative to opcode position (VM does pc += offset)
                let jump_target = self.offset();
                let offset = (jump_target as isize - opcode_pos as isize) as i16;
                self.patch_i16(patch_pos, offset);
            }
            Expr::Not(inner) => {
                self.compile_expr(inner)?;
                self.emit(Opcode::Not as u8);
            }
        }

        Ok(())
    }

    /// Finish compilation and return the compiled filter.
    fn finish(mut self, source: String) -> CompiledFilter {
        self.emit(Opcode::Return as u8);

        CompiledFilter::new(
            self.bytecode,
            self.strings,
            self.regexes,
            self.string_sets,
            self.config.delimiter.clone(),
            source,
        )
    }
}

/// Compile a filter expression string into a CompiledFilter.
///
/// # Arguments
/// * `source` - The filter expression string
/// * `config` - Parser configuration with field mappings
///
/// # Returns
/// A `CompiledFilter` ready for evaluation.
///
/// # Example
/// ```
/// use bytecode_filter::{compile, ParserConfig};
/// use bytes::Bytes;
///
/// let mut config = ParserConfig::default();
/// config.add_field("STATUS", 0);
/// config.add_field("CODE", 1);
/// let filter = compile(r#"STATUS == "ok""#, &config).unwrap();
///
/// let record = Bytes::from("ok;;;200");
/// assert!(filter.evaluate(record));
/// ```
///
/// # Errors
/// Returns `CompileError` if parsing or compilation fails.
pub fn compile(source: &str, config: &ParserConfig) -> Result<CompiledFilter, CompileError> {
    let expr = crate::parser::parse(source, config)?;
    compile_expr(&expr, config, source.to_string())
}

/// Compile a pre-parsed AST into a CompiledFilter.
///
/// # Errors
/// Returns `CompileError` if the expression contains invalid operations.
pub fn compile_expr(
    expr: &Expr,
    config: &ParserConfig,
    source: String,
) -> Result<CompiledFilter, CompileError> {
    let mut compiler = Compiler::new(config);
    compiler.compile_expr(expr)?;
    Ok(compiler.finish(source))
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;

    use super::*;

    fn test_config() -> ParserConfig {
        let mut config = ParserConfig::default();
        config.add_field("LEVEL", 0);
        config.add_field("CODE", 1);
        config.add_field("METHOD", 2);
        config.add_field("PATH", 3);
        config.add_field("HEADERS", 4);
        config.add_field("BODY", 5);
        config
    }

    fn compile_and_test(input: &str, payload: &str, expected: bool) {
        let config = test_config();
        let filter = compile(input, &config).expect("Failed to compile");
        let result = filter.evaluate(Bytes::from(payload.to_string()));
        assert_eq!(
            result, expected,
            "Filter '{}' on payload '{}' expected {} but got {}",
            input, payload, expected, result
        );
    }

    #[test]
    fn test_compile_true() {
        compile_and_test("true", "", true);
    }

    #[test]
    fn test_compile_false() {
        compile_and_test("false", "", false);
    }

    #[test]
    fn test_compile_payload_contains() {
        compile_and_test(r#"payload contains "error""#, "an error occurred", true);
        compile_and_test(r#"payload contains "error""#, "all good", false);
    }

    #[test]
    fn test_compile_field_equals() {
        compile_and_test(r#"LEVEL == "error""#, "error;;;500;;;GET", true);
        compile_and_test(r#"LEVEL == "error""#, "info;;;500;;;GET", false);
    }

    #[test]
    fn test_compile_field_in_set() {
        compile_and_test(r#"LEVEL in {"error", "warn", "fatal"}"#, "error;;;500;;;GET", true);
        compile_and_test(r#"LEVEL in {"error", "warn", "fatal"}"#, "warn;;;500;;;GET", true);
        compile_and_test(r#"LEVEL in {"error", "warn", "fatal"}"#, "info;;;500;;;GET", false);
    }

    #[test]
    fn test_compile_and() {
        compile_and_test(
            r#"LEVEL == "error" AND CODE == "500""#,
            "error;;;500;;;GET",
            true,
        );
        compile_and_test(
            r#"LEVEL == "error" AND CODE == "500""#,
            "error;;;200;;;GET",
            false,
        );
    }

    #[test]
    fn test_compile_or() {
        compile_and_test(
            r#"LEVEL == "error" OR LEVEL == "warn""#,
            "error;;;500;;;GET",
            true,
        );
        compile_and_test(
            r#"LEVEL == "error" OR LEVEL == "warn""#,
            "warn;;;500;;;GET",
            true,
        );
        compile_and_test(
            r#"LEVEL == "error" OR LEVEL == "warn""#,
            "info;;;500;;;GET",
            false,
        );
    }

    #[test]
    fn test_compile_not() {
        compile_and_test(r#"NOT LEVEL == "debug""#, "error;;;500;;;GET", true);
        compile_and_test(r#"NOT LEVEL == "debug""#, "debug;;;500;;;GET", false);
    }

    #[test]
    fn test_compile_header_iequals() {
        let mut parts = vec![""; 6];
        parts[0] = "error";
        parts[4] = "X-Custom: value\r\n";
        let payload = parts.join(";;;");

        let config = test_config();
        let filter = compile(
            r#"HEADERS.header("x-custom") iequals "value""#,
            &config,
        )
        .unwrap();

        assert!(filter.evaluate(Bytes::from(payload)));
    }

    #[test]
    fn test_compile_complex_filter() {
        let filter_str = r#"
            LEVEL == "error"
            AND CODE == "500"
            AND HEADERS.header("Content-Type") iequals "application/json"
        "#;

        let config = test_config();
        let filter = compile(filter_str, &config).unwrap();

        let mut parts = vec![""; 6];
        parts[0] = "error";
        parts[1] = "500";
        parts[4] = "Content-Type: application/json\r\n";
        let payload = parts.join(";;;");
        assert!(filter.evaluate(Bytes::from(payload)));

        parts[0] = "info";
        let payload = parts.join(";;;");
        assert!(!filter.evaluate(Bytes::from(payload)));
    }

    #[test]
    fn test_compile_rand() {
        crate::vm::reset_rand_counter();

        let config = test_config();
        let filter = compile("rand(2)", &config).unwrap();

        assert!(filter.evaluate(Bytes::new()));
        assert!(!filter.evaluate(Bytes::new()));
        assert!(filter.evaluate(Bytes::new()));
        assert!(!filter.evaluate(Bytes::new()));
    }

    #[test]
    fn test_compile_regex() {
        compile_and_test(r#"payload matches "error_[0-9]+""#, "found error_123", true);
        compile_and_test(r#"payload matches "error_[0-9]+""#, "no errors", false);
    }

    #[test]
    fn test_compile_unknown_field() {
        let config = test_config();
        let result = compile(r#"UNKNOWN_FIELD == "x""#, &config);
        assert!(matches!(result, Err(CompileError::UnknownField(_, _))));
    }

    #[test]
    fn test_compile_invalid_regex() {
        let config = test_config();
        let result = compile(r#"payload matches "[invalid""#, &config);
        assert!(matches!(result, Err(CompileError::InvalidRegex { .. })));
    }

    #[test]
    fn test_bytecode_size() {
        let config = test_config();

        let filter = compile(r#"LEVEL == "error""#, &config).unwrap();
        assert_eq!(filter.bytecode_len(), 5); // PartEquals(1 + 1 + 2) + Return(1)

        let filter = compile(
            r#"LEVEL == "error" AND CODE == "500""#,
            &config,
        )
        .unwrap();
        assert_eq!(filter.bytecode_len(), 12); // 2x PartEquals(4) + JumpIfFalse(3) + Return(1)
    }
}