package parkour
All checked builds pass sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
Parser combinators with excellent error messages and error recovery
Install
dune-project
Dependency
Authors
Maintainers
Sources
parkour-0.1.1.tar.gz
sha256=c4a3f934273430011f6c4862c035395268c47b2cdaba52c199f97cf0404dfba9
sha512=45194b2c7d5f8bcc9d2cb92d640b9dff17b2a3d4ad7319c006daf523923d8c69cdaf53e11d25ba01d5a470b027e4fa1788a69669d11481db20773cef6cc6fa49
doc/index.html
Parkour
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.
An example
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.
Features
- Error messages. The failure that got furthest is reported, with what was expected and what was found, secondary labels such as "opened here" for unclosed brackets, and the constructs being parsed.
Parkour.label,Parkour.context,Parkour.hidden,Parkour.failandParkour.validateshape the messages. - Explicit commitment. Choice is ordered and backtracks by default;
Parkour.cutcommits once a construct is recognised andParkour.attemptundoes it. - Error recovery, opt-in:
Parkour.recover,Parkour.or_insert,Parkour.skip_pastandParkour.parse_partialreport many problems in one run and still return a partial result. - Lexical conventions.
Parkour.Lexeme.makebuilds whitespace, comment, identifier and keyword parsers for a language; numbers, quoted strings and JSON-style escapes are built in. - Token input.
Parkour.tokenizeandParkour.Tokenssplit parsing into a lexer and a grammar over tokens, with diagnostics that still point into the original text.Parkour.Makeaccepts other kinds of input. - Expressions.
Parkour.expressionbuilds operator parsers from a precedence table;Parkour.chainl1,Parkour.sep_byandParkour.betweencover the usual shapes. - UTF-8. Diagnostics align underlines for wide and combining characters, and
Parkour.Utf8parses Unicode scalars, reporting invalid bytes. - Robustness. Repetitions are loops, so long inputs do not use the stack, and nesting is limited (
max_depth): deeply nested input is anesting too deeperror. Where OCaml can raiseStack_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 withParkour.cutonce an alternative is certain and factor out shared prefixes, especially for untrusted input.
Where to go next
- The tutorial builds parsers step by step, with the error output of each step.
Parkouris the reference for every combinator, including the precise semantics of commitment, error merging and recovery.- The
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.
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page