OCaml parser combinator that compiles to direct recursive descent
  • OCaml 99.5%
  • Dune 0.5%
Find a file
2026-01-16 22:19:30 +00:00
lib streaming/lazy input support and generic token infrastructure 2026-01-16 22:19:30 +00:00
src streaming/lazy input support and generic token infrastructure 2026-01-16 22:19:30 +00:00
.gitignore parser combinator ppx that compiles to recursive descent 2026-01-16 21:38:19 +00:00
dune-project parser combinator ppx that compiles to recursive descent 2026-01-16 21:38:19 +00:00
README.md parser combinator ppx that compiles to recursive descent 2026-01-16 21:38:19 +00:00

ppx_combin

Stupid PPX library because others are stupidly hacked together. This is a parser combinator syntax that compiles to direct recursive descent.It does first-set analysis and generates tight match/loop code.

usage

open Combin

let is_digit c = c >= '0' && c <= '9'

let digit = [%parser satisfy is_digit <?> "digit"]

let number = [%parser
  some (satisfy is_digit) >>= fun ds ->
  pure (int_of_string (String.concat "" (List.map (String.make 1) ds)))
]

let expr = [%parser
  chainl1
    (satisfy is_digit >>= fun c -> pure (Char.code c - Char.code '0'))
    (token '+' *> pure ( + ))
]

let () =
  match parse_string expr "1+2+3" with
  | Ok (n, _) -> Printf.printf "result: %d\n" n
  | Error e -> print_endline (format_error e)

install

opam pin add ppx_combin .