├── .gitattributes
├── .gitignore
├── OutputTry
├── Bridge
│ ├── bridge.json
│ ├── output
│ │ ├── bridge.js
│ │ ├── bridge.min.js
│ │ └── readme.txt
│ └── www
│ │ └── demo.html
├── OutputTry.csproj
├── Properties
│ └── AssemblyInfo.cs
└── packages.config
├── README.md
├── TypeScriptToCS.sln
└── TypeScriptToCS
├── App.config
├── ClassDefinition.cs
├── ClassElement.cs
├── ComplexType.cs
├── EnumDefinition.cs
├── Field.cs
├── ImplicitMethod.cs
├── Method.cs
├── NamespaceDefinition.cs
├── Program.cs
├── Properties
└── AssemblyInfo.cs
├── Property.cs
├── TypeAndName.cs
├── TypeDefinition.cs
├── TypeNameAndOptional.cs
├── TypeNameOptionalAndParams.cs
├── TypeScriptToCS.csproj
├── TypeType.cs
└── TypescriptFileReader.cs
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | [Xx]64/
19 | [Xx]86/
20 | [Bb]uild/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | artifacts/
46 |
47 | *_i.c
48 | *_p.c
49 | *_i.h
50 | *.ilk
51 | *.meta
52 | *.obj
53 | *.pch
54 | *.pdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *.log
65 | *.vspscc
66 | *.vssscc
67 | .builds
68 | *.pidb
69 | *.svclog
70 | *.scc
71 |
72 | # Chutzpah Test files
73 | _Chutzpah*
74 |
75 | # Visual C++ cache files
76 | ipch/
77 | *.aps
78 | *.ncb
79 | *.opendb
80 | *.opensdf
81 | *.sdf
82 | *.cachefile
83 | *.VC.db
84 |
85 | # Visual Studio profiler
86 | *.psess
87 | *.vsp
88 | *.vspx
89 | *.sap
90 |
91 | # TFS 2012 Local Workspace
92 | $tf/
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 | *.DotSettings.user
101 |
102 | # JustCode is a .NET coding add-in
103 | .JustCode
104 |
105 | # TeamCity is a build add-in
106 | _TeamCity*
107 |
108 | # DotCover is a Code Coverage Tool
109 | *.dotCover
110 |
111 | # NCrunch
112 | _NCrunch_*
113 | .*crunch*.local.xml
114 | nCrunchTemp_*
115 |
116 | # MightyMoose
117 | *.mm.*
118 | AutoTest.Net/
119 |
120 | # Web workbench (sass)
121 | .sass-cache/
122 |
123 | # Installshield output folder
124 | [Ee]xpress/
125 |
126 | # DocProject is a documentation generator add-in
127 | DocProject/buildhelp/
128 | DocProject/Help/*.HxT
129 | DocProject/Help/*.HxC
130 | DocProject/Help/*.hhc
131 | DocProject/Help/*.hhk
132 | DocProject/Help/*.hhp
133 | DocProject/Help/Html2
134 | DocProject/Help/html
135 |
136 | # Click-Once directory
137 | publish/
138 |
139 | # Publish Web Output
140 | *.[Pp]ublish.xml
141 | *.azurePubxml
142 |
143 | # TODO: Un-comment the next line if you do not want to checkin
144 | # your web deploy settings because they may include unencrypted
145 | # passwords
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # NuGet Packages
150 | *.nupkg
151 | # The packages folder can be ignored because of Package Restore
152 | **/packages/*
153 | # except build/, which is used as an MSBuild target.
154 | !**/packages/build/
155 | # Uncomment if necessary however generally it will be regenerated when needed
156 | #!**/packages/repositories.config
157 | # NuGet v3's project.json files produces more ignoreable files
158 | *.nuget.props
159 | *.nuget.targets
160 |
161 | # Microsoft Azure Build Output
162 | csx/
163 | *.build.csdef
164 |
165 | # Microsoft Azure Emulator
166 | ecf/
167 | rcf/
168 |
169 | # Microsoft Azure ApplicationInsights config file
170 | ApplicationInsights.config
171 |
172 | # Windows Store app package directory
173 | AppPackages/
174 | BundleArtifacts/
175 |
176 | # Visual Studio cache files
177 | # files ending in .cache can be ignored
178 | *.[Cc]ache
179 | # but keep track of directories ending in .cache
180 | !*.[Cc]ache/
181 |
182 | # Others
183 | ClientBin/
184 | [Ss]tyle[Cc]op.*
185 | ~$*
186 | *~
187 | *.dbmdl
188 | *.dbproj.schemaview
189 | *.pfx
190 | *.publishsettings
191 | node_modules/
192 | orleans.codegen.cs
193 |
194 | # RIA/Silverlight projects
195 | Generated_Code/
196 |
197 | # Backup & report files from converting an old project file
198 | # to a newer Visual Studio version. Backup files are not needed,
199 | # because we have git ;-)
200 | _UpgradeReport_Files/
201 | Backup*/
202 | UpgradeLog*.XML
203 | UpgradeLog*.htm
204 |
205 | # SQL Server files
206 | *.mdf
207 | *.ldf
208 |
209 | # Business Intelligence projects
210 | *.rdl.data
211 | *.bim.layout
212 | *.bim_*.settings
213 |
214 | # Microsoft Fakes
215 | FakesAssemblies/
216 |
217 | # GhostDoc plugin setting file
218 | *.GhostDoc.xml
219 |
220 | # Node.js Tools for Visual Studio
221 | .ntvs_analysis.dat
222 |
223 | # Visual Studio 6 build log
224 | *.plg
225 |
226 | # Visual Studio 6 workspace options file
227 | *.opt
228 |
229 | # Visual Studio LightSwitch build output
230 | **/*.HTMLClient/GeneratedArtifacts
231 | **/*.DesktopClient/GeneratedArtifacts
232 | **/*.DesktopClient/ModelManifest.xml
233 | **/*.Server/GeneratedArtifacts
234 | **/*.Server/ModelManifest.xml
235 | _Pvt_Extensions
236 |
237 | # LightSwitch generated files
238 | GeneratedArtifacts/
239 | ModelManifest.xml
240 |
241 | # Paket dependency manager
242 | .paket/paket.exe
243 |
244 | # FAKE - F# Make
245 | .fake/
--------------------------------------------------------------------------------
/OutputTry/Bridge/bridge.json:
--------------------------------------------------------------------------------
1 | {
2 | "output": "Bridge/output"
3 |
4 | // Uncomment to add TypeScript Definition file (.d.ts) generation to your project.
5 | // , "generateTypeScript": true
6 |
7 | // More options available at:
8 | // http://bridge.net/kb/global-configuration
9 | }
--------------------------------------------------------------------------------
/OutputTry/Bridge/output/readme.txt:
--------------------------------------------------------------------------------
1 | NOTE: Your Bridge.NET JavaScript files are in this
2 | directory, but Visual Studio may be hiding.
3 |
4 | Click on the "Show All Files" button within your
5 | Visual Studio Solution Explorer panel, or browse
6 | directly to this folder within the file system.
7 |
8 | ------------------------------------------------
9 |
10 | Please see /App_Readme/Bridge.NET/README.txt for
11 | more information about this release.
12 |
13 | ------------------------------------------------
14 |
15 | A copy of the Bridge.NET License copied to
16 | /App_Readme/Bridge.NET/LICENSE.txt
--------------------------------------------------------------------------------
/OutputTry/Bridge/www/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Bridge OutputTry
7 |
8 |
9 |
10 |
11 |
14 |
15 |
--------------------------------------------------------------------------------
/OutputTry/OutputTry.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | 2.0
8 | {5B2738C0-6C56-4F94-863D-ED4F830A26C6}
9 | Library
10 | Properties
11 | OutputTry
12 | OutputTry
13 | v4.5.2
14 | 512
15 |
16 |
17 |
18 |
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 | true
27 |
28 |
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 | true
36 |
37 |
38 |
39 | extras.cs
40 |
41 |
42 | three.new.cs
43 |
44 |
45 | try.cs
46 |
47 |
48 |
49 |
50 |
51 | ..\packages\Bridge.Core.1.14.0\lib\net40\Bridge.dll
52 | True
53 |
54 |
55 | ..\packages\Bridge.Html5.1.14.0\lib\net40\Bridge.Html5.dll
56 | True
57 |
58 |
59 | ..\packages\Bridge.WebGL.1.8.1\lib\net40\Bridge.WebGL.dll
60 | True
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/OutputTry/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("OutputTry")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("OutputTry")]
13 | [assembly: AssemblyCopyright("Copyright 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("5b2738c0-6c56-4f94-863d-ed4f830a26c6")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
--------------------------------------------------------------------------------
/OutputTry/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | See my new repository [TypescriptToCS](https://github.com/michaelcheers/TypescriptToCS2).
2 |
--------------------------------------------------------------------------------
/TypeScriptToCS.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptToCS", "TypeScriptToCS\TypeScriptToCS.csproj", "{4B4BE0B4-7DB8-4F75-9532-0AC1096AD6F0}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OutputTry", "OutputTry\OutputTry.csproj", "{5B2738C0-6C56-4F94-863D-ED4F830A26C6}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {4B4BE0B4-7DB8-4F75-9532-0AC1096AD6F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {4B4BE0B4-7DB8-4F75-9532-0AC1096AD6F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {4B4BE0B4-7DB8-4F75-9532-0AC1096AD6F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {4B4BE0B4-7DB8-4F75-9532-0AC1096AD6F0}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {5B2738C0-6C56-4F94-863D-ED4F830A26C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {5B2738C0-6C56-4F94-863D-ED4F830A26C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {5B2738C0-6C56-4F94-863D-ED4F830A26C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {5B2738C0-6C56-4F94-863D-ED4F830A26C6}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/TypeScriptToCS/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/TypeScriptToCS/ClassDefinition.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class ClassDefinition : TypeDefinition
10 | {
11 | public List methods = new List();
12 | //public List properties = new List();
13 | public List fields = new List();
14 | public List extends = new List();
15 | public bool @abstract;
16 | public bool @static;
17 | public string name { get; set; }
18 | public TypeType type;
19 | public Dictionary> typeWheres = new Dictionary>();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/TypeScriptToCS/ClassElement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class ClassElement
10 | {
11 | public bool @static;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/TypeScriptToCS/ComplexType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class ComplexType
10 | {
11 | public List complexPart = new List();
12 |
13 | public class ComplexPart
14 | {
15 | public int numberOfArrAtEnd;
16 | }
17 |
18 | public class NormalComplexType : ComplexPart
19 | {
20 | public List genericParameters;
21 | public string typeName;
22 |
23 | public class Part
24 | {
25 | public string typeName;
26 | public List implements;
27 | }
28 | }
29 |
30 | public class FunctionType : ComplexPart
31 | {
32 | public ComplexType returnType;
33 | public Parameter[] parameters;
34 |
35 | public class Parameter
36 | {
37 | public ComplexType type;
38 | public string parameterName;
39 | }
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/TypeScriptToCS/EnumDefinition.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class EnumDefinition : TypeDefinition
10 | {
11 | public List members = new List();
12 | public string name { get; set; }
13 | public Emit emit = Emit.Name;
14 |
15 | public enum Emit
16 | {
17 | Name,
18 | Value,
19 | StringName,
20 | StringNamePreserveCase,
21 | StringNameLowerCase,
22 | StringNameUpperCase,
23 | NamePreserveCase,
24 | NameLowerCase,
25 | NameUpperCase
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/TypeScriptToCS/Field.cs:
--------------------------------------------------------------------------------
1 | namespace TypeScriptToCS
2 | {
3 | public class Field : ClassElement
4 | {
5 | public TypeNameAndOptional typeAndName = new TypeNameAndOptional();
6 |
7 | public ClassDefinition from;
8 |
9 | public string CSName
10 | {
11 | get
12 | {
13 | if (string.IsNullOrEmpty(name))
14 | return string.Empty;
15 | return Program.ChangeName((char.ToUpper(name[0]) + name.Substring(1)).Replace("$", "DollarSign"));
16 | }
17 | }
18 |
19 | public string name => typeAndName.name;
20 | }
21 | }
--------------------------------------------------------------------------------
/TypeScriptToCS/ImplicitMethod.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | class ImplicitMethod : Method
10 | {
11 | public string @interface;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/TypeScriptToCS/Method.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace TypeScriptToCS
4 | {
5 | public class Method : ClassElement, TypeDefinition
6 | {
7 | public TypeAndName typeAndName = new TypeAndName();
8 |
9 | public List parameters = new List();
10 |
11 | public Method Clone() => (Method)MemberwiseClone();
12 |
13 | public Dictionary typeWheres = new Dictionary();
14 |
15 | public string name { get { return typeAndName.name.Replace("$", "DollarSign"); } set { typeAndName.name = value; } }
16 | public string CSName { get {
17 | if (string.IsNullOrEmpty(typeAndName.name))
18 | return string.Empty;
19 | return (char.ToUpper(typeAndName.name[0]) + typeAndName.name.Substring(1)).Replace("$", "DollarSign"); } }
20 | public bool Dollar => typeAndName.name.Contains("$");
21 | public char StartBracket => indexer ? '[' : '(';
22 | public char EndBracket => indexer ? ']' : ')';
23 | public bool indexer;
24 | public ClassDefinition from;
25 | }
26 | }
--------------------------------------------------------------------------------
/TypeScriptToCS/NamespaceDefinition.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class NamespaceDefinition
10 | {
11 | public List typeDefinitions = new List();
12 |
13 | public string name;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/TypeScriptToCS/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.CSharp;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Text.RegularExpressions;
8 | using System.Threading.Tasks;
9 |
10 | namespace TypeScriptToCS
11 | {
12 | static class Program
13 | {
14 | static string GetTypeWithOutGenerics (string value)
15 | {
16 | var index = value.IndexOf('<');
17 | if (index == -1)
18 | return value;
19 | else
20 | return value.Substring(0, index);
21 | }
22 |
23 | static void Main(string[] args)
24 | {
25 | Console.WriteLine("Typescript file location?");
26 | string tsFileLocation = Console.ReadLine();
27 | string tsFile = "";
28 |
29 | ReadFile:
30 | try
31 | {
32 | tsFile = File.ReadAllText(tsFileLocation);
33 | }
34 | catch (Exception e)
35 | {
36 | Console.WriteLine(e + " occured when trying to read file. Press enter to exit. Press t to retry.");
37 | ConsoleKey key;
38 | while ((key = Console.ReadKey(true).Key) != ConsoleKey.Enter)
39 | if (key == ConsoleKey.R)
40 | throw;
41 | else if (key == ConsoleKey.T)
42 | goto ReadFile;
43 | Environment.Exit(0);
44 | }
45 |
46 | int index = 0;
47 | ReadTypeScriptFile(tsFile, ref index, nameSpaceDefinitions);
48 |
49 | string endFile = "using System;\nusing Bridge;\nusing Bridge.Html5;\nusing Bridge.WebGL;\nusing any = System.Object;\nusing boolean = System.Boolean;\nusing Function = System.Delegate;\nusing RegExp = Bridge.Text.RegularExpressions.Regex;\nusing number = System.Double;\nusing Number = System.Double;\n\n\n";
50 |
51 | foreach (var namespaceItem in nameSpaceDefinitions)
52 | {
53 | if (string.IsNullOrEmpty(namespaceItem.name))
54 | continue;
55 | if (namespaceItem.typeDefinitions.Count == 1)
56 | {
57 | if (namespaceItem.typeDefinitions[0].name == "GlobalClass")
58 | {
59 | namespaceItem.typeDefinitions[0].name = namespaceItem.name;
60 | (namespaceItem.typeDefinitions[0] as ClassDefinition).@static = true;
61 | ProcessTypeDefinition(namespaceItem.typeDefinitions[0], ref endFile, namespaceItem);
62 | continue;
63 | }
64 | }
65 | if ((namespaceItem.name ?? "") != "")
66 | endFile += $"namespace {namespaceItem.name}\n{ "{" }\n";
67 |
68 | List names = new List();
69 | foreach (var rItem in namespaceItem.typeDefinitions)
70 | {
71 | if (!names.Contains(rItem.name))
72 | {
73 | names.Add(rItem.name);
74 | ProcessTypeDefinition(rItem, ref endFile, namespaceItem);
75 | }
76 | }
77 |
78 | if ((namespaceItem.name ?? "") != "")
79 | endFile += "\n}\n";
80 | }
81 |
82 | Console.WriteLine("Choose a file name...");
83 | File.WriteAllText(Console.ReadLine(), endFile);
84 | }
85 |
86 | static List nameSpaceDefinitions = new List();
87 |
88 | public static void ProcessTypeDefinition (TypeDefinition rItem, ref string endFile, NamespaceDefinition @namespace)
89 | {
90 | if (rItem is ClassDefinition)
91 | {
92 | ClassDefinition classItem = (ClassDefinition)rItem;
93 | if (classItem.name == "GlobalClass" && classItem.fields.Count == 0 && classItem.methods.Count == 0/* && classItem.properties.Count == 0*/)
94 | return;
95 | string extendString = classItem.extends.Count != 0 ? " : " : string.Empty;
96 |
97 | List fields;
98 | List methods;
99 | if (classItem.type == TypeType.@class && !classItem.extends.All(v =>
100 | {
101 | var type = FindType(v) as ClassDefinition;
102 | if (type == null)
103 | return false;
104 | else
105 | return type.type == TypeType.@class;
106 | }))
107 | GetMethodsAndFields(classItem, out fields, out methods);
108 | else
109 | {
110 | fields = new List(classItem.fields);
111 | methods = new List(classItem.methods);
112 | }
113 |
114 | if (classItem.type == TypeType.@interface && !(classItem.fields.Count == 0 && classItem.methods.Count == 0/* && classItem.properties.Count == 0*/))
115 | {
116 | List mFields;
117 | List mMethods;
118 | GetMethodsAndFields(classItem, out mFields, out mMethods);
119 | endFile += $"\t[ObjectLiteral]\n\tpublic class JSON{classItem.name} : {classItem.name}\n\t{"{"}";
120 | List fieldNames = new List();
121 | List methodsDone = new List();
122 | foreach (var item in mFields)
123 | if (!fieldNames.Contains(item.typeAndName.name))
124 | {
125 | fieldNames.Add(item.typeAndName.name);
126 | endFile += $"\n#pragma warning disable CS0626\n\t\tpublic extern {item.typeAndName.type.Replace("$", "DollarSign")}{item.typeAndName.OptionalString} {item.CSName}" + " { get; set; }\n#pragma warning restore CS0626";
127 | }
128 | foreach (var item in mMethods)
129 | {
130 | if (methodsDone.Any(v => item.typeAndName.name == v.typeAndName.name))
131 | continue;
132 | var itemClone = item.Clone();
133 | itemClone.typeAndName = itemClone.typeAndName.Clone();
134 | if (itemClone.typeAndName.name == "")
135 | itemClone.typeAndName.name = classItem.name + "IndexerDelegate";
136 | else
137 | itemClone.typeAndName.name += "Delegate";
138 | endFile += "\n";
139 | ProcessTypeDefinition(itemClone, ref endFile, @namespace);
140 | endFile += $"\n#pragma warning disable CS0626\n\t\tpublic extern {item.typeAndName.type.Replace("$", "DollarSign")} " + (item.indexer ? "this" : item.CSName) + $" {item.StartBracket}" + string.Join(", ", item.parameters.ConvertAll(v => (v.@params ? "params " : string.Empty) + v.type + " " + ChangeName(v.name) + (v.optional ? " = default(" + v.type + ")" : ""))) + item.EndBracket + (item.indexer ? " { get; set; }" : ";") + "\n#pragma warning restore CS0626";
141 | endFile += "\n#pragma warning disable CS0626\n\t\tpublic extern ";
142 | endFile += itemClone.name;
143 | endFile += " ";
144 | if (string.IsNullOrEmpty(item.typeAndName.name))
145 | endFile += "indexer";
146 | else
147 | endFile += ChangeName(char.ToLower(item.CSName[0]) + item.CSName.Substring(1));
148 | endFile += " { get; set; }\n#pragma warning restore CS0626";
149 | methodsDone.Add(item);
150 | }
151 | endFile += "\n\t}\n";
152 | }
153 |
154 | string abstractString = classItem.@abstract ? "abstract " : string.Empty;
155 | string staticString = classItem.@static ? "static " : string.Empty;
156 |
157 | endFile += "\t[External]" + (classItem.name == "GlobalClass" ? $"\n\t[Name(\"{@namespace.name}\")]" : "") + $"\n\tpublic {staticString}{abstractString}{classItem.type} {ChangeName(classItem.name)}{extendString}{string.Join(", ", classItem.extends.ConvertAll(GetType)) + GetWhereString(classItem.typeWheres) + "\n\t{"}";
158 |
159 | string interfacePublic = classItem.type != TypeType.@interface ? "extern " : string.Empty;
160 | string pragmaStart = (classItem.type == TypeType.@class ? "\n#pragma warning disable CS0626" : string.Empty);
161 | string pragmaEnd = (classItem.type == TypeType.@class ? "\n#pragma warning restore CS0626" : string.Empty);
162 |
163 | foreach (var item in fields)
164 | endFile += pragmaStart + "\n\t\t[FieldProperty]\n\t\t" + (char.IsUpper(item.typeAndName.name[0]) ? "[Name(false)]\n\t\t" : "") + (classItem.type != TypeType.@interface ? "public " : string.Empty) + $"{interfacePublic}" + (item.@static || classItem.name == "GlobalClass" ? "static " : "") + $"{item.typeAndName.type}{item.typeAndName.OptionalString} {item.CSName}" + " { get; set; }" + pragmaEnd;
165 |
166 | List methoders = new List();
167 | foreach (var item in methods)
168 | if (!methoders.Any(v => v.typeAndName.name == item.typeAndName.name && v.parameters.SequenceEqual(item.parameters) && v.typeAndName.type == item.typeAndName.type))
169 | {
170 | methoders.Add(item);
171 | if (item.typeAndName.name == "constructor")
172 | endFile += $"\n#pragma warning disable CS0824\n\t\tpublic extern {GetTypeWithOutGenerics(classItem.name)} (" + string.Join(", ", item.parameters.ConvertAll(v => (v.@params ? "params " : string.Empty) + v.type + " " + ChangeName(v.name) + (v.optional ? $" = default({v.type})" : string.Empty))) + ");\n#pragma warning restore CS0824";
173 | else
174 | endFile += pragmaStart + "\n\t\t" + (item.Dollar ? $"[Name(\"{item.typeAndName.name}\")]\n\t\t" : string.Empty) + (((!(item is ImplicitMethod)) && classItem.type != TypeType.@interface) ? "public " : string.Empty) + interfacePublic + (item.@static || classItem.name == "GlobalClass" ? "static " : "") + item.typeAndName.type + " " + (item.indexer ? "this" : ((item is ImplicitMethod ? ((item as ImplicitMethod).@interface + ".") : ""))) + $"{item.CSName} {item.StartBracket}" + string.Join(", ", item.parameters.ConvertAll(v => (v.@params ? "params " : string.Empty) + v.type + " " + ChangeName(v.name) + (v.optional && !(item is ImplicitMethod) ? $" = default({v.type})" : string.Empty))) + $"{item.EndBracket}{GetWhereString(item.typeWheres)}" + (item.indexer ? " { get; set; }" : ";") + pragmaEnd;
175 | }
176 | if (classItem.type == TypeType.@class && !classItem.@static && !classItem.methods.Any(v => v.typeAndName.name == "constructor") && classItem.name != "GlobalClass")
177 | endFile += $"\n#pragma warning disable CS0824\n\t\textern {GetTypeWithOutGenerics(classItem.name)} ();\n#pragma warning restore CS0824";
178 | /*foreach (var item in classItem.properties)
179 | endFile += "\n\t\tpublic " + (item.@static ? "static " : "") + $"extern {item.typeAndName.type} {char.ToUpper(item.typeAndName.name[0])}{item.typeAndName.name.Substring(1)}" + "{ " + (item.get ? "get; " : "") + (item.set ? "set; " : "") + "}";*/
180 | }
181 | else if (rItem is EnumDefinition)
182 | {
183 | EnumDefinition enumItem = (EnumDefinition)rItem;
184 | string emitString = enumItem.emit == EnumDefinition.Emit.Value ? "" : "[Enum(Emit." + enumItem.emit + ")]\n\t";
185 | endFile += $"\t[External]\n\t{emitString}public enum {ChangeName(enumItem.name) + "\n\t{"}\n\t\t{string.Join(",\n\t\t", enumItem.members.ConvertAll(ChangeName))}";
186 | }
187 | else if (rItem is Method)
188 | {
189 | Method item = (Method)rItem;
190 | endFile += $"\t[External]\n\tpublic delegate {item.typeAndName.type} {item.name} (" + string.Join(", ", item.parameters.ConvertAll(v => (v.@params ? "params " : string.Empty) + v.type + " " + ChangeName(v.name) + (v.optional ? $" = default({v.type})" : ""))) + ");\n";
191 | return;
192 | }
193 |
194 | endFile += "\n\t}\n";
195 | }
196 |
197 | static void GetMethodsAndFields (ClassDefinition classItem, out List fields, out List methods)
198 | {
199 | fields = new List(classItem.fields);
200 | methods = new List(classItem.methods);
201 | var extends = GetExtends(classItem);
202 | foreach (var item in extends)
203 | {
204 | if (item.type == TypeType.@interface)
205 | {
206 | foreach (var fieldItem in item.fields)
207 | if (!fields.Any(v => v.typeAndName.name == fieldItem.typeAndName.name))
208 | fields.Add(fieldItem);
209 | foreach (var methodItem in item.methods)
210 | if (!methods.Any(v => v.typeAndName.name == methodItem.typeAndName.name && v.typeAndName.type == methodItem.typeAndName.type && v.parameters.SequenceEqual(methodItem.parameters)))
211 | if (classItem.methods.Any(v => v.typeAndName.name == methodItem.typeAndName.name))
212 | methods.Add(new ImplicitMethod
213 | {
214 | name = methodItem.name,
215 | @interface = item.name,
216 | parameters = methodItem.parameters,
217 | @static = methodItem.@static,
218 | typeAndName = methodItem.typeAndName,
219 | typeWheres = methodItem.typeWheres
220 | });
221 | else
222 | methods.Add(methodItem);
223 | }
224 | }
225 | }
226 |
227 | static string GetWhereString (KeyValuePair value) => $"where {value.Key} : {value.Value}";
228 | static string GetWhereString(Dictionary value)
229 | {
230 | if (value.Count == 0) return "";
231 | else
232 | {
233 | string result = "";
234 | foreach (var item in value)
235 | result += " " + GetWhereString(item);
236 | return result;
237 | }
238 | }
239 |
240 | public static List GetExtends(TypeDefinition definition)
241 | {
242 | List result = new List();
243 | ClassDefinition definitionClass = definition as ClassDefinition;
244 | if (definitionClass != null)
245 | {
246 | foreach (var item in definitionClass.extends)
247 | {
248 | var type = FindType(item) as ClassDefinition;
249 | if (type != null)
250 | {
251 | result.AddRange(GetExtends(type));
252 | result.Add(type);
253 | }
254 | }
255 | }
256 | return result;
257 | }
258 |
259 | public static TypeDefinition FindType (string name)
260 | {
261 | foreach (var @namespace in nameSpaceDefinitions)
262 | foreach (var type in @namespace.typeDefinitions)
263 | {
264 | var tName = type.name;
265 | if (type is Method)
266 | tName = (type as Method).typeAndName.name;
267 | if (tName == name)
268 | return type;
269 | }
270 | return null;
271 | }
272 |
273 | public static string GetType (string value)
274 | {
275 | if (value.Length > 1)
276 | if (value.EndsWith("]") && value[value.Length - 2] != '[') value = value.Substring(0, value.Length - 1);
277 | if (value.StartsWith("Array<"))
278 | return GetType(value.Substring(6, value.Length - 7) + "[]");//Array
279 | else if (value.EndsWith("[]"))
280 | return GetType(value.Substring(0, value.Length - 2)) + "[]";
281 | return value;
282 | }
283 |
284 | private static void ReadTypeScriptFile(string tsFile, ref int index, List namespaces)
285 | {
286 |
287 | }
288 |
289 | public static bool ReadFunctionType (string tsFile, ref int index, ref string outputType, string delegateName, List typeTop, List namespaceTop, NamespaceDefinition global)
290 | {
291 | }
292 |
293 | public static string ChangeName (string name)
294 | {
295 | return new CSharpCodeProvider().CreateEscapedIdentifier(name);
296 | }
297 | }
298 | }
299 |
--------------------------------------------------------------------------------
/TypeScriptToCS/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("TypeScriptToCS")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("TypeScriptToCS")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("4b4be0b4-7db8-4f75-9532-0ac1096ad6f0")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/TypeScriptToCS/Property.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class Property : ClassElement
10 | {
11 | public TypeAndName typeAndName = new TypeAndName();
12 | public bool get;
13 | public bool set;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/TypeScriptToCS/TypeAndName.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace TypeScriptToCS
4 | {
5 | public class TypeAndName
6 | {
7 | public string type { get { return Program.GetType(_type); } set { _type = value; } }
8 | protected string _type;
9 | public string name;
10 |
11 | public TypeAndName Clone() => (TypeAndName)MemberwiseClone();
12 |
13 | public override bool Equals(object obj)
14 | {
15 | var tObj = obj as TypeAndName;
16 | if (tObj == null)
17 | return false;
18 | return tObj._type == _type && tObj.name == name;
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/TypeScriptToCS/TypeDefinition.cs:
--------------------------------------------------------------------------------
1 | namespace TypeScriptToCS
2 | {
3 | public interface TypeDefinition
4 | {
5 | string name { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/TypeScriptToCS/TypeNameAndOptional.cs:
--------------------------------------------------------------------------------
1 | namespace TypeScriptToCS
2 | {
3 | public class TypeNameAndOptional : TypeAndName
4 | {
5 | public bool optional;
6 | public string OptionalString => type == "string" ? "" : (optional ? "?" : "");
7 |
8 | public override bool Equals(object obj)
9 | {
10 | var tObj = obj as TypeNameAndOptional;
11 | if (tObj == null)
12 | return false;
13 | return base.Equals(tObj) && optional == tObj.optional;
14 | }
15 | }
16 | }
--------------------------------------------------------------------------------
/TypeScriptToCS/TypeNameOptionalAndParams.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class TypeNameOptionalAndParams : TypeNameAndOptional
10 | {
11 | public bool @params;
12 |
13 | public override bool Equals(object obj)
14 | {
15 | var tObj = obj as TypeNameOptionalAndParams;
16 | if (tObj == null)
17 | return false;
18 | return base.Equals(tObj) && @params == tObj.@params;
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/TypeScriptToCS/TypeScriptToCS.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {4B4BE0B4-7DB8-4F75-9532-0AC1096AD6F0}
8 | Exe
9 | Properties
10 | TypeScriptToCS
11 | TypeScriptToCS
12 | v4.5.2
13 | 512
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
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 |
75 |
--------------------------------------------------------------------------------
/TypeScriptToCS/TypeType.cs:
--------------------------------------------------------------------------------
1 | namespace TypeScriptToCS
2 | {
3 | public enum TypeType
4 | {
5 | @class,
6 | @interface
7 | }
8 | }
--------------------------------------------------------------------------------
/TypeScriptToCS/TypescriptFileReader.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TypeScriptToCS
8 | {
9 | public class TypescriptFileReader
10 | {
11 | public TypescriptFileReader ()
12 | {
13 |
14 | }
15 |
16 | public string tsFile;
17 | int index;
18 | char Current => End ? '\0' : tsFile[index];
19 |
20 | private string SkipToEndOfWord()
21 | {
22 | SkipEmpty();
23 | if (!char.IsLetter(tsFile, index))
24 | SkipEmpty();
25 | if (tsFile[index] == '[')
26 | return string.Empty;
27 | string result = "";
28 | for (; !End; index++)
29 | {
30 | if (char.IsLetterOrDigit(Current) || Current == '_' || Current == '.' || Current == '$')
31 | result += Current;
32 | else
33 | {
34 | if (result.EndsWith("]") && !result.EndsWith("[]"))
35 | {
36 | GoForward(-1);
37 | result = result.Substring(0, result.Length - 1);
38 | }
39 | return result;
40 | }
41 | }
42 | SkipEmpty();
43 | return result;
44 | }
45 |
46 | private void SkipEmpty()
47 | {
48 | for (; !End; index++)
49 | {
50 | switch (tsFile[index])
51 | {
52 | case '\n':
53 | case '\r':
54 | case '\t':
55 | case ' ':
56 | break;
57 | default:
58 | return;
59 | }
60 | }
61 | }
62 |
63 | private void GotoNextOccurenceOf(string value) => index = NextOccurenceOf(value);
64 | private int NextOccurenceOf(string value) => tsFile.IndexOf(value, index);
65 | private bool End => tsFile.Length >= index;
66 | private void GoForward(int value) => index += value;
67 | private bool IfAt_Skip(char value) { bool result; if (result = Current == value) GoForward(1); return result; }
68 |
69 | private ComplexType ReadType()
70 | {
71 | ComplexType result = new ComplexType { complexPart = new List() };
72 | {
73 | ComplexType.NormalComplexType type;
74 | if (ReadNormalComplexType(out type))
75 | {
76 | result.complexPart.Add(type);
77 | goto Continue;
78 | }
79 | }
80 | {
81 | ComplexType.FunctionType type;
82 | if (ReadFunctionType(out type))
83 | {
84 | result.complexPart.Add(type);
85 | goto Continue;
86 | }
87 | }
88 | Continue:
89 | SkipEmpty();
90 | }
91 |
92 | public bool ReadFunctionType (out ComplexType.FunctionType result)
93 | {
94 | SkipEmpty();
95 | if (IfAt_Skip('('))
96 | {
97 |
98 | }
99 | return true;
100 | }
101 |
102 | public Method ParseMethod ()
103 | {
104 | Method method = new Method
105 | {
106 | typeWheres = ReadType(),
107 | @static = @static,
108 | indexer = Current == '[',
109 | from = typeTop.Last(v => v is ClassDefinition) as ClassDefinition
110 | };
111 | var startBracket = Current;
112 | var endBracket = Current == '[' ? ']' : ')';
113 | method.typeAndName.name = word;
114 |
115 | SkipEmpty();
116 | if (IfAt_Skip(endBracket))
117 | goto Break;
118 |
119 | for (; index < tsFile.Length; index++)
120 | {
121 | SkipEmpty();
122 | optional = false;
123 | bool @params = false;
124 | if (IfAt_Skip('.'))
125 | {
126 | GoForward(2);
127 | @params = true;
128 | }
129 |
130 | string word2 = SkipToEndOfWord();
131 |
132 | if (IfAt_Skip('?'))
133 | optional = true;
134 |
135 | SkipEmpty();
136 |
137 | switch (Current)
138 | {
139 | case ':':
140 | GoForward(1);
141 | SkipEmpty();
142 | bool bracketIn = Current == '{';
143 | int endBracketArrIndex = tsFile.IndexOf('}', index) + 1;
144 | string arr = "";
145 | if (bracketIn)
146 | while (tsFile[endBracketArrIndex] == '[')
147 | {
148 | arr += "[]";
149 | tsFile = tsFile.Remove(endBracketArrIndex, 2);
150 | }
151 | string type2 = null;
152 | if (bracketIn)
153 | type2 = word2 + "Interface" + arr;
154 | else if (!ReadFunctionType(tsFile, ref index, ref type2, method.typeAndName.name + "Param" + (method.parameters.Count + 1) + "Delegate", typeTop, namespaceTop, global))
155 | {
156 | List anys = new List();
157 | do
158 | {
159 | if (tsFile[index] == '\'' || tsFile[index] == '"')
160 | {
161 | SkipEmpty();
162 | anys.Add("string");
163 | index = tsFile.IndexOf(Current, index + 1) + 1;
164 | SkipEmpty();
165 | }
166 | else
167 | anys.Add(SkipToEndOfWord());
168 | }
169 | while (tsFile[index++] == '|');
170 | index--;
171 | type2 = string.Join(", ", anys);
172 | if (anys.Count > 1)
173 | type2 = "Any<" + type2 + ">";
174 | }
175 |
176 | method.parameters.Add(new TypeNameOptionalAndParams
177 | {
178 | optional = optional,
179 | @params = @params,
180 | name = word2,
181 | type = type2
182 | });
183 | SkipEmpty();
184 |
185 |
186 |
187 | if (bracketIn)
188 | {
189 | typeTop.Add(new ClassDefinition
190 | {
191 | type = TypeType.@interface,
192 | name = type2.Substring(0, type2.Length - arr.Length)
193 | });
194 | goto BracketLoop;
195 | }
196 |
197 | if (tsFile[index] != ',')
198 | goto case ')';
199 | }
200 |
201 | public bool ReadNormalComplexType (out ComplexType.NormalComplexType result)
202 | {
203 | string value = SkipToEndOfWord();
204 | result = null;
205 | if (value == "")
206 | return false;
207 | List genericParts = new List();
208 | if (IfAt_Skip('<'))
209 | while (!IfAt_Skip('>'))
210 | {
211 | string parameter = SkipToEndOfWord();
212 | ComplexType.NormalComplexType.Part part = new ComplexType.NormalComplexType.Part();
213 | part.typeName = parameter;
214 | if (!IfAt_Skip(','))
215 | {
216 | string word = SkipToEndOfWord();
217 | if (word != "extends" || word != "implements")
218 | throw new Exception("Word is not \"extends\" or \"implements\".");
219 | while (IfAt_Skip(','))
220 | part.implements.Add(ReadType());
221 | }
222 | genericParts.Add(part);
223 | }
224 | result = new ComplexType.NormalComplexType
225 | {
226 | typeName = value,
227 | genericParameters = genericParts
228 | };
229 | while (IfAt_Skip('['))
230 | if (!IfAt_Skip(']'))
231 | throw new Exception("Array declaration incorrect");
232 | return true;
233 | }
234 |
235 | public void Parse ()
236 | {
237 | List result = new List();
238 | List namespaceTop = new List();
239 | List typeTop = new List();
240 |
241 | while (!End)
242 | {
243 | SkipEmpty();
244 | while (IfAt_Skip('/'))
245 | {
246 | if (Current == '/')
247 | GotoNextOccurenceOf("\n");
248 | else if (Current == '*')
249 | {
250 | GotoNextOccurenceOf("*/");
251 | GoForward(2);
252 | }
253 | }
254 |
255 | SkipEmpty();
256 |
257 | if (End)
258 | return;
259 |
260 | BracketLoop:
261 | if (IfAt_Skip('}'))
262 | {
263 | if (typeTop.Count != 0)
264 | {
265 | if (namespaceTop.Count == 0)
266 | throw new Exception("You must put namespaces around types.");
267 |
268 | namespaceTop.Last().typeDefinitions.Add(typeTop.Last());
269 | typeTop.RemoveAt(typeTop.Count - 1);
270 | goto EndBracket;
271 | }
272 |
273 | if (namespaceTop.Count != 0)
274 | {
275 | result.Add(namespaceTop.Last());
276 | namespaceTop.RemoveAt(namespaceTop.Count - 1);
277 | }
278 | else
279 | throw new Exception("No more namespaces to end.");
280 | SkipEmpty();
281 | goto EndBracket;
282 | }
283 |
284 | if (IfAt_Skip('{'))
285 | {
286 | SkipEmpty();
287 | goto Continue;
288 | }
289 | else
290 | goto After;
291 |
292 | EndBracket:
293 | SkipEmpty();
294 | if (End) return;
295 | goto Continue;
296 | After:
297 | string word;
298 | bool @static = false;
299 | bool @abstract = false;
300 | bool optionalField = false;
301 | SkipEmpty();
302 | do
303 | {
304 | word = SkipToEndOfWord();
305 | switch (word)
306 | {
307 | case "static":
308 | case "function":
309 | case "var":
310 | case "let":
311 | case "const":
312 | @static = true;
313 | break;
314 | case "abstract":
315 | @abstract = true;
316 | break;
317 | }
318 | SkipEmpty();
319 | }
320 | while (word == "export" || word == "declare" || word == "static" || word == "function" || word == "var" || word == "const" || word == "abstract" || word == "let");
321 | var whereTypesExt = GenericRead(tsFile, ref index, ref word);
322 | switch (word)
323 | {
324 | case "type":
325 | {
326 | string name = SkipToEndOfWord();
327 | var wheres = GenericRead(tsFile, ref index, ref name);
328 | SkipEmpty();
329 | if (!IfAt_Skip('-'))
330 | {
331 | index--;
332 | goto default;
333 | }
334 | SkipEmpty();
335 | string type = default(string);
336 | if (!ReadFunctionType(tsFile, ref index, ref type, name, typeTop, namespaceTop, global))
337 | {
338 | List anys = new List();
339 | List strings = new List();
340 | do
341 | {
342 | if (tsFile[index] == '\'' || tsFile[index] == '"')
343 | {
344 | SkipEmpty();
345 | strings.Add(tsFile.Substring(index + 1, tsFile.IndexOf(tsFile[index], index + 1) - index - 1));
346 | anys.Add("string");
347 | index = tsFile.IndexOf(tsFile[index], index + 1) + 1;
348 | SkipEmpty();
349 | }
350 | else
351 | anys.Add(SkipToEndOfWord());
352 | }
353 | while (tsFile[index++] == '|');
354 | index--;
355 | if (strings.Count == anys.Count)
356 | {
357 | var typeDefinitions = namespaceTop.Count == 0 ? global.typeDefinitions : namespaceTop.Last().typeDefinitions;
358 | typeDefinitions.Add(new EnumDefinition
359 | {
360 | name = name,
361 | emit = EnumDefinition.Emit.StringNamePreserveCase,
362 | members = strings
363 | });
364 | type = name;
365 | break;
366 | }
367 | type = string.Join(", ", anys);
368 | if (anys.Count > 1)
369 | type = "Any<" + type + ">";
370 | }
371 | break;
372 | }
373 | case "class":
374 | case "interface":
375 | {
376 | var type = ReadType();
377 | typeTop.Add(new ClassDefinition
378 | {
379 | name = type.typeName,
380 | typeWheres = type.genericParameters.ToDictionary()
381 | });
382 | break;
383 | }
384 |
385 | case "enum":
386 | typeTop.Add(new EnumDefinition
387 | {
388 | name = SkipToEndOfWord()
389 | });
390 | break;
391 |
392 | case "module":
393 | case "namespace":
394 | if (tsFile[index] == '\'')
395 | index++;
396 | namespaceTop.Add(new NamespaceDefinition
397 | {
398 | name = SkipToEndOfWord()
399 | });
400 | if (string.IsNullOrEmpty(namespaceTop.Last().name))
401 | {
402 | namespaceTop.RemoveAt(namespaceTop.Count - 1);
403 | goto default;
404 | }
405 | ClassDefinition globalClass = new ClassDefinition
406 | {
407 | name = "GlobalClass",
408 | type = TypeType.@class,
409 | @static = true
410 | };
411 | Array.ForEach(new Action[] { typeTop.Add, namespaceTop.Last().typeDefinitions.Add }, v => v(globalClass));
412 | if (tsFile[index] == '\'')
413 | index++;
414 | break;
415 |
416 | default:
417 | bool optional = tsFile[index] == '?';
418 | if (optional)
419 | {
420 | index++;
421 | }
422 | SkipEmpty();
423 | switch (Current)
424 | {
425 | case ',':
426 | case '}':
427 | var enumCurrent = typeTop.Last() as EnumDefinition;
428 | if (enumCurrent != null)
429 | enumCurrent.members.Add(word);
430 |
431 | switch (Current)
432 | {
433 | case '}':
434 | index--;
435 | goto BracketLoop;
436 | case ',':
437 | goto After;
438 | default:
439 | break;
440 | }
441 | break;
442 |
443 | case ':':
444 | {
445 | string type = null;
446 | string arr = "";
447 | bool bracket = tsFile[index] == '{';
448 | int endBracketArrIndex = tsFile.IndexOf('}', index) + 1;
449 | if (bracket)
450 | while (tsFile[endBracketArrIndex] == '[')
451 | {
452 | arr += "[]";
453 | tsFile = tsFile.Remove(endBracketArrIndex, 2);
454 | }
455 | if (bracket)
456 | type = char.ToUpper(word[0]) + word.Substring(1) + "Interface";
457 | else if (!ReadFunctionType(tsFile, ref index, ref type, word + "Delegate", typeTop, namespaceTop, global))
458 | {
459 | List anys = new List();
460 | do
461 | anys.Add(SkipToEndOfWord());
462 | while (IfAt_Skip('|'));
463 | index--;
464 | type = string.Join(", ", anys);
465 | if (anys.Count > 1)
466 | type = $"Union<{type}>";
467 | }
468 | SkipEmpty();
469 |
470 | int i = 0;
471 | var typeDefinitions = namespaceTop.Count == 0 ? global.typeDefinitions : namespaceTop.Last().typeDefinitions;
472 | foreach (var it in typeDefinitions)
473 | {
474 | if (it is ClassDefinition && (it as ClassDefinition).name == type)
475 | {
476 | type = (i == 0 || i == 1 ? type : type.Substring(0, type.Length - i.ToString().Length)) + (i == 0 ? "" : i.ToString());
477 | i++;
478 | }
479 | }
480 |
481 | (typeTop.Last(v => v is ClassDefinition) as ClassDefinition).fields.Add(new Field
482 | {
483 | @static = @static,
484 | from = typeTop.Last(v => v is ClassDefinition) as ClassDefinition,
485 | typeAndName = new TypeNameAndOptional
486 | {
487 | type = type,
488 | name = word,
489 | optional = optionalField
490 | }
491 | });
492 | if (bracket)
493 | typeTop.Add(new ClassDefinition
494 | {
495 | type = TypeType.@interface,
496 | name = type
497 | });
498 | if (tsFile[index] == '}')
499 | index--;
500 | continue;
501 | }
502 | default:
503 | continue;
504 |
505 | case '(':
506 | case '[':
507 | {
508 | Method method = new Method
509 | {
510 | typeWheres = whereTypesExt,
511 | @static = @static,
512 | indexer = Current == '[',
513 | from = typeTop.Last(v => v is ClassDefinition) as ClassDefinition
514 | };
515 | var startBracket = Current;
516 | var endBracket = Current == '[' ? ']' : ')';
517 | method.typeAndName.name = word;
518 |
519 | SkipEmpty();
520 | if (IfAt_Skip(endBracket))
521 | goto Break;
522 |
523 | for (; index < tsFile.Length; index++)
524 | {
525 | SkipEmpty();
526 | optional = false;
527 | bool @params = false;
528 | if (IfAt_Skip('.'))
529 | {
530 | GoForward(2);
531 | @params = true;
532 | }
533 |
534 | string word2 = SkipToEndOfWord();
535 |
536 | if (IfAt_Skip('?'))
537 | optional = true;
538 |
539 | SkipEmpty();
540 |
541 | switch (Current)
542 | {
543 | case ':':
544 | GoForward(1);
545 | SkipEmpty();
546 | bool bracketIn = Current == '{';
547 | int endBracketArrIndex = tsFile.IndexOf('}', index) + 1;
548 | string arr = "";
549 | if (bracketIn)
550 | while (tsFile[endBracketArrIndex] == '[')
551 | {
552 | arr += "[]";
553 | tsFile = tsFile.Remove(endBracketArrIndex, 2);
554 | }
555 | string type2 = null;
556 | if (bracketIn)
557 | type2 = word2 + "Interface" + arr;
558 | else if (!ReadFunctionType(tsFile, ref index, ref type2, method.typeAndName.name + "Param" + (method.parameters.Count + 1) + "Delegate", typeTop, namespaceTop, global))
559 | {
560 | List anys = new List();
561 | do
562 | {
563 | if (tsFile[index] == '\'' || tsFile[index] == '"')
564 | {
565 | SkipEmpty();
566 | anys.Add("string");
567 | index = tsFile.IndexOf(Current, index + 1) + 1;
568 | SkipEmpty();
569 | }
570 | else
571 | anys.Add(SkipToEndOfWord());
572 | }
573 | while (tsFile[index++] == '|');
574 | index--;
575 | type2 = string.Join(", ", anys);
576 | if (anys.Count > 1)
577 | type2 = "Any<" + type2 + ">";
578 | }
579 |
580 | method.parameters.Add(new TypeNameOptionalAndParams
581 | {
582 | optional = optional,
583 | @params = @params,
584 | name = word2,
585 | type = type2
586 | });
587 | SkipEmpty();
588 |
589 |
590 |
591 | if (bracketIn)
592 | {
593 | typeTop.Add(new ClassDefinition
594 | {
595 | type = TypeType.@interface,
596 | name = type2.Substring(0, type2.Length - arr.Length)
597 | });
598 | goto BracketLoop;
599 | }
600 |
601 | if (tsFile[index] != ',')
602 | goto case ')';
603 |
604 | break;
605 |
606 | case ']':
607 | case ')':
608 | GoForward(1);
609 | SkipEmpty();
610 | goto Break;
611 | }
612 | }
613 | Break:
614 | bool bracket = false;
615 | string type = "object";
616 | if (Current == ':')
617 | {
618 | index++;
619 | SkipEmpty();
620 | bracket = Current == '{';
621 | if (bracket)
622 | type = char.ToUpper(word[0]) + word.Substring(1) + "Interface";
623 | else if (!ReadFunctionType(tsFile, ref index, ref type, method.typeAndName.name + "Delegate", typeTop, namespaceTop, global))
624 | {
625 | List anys = new List();
626 | List strings = new List();
627 | do
628 | {
629 | if (tsFile[index] == '\'' || tsFile[index] == '"')
630 | {
631 | SkipEmpty();
632 | strings.Add(tsFile.Substring(index + 1, tsFile.IndexOf(Current, index + 1) - index - 1));
633 | anys.Add("string");
634 | index = tsFile.IndexOf(tsFile[index], index + 1) + 1;
635 | SkipEmpty();
636 | }
637 | else
638 | anys.Add(SkipToEndOfWord());
639 | }
640 | while (tsFile[index++] == '|');
641 | index--;
642 | if (strings.Count == anys.Count)
643 | {
644 | var typeDefinitions = namespaceTop.Count == 0 ? global.typeDefinitions : namespaceTop.Last().typeDefinitions;
645 | typeDefinitions.Add(new EnumDefinition
646 | {
647 | name = word,
648 | emit = EnumDefinition.Emit.StringNamePreserveCase,
649 | members = strings
650 | });
651 | type = word;
652 | break;
653 | }
654 | type = string.Join(", ", anys);
655 | if (anys.Count > 1)
656 | type = "Any<" + type + ">";
657 | }
658 | method.typeAndName.type = type;
659 | SkipEmpty();
660 | }
661 | else
662 | {
663 | method.typeAndName.type = "object";
664 | }
665 |
666 | /*if (get || set)
667 | {
668 | (typeTop.Last() as ClassDefinition).properties.Add(new Property
669 | {
670 | get = get,
671 | set = set,
672 | @static = @static,
673 | typeAndName = method.typeAndName
674 | });
675 | }
676 | else */
677 | if (string.IsNullOrEmpty(method.typeAndName.name) && !method.indexer)
678 | {
679 | var oldTypeName = (typeTop.Last(v => v is ClassDefinition) as ClassDefinition).name;
680 | typeTop.RemoveAt(typeTop.Count - 1);
681 | method.typeAndName.name = oldTypeName;
682 | typeTop.Add(method);
683 | }
684 | else
685 | {
686 | (typeTop.Last(v => v is ClassDefinition) as ClassDefinition).methods.Add(method);
687 | }
688 | if (bracket)
689 | typeTop.Add(new ClassDefinition
690 | {
691 | type = TypeType.@interface,
692 | name = type
693 | });
694 | if (tsFile[index] == '}')
695 | index--;
696 | goto Continue;
697 | }
698 | }
699 | break;
700 | }
701 | Continue:;
702 | }
703 | }
704 | }
705 | }
706 |
--------------------------------------------------------------------------------