aboutsummaryrefslogtreecommitdiff
path: root/lib/turtle/parser.ml
blob: a586c55316773f8f238cbb5c80905f5cd4cf7f07 (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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
open Angstrom

let string_starts_with s subs =
  if String.length subs > String.length s then
    false
  else if subs <> String.sub s 0 (String.length subs) then
    false
  else
    true

let whitespace_lst = ['\x20'; '\x0a'; '\x0d'; '\x09'; ]

let is_whitespace = function
  | '\x20' | '\x0a' | '\x0d' | '\x09' -> true
  | _ -> false

let is_not_whitespace c = c |> is_whitespace |> not

let char_is_not_equal_to lst d =
  List.for_all (fun x -> x != d) lst

let whitespace =
  many @@ choice [string " "; string "\n"; string "\t"]
  >>| ignore

let whitespace1 =
  many1 @@ choice [string " "; string "\n"; string "\t"]
  >>| ignore

let delimiters c1 c2 =
  char c1
  *> take_while (fun d -> not @@ Char.equal c2 d)
  <* char c2

let iriref =
  lift
    Ast.Iriref.of_string
    (delimiters '<' '>')

let prefixed_name =
  lift2
    Ast.Prefixed_name.of_strings
    (peek_char >>= function
      | Some '_' -> fail "A prefixed_name can not start with _"
      | _ ->
          (take_while (char_is_not_equal_to ([':'] @ whitespace_lst))
          <* char ':'))
          (take_while (char_is_not_equal_to ([']'; ')'; '('] @ whitespace_lst)))

let language =
  lift
    Ast.Language.of_string
    (char '@'
     *> take_while (char_is_not_equal_to ([']'; ')'; '('] @ whitespace_lst)))

let blank_node =
  lift
    Ast.Blank_node.of_string
    (char '_'
     *> char ':'
     *> take_while is_not_whitespace)

let iri =
  (lift Ast.Iri.of_iriref iriref)
  <|>
  (lift Ast.Iri.of_prefixed_name prefixed_name)

(*   TODO the iri of the literal defaults to xds:string. This is the case,
 *   according to the spec, but it can also happen elsewhere.
 *   Moreover: an absent language results in an empty string. This could also be different. *)
let literal =
let datatype_str = Ast.Iri.of_prefixed_name (Ast.Prefixed_name.of_strings "xsd" "string") in
  choice ~failure_msg:"None of the parsers worked for Literal"
    [
      lift2
        (fun value lang ->
           Ast.Literal.make value ~language:lang datatype_str)
        (char '"'
         *> take_while (char_is_not_equal_to (['"']))
         <* char '"')
        (language)
      ;
      lift2
        (fun value datatype ->
           Ast.Literal.make value datatype)
        (char '"'
         *> take_while (char_is_not_equal_to ([':'; '"']))
         <* char '"')
        (string "^^" *> iri)
      ;
      lift
        (fun value ->
           Ast.Literal.make value datatype_str)
        (char '"'
         *> take_while (char_is_not_equal_to (['"']))
         <* char '"')
      ;
    ]

let predicate =
  choice [
    lift (fun _ ->
        Fmt.pr "PRED_A:@.";
        Ast.Predicate.a)  (char 'a' <* whitespace1);
    lift (fun iri ->
        Fmt.pr "PRED_IRI: %a@." Ast.Iri.pp iri;
        Ast.Predicate.of_iri iri)
      iri
  ]

(*   TODO if you change the order of literal and iri, the tests won't work anymore. *)
(*   This is because the prefixed name parser accepts, for example: *)
(*   "\"That Seventies Show\"^^xsd:string" *)
(*   Same thing for blank_node! *)
let object_ collection bnodps =
  choice [
    (lift
       (fun collection ->
          Ast.Obj_coll collection)
       collection
    );
    (lift
       (fun bnodps ->
          Ast.Obj_bnodps bnodps)
       bnodps
    );
    (lift
       (fun literal ->
          Fmt.pr "OBJ_LITERAL %a" Ast.Literal.pp literal;
          Ast.Obj_literal literal)
       literal
    );
    (lift
       (fun blank_node ->
          Fmt.pr "OBJ_BNODE %a" Ast.Blank_node.pp blank_node;
          Ast.Obj_blank_node blank_node)
       blank_node
    );
    (lift
       (fun iri ->
          Fmt.pr "OBJ_IRI %a@." Ast.Iri.pp iri;
          Ast.Obj_iri iri)
       iri);
  ]

let collection_ bnodps =
  fix (fun collection ->
      let object' = object_ collection bnodps in
      (lift
         (fun collection -> Ast.Collection collection)
         (
           char '('
           *> whitespace
           *>
           (sep_by whitespace object')
           <* whitespace
           <*  char ')'
         )
      )
    )

let subject_ bnodps =
  let collection = collection_ bnodps in
  choice [
    (lift
       (fun collection -> Ast.Sub_coll collection)
       collection
    );
    (lift
       (fun blank_node -> Ast.Sub_blank_node blank_node)
       blank_node
    );
    (lift
       (fun iri -> Ast.Sub_iri iri)
       iri
    );
  ]

let bnodps predobjs =
  lift
    (fun predobjs ->
       Fmt.pr "BNODPS";
       Ast.BnodPs predobjs)
    (
      char '['
      *> whitespace
      *> predobjs
      <* whitespace
      <* char ']'
    )

let predobjs =
  let semicolon =
    whitespace
    *> char ';'
    <* whitespace
  in
  let comma =
    whitespace
    *> char ','
    <* whitespace
  in
  fix (fun predobjs ->
      Fmt.pr "PREDOBJS";
      let bnodps = bnodps predobjs in
      let collection = collection_ bnodps in
      let object' = object_ collection bnodps in
      sep_by semicolon (
        lift2
          (fun p objs -> (p, objs))
          (whitespace *> predicate <* whitespace)
          (sep_by comma (whitespace *> object' <* whitespace))
      )
    )

let bnodps = bnodps predobjs
let subject = subject_ bnodps
let collection = collection_ bnodps
let object' = object_ collection bnodps

let triples =
  choice [
    (lift2
       Ast.Triples.of_subject_and_predobjs
       (subject <* whitespace)
       predobjs);
    (lift
       Ast.Triples.of_bnodps
       bnodps
    )
  ]

let directive =
  choice [
    lift2
      Ast.Directive.of_string_and_iriref
      (string "@prefix"
       *> whitespace
       *> (take_while (char_is_not_equal_to ([':'] @ whitespace_lst)))
       <* char ':'
       <* whitespace)
      (iriref
       <* whitespace
       <* char '.'
       <* whitespace
      )
    ;
    lift
      Ast.Directive.of_iriref
      (string "@base"
       *> whitespace
       *> iriref
       <* whitespace
       <* char '.'
      )
  ]

let statement =
  choice [
    lift
      Ast.Statement.of_directive
      (directive)
    ;
    lift
      Ast.Statement.of_triples
      (triples
       <* whitespace
       <* char '.'
       <* whitespace
      )
  ]

let turtle =
  many statement


  (*
  This is the idea for mutually recursive parsers (because Angstrom doesn't have a 'fix_poly'. The 'fix' function allows recursion, but the definition of the function can only depend on itself, not on other functions. That's why we have
let a b c d = ...
We just make a function a that depends on b, c and d, and later we can compute a if the variables b, c and d are available.
In principle, you have as many parsers as you like, but it becomes rather lengthy at some point.
     Note that a plays two different roles here, namely that of the function that sends the parsers b, c and d to the parser a, and the parser a itself.
In the implementation, we chose a_ (or for example object_) for the function.
Because some of the types did not depend on all the other types, our functions are simpler (but it is harder to see the general pattern).

  let a b c d = fix (fun a -> (* definition of `a` in terms of `a` and `b` and `c` and `d`*))

  let b c d =
    fix (fun b ->
        let a = a b c d in
      (* definition of b in terms of `a` and `b` and `c` and `d`*)

  let c d =
    fix (fun c ->
        let b = b c d in
        let a = a b c d in
        (* definition of c in terms of `a` and `b` and `c` and `d`*)
      )

  let d =
    fix (fun d ->
        let c = c d in
        let b = b c d in
        let a = a b c d in
        (* definition of c in terms of `a` and `b` and `c` and `d`*)
      )

  let c = c d

  let b = b c d

  let a = a b c d
   *)