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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System;
using System.Xml.XPath;
using System.Globalization;
namespace Microsoft.Ddue.Tools {
/// <summary>
/// Generates syntax that corresponds to JavaScript that Script# generates.
/// </summary>
public class ScriptSharpDeclarationSyntaxGenerator : SyntaxGeneratorTemplate {
private static readonly XPathExpression typeIsRecordExpression = XPathExpression.Compile("boolean(apidata/@record)");
private static readonly XPathExpression memberIsGlobalExpression = XPathExpression.Compile("boolean(apidata/@global)");
/// <summary>
/// Initializes a new instance of the <c>ScriptSharpDeclarationSyntaxGenerator</c> class.
/// </summary>
/// <param name="configuration"></param>
public ScriptSharpDeclarationSyntaxGenerator(XPathNavigator configuration) : base(configuration) {
if (String.IsNullOrEmpty(Language)) Language = "JavaScript";
}
/// <summary>
/// Determines whether the feature is unsupported by this language.
/// </summary>
/// <param name="reflection"></param>
/// <param name="writer"></param>
/// <returns></returns>
private bool IsUnsupported(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupportedGeneric(reflection, writer)) {
return true;
}
if (IsUnsupportedExplicit(reflection, writer)) {
return true;
}
if (IsUnsupportedUnsafe(reflection, writer)) {
return true;
}
if (HasAttribute(reflection, "System.NonScriptableAttribute")) {
writer.WriteMessage("UnsupportedType_ScriptSharp");
return true;
}
return false;
}
private string ReadNamespaceName(XPathNavigator reflection) {
return (string)reflection.Evaluate(apiContainingNamespaceNameExpression);
}
private string ReadTypeName(XPathNavigator reflection) {
return (string)reflection.Evaluate(apiNameExpression);
}
private string ReadContainingTypeName(XPathNavigator reflection) {
return (string)reflection.Evaluate(apiContainingTypeNameExpression);
}
private string ReadFullTypeName(XPathNavigator reflection) {
string namespaceName = ReadNamespaceName(reflection);
string typeName = ReadTypeName(reflection);
if (String.IsNullOrEmpty(namespaceName) || HasAttribute(reflection, "System.IgnoreNamespaceAttribute")) {
return typeName;
}
else {
return String.Format("{0}.{1}", namespaceName, typeName);
}
}
private string ReadFullContainingTypeName(XPathNavigator reflection) {
string namespaceName = ReadNamespaceName(reflection);
string typeName = ReadContainingTypeName(reflection);
if (String.IsNullOrEmpty(namespaceName) || HasAttribute(reflection, "System.IgnoreNamespaceAttribute")) {
return typeName;
}
else {
return String.Format("{0}.{1}", namespaceName, typeName);
}
}
private string ReadMemberName(XPathNavigator reflection) {
string identifier = (string)reflection.Evaluate(apiNameExpression);
if (!HasAttribute(reflection, "System.PreserveCaseAttribute")) {
identifier = CreateCamelCaseName(identifier);
}
return identifier;
}
private bool HasAttribute(XPathNavigator reflection, string attributeName) {
attributeName = "T:" + attributeName;
XPathNodeIterator iterator = (XPathNodeIterator)reflection.Evaluate(apiAttributesExpression);
foreach (XPathNavigator navigator in iterator) {
XPathNavigator reference = navigator.SelectSingleNode(attributeTypeExpression);
if (reference.GetAttribute("api", string.Empty) == attributeName) {
return true;
}
}
return false;
}
public static string CreateCamelCaseName(string name) {
if (String.IsNullOrEmpty(name)) {
return name;
}
// Some exceptions that simply need to be special cased
if (name.Equals("ID", StringComparison.Ordinal)) {
return "id";
}
bool hasLowerCase = false;
int conversionLength = 0;
for (int i = 0; i < name.Length; i++) {
if (Char.IsUpper(name, i)) {
conversionLength++;
} else {
hasLowerCase = true;
break;
}
}
if (((hasLowerCase == false) && (name.Length != 1)) || (conversionLength == 0)) {
// Name is all upper case, or all lower case; leave it as-is.
return name;
}
if (conversionLength > 1) {
// Convert the leading uppercase segment, except the last character
// which is assumed to be the first letter of the next word
return name.Substring(0, conversionLength - 1).ToLower(CultureInfo.InvariantCulture) + name.Substring(conversionLength - 1);
}
else if (name.Length == 1) {
return name.ToLower(CultureInfo.InvariantCulture);
}
else {
// Convert the leading upper case character to lower case
return Char.ToLower(name[0], CultureInfo.InvariantCulture) + name.Substring(1);
}
}
private void WriteIndentedNewLine(SyntaxWriter writer) {
writer.WriteString(",");
writer.WriteLine();
writer.WriteString("\t");
}
private void WriteParameterList(XPathNavigator reflection, SyntaxWriter writer) {
XPathNodeIterator parameters = reflection.Select(apiParametersExpression);
writer.WriteString("(");
while (parameters.MoveNext()) {
XPathNavigator parameter = parameters.Current;
WriteParameter(parameter, writer);
if (parameters.CurrentPosition < parameters.Count) {
writer.WriteString(", ");
}
}
writer.WriteString(")");
}
private void WriteParameter(XPathNavigator parameter, SyntaxWriter writer) {
string text = (string)parameter.Evaluate(parameterNameExpression);
XPathNavigator reference = parameter.SelectSingleNode(parameterTypeExpression);
if ((bool)parameter.Evaluate(parameterIsParamArrayExpression)) {
writer.WriteString("... ");
}
writer.WriteParameter(text);
}
private void WriteTypeReference(XPathNavigator reference, SyntaxWriter writer)
{
switch (reference.LocalName)
{
case "arrayOf":
int rank = Convert.ToInt32(reference.GetAttribute("rank", string.Empty));
XPathNavigator navigator = reference.SelectSingleNode(typeExpression);
WriteTypeReference(navigator, writer);
writer.WriteString("[");
for (int i = 1; i < rank; i++) { writer.WriteString(","); }
writer.WriteString("]");
break;
case "type":
string id = reference.GetAttribute("api", string.Empty);
WriteNormalTypeReference(id, writer);
break;
case "pointerTo":
case "referenceTo":
case "template":
case "specialization":
// Not supported
break;
}
}
private void WriteNormalTypeReference(string api, SyntaxWriter writer) {
switch (api) {
case "T:System.Byte":
writer.WriteReferenceLink(api, "Byte");
return;
case "T:System.SByte":
writer.WriteReferenceLink(api, "SByte");
return;
case "T:System.Char":
writer.WriteReferenceLink(api, "Char");
return;
case "T:System.Int16":
writer.WriteReferenceLink(api, "Int16");
return;
case "T:System.Int32":
writer.WriteReferenceLink(api, "Int32");
return;
case "T:System.Int64":
writer.WriteReferenceLink(api, "Int64");
return;
case "T:System.UInt16":
writer.WriteReferenceLink(api, "UInt16");
return;
case "T:System.UInt32":
writer.WriteReferenceLink(api, "UInt32");
return;
case "T:System.UInt64":
writer.WriteReferenceLink(api, "UInt64");
return;
case "T:System.Single":
writer.WriteReferenceLink(api, "Single");
return;
case "T:System.Double":
writer.WriteReferenceLink(api, "Double");
return;
case "T:System.Decimal":
writer.WriteReferenceLink(api, "Decimal");
return;
case "T:System.Boolean":
writer.WriteReferenceLink(api, "Boolean");
return;
}
// Remove 'T:'
string name = api.Substring(2);
// Strip System namespaces
if (name.StartsWith("System.")) {
int idx = name.LastIndexOf('.');
name = name.Substring(idx + 1);
}
writer.WriteReferenceLink(api, name);
}
private void WriteRecordSyntax(XPathNavigator reflection, SyntaxWriter writer) {
string namespaceName = ReadNamespaceName(reflection);
string typeName = ReadTypeName(reflection);
writer.WriteString(namespaceName);
writer.WriteString(".$create_");
writer.WriteString(typeName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
writer.WriteString("();");
}
private void WriteRecordConstructorSyntax(XPathNavigator reflection, SyntaxWriter writer) {
string namespaceName = ReadNamespaceName(reflection);
string typeName = ReadContainingTypeName(reflection);
writer.WriteString(namespaceName);
writer.WriteString(".$create_");
writer.WriteString(typeName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
WriteParameterList(reflection, writer);
writer.WriteString(";");
}
public override void WriteClassSyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) return;
if (HasAttribute(reflection, "System.RecordAttribute")) {
WriteRecordSyntax(reflection, writer);
return;
}
string typeName = ReadFullTypeName(reflection);
writer.WriteIdentifier(typeName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
writer.WriteString("();");
writer.WriteLine();
writer.WriteLine();
writer.WriteIdentifier("Type");
writer.WriteString(".createClass(");
writer.WriteLine();
writer.WriteString("\t'");
writer.WriteString(typeName);
writer.WriteString("'");
bool hasBaseClass = false;
// Write the base class.
XPathNavigator reference = reflection.SelectSingleNode(apiBaseClassExpression);
if (!((reference == null) || ((bool)reference.Evaluate(typeIsObjectExpression)))) {
WriteIndentedNewLine(writer);
WriteTypeReference(reference, writer);
hasBaseClass = true;
}
// Write the interfaces.
XPathNodeIterator iterator = reflection.Select(apiImplementedInterfacesExpression);
if (iterator.Count != 0) {
if (!hasBaseClass) {
WriteIndentedNewLine(writer);
writer.WriteString("null");
}
WriteIndentedNewLine(writer);
while (iterator.MoveNext()) {
XPathNavigator interfaceRef = iterator.Current;
WriteTypeReference(interfaceRef, writer);
if (iterator.CurrentPosition < iterator.Count) {
WriteIndentedNewLine(writer);
}
}
}
writer.WriteString(");");
}
public override void WriteConstructorSyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) {
return;
}
bool isRecord = (bool)reflection.Evaluate(typeIsRecordExpression);
if (isRecord) {
WriteRecordConstructorSyntax(reflection, writer);
return;
}
string typeName = ReadFullContainingTypeName(reflection);
writer.WriteIdentifier(typeName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
WriteParameterList(reflection, writer);
writer.WriteString(";");
}
public override void WriteNormalMethodSyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) return;
if (HasAttribute(reflection, "System.AttachedPropertyAttribute")) {
WriteAttachedPropertySyntax(reflection, writer);
return;
}
string memberName = ReadMemberName(reflection);
bool isStatic = (bool)reflection.Evaluate(apiIsStaticExpression);
bool isGlobal = (bool)reflection.Evaluate(memberIsGlobalExpression);
if (isStatic && !isGlobal) {
writer.WriteIdentifier(ReadFullContainingTypeName(reflection));
writer.WriteString(".");
writer.WriteIdentifier(memberName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
}
else {
writer.WriteKeyword("function");
writer.WriteString(" ");
writer.WriteIdentifier(memberName);
}
WriteParameterList(reflection, writer);
writer.WriteString(";");
}
public override void WriteDelegateSyntax(XPathNavigator reflection, SyntaxWriter writer) {
writer.WriteKeyword("function");
WriteParameterList(reflection, writer);
writer.WriteString(";");
}
public override void WriteEnumerationSyntax(XPathNavigator reflection, SyntaxWriter writer) {
string typeName = ReadFullTypeName(reflection);
writer.WriteIdentifier(typeName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
writer.WriteString("();");
writer.WriteLine();
writer.WriteIdentifier(typeName);
writer.WriteString(".createEnum('");
writer.WriteIdentifier(typeName);
writer.WriteString("', ");
writer.WriteString(HasAttribute(reflection, "System.FlagsAttribute") ? "true" : "false");
writer.WriteString(");");
}
public override void WriteEventSyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) return;
if (reflection.Select(apiParametersExpression).Count > 0) {
writer.WriteMessage("UnsupportedIndex_" + Language);
return;
}
string memberName = ReadMemberName(reflection);
writer.WriteKeyword("function");
writer.WriteString(" add_");
writer.WriteIdentifier(memberName);
writer.WriteString("(");
writer.WriteParameter("value");
writer.WriteString(");");
writer.WriteLine();
writer.WriteKeyword("function");
writer.WriteString(" remove_");
writer.WriteIdentifier(memberName);
writer.WriteString("(");
writer.WriteParameter("value");
writer.WriteString(");");
}
public override void WriteFieldSyntax(XPathNavigator reflection, SyntaxWriter writer)
{
if (IsUnsupported(reflection, writer)) {
return;
}
string memberName = ReadMemberName(reflection);
bool isStatic = (bool)reflection.Evaluate(apiIsStaticExpression);
if (isStatic) {
string typeName = ReadFullContainingTypeName(reflection);
writer.WriteIdentifier(typeName);
writer.WriteString(".");
}
writer.WriteIdentifier(memberName);
}
public override void WriteInterfaceSyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) return;
string typeName = ReadFullTypeName(reflection);
writer.WriteIdentifier(typeName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
writer.WriteString("();");
writer.WriteLine();
writer.WriteIdentifier(typeName);
writer.WriteString(".createInterface('");
writer.WriteIdentifier(typeName);
writer.WriteString("');");
}
public override void WriteAttachedEventSyntax(XPathNavigator reflection, SyntaxWriter writer) {
// Not supported
}
public override void WriteAttachedPropertySyntax(XPathNavigator reflection, SyntaxWriter writer) {
string typeName = ReadContainingTypeName(reflection);
string methodName = ReadMemberName(reflection);
string propertyName = String.Format("{0}.{1}", typeName, methodName.Substring(3));
if (methodName.StartsWith("Get", StringComparison.OrdinalIgnoreCase)) {
writer.WriteKeyword("var");
writer.WriteString(" value = obj['");
writer.WriteString(propertyName);
writer.WriteString("'];");
}
else if (methodName.StartsWith("Set", StringComparison.OrdinalIgnoreCase)) {
writer.WriteString("obj['");
writer.WriteString(propertyName);
writer.WriteString("'] = value;");
}
}
public override void WriteOperatorSyntax(XPathNavigator reflection, SyntaxWriter writer) {
writer.WriteMessage("UnsupportedOperator_" + Language);
}
public override void WriteCastSyntax(XPathNavigator reflection, SyntaxWriter writer) {
writer.WriteMessage("UnsupportedCast_" + Language);
}
public override void WriteNamespaceSyntax(XPathNavigator reflection, SyntaxWriter writer) {
string name = reflection.Evaluate(apiNameExpression).ToString();
writer.WriteString("Type.createNamespace('");
writer.WriteIdentifier(name);
writer.WriteString("');");
}
public override void WritePropertySyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) return;
if (HasAttribute(reflection, "System.IntrinsicPropertyAttribute")) {
WriteFieldSyntax(reflection, writer);
return;
}
string memberName = ReadMemberName(reflection);
bool isStatic = (bool)reflection.Evaluate(apiIsStaticExpression);
bool isGetter = (bool)reflection.Evaluate(apiIsReadPropertyExpression);
bool isSetter = (bool)reflection.Evaluate(apiIsWritePropertyExpression);
XPathNavigator reference = reflection.SelectSingleNode(apiReturnTypeExpression);
if (isGetter) {
if (isStatic) {
writer.WriteIdentifier(ReadFullContainingTypeName(reflection));
writer.WriteString(".");
writer.WriteString("get_");
writer.WriteIdentifier(memberName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
}
else {
writer.WriteKeyword("function");
writer.WriteString(" ");
writer.WriteString("get_");
writer.WriteIdentifier(memberName);
}
WriteParameterList(reflection, writer);
writer.WriteString(";");
writer.WriteLine();
}
if (isSetter) {
if (isStatic) {
writer.WriteIdentifier(ReadFullContainingTypeName(reflection));
writer.WriteString(".");
writer.WriteString("set_");
writer.WriteIdentifier(memberName);
writer.WriteString(" = ");
writer.WriteKeyword("function");
}
else {
writer.WriteKeyword("function");
writer.WriteString(" ");
writer.WriteString("set_");
writer.WriteIdentifier(memberName);
}
writer.WriteString("(");
writer.WriteParameter("value");
writer.WriteString(");");
}
}
public override void WriteStructureSyntax(XPathNavigator reflection, SyntaxWriter writer) {
if (IsUnsupported(reflection, writer)) return;
writer.WriteMessage("UnsupportedStructure_" + Language);
}
}
}
|