forked from ocaml-community/yojson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ml
More file actions
79 lines (63 loc) · 1.65 KB
/
Copy pathcommon.ml
File metadata and controls
79 lines (63 loc) · 1.65 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(* $Id$ *)
exception Json_error of string
let json_error s = raise (Json_error s)
exception End_of_array
exception End_of_object
exception End_of_tuple
exception End_of_input
type in_param = {
string_buf : Buffer.t
}
let create_in_param ?(len = 256) () = {
string_buf = Buffer.create len
}
let utf8_of_bytes buf a b c d =
let i = (a lsl 12) lor (b lsl 8) lor (c lsl 4) lor d in
if i < 0x80 then
Bi_outbuf.add_char buf (Char.chr i)
else if i < 0x800 then (
Bi_outbuf.add_char buf (Char.chr (0xc0 lor ((i lsr 6) land 0x1f)));
Bi_outbuf.add_char buf (Char.chr (0x80 lor (i land 0x3f)))
)
else (* i < 0x10000 *) (
Bi_outbuf.add_char buf (Char.chr (0xe0 lor ((i lsr 12) land 0xf)));
Bi_outbuf.add_char buf (Char.chr (0x80 lor ((i lsr 6) land 0x3f)));
Bi_outbuf.add_char buf (Char.chr (0x80 lor (i land 0x3f)))
)
let is_object_or_array x =
match x with
`List _
| `Assoc _ -> true
| _ -> false
type lexer_state = {
buf : Bi_outbuf.t;
(* Buffer used to accumulate substrings *)
mutable lnum : int;
(* Current line number (starting from 1) *)
mutable bol : int;
(* Absolute position of the first character of the current line
(starting from 0) *)
mutable fname : string option;
(* Name describing the input file *)
}
module Lexer_state =
struct
type t = lexer_state = {
buf : Bi_outbuf.t;
mutable lnum : int;
mutable bol : int;
mutable fname : string option;
}
end
let init_lexer ?buf ?fname ?(lnum = 1) () =
let buf =
match buf with
None -> Bi_outbuf.create 256
| Some buf -> buf
in
{
buf = buf;
lnum = lnum;
bol = 0;
fname = fname
}