807 B
807 B
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 .