ctx-parser (Rust)
ctx-parser (Rust)
Section titled “ctx-parser (Rust)”The canonical Rust implementation of the CTX parser. Compiles to native code, Node.js (NAPI), Python (PyO3), and WebAssembly.
Installation
Section titled “Installation”Rust (Cargo)
Section titled “Rust (Cargo)”[dependencies]ctx-parser = "0.1"Node.js (NAPI)
Section titled “Node.js (NAPI)”npm install @agentctx/ctx-parser-nativePython (PyO3)
Section titled “Python (PyO3)”pip install ctx-parserWebAssembly
Section titled “WebAssembly”npm install @agentctx/ctx-parser-wasmuse ctx_parser::{Parser, Statement, Operator, Plane};
let parser = Parser::new();let stmt: Statement = parser.parse("?k \"auth\" #code @7d ^3")?;
assert_eq!(stmt.operator, Operator::Search);assert_eq!(stmt.plane, Plane::Knowledge);assert_eq!(stmt.target, "auth");assert_eq!(stmt.filters.len(), 3);Node.js (NAPI)
Section titled “Node.js (NAPI)”import { parse } from '@agentctx/ctx-parser-native';
const stmt = parse('?k "auth" #code ^3');// Same interface as TypeScript CTXParserPython (PyO3)
Section titled “Python (PyO3)”from ctx_parser import parse
stmt = parse('?k "auth" #code ^3')print(stmt.operator) # "?"print(stmt.plane) # "k"print(stmt.target) # "auth"WebAssembly
Section titled “WebAssembly”import init, { parse } from '@agentctx/ctx-parser-wasm';
await init();const stmt = parse('?k "auth" #code ^3');Performance
Section titled “Performance”| Environment | Parse (simple) | Parse (complex) |
|---|---|---|
| Rust native | 0.004ms | 0.008ms |
| Node.js (NAPI) | 0.005ms | 0.010ms |
| Python (PyO3) | 0.006ms | 0.012ms |
| WASM (browser) | 0.010ms | 0.020ms |
| TypeScript (baseline) | 0.080ms | 0.150ms |
The Rust parser is 20x faster than the TypeScript baseline.
Test Parity
Section titled “Test Parity”The Rust parser passes the same 75 test cases as the TypeScript parser, ensuring byte-for-byte compatibility:
cd crates/ctx-parsercargo test# 75 tests passedBuilding from Source
Section titled “Building from Source”# Nativecargo build --release
# Node.js NAPInapi build --release
# Python PyO3maturin develop --release
# WebAssemblywasm-pack build --target webSee Also
Section titled “See Also”- Rust Crates — all Rust crates
- Formal Specification — the grammar