Skip to content

Commit c6c4f09

Browse files
committed
Removal of attributes does not break formatting anymore.
Added rule for accessing all methods in the methods bodies by this reference. Fixed rules for constants, inline methods, and array initialization.
1 parent 0b63515 commit c6c4f09

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

Platform.RegularExpressions.Transformer.CSharpToCpp/CSharpToCppTransformer.cs

+26-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class CSharpToCppTransformer : Transformer
4646
(new Regex(@"Action<([a-zA-Z0-9]+)> ([a-zA-Z0-9]+)"), "std::function<void($1)> $2", null, 0),
4747
// private const int MaxPath = 92;
4848
// static const int MaxPath = 92;
49-
(new Regex(@"private const ([a-zA-Z0-9]+) ([_a-zA-Z0-9]+) = ([a-zA-Z0-9]+);"), "static const $1 $2 = $3;", null, 0),
49+
(new Regex(@"private (const|static readonly) ([a-zA-Z0-9]+) ([_a-zA-Z0-9]+) = ([^;]+);"), "static const $2 $3 = $4;", null, 0),
5050
// protected virtual
5151
// virtual
5252
(new Regex(@"protected virtual"), "virtual", null, 0),
@@ -70,7 +70,10 @@ public class CSharpToCppTransformer : Transformer
7070
(new Regex(@"(\W)(private|protected|public|internal) "), "$1", null, 0),
7171
// SizeBalancedTree(int capacity) => a = b;
7272
// SizeBalancedTree(int capacity) { a = b; }
73-
(new Regex(@"(^\s+)(override )?(void )?([a-zA-Z0-9]+)\(([^\(]+)\)\s+=>\s+([^;]+);"), "$1$2$3$4($5) { $6; }", null, 0),
73+
(new Regex(@"(^\s+)(override )?(void )?([a-zA-Z0-9]+)\(([^\(]*)\)\s+=>\s+([^;]+);"), "$1$2$3$4($5) { $6; }", null, 0),
74+
// int SizeBalancedTree(int capacity) => a;
75+
// int SizeBalancedTree(int capacity) { return a; }
76+
(new Regex(@"(^\s+)(override )?([a-zA-Z0-9]+ )([a-zA-Z0-9]+)\(([^\(]*)\)\s+=>\s+([^;]+);"), "$1$2$3$4($5) { return $6; }", null, 0),
7477
// () => Integer<TElement>.Zero,
7578
// () { return Integer<TElement>.Zero; },
7679
(new Regex(@"\(\)\s+=>\s+([^\r\n,;]+?),"), "() { return $1; },", null, 0),
@@ -149,8 +152,8 @@ public class CSharpToCppTransformer : Transformer
149152
// ref TElement
150153
// TElement*
151154
(new Regex(@"( |\()ref ([a-zA-Z0-9]+) "), "$1$2* ", null, 0),
152-
// ref sizeBalancedTree2.Root
153-
// &sizeBalancedTree2->Root
155+
// ref sizeBalancedTree.Root
156+
// &sizeBalancedTree->Root
154157
(new Regex(@"ref ([a-zA-Z0-9]+)\.([a-zA-Z0-9\*]+)"), "&$1->$2", null, 0),
155158
// ref GetElement(node).Right
156159
// &GetElement(node)->Right
@@ -175,7 +178,7 @@ public class CSharpToCppTransformer : Transformer
175178
(new Regex(@"(\r?\n[\t ]+)([a-zA-Z0-9]+) ([_a-zA-Z0-9]+)\[([_a-zA-Z0-9]+)\];"), "$1$2 $3[$4] = { {0} };", null, 0),
176179
// auto path = new TElement[MaxPath];
177180
// TElement path[MaxPath] = { {0} };
178-
(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),
181+
(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),
179182
// Insert scope borders.
180183
// auto added = new HashSet<TElement>();
181184
// ~!added!~std::unordered_set<TElement> added;
@@ -207,6 +210,23 @@ public class CSharpToCppTransformer : Transformer
207210
// ~!random!~
208211
//
209212
(new Regex(@"~!(?<pointer>[a-zA-Z0-9]+)!~"), "", null, 5),
213+
// Insert method body scope starts.
214+
// void PrintNodes(TElement node, StringBuilder sb, int level) {
215+
// void PrintNodes(TElement node, StringBuilder sb, int level) {/*method-start*/
216+
(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),
217+
// Insert method body scope ends.
218+
// {/*method-start*/...}
219+
// {/*method-start*/.../*method-end*/}
220+
(new Regex(@"\{/\*method-start\*/(?<body>((?<bracket>\{)|(?<-bracket>\})|[^\{\}]*)+)\}"), "{/*method-start*/${body}/*method-end*/}", null, 0),
221+
// Inside method bodies replace:
222+
// GetFirst(
223+
// this->GetFirst(
224+
//(new Regex(@"(?<separator>(\(|, |([\W]) |return ))(?<!(->|\* ))(?<method>(?!sizeof)[a-zA-Z0-9]+)\((?!\) \{)"), "${separator}this->${method}(", null, 1),
225+
(new Regex(@"(?<scope>/\*method-start\*/)(?<before>((?<!/\*method-end\*/)(.|\n))*?)(?<separator>[\W](?<!(::|\.|->)))(?<method>(?!sizeof)[a-zA-Z0-9]+)\((?!\) \{)(?<after>(.|\n)*?)(?<scopeEnd>/\*method-end\*/)"), "${scope}${before}${separator}this->${method}(${after}${scopeEnd}", null, 100),
226+
// Remove scope borders.
227+
// /*method-start*/
228+
//
229+
(new Regex(@"/\*method-(start|end)\*/"), "", null, 0),
210230
}.Cast<ISubstitutionRule>().ToList();
211231

212232
public static readonly IList<ISubstitutionRule> LastStage = new List<SubstitutionRule>
@@ -231,7 +251,7 @@ public class CSharpToCppTransformer : Transformer
231251
(new Regex(@"#if [a-zA-Z0-9]+\s+#endif"), "", null, 0),
232252
// [Fact]
233253
//
234-
(new Regex(@"(?<firstNewLine>\r?\n|\A)(?<indent>[\t ]+)\[[a-zA-Z0-9]+(\((?<expression>((?<parenthesis>\()|(?<-parenthesis>\))|[^()]*)+)(?(parenthesis)(?!))\))?\]\s*(\r?\n\k<indent>)?"), "${firstNewLine}${indent}", null, 5),
254+
(new Regex(@"(?<firstNewLine>\r?\n|\A)(?<indent>[\t ]+)\[[a-zA-Z0-9]+(\((?<expression>((?<parenthesis>\()|(?<-parenthesis>\))|[^()]*)+)(?(parenthesis)(?!))\))?\][ \t]*(\r?\n\k<indent>)?"), "${firstNewLine}${indent}", null, 5),
235255
// \n ... namespace
236256
// namespace
237257
(new Regex(@"(\S[\r\n]{1,2})?[\r\n]+namespace"), "$1namespace", null, 0),

0 commit comments

Comments
 (0)