package js_of_ocaml-webidl

  1. Overview
  2. Docs

Source file ast_to_data.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
module Syntax = Webidl_syntax
open Syntax
open Data

let of_attribute qualifiers (type_with_ext, name) =
  let is_static = List.mem `Static qualifiers in
  let is_readonly = List.mem `ReadOnly qualifiers in
  let is_inherit = List.mem `Inherit qualifiers in
  `Attribute { type_with_ext; name; is_static; is_readonly; is_inherit }
;;

let of_operation specials is_static = function
  | `NoSpecialOperation (return_type, (ident, arguments)) ->
    `Operation { specials; is_static; return_type; ident; arguments }
  | `SpecialOperation (specials, return_type, (ident, arguments)) ->
    `Operation { specials; is_static; return_type; ident; arguments }
;;

let of_readonly = function
  | `Maplike (key_type, value_type) ->
    `Maplike { is_readonly = true; key_type; value_type }
  | `Setlike key_type -> `Setlike { is_readonly = true; key_type }
  | `AttributeRest attribute -> of_attribute [ `ReadOnly ] attribute
;;

let of_static = function
  | `ReadOnly (`AttributeRest attribute) ->
    of_attribute [ `ReadOnly; `Static ] attribute
  | `AttributeRest attribute -> of_attribute [ `Static ] attribute
  | `NoSpecialOperation _ as operation -> of_operation [] true operation
;;

let of_stringifier = function
  | `ReadOnly (`AttributeRest attribute) ->
    of_attribute [ `ReadOnly ] attribute
  | `AttributeRest attribute -> of_attribute [] attribute
  | `NoSpecialOperation _ as operation -> of_operation [] false operation
  | `None -> `None
;;

let of_interface_member : Ast.interface_member -> Data.interface_member
  = function
  | `Const const -> `Const const
  | `Operation operation -> of_operation [] false operation
  | `Stringifier stringifier -> `Stringifier (of_stringifier stringifier)
  | `Static static -> of_static static
  | `Iterable iterable -> `Iterable iterable
  | `Attribute (`Inherit (`AttributeRest attribute)) ->
    of_attribute [ `Inherit ] attribute
  | `Attribute (`AttributeRest attribute) -> of_attribute [] attribute
  | `ReadOnly member -> of_readonly member
  | `Maplike (key_type, value_type) ->
    `Maplike { is_readonly = false; key_type; value_type }
  | `Setlike key_type -> `Setlike { is_readonly = false; key_type }
;;

let of_mixin_member : Ast.mixin_member -> Data.mixin_member = function
  | `Const const -> `Const const
  | `Operation operation -> of_operation [] false operation
  | `Stringifier stringifier -> `Stringifier (of_stringifier stringifier)
  | `ReadOnly member -> of_readonly member
  | `Attribute (`Inherit (`AttributeRest attribute)) ->
    of_attribute [ `Inherit ] attribute
  | `Attribute (`AttributeRest attribute) -> of_attribute [] attribute
;;

let map_snd f dst = List.map (fun (x, y) -> x, f y) dst

let of_mixin (ident, members) =
  let mixin_members = map_snd of_mixin_member members in
  `Mixin { ident; mixin_members }
;;

let of_interface (ident, inheritance, members) =
  let interface_members = map_snd of_interface_member members in
  `Interface { ident; inheritance; interface_members }
;;

let of_namespace_member = function
  | `NoSpecialOperation _ as operation -> of_operation [] false operation
  | `ReadOnly (`AttributeRest attribute) ->
    of_attribute [ `ReadOnly ] attribute
  | `AttributeRest attribute -> of_attribute [] attribute
;;

let of_namespace (ident, members) =
  let namespace_members = map_snd of_namespace_member members in
  `Namespace { ident; namespace_members }
;;

let of_dictionary_member = function
  | `Required (type_with_ext, ident, default) ->
    { is_required = true; type_with_ext; ident; default }
  | `NotRequired (type_, ident, default) ->
    let type_with_ext = [], type_ in
    { is_required = false; type_with_ext; ident; default }
;;

let of_dictionary (ident, inheritance, members) =
  let dictionary_members = map_snd of_dictionary_member members in
  `Dictionary { ident; inheritance; dictionary_members }
;;

let of_partial = function
  | `PartialInterface (ident, members) ->
    of_interface (ident, None, members)
  | `Mixin mixin -> of_mixin mixin
  | `PartialDictionary (ident, members) ->
    of_dictionary (ident, None, members)
  | `Namespace namespace -> of_namespace namespace
;;

let of_callback = function
  | `CallbackRest (ident, return_type, arguments) ->
    of_operation
      []
      false
      (`NoSpecialOperation (return_type, (Some ident, arguments)))
  | `Interface interface -> of_interface interface
;;

let of_definition : Ast.definition -> Data.definition = function
  | `Callback callback -> `Callback (of_callback callback)
  | `Includes includes -> `Includes includes
  | `Interface interface -> of_interface interface
  | `Mixin mixin -> of_mixin mixin
  | `Namespace namespace -> of_namespace namespace
  | `Partial partial -> `Partial (of_partial partial)
  | `Dictionary dictionary -> of_dictionary dictionary
  | `Enum enum -> `Enum enum
  | `Typedef typedef -> `Typedef typedef
  | `Implements implements -> `Implements implements
;;

let of_difinitions definitions = map_snd of_definition definitions
OCaml

Innovation. Community. Security.