-
Notifications
You must be signed in to change notification settings - Fork 10
/
goji_emit_struct.ml
147 lines (133 loc) · 5.53 KB
/
goji_emit_struct.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
(****************************************************************************)
(* GOJI (JavaScript Interface Generator for OCaml) *)
(* This file is published under the CeCILL licence *)
(* (C) 2013 Benjamin Canou *)
(****************************************************************************)
open Goji_ast
open Goji_pprint
open Goji_registry
class virtual emitter = object (self)
method format_implementation component =
self # format_header component
^^ hardline
^^ format_phrases (self # format_elements component.elements)
^^ hardline
method format_interface component =
self # format_header component
^^ hardline
^^ format_phrases (self # format_interface_elements component.elements)
^^ hardline
method format_header { doc ; name ; version ; license ; author } =
let text =
self # format_doc (Doc doc)
^^ twice hardline
^^ self # format_doc
(Doc ( "This Binding for the JavaScript library "
^ name
^ (match version with Some v -> " version " ^ v | None -> "")
^ (match author with Some v -> " by " ^ v | None -> "")
^ " has been automatically generated \
by the Goji binding generator. \
It should not be edited by hand."))
^^ twice hardline
^^ self # format_doc
(Doc
(match license with
| None -> "IT DOES NOT BEAR ANY LICENSING INFORMATION !"
| Some l -> "It is licensed under the " ^ l.Goji_license.long_name
^ " (respecting the original library license). \
See the LICENSE file for more details."))
in
format_comment true text
method format_doc = function
| Doc text -> flow (break 1) (words text)
| Nodoc -> empty
method format_elements elements =
List.flatten (List.map (self # format_element) elements)
method format_interface_elements elements =
List.flatten (List.map (self # format_interface_element) elements)
method format_element = function
| Structure (name, doc, elements) ->
let doc = self # format_doc doc in
let contents = format_phrases (self # format_elements elements) in
[ format_module name ~doc contents ]
| Section (text, elements) ->
format_comment false (!^"{2 " ^^ !^text ^^ !^ "}")
:: self # format_elements elements
| Doc_block doc ->
[ format_comment false (self # format_doc doc) ]
| Group elements ->
self # format_elements elements
| Type (tparams, name, doc, type_mapping) ->
self # format_type_definition tparams name doc type_mapping
| Method (abbrv, name, params, body, ret, doc) ->
self # format_method_definition abbrv name params body ret doc
| Function (name, params, body, ret, doc) ->
self # format_function_definition name params body ret doc
| Let (name, body, ret, doc) ->
self # format_value_definition name body ret doc
| Exception (name, doc) ->
let doc = self # format_doc doc in
[ format_exception name ~doc [] ]
| Inherits (n, t1, t2, doc) ->
self # format_inherits_definition n t1 t2 doc
method format_interface_element = function
| Structure (name, doc, elements) ->
let doc = self # format_doc doc in
let contents = format_phrases (self # format_interface_elements elements) in
[ format_module_sig name ~doc contents ]
| Section (text, elements) ->
format_comment false (!^"{2 " ^^ !^text ^^ !^ "}")
:: self # format_interface_elements elements
| Doc_block doc ->
[ format_comment false (self # format_doc doc) ]
| Group elements ->
self # format_interface_elements elements
| Type (tparams, name, doc, type_mapping) ->
self # format_type_interface tparams name doc type_mapping
| Method (abbrv, name, params, body, ret, doc) ->
self # format_method_interface abbrv name params ret doc
| Function (name, params, body, ret, doc) ->
self # format_function_interface name params ret doc
| Let (name, body, ret, doc) ->
self # format_value_interface name ret doc
| Exception (name, doc) ->
let doc = self # format_doc doc in
[ format_exception name ~doc [] ]
| Inherits (n, t1, t2, doc) ->
self # format_inherits_interface n t1 t2 doc
method virtual format_type_definition
: (variance option * string) list -> string -> typedef -> comment
-> document list
method virtual format_method_definition
: abbrv -> string -> parameter list -> body -> value -> comment
-> document list
method virtual format_function_definition
: string -> parameter list -> body -> value -> comment
-> document list
method virtual format_value_definition
: string -> body -> value -> comment
-> document list
method virtual format_inherits_definition
: string -> abbrv -> abbrv -> comment
-> document list
method virtual format_type_interface
: (variance option * string) list -> string -> typedef -> comment
-> document list
method virtual format_method_interface
: abbrv -> string -> parameter list -> value -> comment
-> document list
method virtual format_function_interface
: string -> parameter list -> value -> comment
-> document list
method virtual format_value_interface
: string -> value -> comment
-> document list
method virtual format_inherits_interface
: string -> abbrv -> abbrv -> comment
-> document list
end
let emit_implementation emitter fp component =
ToChannel.pretty 0.9 80 fp (emitter # format_implementation component)
let emit_interface emitter fp component =
ToChannel.pretty 0.9 80 fp (emitter # format_interface component)