1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var c_sharp_objects_1 = require("./c-sharp-objects");
|
4 | var CSharpParser = (function () {
|
5 | function CSharpParser() {
|
6 | }
|
7 | CSharpParser.parse = function (input) {
|
8 | input = CSharpParser.stripIgnored(input);
|
9 | input = CSharpParser.stripComments(input);
|
10 | var namespaceMatch = input.match(/namespace\s+([\w\.]+)/);
|
11 | var namespace = namespaceMatch ? namespaceMatch[1] : '';
|
12 | var types = [];
|
13 | var typeMatch;
|
14 |
|
15 | var typeRegex = /^([\t ]*)(?:public\s*|partial\s*|abstract\s*)*\s*(enum|class|struct|interface)\s+([\w\d_<>]+)(?:\s*:\s*((?:(?:[\w\d\.\<\>_]+)(?:,\s+)?)+))?\s*\{((?:.|\n|\r)*?)^\1\}/gm;
|
16 | var getNextTypeMatch = function () { return typeMatch = typeRegex.exec(input); };
|
17 | while (getNextTypeMatch() !== null) {
|
18 | var type = typeMatch[2];
|
19 | var name = typeMatch[3];
|
20 | var inherits = CSharpParser.parseInherits(typeMatch[4]);
|
21 | var body = typeMatch[5];
|
22 | if (type === 'class' || type === 'struct') {
|
23 | types.push(CSharpParser.parseClassOrStruct(namespace, name, inherits, body));
|
24 | }
|
25 | else if (type === 'interface') {
|
26 | types.push(CSharpParser.parseInterface(namespace, name, inherits, body));
|
27 | }
|
28 | else if (type === 'enum') {
|
29 | types.push(CSharpParser.parseEnum(namespace, name, inherits, body));
|
30 | }
|
31 | }
|
32 | return types;
|
33 | };
|
34 | CSharpParser.stripIgnored = function (input) {
|
35 | var ignoredRegex = /\/\/\s*ts-generator-ignore\s*.*(enum|class|struct|interface)\s+/gm;
|
36 | return input.replace(ignoredRegex, '');
|
37 | };
|
38 | CSharpParser.stripComments = function (input) {
|
39 | var blockCommentRegex = /\/\*([\s\S]*?)\*\//gm;
|
40 | var lineCommentRegex = /\/\/(.*)/g;
|
41 | return input
|
42 | .replace(blockCommentRegex, '')
|
43 | .split('\n')
|
44 | .map(function (line) { return line.replace(lineCommentRegex, ''); })
|
45 | .join('\n');
|
46 | };
|
47 | CSharpParser.parseInherits = function (inheritsStr) {
|
48 | var inherits = [];
|
49 | if (inheritsStr) {
|
50 | var inheritsMatch_1;
|
51 | var inheritsRegex_1 = /(?:[\w\d\.\_]+)(?:\<.+?\>)?/g;
|
52 | var getNextInheritsMatch = function () { return inheritsMatch_1 = inheritsRegex_1.exec(inheritsStr); };
|
53 | while (getNextInheritsMatch() !== null) {
|
54 | inherits.push(inheritsMatch_1[0]);
|
55 | }
|
56 | }
|
57 | return inherits;
|
58 | };
|
59 | CSharpParser.parseClassOrStruct = function (namespace, name, inherits, body) {
|
60 | var constructors = [];
|
61 | var constructorMatch;
|
62 | var constructorRegex = /public\s+([\w]+)\s*\(((?:.|\r|\n)*?)\)/gm;
|
63 | var getNextConstructorMatch = function () { return constructorMatch = constructorRegex.exec(body); };
|
64 | while (getNextConstructorMatch() !== null) {
|
65 | var typeName = constructorMatch[1];
|
66 | var parameterList = constructorMatch[2];
|
67 | if (typeName === name) {
|
68 | var parameters = parameterList.length ? parameterList
|
69 | .split(',')
|
70 | .map(function (parameter) {
|
71 | var parameterMatch = parameter.trim().match(/([^\s]+)\s+([\w\d]+)(?:\s+=\s+((?:[\w\d]+)|(?:"[\w\d ]+")))?/);
|
72 | var parameterType = CSharpParser.parseMemberTypeName(parameterMatch[1]);
|
73 | var parameterName = parameterMatch[2];
|
74 | var rawDefaultParameter = parameterMatch[3];
|
75 | var defaultValue = rawDefaultParameter && rawDefaultParameter.startsWith('"') && rawDefaultParameter.endsWith('"') ?
|
76 | "'" + rawDefaultParameter.substr(1, rawDefaultParameter.length - 2) + "'" : rawDefaultParameter;
|
77 | return new c_sharp_objects_1.CSharpParameter(parameterType, parameterName, defaultValue);
|
78 | }) : [];
|
79 | constructors.push(new c_sharp_objects_1.CSharpContructor(parameters));
|
80 | }
|
81 | }
|
82 | var properties = CSharpParser.getProperties(body, false);
|
83 | return new c_sharp_objects_1.CSharpClassOrStructOrInterface(namespace, name, inherits, constructors, properties);
|
84 | };
|
85 | CSharpParser.parseInterface = function (namespace, name, inherits, body) {
|
86 | var properties = CSharpParser.getProperties(body, true);
|
87 | return new c_sharp_objects_1.CSharpClassOrStructOrInterface(namespace, name, inherits, null, properties);
|
88 | };
|
89 | CSharpParser.parseEnum = function (namespace, name, inherits, body) {
|
90 | var entries = [];
|
91 | var entryMatch;
|
92 | var entryRegex = /^\s*([\w\d_]+)\s*=?\s*((?:0x|-|~)?[0-9A-Za-z_]+)?,?\s*$/gm;
|
93 | var getNextEntryMatch = function () { return entryMatch = entryRegex.exec(body); };
|
94 | while (getNextEntryMatch() !== null) {
|
95 | var entryName = entryMatch[1];
|
96 | var entryValue = entryMatch[2];
|
97 | if (entryName.indexOf('[') === -1) {
|
98 | entries.push(new c_sharp_objects_1.CSharpEnumEntry(entryName, entryValue));
|
99 | }
|
100 | }
|
101 | return new c_sharp_objects_1.CSharpEnum(namespace, name, inherits, entries);
|
102 | };
|
103 | CSharpParser.parseMemberTypeName = function (typeName) {
|
104 | var lastNameRegex = /\.?([\w<>\[\]\?]+)$/;
|
105 | var nullableRegex = /(?:^Nullable<([^\s]+)>$)|(?:^([^\s]+)\?$)/;
|
106 | var nullableMatch = nullableRegex.exec(typeName);
|
107 | if (nullableMatch) {
|
108 | typeName = (nullableMatch[1] || nullableMatch[2]);
|
109 | }
|
110 | var collectionRegex = /(?:List|IEnumerable)<([\w\d\.]+)>$|(?:^([\w\d\.]+)\[\]$)/;
|
111 | var collectionMatch = collectionRegex.exec(typeName);
|
112 | if (collectionMatch) {
|
113 | typeName = (collectionMatch[1] || collectionMatch[2]);
|
114 | }
|
115 | typeName = typeName.match(lastNameRegex)[1];
|
116 | return new c_sharp_objects_1.CSharpMemberType(typeName, !!nullableMatch, !!collectionMatch);
|
117 | };
|
118 | CSharpParser.getProperties = function (body, isInterface) {
|
119 | var properties = [];
|
120 | var propertyMatch;
|
121 | var propertyRegex = isInterface ? /\s*([^\s]+)\s*([\w\d]+)\s*{\s*get/gm : /public\s*(?:virtual|abstract|override)?\s*([^\s]+)\s*([\w\d]+)\s*{\s*get/gm;
|
122 | var getNextPropertyMatch = function () { return propertyMatch = propertyRegex.exec(body); };
|
123 | while (getNextPropertyMatch() !== null) {
|
124 | var typeName = propertyMatch[1];
|
125 | var propertyName = propertyMatch[2];
|
126 | var memberType = CSharpParser.parseMemberTypeName(typeName);
|
127 | properties.push(new c_sharp_objects_1.CSharpProperty(memberType, propertyName));
|
128 | }
|
129 | return properties;
|
130 | };
|
131 | return CSharpParser;
|
132 | }());
|
133 | exports.CSharpParser = CSharpParser;
|
134 |
|
\ | No newline at end of file |