File: common.ml

package info (click to toggle)
yojson 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 252 kB
  • sloc: ml: 1,957; makefile: 208
file content (79 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
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
  }