aboutsummaryrefslogtreecommitdiff
path: root/lib/qparser/lex_state.ml
blob: 2ec1857fe2677f499613e0ea9775b86d75160262 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(** This module provide functions used to parse the strings. *)

exception Out_of_context

let space = [%sedlex.regexp? ' ' | '\t']
let spaces = [%sedlex.regexp? Plus space]
let single_quote = [%sedlex.regexp? '\'']
let double_quote = [%sedlex.regexp? '"']

let leave_text end_wrapper buf buffer =
  Lexbuf.leave_state buffer;
  Lexbuf.enter_state buffer (Lexbuf.EndString end_wrapper);
  Lexbuf.rollback buffer;
  Tokens.LITERAL (Buffer.contents buf)

let rec nestedQuotedStringWraper : Lexbuf.stringWraper =
  let start_string f buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    (* There is no more way to add start a quoted string here *)
    | _ -> f buffer
  in

  {
    start_string;
    wrap =
      (fun f ?(nested = false) buf buffer ->
        let lexbuf = Lexbuf.buffer buffer in
        match%sedlex lexbuf with
        | "''" -> leave_text nestedQuotedStringWraper buf buffer
        | _ -> f ~nested buf buffer);
    end_string =
      (fun buffer ->
        let lexbuf = Lexbuf.buffer buffer in
        match%sedlex lexbuf with
        | "''" ->
            Lexbuf.leave_state buffer;
            TEXT_MARKER
        | _ -> raise Not_found);
  }

and nestedDquotedStringWraper : Lexbuf.stringWraper =
  let start_string f buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    (* There is no more way to add start a quoted string here *)
    | _ -> f buffer
  in

  let rec wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    | double_quote ->
        Buffer.add_char buf '"';
        wrap ~nested:true f buf buffer
    | double_quote, double_quote ->
        leave_text nestedDquotedStringWraper buf buffer
    | _ -> f ~nested buf buffer
  in
  {
    start_string;
    wrap;
    end_string =
      (fun buffer ->
        let lexbuf = Lexbuf.buffer buffer in
        match%sedlex lexbuf with
        | double_quote, double_quote ->
            Lexbuf.leave_state buffer;
            TEXT_MARKER
        | _ -> raise Not_found);
  }

and quotedStringWraper : Lexbuf.stringWraper =
  let rec start_string f buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    | spaces -> start_string f buffer
    | single_quote -> raise Out_of_context
    | single_quote, single_quote ->
        Lexbuf.enter_state buffer (Lexbuf.String nestedQuotedStringWraper);
        Tokens.TEXT_MARKER
    | _ -> f buffer
  in

  let rec quoted_wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    | single_quote, single_quote ->
        Buffer.add_char buf '\'';
        quoted_wrap ~nested f buf buffer
    | single_quote -> leave_text quotedStringWraper buf buffer
    | _ -> f ~nested buf buffer
  in
  {
    start_string;
    wrap = quoted_wrap;
    end_string =
      (fun buffer ->
        let lexbuf = Lexbuf.buffer buffer in
        match%sedlex lexbuf with
        | single_quote ->
            Lexbuf.leave_state buffer;
            TEXT_MARKER
        | _ -> raise Not_found);
  }

and dQuotedStringWraper : Lexbuf.stringWraper =
  let rec wrap (f : Lexbuf.buffer_builder) ?(nested = false) buf buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    | single_quote ->
        if nested then (
          Buffer.add_char buf '\'';
          wrap ~nested:false f buf buffer)
        else (
          Buffer.add_char buf '\'';
          wrap ~nested:true f buf buffer)
    | double_quote, double_quote ->
        Buffer.add_char buf '"';
        wrap f buf buffer
    | double_quote -> leave_text dQuotedStringWraper buf buffer
    | _ -> f ~nested buf buffer
  in

  let rec start_string f buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    | spaces -> start_string f buffer
    | double_quote -> raise Out_of_context
    | double_quote, double_quote ->
        Lexbuf.enter_state buffer (Lexbuf.String nestedDquotedStringWraper);
        Tokens.TEXT_MARKER
    | _ -> f buffer
  in

  {
    start_string;
    wrap;
    end_string =
      (fun buffer ->
        let lexbuf = Lexbuf.buffer buffer in
        match%sedlex lexbuf with
        | double_quote ->
            Lexbuf.leave_state buffer;
            TEXT_MARKER
        | _ -> raise Not_found);
  }

let defaultWraper : Lexbuf.stringWraper =
  let rec start_string f buffer =
    let lexbuf = Lexbuf.buffer buffer in
    match%sedlex lexbuf with
    | spaces -> start_string f buffer
    | single_quote ->
        Lexbuf.enter_state buffer (Lexbuf.String quotedStringWraper);
        Tokens.TEXT_MARKER
    | double_quote ->
        Lexbuf.enter_state buffer (Lexbuf.String dQuotedStringWraper);
        Tokens.TEXT_MARKER
    | '{' ->
        Lexbuf.enter_state buffer (Lexbuf.MString 0);
        Tokens.TEXT_MARKER
    | _ -> f buffer
  in
  {
    start_string;
    wrap =
      (fun _f ?nested _buf _buffer ->
        ignore nested;
        raise Out_of_context);
    end_string = (fun _buffer -> raise Out_of_context);
  }

let readLongStringWraper : Lexbuf.stringWraper =
  {
    defaultWraper with
    end_string =
      (fun buffer ->
        let lexbuf = Lexbuf.buffer buffer in
        match%sedlex lexbuf with
        | "}" ->
            Lexbuf.leave_state buffer;
            TEXT_MARKER
        | _ -> raise Not_found);
  }