Debian Codegraph @

vendor/re/src/parse_buffer.ml

raw
1 type t =
2 { str : string
3 ; mutable pos : int
4 }
5
6 exception Parse_error
7
8 let create str = { str; pos = 0 }
9 let unget t = t.pos <- t.pos - 1
10 let junk t = t.pos <- t.pos + 1
11 let eos t = t.pos = String.length t.str
12 let test t c = (not (eos t)) && t.str.[t.pos] = c
13
14 let test2 t c c' =
15 t.pos + 1 < String.length t.str && t.str.[t.pos] = c && t.str.[t.pos + 1] = c'
16 ;;
17
18 let accept t c =
19 let r = test t c in
20 if r then t.pos <- t.pos + 1;
21 r
22 ;;
23
24 let accept2 t c c' =
25 let r = test2 t c c' in
26 if r then t.pos <- t.pos + 2;
27 r
28 ;;
29
30 let get t =
31 let r = t.str.[t.pos] in
32 t.pos <- t.pos + 1;
33 r
34 ;;
35
36 let accept_s t s' =
37 let len = String.length s' in
38 try
39 for j = 0 to len - 1 do
40 try if s'.[j] <> t.str.[t.pos + j] then raise_notrace Exit with
41 | _ -> raise_notrace Exit
42 done;
43 t.pos <- t.pos + len;
44 true
45 with
46 | Exit -> false
47 ;;
48
49 let rec integer' t i =
50 if eos t
51 then Some i
52 else (
53 match get t with
54 | '0' .. '9' as d ->
55 let i' = (10 * i) + (Char.code d - Char.code '0') in
56 if i' < i then raise Parse_error;
57 integer' t i'
58 | _ ->
59 unget t;
60 Some i)
61 ;;
62
63 let integer t =
64 if eos t
65 then None
66 else (
67 match get t with
68 | '0' .. '9' as d -> integer' t (Char.code d - Char.code '0')
69 | _ ->
70 unget t;
71 None)
72 ;;