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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(* This file is part of Markup.ml, released under the MIT license. See
   LICENSE.md for details, or visit https://github.com/aantron/markup.ml. *)

open Common
open Kstream
open Token_tag

let is_whitespace_only strings = List.for_all is_whitespace_only strings

let parse context namespace report tokens =
  let open_elements = ref [] in
  let namespaces = Namespace.Parsing.init namespace in
  let is_fragment = ref false in
  let fragment_allowed = ref true in

  let throw = ref (fun _ -> ()) in
  let ended = ref (fun _ -> ()) in
  let output = ref (fun _ -> ()) in

  let rec current_state = ref (fun () ->
    match context with
    | None -> initial_state []
    | Some `Document ->
      fragment_allowed := false;
      document_state ()
    | Some `Fragment ->
      is_fragment := false;
      content_state ())

  and emit l signal state = current_state := state; !output (l, signal)

  and push_and_emit l {name = raw_name; attributes} state =
    Namespace.Parsing.push (fun () -> report l) namespaces raw_name attributes
      !throw (fun (expanded_name, attributes) ->

    let rec deduplicate acc attributes k =
      match attributes with
      | [] -> k (List.rev acc)
      | ((n, _) as attr)::more ->
        if acc |> List.exists (fun (n', _) -> n' = n) then
          report l (`Bad_token (snd n, "tag", "duplicate attribute")) !throw
            (fun () -> deduplicate acc more k)
        else
          deduplicate (attr::acc) more k
    in

    deduplicate [] attributes (fun attributes ->
    open_elements := (l, expanded_name, raw_name)::!open_elements;
    emit l (`Start_element (expanded_name, attributes)) state))

  and pop l state =
    match !open_elements with
    | [] -> state ()
    | _::more ->
      Namespace.Parsing.pop namespaces;
      open_elements := more;
      emit l `End_element state

  and emit_end () =
    current_state := (fun () -> !ended ());
    !ended ()

  and initial_state leading =
    next_expected tokens !throw begin function
      | _, (`Xml _ | `Doctype _ | `Start _ | `End _) as v ->
        push tokens v;
        push_list tokens (List.rev leading);
        document_state ()

      | _, `Chars s as v when is_whitespace_only s ->
        initial_state (v::leading)

      | _, (`Comment _ | `PI _) as v ->
        initial_state (v::leading)

      | _, (`Chars _ | `EOF) as v ->
        is_fragment := true;
        push tokens v;
        push_list tokens (List.rev leading);
        content_state ()
    end

  and document_state () =
    next_expected tokens !throw begin function
      | l, `Xml declaration ->
        fragment_allowed := false;
        emit l (`Xml declaration) doctype_state

      | v ->
        push tokens v;
        doctype_state ()
    end

  and doctype_state () =
    next_expected tokens !throw begin function
      | l, `Doctype d ->
        fragment_allowed := false;
        emit l (`Doctype d) root_state

      | _, `Chars s when is_whitespace_only s ->
        doctype_state ()

      | l, `Comment s ->
        emit l (`Comment s) doctype_state

      | l, `PI s ->
        emit l (`PI s) doctype_state

      | l, `Xml _ ->
        report l (`Bad_document "XML declaration must be first") !throw
          doctype_state

      | l, `Chars _ ->
        report l (`Bad_document "text at top level") !throw doctype_state

      | v ->
        push tokens v;
        root_state ()
    end

  and root_state () =
    next_expected tokens !throw begin function
      | l, `Start t ->
        if t.self_closing then
          push_and_emit l t (fun () ->
          pop l after_root_state)
        else
          push_and_emit l t content_state

      | _, `Chars s when is_whitespace_only s ->
        root_state ()

      | l, `Comment s ->
        emit l (`Comment s) root_state

      | l, `PI s ->
        emit l (`PI s) root_state

      | l, `Xml _ ->
        report l (`Bad_document "XML declaration must be first") !throw
          root_state

      | l, `EOF ->
        report l (`Unexpected_eoi "document before root element") !throw
          emit_end

      | l, _ ->
        report l (`Bad_document "expected root element") !throw root_state
    end

  and after_root_state () =
    next_expected tokens !throw begin function
      | _, `Chars s when is_whitespace_only s ->
        after_root_state ()

      | l, `Comment s ->
        emit l (`Comment s) after_root_state

      | l, `PI s ->
        emit l (`PI s) after_root_state

      | _, `EOF ->
        emit_end ()

      | _, (`Chars _ | `Start _ | `End _) as v when !fragment_allowed ->
        is_fragment := true;
        push tokens v;
        content_state ()

      | l, _ as v ->
        report l (`Bad_document "not allowed after root element") !throw
          (fun () ->
        is_fragment := true;
        push tokens v;
        content_state ())
    end

  and content_state () =
    next_expected tokens !throw begin function
      | l, `Start t ->
        if t.self_closing then
          push_and_emit l t (fun () ->
          pop l content_state)
        else
          push_and_emit l t content_state

      | l, `End {name = raw_name} ->
        Namespace.Parsing.expand_element (fun () -> report l) namespaces
          raw_name !throw (fun expanded_name ->

        let is_on_stack =
          !open_elements
          |> List.exists (fun (_, name, _) -> name = expanded_name)
        in

        if not is_on_stack then
          report l (`Unmatched_end_tag raw_name) !throw content_state
        else
          let rec pop_until_match () =
            match !open_elements with
            | (_, name, _)::_ when name = expanded_name ->
              pop l (fun () ->
              match !open_elements with
              | [] when not !is_fragment -> after_root_state ()
              | _ -> content_state ())

            | (l', _, name)::_ ->
              report l' (`Unmatched_start_tag name) !throw (fun () ->
              pop l pop_until_match)

            | _ -> failwith "impossible"
          in
          pop_until_match ())

      | l, `Chars s ->
        emit l (`Text s) content_state

      | l, `PI s ->
        emit l (`PI s) content_state

      | l, `Comment s ->
        emit l (`Comment s) content_state

      | l, `EOF ->
        let rec pop_stack () =
          match !open_elements with
          | [] -> emit_end ()
          | (l', _, raw_name)::_ ->
            report l' (`Unmatched_start_tag raw_name) !throw (fun () ->
            pop l pop_stack)
        in
        pop_stack ()

      | l, `Xml _ ->
        report l (`Bad_document "XML declaration should be at top level") !throw
          content_state

      | l, `Doctype _ ->
        report l (`Bad_document "doctype should be at top level") !throw
          content_state
    end

  in

  (fun throw_ e k ->
    throw := throw_;
    ended := e;
    output := k;
    !current_state ())
  |> make