Source file attribute_value.ml
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
[@@@ocaml.warning "-3"]
open Ppxlib.Ast_helper
type 'a gparser =
?separated_by:string -> ?default:string -> Location.t -> string -> 'a ->
expression option
type parser = string gparser
type vparser = string Common.value gparser
let expr (parser : parser) : vparser =
fun ?separated_by ?default loc name v ->
match v with
| Antiquot e -> Some e
| Val s -> parser ?separated_by ?default loc name s
let option none (parser : parser) ?separated_by:_ ?default:_ loc name s =
if s = none then Some [%expr None] [@metaloc loc]
else
match parser ~default:none loc name s with
| None -> None
| Some e -> Some [%expr Some [%e e]] [@metaloc loc]
let filter_map f l =
l
|> List.fold_left (fun acc v ->
match f v with
| None -> acc
| Some v' -> v'::acc)
[]
|> List.rev
let exp_list delimiter separated_by (element_parser : parser) loc name s =
Re_str.split delimiter s
|> filter_map (element_parser ~separated_by loc name)
let list
delimiter separated_by element_parser ?separated_by:_ ?default:_ loc name s =
exp_list delimiter separated_by element_parser loc name s
|> Common.list loc
|> fun e -> Some e
let spaces = list (Re_str.regexp " +") "space"
let commas = list (Re_str.regexp " *, *") "comma"
let semicolons = list (Re_str.regexp " *; *") "semicolon"
let spaces_or_commas_regexp = Re_str.regexp "\\( *, *\\)\\| +"
let spaces_or_commas_ = exp_list spaces_or_commas_regexp "space- or comma"
let spaces_or_commas = list spaces_or_commas_regexp "space- or comma"
let wrap (parser : parser) implementation =
expr @@
fun ?separated_by:_ ?default:_ loc name s ->
match parser loc name s with
| None -> Common.error loc "wrap applied to presence; nothing to wrap"
| Some e -> Some (Common.wrap implementation loc e)
let nowrap (parser : parser) _ =
expr @@
fun ?separated_by:_ ?default:_ loc name s ->
parser loc name s
let must_be_a
singular_description plural_description separated_by default loc name =
let description =
match separated_by with
| Some separated_by ->
Printf.sprintf "a %s-separated list of %s" separated_by plural_description
| None ->
match default with
| Some default -> Printf.sprintf "%s or %s" singular_description default
| None -> singular_description
in
Common.error loc "Value of %s must be %s" name description
let does_match regexp s =
Re_str.string_match regexp s 0 && Re_str.match_end () = String.length s
let group_matched index s =
try Re_str.matched_group index s |> ignore; true
with Not_found -> false
let int_exp loc s =
try Some (Common.int loc (int_of_string s))
with Failure _ -> None
let float_exp loc s =
try
Some (Common.float loc @@ float_of_string s)
with Failure _ ->
None
let bool_exp loc b =
let s = if b then "true" else "false" in
Exp.construct ~loc ({ txt = (Longident.Lident s); loc }) None
let char ?separated_by:_ ?default:_ loc name s =
let encoding = `UTF_8 in
let decoder = Uutf.decoder ~encoding (`String s) in
let c =
match Uutf.decode decoder with
| `End -> Common.error loc "No character in attribute %s" name
| `Uchar i when Uchar.is_char i -> Uchar.to_char i
| `Uchar _ ->
Common.error loc "Character out of range in attribute %s" name
| `Await -> assert false
| `Malformed s ->
Common.error loc "Malformed character %s in attribute %s" s name
in
begin match Uutf.decode decoder with
| `End -> ()
| _ -> Common.error loc "Multiple characters in attribute %s" name
end;
Some (Ast_builder.Default.echar ~loc c)
let onoff ?separated_by:_ ?default:_ loc name s =
let b = match s with
| "" | "on" -> true
| "off" -> false
| _ ->
Common.error loc {|Value of %s must be "on", "" or "off"|} name
in
Some (bool_exp loc b)
let bool ?separated_by:_ ?default:_ loc name s =
let b = match s with
| "" | "true" -> true
| "false" -> false
| _ ->
Common.error loc {|Value of %s must be "true", "" or "false"|} name
in
Some (bool_exp loc b)
let unit ?separated_by:_ ?default:_ loc name s =
if s = "" || s = name then
Some (Ast_builder.Default.eunit ~loc)
else
Common.error loc
{|Value of %s must be %s or "".|}
name name
let int ?separated_by ?default loc name s =
match int_exp loc s with
| Some _ as e -> e
| None ->
must_be_a "a whole number" "whole numbers" separated_by default loc name
let float ?separated_by ?default loc name s =
match float_exp loc s with
| Some _ as e -> e
| None ->
must_be_a
"a number (decimal fraction)" "numbers (decimal fractions)"
separated_by default loc name
let points ?separated_by:_ ?default:_ loc name s =
let expressions = spaces_or_commas_ float loc name s in
let rec pair acc = function
| [] -> List.rev acc |> Common.list loc
| [_] -> Common.error loc "Unpaired coordinate in %s" name
| ex::ey::rest -> pair (([%expr [%e ex], [%e ey]] [@metaloc loc])::acc) rest
in
Some (pair [] expressions)
let number_pair ?separated_by:_ ?default:_ loc name s =
let e =
begin match spaces_or_commas_ float loc name s with
| [orderx] -> [%expr [%e orderx], None]
| [orderx; ordery] -> [%expr [%e orderx], Some [%e ordery]]
| _ -> Common.error loc "%s requires one or two numbers" name
end [@metaloc loc]
in
Some e
let fourfloats ?separated_by:_ ?default:_ loc name s =
match spaces_or_commas_ float loc name s with
| [min_x; min_y; width; height] ->
Some [%expr ([%e min_x], [%e min_y], [%e width], [%e height])]
[@metaloc loc]
| _ -> Common.error loc "Value of %s must be four numbers" name
let icon_size =
let regexp = Re_str.regexp "\\([0-9]+\\)[xX]\\([0-9]+\\)" in
fun ?separated_by:_ ?default:_ loc name s ->
if not @@ does_match regexp s then
Common.error loc "Value of %s must be a %s, or %s"
name "space-separated list of icon sizes, such as 16x16" "any";
let width, height =
try
int_of_string (Re_str.matched_group 1 s),
int_of_string (Re_str.matched_group 2 s)
with Invalid_argument _ ->
Common.error loc "Icon dimension out of range in %s" name
in
Some
[%expr
[%e Common.int loc width],
[%e Common.int loc height]] [@metaloc loc]
let svg_quantity =
let integer = "[+-]?[0-9]+" in
let integer_scientific = Printf.sprintf "%s\\([Ee]%s\\)?" integer integer in
let fraction = Printf.sprintf "[+-]?[0-9]*\\.[0-9]+\\([Ee]%s\\)?" integer in
let number = Printf.sprintf "%s\\|%s" integer_scientific fraction in
let quantity = Printf.sprintf "\\(%s\\)\\([^0-9]*\\)$" number in
let regexp = Re_str.regexp quantity in
fun kind_singular kind_plural parse_unit ?separated_by ?default loc name s ->
if not @@ does_match regexp s then
must_be_a kind_singular kind_plural separated_by default loc name;
let n =
match float_exp loc (Re_str.matched_group 1 s) with
| Some n -> n
| None -> Common.error loc "Number out of range in %s" name
in
let unit_string = Re_str.matched_group 4 s in
let unit =
(if unit_string = "" then [%expr None]
else [%expr Some [%e parse_unit loc name unit_string]]) [@metaloc loc]
in
[%expr [%e n], [%e unit]] [@metaloc loc]
let svg_length =
let parse_unit loc name unit =
begin match unit with
| "cm" -> [%expr `Cm]
| "em" -> [%expr `Em]
| "ex" -> [%expr `Ex]
| "in" -> [%expr `In]
| "mm" -> [%expr `Mm]
| "pc" -> [%expr `Pc]
| "pt" -> [%expr `Pt]
| "px" -> [%expr `Px]
| "%" -> [%expr `Percent]
| s -> Common.error loc "Invalid length unit %s in %s" s name
end [@metaloc loc]
in
fun ?separated_by ?default loc name s ->
Some
(svg_quantity "an SVG length" "SVG lengths" parse_unit
?separated_by ?default loc name s)
let angle_ =
let parse_unit loc name unit =
begin match unit with
| "deg" -> [%expr `Deg]
| "rad" -> [%expr `Rad]
| "grad" -> [%expr `Grad]
| s -> Common.error loc "Invalid angle unit %s in %s" s name
end [@metaloc loc]
in
svg_quantity "an SVG angle" "SVG angles" parse_unit
let angle ?separated_by ?default loc name s =
Some (angle_ ?separated_by ?default loc name s)
let offset =
let bad_form name loc =
Common.error loc "Value of %s must be a number or percentage" name in
let regexp = Re_str.regexp "\\([-+0-9eE.]+\\)\\(%\\)?" in
fun ?separated_by:_ ?default:_ loc name s ->
if not @@ does_match regexp s then bad_form name loc;
begin
let n =
match float_exp loc (Re_str.matched_group 1 s) with
| Some n -> n
| None -> bad_form name loc
in
if group_matched 2 s then Some [%expr `Percentage [%e n]]
else Some [%expr `Number [%e n]]
end [@metaloc loc]
let transform =
let regexp = Re_str.regexp "\\([^(]+\\)(\\([^)]*\\))" in
fun ?separated_by:_ ?default:_ loc name s ->
if not @@ does_match regexp s then
Common.error loc "Value of %s must be an SVG transform" name;
let kind = Re_str.matched_group 1 s in
let values = Re_str.matched_group 2 s in
let e =
begin match kind with
| "matrix" ->
begin match spaces_or_commas_ float loc "matrix" values with
| [a; b; c; d; e; f] ->
[%expr `Matrix ([%e a], [%e b], [%e c], [%e d], [%e e], [%e f])]
| _ ->
Common.error loc "%s: matrix requires six numbers" name
end
| "translate" ->
begin match spaces_or_commas_ float loc "translate" values with
| [tx; ty] -> [%expr `Translate ([%e tx], Some [%e ty])]
| [tx] -> [%expr `Translate ([%e tx], None)]
| _ ->
Common.error loc "%s: translate requires one or two numbers" name
end
| "scale" ->
begin match spaces_or_commas_ float loc "scale" values with
| [sx; sy] -> [%expr `Scale ([%e sx], Some [%e sy])]
| [sx] -> [%expr `Scale ([%e sx], None)]
| _ -> Common.error loc "%s: scale requires one or two numbers" name
end
| "rotate" ->
begin match Re_str.bounded_split spaces_or_commas_regexp values 2 with
| [a] -> [%expr `Rotate ([%e angle_ loc "rotate" a], None)]
| [a; axis] ->
begin match spaces_or_commas_ float loc "rotate axis" axis with
| [cx; cy] ->
[%expr `Rotate
([%e angle_ loc "rotate" a], Some ([%e cx], [%e cy]))]
| _ ->
Common.error loc "%s: rotate center requires two numbers" name
end
| _ ->
Common.error loc
"%s: rotate requires an angle and an optional center" name
end
| "skewX" -> [%expr `SkewX [%e angle_ loc "skewX" values]]
| "skewY" -> [%expr `SkewY [%e angle_ loc "skewY" values]]
| s -> Common.error loc "%s: %s is not a valid transform type" name s
end [@metaloc loc]
in
Some e
let string ?separated_by:_ ?default:_ loc _ s =
Some (Ast_builder.Default.estring ~loc s)
let variand s =
let without_backtick s =
let length = String.length s in
String.sub s 1 (length - 1)
in
s |> Name_convention.polyvar |> without_backtick
let variant ?separated_by:_ ?default:_ loc _ s =
Some (Exp.variant ~loc (variand s) None)
let total_variant (unary, nullary) ?separated_by:_ ?default:_ loc _name s =
let variand = variand s in
if List.mem variand nullary then Some (Exp.variant ~loc variand None)
else Some (Exp.variant ~loc unary (Some (Common.string loc s)))
let variant_or_empty empty ?separated_by:_ ?default:_ loc _ s =
let variand = variand s in
let variand = if variand = "" then empty else variand in
Some (Exp.variant ~loc variand None)
let presence ?separated_by:_ ?default:_ _ _ _ = None
let paint_without_icc loc _name s =
begin match s with
| "none" ->
[%expr `None]
| "currentColor" ->
[%expr `CurrentColor]
| _ ->
let icc_color_start =
try Some (Re_str.search_forward (Re_str.regexp "icc-color(\\([^)]*\\))") s 0)
with Not_found -> None
in
match icc_color_start with
| None -> [%expr `Color ([%e Common.string loc s], None)]
| Some i ->
let icc_color = Re_str.matched_group 1 s in
let color = String.sub s 0 i in
[%expr `Color
([%e Common.string loc color],
Some [%e Common.string loc icc_color])]
end [@metaloc loc]
let paint ?separated_by:_ ?default:_ loc name s =
if not @@ Re_str.string_match (Re_str.regexp "url(\\([^)]+\\))") s 0 then
Some (paint_without_icc loc name s)
else
let iri = Re_str.matched_group 1 s |> Common.string loc in
let remainder_start = Re_str.group_end 0 in
let remainder_length = String.length s - remainder_start in
let remainder =
String.sub s remainder_start remainder_length |> String.trim in
begin
if remainder = "" then
Some [%expr `Icc ([%e iri], None)]
else
Some
[%expr
`Icc ([%e iri], Some [%e paint_without_icc loc name remainder])]
end [@metaloc loc]
let fill_rule ?separated_by:_ ?default:_ loc _name s =
begin match s with
| "nonzero" ->
Some [%expr `Nonzero]
| "evenodd" ->
Some [%expr `Evenodd]
| _ -> None
end [@metaloc loc]
let srcset_element =
let space = Re_str.regexp " +" in
fun ?separated_by:_ ?default:_ loc name s ->
let e =
begin match Re_str.bounded_split space s 2 with
| [url] ->
[%expr `Url [%e Common.string loc url]]
| [url; descriptor] ->
let bad_descriptor () =
Common.error loc "Bad width or density descriptor in %s" name in
let url = Common.string loc url in
let suffix_index = String.length descriptor - 1 in
let is_width =
match descriptor.[suffix_index] with
| 'w' -> true
| 'x' -> false
| _ -> bad_descriptor ()
| exception Invalid_argument _ -> bad_descriptor ()
in
if is_width then
let n =
match int_exp loc (String.sub descriptor 0 suffix_index) with
| Some n -> n
| None ->
Common.error loc "Bad number for width in %s" name
in
[%expr `Url_width ([%e url], [%e n])]
else
let n =
match float_exp loc (String.sub descriptor 0 suffix_index) with
| Some n -> n
| None ->
Common.error loc "Bad number for pixel density in %s" name
in
[%expr `Url_pixel ([%e url], [%e n])]
| _ -> Common.error loc "Missing URL in %s" name
end [@metaloc loc]
in
Some e
let number_or_datetime ?separated_by:_ ?default:_ loc _ s =
match int_exp loc s with
| Some n -> Some [%expr `Number [%e n]]
| None -> Some [%expr `Datetime [%e Common.string loc s]]
[@metaloc loc]
let autocomplete ?separated_by:_ ?default:_ loc name s =
match s with
| "on" | "" -> Some [%expr `On]
| "off" -> Some [%expr `Off]
| tks ->
match spaces (string) loc name tks with
| Some tks -> Some [%expr `Tokens [%e tks]]
| None -> Common.error loc "Bad autocomplete tokens"
[@metaloc loc]
let script_type =
let maybeMime = Re.Str.regexp ".+/.+" in
fun ?separated_by:_ ?default:_ loc _ s ->
if s = "module"
then Some [%expr `Module]
else if Re.Str.string_match maybeMime s 0
then Some [%expr `Mime [%e Common.string loc s]]
else Common.error loc {|script type attribute must be "module" or a mime type|}
[@metaloc loc]
let sandbox = spaces variant
let in_ = total_variant Svg_types_reflected.in_value
let in2 = in_
let xmlns ?separated_by:_ ?default:_ loc name s =
if s <> "http://www.w3.org/1999/xhtml" then
Common.error loc "%s: namespace must be http://www.w3.org/1999/xhtml" name;
Some [%expr `W3_org_1999_xhtml] [@metaloc loc]