Skip to content

Commit 99286d7

Browse files
committed
Added support for translation of GetHashCode method implementations.
1 parent 990aba1 commit 99286d7

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

csharp/Platform.RegularExpressions.Transformer.CSharpToCpp/CSharpToCppTransformer.cs

+24-1
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,26 @@ public class CSharpToCppTransformer : TextTransformer
517517
// /*~ex~*/
518518
//
519519
(new Regex(@"/\*~[^~\n]+~[^~\n]+~\*/"), "", 0),
520+
// Insert scope borders.
521+
// namespace Platform::Ranges { ... }
522+
// namespace Platform::Ranges {/*~start~namespace~Platform::Ranges~*/ ... /*~end~namespace~Platform::Ranges~*/}
523+
(new Regex(@"(?<namespaceDeclarationBegin>\r?\n(?<indent>[\t ]*)namespace (?<namespaceName>(?<namePart>[a-zA-Z][a-zA-Z0-9]+)(?<nextNamePart>::[a-zA-Z][a-zA-Z0-9]+)+)(\s|\n)*{)(?<middle>(.|\n)*)(?<end>(?<=\r?\n)\k<indent>}(?!;))"), "${namespaceDeclarationBegin}/*~start~namespace~${namespaceName}~*/${middle}/*~end~namespace~${namespaceName}~*/${end}", 0),
524+
// Insert scope borders.
525+
// class Range<T> { ... };
526+
// class Range<T> {/*~start~type~Range<T>~T~*/ ... /*~start~type~Range<T>~T~*/};
527+
(new Regex(@"(?<classDeclarationBegin>\r?\n(?<indent>[\t ]*)template <typename (?<typeParameter>[^\n]+)> (struct|class) (?<type>[a-zA-Z0-9]+<\k<typeParameter>>)(\s*:\s*[^{\n]+)?[\t ]*(\r?\n)?[\t ]*{)(?<middle>(.|\n)*)(?<endIndent>(?<=\r?\n)\k<indent>)(?<end>};)"), "${classDeclarationBegin}/*~start~type~${type}~${typeParameter}~*/${middle}${endIndent}/*~end~type~${type}~${typeParameter}~*/${end}", 0),
528+
// Inside scopes replace:
529+
// /*~start~namespace~Platform::Ranges~*/ ... /*~start~type~Range<T>~T~*/ ... public: override std::int32_t GetHashCode() { return {Minimum, Maximum}.GetHashCode(); } ... /*~start~type~Range<T>~T~*/ ... /*~end~namespace~Platform::Ranges~*/
530+
// /*~start~namespace~Platform::Ranges~*/ ... /*~start~type~Range<T>~T~*/ ... /*~start~type~Range<T>~T~*/ ... /*~end~namespace~Platform::Ranges~*/ namespace std { template <typename T> struct hash<Platform::Ranges::Range<T>> { std::size_t operator()(const Platform::Ranges::Range<T> &obj) const { return {Minimum, Maximum}.GetHashCode(); } }; }
531+
(new Regex(@"(?<namespaceScopeStart>/\*~start~namespace~(?<namespace>[^~\n\*]+)~\*/)(?<betweenStartScopes>(.|\n)+)(?<typeScopeStart>/\*~start~type~(?<type>[^~\n\*]+)~(?<typeParameter>[^~\n\*]+)~\*/)(?<before>(.|\n)+?)(?<hashMethodDeclaration>\r?\n[ \t]*(?<access>(private|protected|public): )override std::int32_t GetHashCode\(\)(\s|\n)*{\s*(?<methodBody>[^\s][^\n]+[^\s])\s*}\s*)(?<after>(.|\n)+?)(?<typeScopeEnd>/\*~end~type~\k<type>~\k<typeParameter>~\*/)(?<betweenEndScopes>(.|\n)+)(?<namespaceScopeEnd>/\*~end~namespace~\k<namespace>~\*/)}\r?\n"), "${namespaceScopeStart}${betweenStartScopes}${typeScopeStart}${before}${after}${typeScopeEnd}${betweenEndScopes}${namespaceScopeEnd}}" + Environment.NewLine + Environment.NewLine + "namespace std" + Environment.NewLine + "{" + Environment.NewLine + " template <typename ${typeParameter}>" + Environment.NewLine + " struct hash<${namespace}::${type}>" + Environment.NewLine + " {" + Environment.NewLine + " std::size_t operator()(const ${namespace}::${type} &obj) const" + Environment.NewLine + " {" + Environment.NewLine + " /*~start~method~*/${methodBody}/*~end~method~*/" + Environment.NewLine + " }" + Environment.NewLine + " };" + Environment.NewLine + "}" + Environment.NewLine, 10),
532+
// Inside scope of /*~start~method~*/ replace:
533+
// /*~start~method~*/ ... Minimum ... /*~end~method~*/
534+
// /*~start~method~*/ ... obj.Minimum ... /*~end~method~*/
535+
(new Regex(@"(?<methodScopeStart>/\*~start~method~\*/)(?<before>.+({|, ))(?<name>[a-zA-Z][a-zA-Z0-9]+)(?<after>[^\n\.\(a-zA-Z0-9]((?!/\*~end~method~\*/)[^\n])+)(?<methodScopeEnd>/\*~end~method~\*/)"), "${methodScopeStart}${before}obj.${name}${after}${methodScopeEnd}", 10),
536+
// Remove scope borders.
537+
// /*~start~type~Range<T>~*/
538+
//
539+
(new Regex(@"/\*~[^~\*\n]+(~[^~\*\n]+)*~\*/"), "", 0),
520540
}.Cast<ISubstitutionRule>().ToList();
521541

522542
public static readonly IList<ISubstitutionRule> LastStage = new List<SubstitutionRule>
@@ -539,6 +559,9 @@ public class CSharpToCppTransformer : TextTransformer
539559
// ((1, 2))
540560
// ({1, 2})
541561
(new Regex(@"(?<before>\(|, )\((?<first>[^\n()]+), (?<second>[^\n()]+)\)(?<after>\)|, )"), "${before}{${first}, ${second}}${after}", 10),
562+
// {1, 2}.GetHashCode()
563+
// Platform::Hashing::Hash({1, 2})
564+
(new Regex(@"{(?<first>[^\n{}]+), (?<second>[^\n{}]+)}\.GetHashCode\(\)"), "Platform::Hashing::Hash({${first}, ${second}})", 10),
542565
// range.ToString()
543566
// Platform::Converters::To<std::string>(range).data()
544567
(new Regex(@"(?<before>\W)(?<variable>[_a-zA-Z][_a-zA-Z0-9]+)\.ToString\(\)"), "${before}Platform::Converters::To<std::string>(${variable}).data()", 10),
@@ -597,7 +620,7 @@ public class CSharpToCppTransformer : TextTransformer
597620
// {\n
598621
(new Regex(@"{[ \t]*\r?\n[ \t]*\r?\n"), "{" + Environment.NewLine, 10),
599622
// \n\n}
600-
// {\n
623+
// \n}
601624
(new Regex(@"\r?\n[ \t]*\r?\n(?<end>[ \t]*})"), Environment.NewLine + "${end}", 10),
602625
}.Cast<ISubstitutionRule>().ToList();
603626

0 commit comments

Comments
 (0)