Skip to content

Commit 566d5dc

Browse files
committed
Version 0.1.0. Added support for access modifiers.
1 parent e12ddb6 commit 566d5dc

File tree

3 files changed

+55
-56
lines changed

3 files changed

+55
-56
lines changed

Platform.RegularExpressions.Transformer.CSharpToCpp.Tests/CSharpToCppTransformerTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ namespace Platform.RegularExpressions.Transformer.CSharpToCpp.Tests
44
{
55
public class CSharpToCppTransformerTests
66
{
7+
[Fact]
8+
public void EmptyLineTest()
9+
{
10+
// This test can help to test basic problems with regular expressions like incorrect syntax
11+
var transformer = new CSharpToCppTransformer();
12+
var actualResult = transformer.Transform("", new Context(null));
13+
Assert.Equal("", actualResult);
14+
}
15+
716
[Fact]
817
public void HelloWorldTest()
918
{
@@ -17,8 +26,7 @@ public static void Main(string[] args)
1726
}";
1827
const string expectedResult = @"class Program
1928
{
20-
public:
21-
static void Main(const char* args[])
29+
public: static void Main(const char* args[])
2230
{
2331
printf(""Hello, world!\n"");
2432
}

Platform.RegularExpressions.Transformer.CSharpToCpp/CSharpToCppTransformer.cs

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@ public class CSharpToCppTransformer : Transformer
2626
// out TProduct
2727
// TProduct
2828
(new Regex(@"(?<before>(<|, ))(in|out) (?<typeParameter>[a-zA-Z0-9]+)(?<after>(>|,))"), "${before}${typeParameter}${after}", null, 10),
29-
// public static bool CollectExceptions { get; set; }
30-
// public static bool CollectExceptions;
31-
(new Regex(@"(?<before>(private|protected|public)( static?) [^\r\n]+ )(?<name>[a-zA-Z0-9]+) {[^;}]*(?<=\W)get;[^;}]*(?<=\W)set;[^;}]*}"), "${before}${name};", null, 0),
29+
// public ...
30+
// public: ...
31+
(new Regex(@"(?<newLineAndIndent>\r?\n?[ \t]*)(?<before>[^\{\(\r\n]*)(?<access>private|protected|public)[ \t]+(?![^\{\(\r\n]*(interface|class|struct)[^\{\(\r\n]*[\{\(\r\n])"), "${newLineAndIndent}${access}: ${before}", null, 0),
32+
// public: static bool CollectExceptions { get; set; }
33+
// public: static bool CollectExceptions;
34+
(new Regex(@"(?<before>(private|protected|public): (static )?[^\r\n]+ )(?<name>[a-zA-Z0-9]+) {[^;}]*(?<=\W)get;[^;}]*(?<=\W)set;[^;}]*}"), "${before}${name};", null, 0),
3235
// public abstract class
3336
// class
34-
(new Regex(@"(public abstract|static) class"), "class", null, 0),
35-
// class GenericCollectionMethodsBase {
36-
// class GenericCollectionMethodsBase { public:
37-
(new Regex(@"class ([a-zA-Z0-9]+)(\s+){"), "class $1$2{" + Environment.NewLine + " public:", null, 0),
37+
(new Regex(@"((public|protected|private|internal|abstract|static) )*(?<category>interface|class|struct)"), "${category}", null, 0),
3838
// class GenericCollectionMethodsBase<TElement> {
39-
// template <typename TElement> class GenericCollectionMethodsBase { public:
40-
(new Regex(@"class ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>([^{]+){"), "template <typename $2> class $1$3{" + Environment.NewLine + " public:", null, 0),
39+
// template <typename TElement> class GenericCollectionMethodsBase {
40+
(new Regex(@"class ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>([^{]+){"), "template <typename $2> class $1$3{", null, 0),
4141
// static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
4242
// template<typename T> static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
4343
(new Regex(@"static ([a-zA-Z0-9]+) ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>\(([^\)\r\n]+)\)"), "template <typename $3> static $1 $2($4)", null, 0),
4444
// interface IFactory<out TProduct> {
45-
// template <typename TProduct> class IFactory { public:
46-
(new Regex(@"interface (?<interface>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<whitespace>[^{]+){"), "template <typename...> class ${interface}; template <typename ${typeParameters}> class ${interface}<${typeParameters}>${whitespace}{" + Environment.NewLine + " public:", null, 0),
45+
// template <typename TProduct> class IFactory {
46+
(new Regex(@"interface (?<interface>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<whitespace>[^{]+){"), "template <typename...> class ${interface}; template <typename ${typeParameters}> class ${interface}<${typeParameters}>${whitespace}{", null, 0),
4747
// template <typename TObject, TProperty, TValue>
4848
// template <typename TObject, typename TProperty, TValue>
4949
(new Regex(@"(?<before>template <((, )?typename [a-zA-Z0-9]+)+, )(?<typeParameter>[a-zA-Z0-9]+)(?<after>(,|>))"), "${before}typename ${typeParameter}${after}", null, 10),
5050
// Insert markers
51-
// private static void BuildExceptionString(this StringBuilder sb, Exception exception, int level)
52-
// /*~extensionMethod~BuildExceptionString~*/private static void BuildExceptionString(this StringBuilder sb, Exception exception, int level)
53-
(new Regex(@"private static [^\r\n]+ (?<name>[a-zA-Z0-9]+)\(this [^\)\r\n]+\)"), "/*~extensionMethod~${name}~*/$0", null, 0),
51+
// private: static void BuildExceptionString(this StringBuilder sb, Exception exception, int level)
52+
// /*~extensionMethod~BuildExceptionString~*/private: static void BuildExceptionString(this StringBuilder sb, Exception exception, int level)
53+
(new Regex(@"private: static [^\r\n]+ (?<name>[a-zA-Z0-9]+)\(this [^\)\r\n]+\)"), "/*~extensionMethod~${name}~*/$0", null, 0),
5454
// Move all markers to the beginning of the file.
5555
(new Regex(@"\A(?<before>[^\r\n]+\r?\n(.|\n)+)(?<marker>/\*~extensionMethod~(?<name>[a-zA-Z0-9]+)~\*/)"), "${marker}${before}", null, 10),
5656
// /*~extensionMethod~BuildExceptionString~*/...sb.BuildExceptionString(exception.InnerException, level + 1);
@@ -63,51 +63,42 @@ public class CSharpToCppTransformer : Transformer
6363
// (this
6464
// (
6565
(new Regex(@"\(this "), "(", null, 0),
66-
// public static readonly EnsureAlwaysExtensionRoot Always = new EnsureAlwaysExtensionRoot();
67-
// inline static EnsureAlwaysExtensionRoot Always;
68-
(new Regex(@"public static readonly (?<type>[a-zA-Z0-9]+) (?<name>[a-zA-Z0-9_]+) = new \k<type>\(\);"), "inline static ${type} ${name};", null, 0),
69-
// public static readonly string ExceptionContentsSeparator = "---";
70-
// inline static const char* ExceptionContentsSeparator = "---";
71-
(new Regex(@"public static readonly string (?<name>[a-zA-Z0-9_]+) = ""(?<string>(\""|[^""\r\n])+)"";"), "inline static const char* ${name} = \"${string}\";", null, 0),
72-
// private const int MaxPath = 92;
73-
// static const int MaxPath = 92;
74-
(new Regex(@"private (const|static readonly) ([a-zA-Z0-9]+) ([_a-zA-Z0-9]+) = ([^;\r\n]+);"), "static const $2 $3 = $4;", null, 0),
66+
// public: static readonly EnsureAlwaysExtensionRoot Always = new EnsureAlwaysExtensionRoot();
67+
// public:inline static EnsureAlwaysExtensionRoot Always;
68+
(new Regex(@"(?<access>(private|protected|public): )?static readonly (?<type>[a-zA-Z0-9]+) (?<name>[a-zA-Z0-9_]+) = new \k<type>\(\);"), "${access}inline static ${type} ${name};", null, 0),
69+
// public: static readonly string ExceptionContentsSeparator = "---";
70+
// public: inline static const char* ExceptionContentsSeparator = "---";
71+
(new Regex(@"(?<access>(private|protected|public): )?static readonly string (?<name>[a-zA-Z0-9_]+) = ""(?<string>(\""|[^""\r\n])+)"";"), "${access}inline static const char* ${name} = \"${string}\";", null, 0),
72+
// private: const int MaxPath = 92;
73+
// private: static const int MaxPath = 92;
74+
(new Regex(@"(?<access>(private|protected|public): )?(const|static readonly) (?<type>[a-zA-Z0-9]+) (?<name>[_a-zA-Z0-9]+) = (?<value>[^;\r\n]+);"), "${access}static const ${type} ${name} = ${value};", null, 0),
7575
// ArgumentNotNull(EnsureAlwaysExtensionRoot root, TArgument argument) where TArgument : class
7676
// ArgumentNotNull(EnsureAlwaysExtensionRoot root, TArgument* argument)
7777
(new Regex(@"(?<before> [a-zA-Z]+\(([a-zA-Z *,]+, |))(?<type>[a-zA-Z]+)(?<after>(| [a-zA-Z *,]+)\))[ \r\n]+where \k<type> : class"), "${before}${type}*${after}", null, 0),
78-
// protected virtual
79-
// virtual
80-
(new Regex(@"protected virtual"), "virtual", null, 0),
81-
// protected abstract TElement GetFirst();
82-
// virtual TElement GetFirst() = 0;
83-
(new Regex(@"protected abstract ([^;\r\n]+);"), "virtual $1 = 0;", null, 0),
78+
// protected: abstract TElement GetFirst();
79+
// protected: virtual TElement GetFirst() = 0;
80+
(new Regex(@"(?<access>(private|protected|public): )?abstract (?<method>[^;\r\n]+);"), "${access}virtual ${method} = 0;", null, 0),
8481
// TElement GetFirst();
8582
// virtual TElement GetFirst() = 0;
8683
(new Regex(@"([\r\n]+[ ]+)((?!return)[a-zA-Z0-9]+ [a-zA-Z0-9]+\([^\)\r\n]*\))(;[ ]*[\r\n]+)"), "$1virtual $2 = 0$3", null, 1),
87-
// public virtual
88-
// virtual
89-
(new Regex(@"public virtual"), "virtual", null, 0),
90-
// protected readonly
91-
//
92-
(new Regex(@"protected readonly "), "", null, 0),
93-
// protected readonly TreeElement[] _elements;
94-
// TreeElement _elements[N];
95-
(new Regex(@"(protected|private) readonly ([a-zA-Z<>0-9]+)([\[\]]+) ([_a-zA-Z0-9]+);"), "$2 $4[N];", null, 0),
96-
// protected readonly TElement Zero;
97-
// TElement Zero;
98-
(new Regex(@"(protected|private) readonly ([a-zA-Z<>0-9]+) ([_a-zA-Z0-9]+);"), "$2 $3;", null, 0),
99-
// private
84+
// protected: readonly TreeElement[] _elements;
85+
// protected: TreeElement _elements[N];
86+
(new Regex(@"(?<access>(private|protected|public): )?readonly (?<type>[a-zA-Z<>0-9]+)([\[\]]+) (?<name>[_a-zA-Z0-9]+);"), "${access}${type} ${name}[N];", null, 0),
87+
// protected: readonly TElement Zero;
88+
// protected: TElement Zero;
89+
(new Regex(@"(?<access>(private|protected|public): )?readonly (?<type>[a-zA-Z<>0-9]+) (?<name>[_a-zA-Z0-9]+);"), "${access}${type} ${name};", null, 0),
90+
// internal
10091
//
101-
(new Regex(@"(\W)(private|protected|public|internal) "), "$1", null, 0),
92+
(new Regex(@"(\W)internal\s+"), "$1", null, 0),
10293
// static void NotImplementedException(ThrowExtensionRoot root) => throw new NotImplementedException();
10394
// static void NotImplementedException(ThrowExtensionRoot root) { return throw new NotImplementedException(); }
104-
(new Regex(@"(^\s+)(template \<[^>\r\n]+\> )?(static )?(override )?([a-zA-Z0-9]+ )([a-zA-Z0-9]+)\(([^\(\r\n]*)\)\s+=>\s+throw([^;\r\n]+);"), "$1$2$3$4$5$6($7) { throw$8; }", null, 0),
95+
(new Regex(@"(^\s+)(private|protected|public)?(: )?(template \<[^>\r\n]+\> )?(static )?(override )?([a-zA-Z0-9]+ )([a-zA-Z0-9]+)\(([^\(\r\n]*)\)\s+=>\s+throw([^;\r\n]+);"), "$1$2$3$4$5$6$7$8($9) { throw$10; }", null, 0),
10596
// SizeBalancedTree(int capacity) => a = b;
10697
// SizeBalancedTree(int capacity) { a = b; }
107-
(new Regex(@"(^\s+)(template \<[^>\r\n]+\> )?(static )?(override )?(void )?([a-zA-Z0-9]+)\(([^\(\r\n]*)\)\s+=>\s+([^;\r\n]+);"), "$1$2$3$4$5$6($7) { $8; }", null, 0),
98+
(new Regex(@"(^\s+)(private|protected|public)?(: )?(template \<[^>\r\n]+\> )?(static )?(override )?(void )?([a-zA-Z0-9]+)\(([^\(\r\n]*)\)\s+=>\s+([^;\r\n]+);"), "$1$2$3$4$5$6$7$8($9) { $10; }", null, 0),
10899
// int SizeBalancedTree(int capacity) => a;
109100
// int SizeBalancedTree(int capacity) { return a; }
110-
(new Regex(@"(^\s+)(template \<[^>\r\n]+\> )?(static )?(override )?([a-zA-Z0-9]+ )([a-zA-Z0-9]+)\(([^\(\r\n]*)\)\s+=>\s+([^;\r\n]+);"), "$1$2$3$4$5$6($7) { return $8; }", null, 0),
101+
(new Regex(@"(^\s+)(private|protected|public)?(: )?(template \<[^>\r\n]+\> )?(static )?(override )?([a-zA-Z0-9]+ )([a-zA-Z0-9]+)\(([^\(\r\n]*)\)\s+=>\s+([^;\r\n]+);"), "$1$2$3$4$5$6$7$8($9) { return $10; }", null, 0),
111102
// () => Integer<TElement>.Zero,
112103
// () { return Integer<TElement>.Zero; },
113104
(new Regex(@"\(\)\s+=>\s+([^,;\r\n]+?),"), "() { return $1; },", null, 0),
@@ -210,9 +201,9 @@ public class CSharpToCppTransformer : Transformer
210201
// GetElement(node).Right
211202
// GetElement(node)->Right
212203
(new Regex(@"([a-zA-Z0-9]+)\(([a-zA-Z0-9\*]+)\)\.([a-zA-Z0-9]+)"), "$1($2)->$3", null, 0),
213-
// [Fact]\npublic static void SizeBalancedTreeMultipleAttachAndDetachTest()
214-
// TEST_METHOD(SizeBalancedTreeMultipleAttachAndDetachTest)
215-
(new Regex(@"\[Fact\][\s\n]+(static )?void ([a-zA-Z0-9]+)\(\)"), "TEST_METHOD($2)", null, 0),
204+
// [Fact]\npublic: static void SizeBalancedTreeMultipleAttachAndDetachTest()
205+
// public: TEST_METHOD(SizeBalancedTreeMultipleAttachAndDetachTest)
206+
(new Regex(@"\[Fact\][\s\n]+(public: )?(static )?void ([a-zA-Z0-9]+)\(\)"), "public: TEST_METHOD($3)", null, 0),
216207
// class TreesTests
217208
// TEST_CLASS(TreesTests)
218209
(new Regex(@"class ([a-zA-Z0-9]+)Tests"), "TEST_CLASS($1)", null, 0),
@@ -230,10 +221,10 @@ public class CSharpToCppTransformer : Transformer
230221
(new Regex(@"Console\.WriteLine\(""([^""\r\n]+)""\)"), "printf(\"$1\\n\")", null, 0),
231222
// TElement Root;
232223
// TElement Root = 0;
233-
(new Regex(@"(\r?\n[\t ]+)([a-zA-Z0-9:_]+(?<!return)) ([_a-zA-Z0-9]+);"), "$1$2 $3 = 0;", null, 0),
224+
(new Regex(@"(\r?\n[\t ]+)(private|protected|public)?(: )?([a-zA-Z0-9:_]+(?<!return)) ([_a-zA-Z0-9]+);"), "$1$2$3$4 $5 = 0;", null, 0),
234225
// TreeElement _elements[N];
235226
// TreeElement _elements[N] = { {0} };
236-
(new Regex(@"(\r?\n[\t ]+)([a-zA-Z0-9]+) ([_a-zA-Z0-9]+)\[([_a-zA-Z0-9]+)\];"), "$1$2 $3[$4] = { {0} };", null, 0),
227+
(new Regex(@"(\r?\n[\t ]+)(private|protected|public)?(: )?([a-zA-Z0-9]+) ([_a-zA-Z0-9]+)\[([_a-zA-Z0-9]+)\];"), "$1$2$3$4 $5[$6] = { {0} };", null, 0),
237228
// auto path = new TElement[MaxPath];
238229
// TElement path[MaxPath] = { {0} };
239230
(new Regex(@"(\r?\n[\t ]+)[a-zA-Z0-9]+ ([a-zA-Z0-9]+) = new ([a-zA-Z0-9]+)\[([_a-zA-Z0-9]+)\];"), "$1$3 $2[$4] = { {0} };", null, 0),
@@ -295,7 +286,7 @@ public class CSharpToCppTransformer : Transformer
295286
// Insert method body scope starts.
296287
// void PrintNodes(TElement node, StringBuilder sb, int level) {
297288
// void PrintNodes(TElement node, StringBuilder sb, int level) {/*method-start*/
298-
(new Regex(@"(?<start>\r?\n[\t ]+)(?<prefix>((virtual )?[a-zA-Z0-9:_]+ )?)(?<method>[a-zA-Z][a-zA-Z0-9]*)\((?<arguments>[^\)]*)\)(?<override>( override)?)(?<separator>[ \t\r\n]*)\{(?<end>[^~])"), "${start}${prefix}${method}(${arguments})${override}${separator}{/*method-start*/${end}", null, 0),
289+
(new Regex(@"(?<start>\r?\n[\t ]+)(?<prefix>((private|protected|public): )?(virtual )?[a-zA-Z0-9:_]+ )?(?<method>[a-zA-Z][a-zA-Z0-9]*)\((?<arguments>[^\)]*)\)(?<override>( override)?)(?<separator>[ \t\r\n]*)\{(?<end>[^~])"), "${start}${prefix}${method}(${arguments})${override}${separator}{/*method-start*/${end}", null, 0),
299290
// Insert method body scope ends.
300291
// {/*method-start*/...}
301292
// {/*method-start*/.../*method-end*/}

Platform.RegularExpressions.Transformer.CSharpToCpp/Platform.RegularExpressions.Transformer.CSharpToCpp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Description>LinksPlatform's Platform.RegularExpressions.Transformer.CSharpToCpp Class Library</Description>
55
<Copyright>Konstantin Diachenko</Copyright>
66
<AssemblyTitle>Platform.RegularExpressions.Transformer.CSharpToCpp</AssemblyTitle>
7-
<VersionPrefix>0.0.39</VersionPrefix>
7+
<VersionPrefix>0.1.0</VersionPrefix>
88
<Authors>Konstantin Diachenko</Authors>
99
<TargetFrameworks>net471;netstandard2.0;netstandard2.1</TargetFrameworks>
1010
<AssemblyName>Platform.RegularExpressions.Transformer.CSharpToCpp</AssemblyName>
@@ -24,7 +24,7 @@
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2626
<LangVersion>latest</LangVersion>
27-
<PackageReleaseNotes>Converting automatic properties to fields.</PackageReleaseNotes>
27+
<PackageReleaseNotes>Added support for access modifiers.</PackageReleaseNotes>
2828
</PropertyGroup>
2929

3030
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">

0 commit comments

Comments
 (0)