summaryrefslogtreecommitdiff
path: root/ocaml-binary-annot/camlp4/test/fixtures/parser.ml
blob: 7e41f35b6badda38f0efbe5337e668bbea40442d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
open Camlp4.PreCast;
type t = [ A of t and t | B of string ];
value lex = Lexer.mk ();

    (* value list0 symb =
      let rec loop al =
        parser
        [ [: a = symb; s :] -> loop [a :: al] s
        | [: :] -> al ]
      in
      parser [: a = loop [] :] -> List.rev a
    ;
    value list0sep symb sep =
      let rec kont al =
        parser
        [ [: v = sep; a = symb; s :] -> kont [a :: al] s
        | [: :] -> al ]
      in
      parser
      [ [: a = symb; s :] -> List.rev (kont [a] s)
      | [: :] -> [] ]
    ;
    value list1 symb =
      let rec loop al =
        parser
        [ [: a = symb; s :] -> loop [a :: al] s
        | [: :] -> al ]
      in
      parser [: a = symb; s :] -> List.rev (loop [a] s)
    ;
    value list1sep symb sep =
      let rec kont al =
        parser
        [ [: v = sep; a = symb; s :] -> kont [a :: al] s
        | [: :] -> al ]
      in
      parser [: a = symb; s :] -> List.rev (kont [a] s)
    ;                                                       *)

value list1 =
  let rec self stream acc =
    match stream with parser
    [ [: `(EOI, _) :] -> acc
    | [: `(LIDENT x, _); xs :] -> self xs (A acc (B x))
    | [: `(BLANKS _ | NEWLINE, _); xs :] -> self xs acc ]
  in
  parser [: `(LIDENT x, _); xs :] -> self xs (B x);
value rec length x acc =
  match x with
  [ A x y -> length x (length y acc)
  | B _ -> succ acc ];
(* value length _ _ = -1; *)
open Format;
try
  let f = Sys.argv.(1) in
  let () = printf "parsing...@." in
  let a = list1 (lex (Loc.mk f) (Stream.of_channel (open_in f))) in
  let () = printf "counting...@." in
  let n = length a 0 in
  printf "%d@." n
with e -> eprintf "error: %a@." Camlp4.ErrorHandler.print e;