sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
Parkour is a parser-combinator library for human-authored text: configuration files, DSLs and small languages. Its aims are error messages that tell the user what is wrong and where, an API that reads well, opt-in error recovery, and stack safety on long and deeply nested input. It depends on nothing beyond the OCaml standard library.
open Parkour
let assignment =
context "assignment"
(let+ name = take_while1 "identifier" is_letter
and+ () = skip_while is_space *> char '=' *> cut *> skip_while is_space
and+ value = integer in
(name, value))
let () =
let input = "ammo = banana" in
match parse assignment input with
| Ok (name, n) -> Printf.printf "%s = %d\n" name n
| Error diagnostics ->
let source = Source.make ~name:"config.txt" input in
List.iter
(fun d -> print_string (Diagnostic.render source d))
diagnosticsprints
error: expected integer, found "banana" --> config.txt:1:8 | 1 | ammo = banana | ^^^^^^ expected integer = while parsing assignment, from 1:1
Diagnostics are also plain data (Parkour.Diagnostic: span, expected, found, context, labels, notes), so editors and other tools can use them directly.
Parkour.label, Parkour.context, Parkour.hidden, Parkour.fail and Parkour.validate shape the messages.Parkour.cut commits once a construct is recognised and Parkour.attempt undoes it.Parkour.recover, Parkour.or_insert, Parkour.skip_past and Parkour.parse_partial report many problems in one run and still return a partial result.Parkour.Lexeme.make builds whitespace, comment, identifier and keyword parsers for a language; numbers, quoted strings and JSON-style escapes are built in.Parkour.tokenize and Parkour.Tokens split parsing into a lexer and a grammar over tokens, with diagnostics that still point into the original text. Parkour.Make accepts other kinds of input.Parkour.expression builds operator parsers from a precedence table; Parkour.chainl1, Parkour.sep_by and Parkour.between cover the usual shapes.Parkour.Utf8 parses Unicode scalars, reporting invalid bytes.max_depth): deeply nested input is a nesting too deep error. Where OCaml can raise Stack_overflow, the runners also turn that into the same kind of error. Running time is not bounded: <|> backtracks by default, so an ambiguous grammar can take exponential time on short input. Commit with Parkour.cut once an alternative is certain and factor out shared prefixes, especially for untrusted input.Parkour is the reference for every combinator, including the precise semantics of commitment, error merging and recovery.examples/ directory of the source tree has complete programs, each printing a gallery of broken inputs: JSON, CSV, typed values, a calculator, an INI-like configuration with recovery, and a small C-like language parsed both directly from text and from tokens.