Graded byte parsers for Lean 4. Parsers consume ByteArray; grades record failure and input
consumption. Repetition requires an always-consuming parser, so many (pure x) is rejected by
the type checker. Erased witnesses also keep every reported success or failure offset within the
input interval reached from the parser's starting position.
The core package is Mathlib-free: its only runtime dependency is Batteries. The optional
grip-props/ proof project imports Mathlib for the machine-checked law suite and is not part of a
normal require grip dependency closure.
Recursive byte parsers need a way to rule out non-consuming repetition and keep reported offsets within the input that was actually parsed.
API documentation: https://jonaprieto.github.io/lean-grip/.
This project is maintained by its author with AI-assisted development tools. Changes are reviewed, tested, and remain the maintainer's responsibility.
import Grip
open Grip GParser
def point : Parser (Nat × Nat) := do
ch '('
let x ← nat
ch ','
let y ← nat
ch ')'
return (x, y)
#eval run? point "(3,14)".toUTF8 -- some (3, 14)The graded API uses GParser, gdo, fix, dispatch, capture, and many. ParseError.pretty
provides a dependency-free fallback; grip-diagnostics
adds source context and styled output.
grip-props contains the optional machine-checked laws. Core parser benchmarks are described in
bench/RESULTS.md; JSON conformance and DOM benchmarks live in
grip-json.
lake build Grip Test Examples bench
lake exe benchtptp uses Grip for TPTP/TSTP parsing;
lean-argus,
lean-calc-chat, and
oatp build on the parser.
Grip took its inspiration from prim-parser by Jan
Mas Rovira. It diverges by encoding parser behavior at the type level: grades track failure and
input consumption, making repetition safe by construction. Grip also uses a byte-level core,
ParseResult, first-byte dispatch, and @[specialize] scan loops.
Apache-2.0.