Sprint 201: Panic-Free Parser & CLI Hardening ๐ก๏ธ
Welcome to Sprint 201! Building upon the high-performance foundations of Sprint 200, we have completed a major stability refactoring: migrating the entire compiler parser from panic-driven error propagation to a compile-time safe, Result-based model. We also purged raw `unwrap()`s from the CLI to ensure autonomous AI agents can run code safely without crash loops.
๐ก๏ธ Result-Based Parsing
Previously, the parser would panic whenever it hit an unexpected character or syntax error, using a JSON-wrapped diagnostic layout. This forced our CLI runner to catch panics via `catch_unwind` โ a heavy and unidiomatic approach in Rust.
In Sprint 201, we changed the parser signatures to propagate errors safely using the `?` operator. We defined a formal `ParseError` enum:
pub enum ParseError {
InvalidJson(String),
MissingField(String),
UnexpectedToken { expected: String, found: String },
UnexpectedChar(char),
UnexpectedNode(String),
Other(String),
}
๐๏ธ Refactoring Map: Panic Purge
Here is the transition map from panic-driven logic to structured Rust error propagation:
| Before | After (Sprint 201) |
|---|---|
| parse() -> Node | parse() -> Result<Node, ParseError> |
| diagnostic_panic() -> panic!() | parse_error() -> Err(ParseError) |
| expect(token) -> panic!() | expect(token) -> Result<(), ParseError> |
| peek_char().unwrap() | peek_char().ok_or_else(...)? |
| s.parse().unwrap() | s.parse().map_err(...)? |
๐ Affected Callers & CLI Updates
All parts of the engine calling the parser have been updated to cleanly match on the `Result`:
src/bin/run_knc.rs: Removed legacycatch_unwind; handles the parser Result directly for a clean exit.src/vm/compiler.rs: Performs error-matching on the output ofparser.parse().src/validator.rs: Retired the old panic handler, mapping parser errors using.map_err(...).src/bin/knoten_build.rs: Updated compilation scaffold to handle parsing errors safely.
๐งช Robust Testing & CI
To verify this, we added the regression test test_parser_invalid_syntax_returns_err, which passes an invalid assignment "let x = ;" and verifies that it is correctly caught as a Err(ParseError) rather than crashing the thread.
All 140 integration and unit tests are fully passing, and cargo clippy returns 0 warnings across the entire workspace!