-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGlueAST.fs
269 lines (238 loc) · 6.46 KB
/
GlueAST.fs
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
/// <summary>
/// Glue AST aims to reflect the TypeScript AST as closely as possible
/// while being easy to manipulate for converting to F# AST and "optimising"
/// portion of the code.
///
/// Optimising in this context means for example converting a method taking
/// literal union into F# overloaded methods. This makes the F# API feels more
/// native and easier to use.
///
/// Sementics in the Glue AST follows the TypeScript sementics.
/// </summary>
module rec Glutinum.Converter.GlueAST
type GlueCommentParam =
{ Name: string; Content: string option }
[<RequireQualifiedAccess>]
type GlueComment =
| Summary of string list
| Returns of string
| Param of GlueCommentParam
| Deprecated of string option
| Remarks of string
| DefaultValue of string
| Example of string
type GlueParameter =
{
Name: string
IsOptional: bool
IsSpread: bool
Type: GlueType
}
type GlueTypeParameter =
{
Name: string
Constraint: GlueType option
Default: GlueType option
}
type GlueMethod =
{
Name: string
Parameters: GlueParameter list
Type: GlueType
IsOptional: bool
IsStatic: bool
}
type GlueCallSignature =
{
Parameters: GlueParameter list
Type: GlueType
}
[<RequireQualifiedAccess>]
type GlueAccessor =
| ReadOnly
| WriteOnly
| ReadWrite
type GlueProperty =
{
Name: string
Documentation: GlueComment list
Type: GlueType
IsStatic: bool
IsOptional: bool
Accessor: GlueAccessor
IsPrivate: bool
}
type GlueIndexSignature =
{
Parameters: GlueParameter list
Type: GlueType
}
type GlueMethodSignature =
{
Name: string
Parameters: GlueParameter list
Type: GlueType
}
type GlueConstructSignature =
{
Parameters: GlueParameter list
Type: GlueType
}
[<RequireQualifiedAccess>]
type GlueMember =
| Method of GlueMethod
| Property of GlueProperty
| CallSignature of GlueCallSignature
| IndexSignature of GlueIndexSignature
| MethodSignature of GlueMethodSignature
| ConstructSignature of GlueConstructSignature
type GlueInterface =
{
Name: string
Members: GlueMember list
TypeParameters: GlueTypeParameter list
}
type GlueTypeLiteral = { Members: GlueMember list }
type GlueVariable = { Name: string; Type: GlueType }
[<RequireQualifiedAccess>]
type GluePrimitive =
| String
| Int
| Float
| Bool
| Unit
| Number
| Any
| Null
| Undefined
| Object
[<RequireQualifiedAccess>]
type GlueLiteral =
| String of string
| Int of int
| Float of float
| Bool of bool
| Null
member this.ToText() =
match this with
| String value -> value
| Int value -> string value
| Float value -> string value
| Bool value -> string value
| Null -> "null"
type GlueEnumMember = { Name: string; Value: GlueLiteral }
type GlueEnum =
{
Name: string
Members: GlueEnumMember list
}
type GlueTypeAliasDeclaration =
{
Name: string
Type: GlueType
TypeParameters: GlueTypeParameter list
}
type GlueFunctionDeclaration =
{
Documentation: GlueComment list
IsDeclared: bool
Name: string
Type: GlueType
Parameters: GlueParameter list
TypeParameters: GlueTypeParameter list
}
type GlueModuleDeclaration =
{
Name: string
IsNamespace: bool
IsRecursive: bool
Types: GlueType list
}
type GlueConstructor = GlueConstructor of GlueParameter list
type GlueClassDeclaration =
{
Name: string
Constructors: GlueConstructor list
Members: GlueMember list
TypeParameters: GlueTypeParameter list
}
type GlueTypeReference =
{
Name: string
FullName: string
TypeArguments: GlueType list
}
type GlueTypeUnion = GlueTypeUnion of GlueType list
[<RequireQualifiedAccess>]
type ExcludedMember = Literal of GlueLiteral
// | Function
type GlueFunctionType =
{
Type: GlueType
Parameters: GlueParameter list
}
[<RequireQualifiedAccess>]
type GlueType =
| Discard
| Interface of GlueInterface
| Variable of GlueVariable
| Primitive of GluePrimitive
| Enum of GlueEnum
| TypeAliasDeclaration of GlueTypeAliasDeclaration
| FunctionDeclaration of GlueFunctionDeclaration
| Union of GlueTypeUnion
| Literal of GlueLiteral
| KeyOf of GlueType
| IndexedAccessType of GlueType
| ModuleDeclaration of GlueModuleDeclaration
| ClassDeclaration of GlueClassDeclaration
| TypeReference of GlueTypeReference
| Partial of GlueInterface
| Array of GlueType
| FunctionType of GlueFunctionType
| TypeParameter of string
| ThisType of typeName: string
| TupleType of GlueType list
| IntersectionType of GlueMember list
| TypeLiteral of GlueTypeLiteral
| OptionalType of GlueType
| Unknown
| ExportDefault of GlueType
member this.Name =
match this with
| Interface info -> info.Name
| Variable info -> info.Name
| Primitive info ->
match info with
| GluePrimitive.String -> "string"
| GluePrimitive.Int -> "int"
| GluePrimitive.Float -> "float"
| GluePrimitive.Bool -> "bool"
| GluePrimitive.Unit -> "unit"
| GluePrimitive.Number -> "float"
| GluePrimitive.Any -> "obj"
| GluePrimitive.Null -> "obj option"
| GluePrimitive.Undefined -> "obj"
| GluePrimitive.Object -> "obj"
| Enum info -> info.Name
| TypeAliasDeclaration info -> info.Name
| TypeParameter name -> name
| Literal info -> info.ToText()
| KeyOf _ -> "string"
| FunctionDeclaration info -> info.Name
| ModuleDeclaration info -> info.Name
| ClassDeclaration info -> info.Name
| TypeReference info -> info.Name
| Array info -> $"ResizeArray<{info.Name}>"
| ThisType typeName -> typeName
| TypeLiteral _
| IntersectionType _
| IndexedAccessType _
| Union _
| Partial _
| FunctionType _
| TupleType _
| OptionalType _ // TODO: Should we take the name of the underlying type and add option to it?
| Discard
| ExportDefault _
| Unknown -> "obj"