-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFsharpAST.fs
390 lines (346 loc) · 9.64 KB
/
FsharpAST.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
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
module rec Glutinum.Converter.FSharpAST
open TypeScript
type FSharpCommentParam = { Name: string; Content: string }
[<RequireQualifiedAccess>]
type FSharpXmlDoc =
| Summary of string list
| Param of FSharpCommentParam
| Returns of string
| Remarks of string
| DefaultValue of string
| Example of string
[<RequireQualifiedAccess>]
type FSharpLiteral =
| 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"
// [<RequireQualifiedAccess>]
// type FSharpEnumCaseValue =
// | Int of int
// | Float of float
type FSharpEnumCase = { Name: string; Value: FSharpLiteral }
[<RequireQualifiedAccess>]
type FSharpEnumType =
| String
| Numeric
| Unknown // or Mixed?
type FSharpEnum =
{
Name: string
Cases: FSharpEnumCase list
}
member this.Type =
let isString =
this.Cases
|> List.forall (fun c ->
match c.Value with
| FSharpLiteral.String _ -> true
| FSharpLiteral.Float _
| FSharpLiteral.Int _
| FSharpLiteral.Bool _
| FSharpLiteral.Null -> false
)
let isNumeric =
this.Cases
|> List.forall (fun c ->
match c.Value with
| FSharpLiteral.String _
| FSharpLiteral.Bool _ -> false
| FSharpLiteral.Float _
| FSharpLiteral.Int _
| FSharpLiteral.Null -> true
)
if isNumeric then
FSharpEnumType.Numeric
elif isString then
FSharpEnumType.String
else
FSharpEnumType.Unknown
[<RequireQualifiedAccess>]
type FSharpUnionCaseType =
| Named of string
| Literal of string
// | Float of float
// | Int of int
type FSharpUnionCase =
{
Attributes: FSharpAttribute list
Name: string
}
[<RequireQualifiedAccess>]
type FSharpUnionType =
| String
| Numeric
| Unknown
type FSharpUnion =
{
Attributes: FSharpAttribute list
Name: string
Cases: FSharpUnionCase list
IsOptional: bool
}
// member this.Type =
// let isString =
// this.Cases
// |> List.forall (fun c ->
// match c.Value with
// | FSharpUnionCaseType.Literal _ ->
// true
// | FSharpUnionCaseType.Float _
// | FSharpUnionCaseType.Int _ ->
// false
// )
// let isNumeric =
// this.Cases
// |> List.forall (fun c ->
// match c.Value with
// | FSharpUnionCaseType.String _ ->
// false
// | FSharpUnionCaseType.Float _
// | FSharpUnionCaseType.Int _ ->
// true
// )
// if isNumeric then
// FSharpUnionType.Numeric
// elif isString then
// FSharpUnionType.String
// else
// FSharpUnionType.Unknown
type FSharpModule =
{
Name: string
IsRecursive: bool
Types: FSharpType list
}
[<RequireQualifiedAccess>]
type FSharpAccessor =
| ReadOnly
| WriteOnly
| ReadWrite
[<RequireQualifiedAccess>]
type FSharpAccessibility =
| Public
| Private
| Protected
member this.Text =
match this with
| Public -> "public"
| Private -> "private"
| Protected -> "protected"
[<RequireQualifiedAccess>]
type FSharpAttribute =
| Text of string
/// <summary>
/// Generates <c>[<Emit("$0($1...)")>]</c> attribute.
/// </summary>
| EmitSelfInvoke
/// <summary>
/// Generates <c>[<Emit("$0")>]</c> attribute.
/// </summary>
| EmitSelf
/// <summary>
/// Generates <c>[<Import(selector, from)>]</c> attribute.
/// </summary>
| Import of selector: string * from: string
/// <summary>
/// Generates <c>[<ImportAll(moduleName)>]</c> attribute.
/// </summary>
| ImportAll of moduleName: string
| Erase
| AllowNullLiteral
| Obsolete of string option
| StringEnum of Fable.Core.CaseRules
| CompiledName of string
| RequireQualifiedAccess
| EmitConstructor
/// <summary>
/// Generates <c>[<Emit("new $0.className($1...)")>]"</c> attribute.
/// </summary>
| EmitMacroConstructor of className: string
| EmitIndexer
| Global
| ParamObject
| ParamArray
| Interface
type FSharpParameter =
{
Attributes: FSharpAttribute list
Name: string
IsOptional: bool
Type: FSharpType
}
type FSharpMemberInfo =
{
Attributes: FSharpAttribute list
Name: string
OriginalName: string
TypeParameters: FSharpTypeParameter list
Parameters: FSharpParameter list
Type: FSharpType
IsOptional: bool
IsStatic: bool
Accessor: FSharpAccessor option
Accessibility: FSharpAccessibility
XmlDoc: FSharpXmlDoc list
}
type FSharpStaticMemberInfo =
{
Attributes: FSharpAttribute list
Name: string
// We need the original because we emit actual JavaScript code
// for interface static members.
OriginalName: string
TypeParameters: FSharpTypeParameter list
Parameters: FSharpParameter list
Type: FSharpType
IsOptional: bool
Accessor: FSharpAccessor option
Accessibility: FSharpAccessibility
}
[<RequireQualifiedAccess>]
type FSharpMember =
| Method of FSharpMemberInfo
| Property of FSharpMemberInfo
/// <summary>
/// Special case for static members used, when generating a static interface
/// binding on a class.
///
/// For binding a static method of a class, we need to generate the body of the
/// method so instead of trying to hack our way via the standard method binding
/// we use this special case.
/// See: https://github.com/glutinum-org/cli/issues/60
/// </summary>
| StaticMember of FSharpStaticMemberInfo
type FSharpInterface =
{
Attributes: FSharpAttribute list
Name: string
// We need the original because we emit actual JavaScript code
// for interface static members.
OriginalName: string
TypeParameters: FSharpTypeParameter list
Members: FSharpMember list
}
type FSharpExplicitField = { Name: string; Type: FSharpType }
type FSharpConstructor =
{
Parameters: FSharpParameter list
Attributes: FSharpAttribute list
Accessibility: FSharpAccessibility
}
static member EmptyPublic =
{
Parameters = []
Attributes = []
Accessibility = FSharpAccessibility.Public
}
static member EmptyPrivate =
{
Parameters = []
Attributes = []
Accessibility = FSharpAccessibility.Private
}
type FSharpClass =
{
Attributes: FSharpAttribute list
Name: string
TypeParameters: FSharpTypeParameter list
PrimaryConstructor: FSharpConstructor
SecondaryConstructors: FSharpConstructor list
ExplicitFields: FSharpExplicitField list
}
type FSharpMapped =
{
Name: string
Declarations: FSharpType list
}
[<RequireQualifiedAccess>]
type FSharpPrimitive =
| String
| Int
| Float
| Bool
| Unit
| Number
| Null
type FSharpTypeParameter =
{
Name: string
Constraint: FSharpType option
Default: FSharpType option
}
static member Create
(name: string, ?constraint_: FSharpType, ?default_: FSharpType)
=
{
Name = name
Constraint = constraint_
Default = default_
}
type FSharpTypeAlias =
{
Name: string
Type: FSharpType
TypeParameters: FSharpTypeParameter list
}
// type FSharpClass =
// {
// Name : string
// Members : FSharpMember list
// }
type FSharpTypeReference =
{
Name: string
FullName: string
TypeArguments: FSharpType list
Type: FSharpType
}
type FSharpFunctionType =
{
Parameters: FSharpParameter list
TypeArguments: FSharpType list
ReturnType: FSharpType
}
[<RequireQualifiedAccess>]
type FSharpType =
| Enum of FSharpEnum
| Union of FSharpUnion
| Option of FSharpType
// Create ErasedUnion type to make a difference between standard F# union
// and Fable U2, U3, etc. types.
// It is also possible that a third type of union exist which are
// specialized ErasedUnion types.
// To avoid using U2, U3, etc. types, but insteand things like:
// [<Erased>]
// type ConfigType =
// | String of string
// | Numeric of float
// Allowing for a more natural syntax, as you know what each cases expects
// compared to U2, U3, etc. for which you have to look at the type definition.
// | ErasedUnion of FSharpUnion
| Module of FSharpModule
| Interface of FSharpInterface
| Unsupported of Ts.SyntaxKind
| Mapped of FSharpMapped
| Primitive of FSharpPrimitive
| TypeAlias of FSharpTypeAlias
// | Class of FSharpClass
| Discard
| TypeReference of FSharpTypeReference
| Tuple of FSharpType list
| TypeParameter of string
| ResizeArray of FSharpType
| ThisType of typeName: string
| Function of FSharpFunctionType
| Class of FSharpClass
| Object
type FSharpOutFile = { Name: string; Opens: string list }