OCaml parser combinator that compiles to direct recursive descent
807 B
35 lines
1# ppx_combin
2
3Stupid 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.
4
5## usage
6
7```ocaml
8open Combin
9
10let is_digit c = c >= '0' && c <= '9'
11
12let digit = [%parser satisfy is_digit <?> "digit"]
13
14let number = [%parser
15 some (satisfy is_digit) >>= fun ds ->
16 pure (int_of_string (String.concat "" (List.map (String.make 1) ds)))
17]
18
19let expr = [%parser
20 chainl1
21 (satisfy is_digit >>= fun c -> pure (Char.code c - Char.code '0'))
22 (token '+' *> pure ( + ))
23]
24
25let () =
26 match parse_string expr "1+2+3" with
27 | Ok (n, _) -> Printf.printf "result: %d\n" n
28 | Error e -> print_endline (format_error e)
29```
30
31## install
32
33```
34opam pin add ppx_combin .
35```