public override void EnterPackageClause(GolangParser.PackageClauseContext context)
        {
            // Go package clause is the first keyword encountered - cache details that
            // will be written out after imports. C# import statements (i.e., usings)
            // typically occur before namespace and class definitions
            string[] paths            = PackageImport.Split('/').Select(SanitizedIdentifier).ToArray();
            string   packageNamespace = $"{RootNamespace}.{string.Join(".", paths)}";

            PackageUsing     = $"{Package} = {packageNamespace}{ClassSuffix}";
            PackageNamespace = packageNamespace.Substring(0, packageNamespace.LastIndexOf('.'));

            // Track file name associated with package
            AddFileToPackage(Package, TargetFileName, PackageNamespace);

            // Define namespaces
            List <string> packageNamespaces = new List <string> {
                RootNamespace
            };

            if (paths.Length > 1)
            {
                packageNamespaces.AddRange(paths);
                packageNamespaces.RemoveAt(packageNamespaces.Count - 1);
            }

            PackageNamespaces = packageNamespaces.ToArray();

            string headerLevelComments = CheckForCommentsLeft(context);

            m_packageLevelComments = CheckForCommentsRight(context);

            if (!string.IsNullOrWhiteSpace(headerLevelComments))
            {
                m_targetFile.Append(headerLevelComments);

                if (!EndsWithLineFeed(headerLevelComments))
                {
                    m_targetFile.AppendLine();
                }
            }

            m_targetFile.AppendLine($"// package {Package} -- go2cs converted at {DateTime.UtcNow:yyyy MMMM dd HH:mm:ss} UTC");

            if (!PackageImport.Equals("main"))
            {
                m_targetFile.AppendLine($"// import \"{PackageImport}\" ==> using {PackageUsing}");
            }

            m_targetFile.AppendLine($"// Original source: {SourceFileName}");


            // Add commonly required using statements
            RequiredUsings.Add("static go.builtin");
        }
Exemple #2
0
        public override void EnterPackageClause(GolangParser.PackageClauseContext context)
        {
            Package = SanitizedIdentifier(context.IDENTIFIER().GetText());

            if (Package.Equals("main"))
            {
                PackageImport = Package;
            }
            else
            {
                // Define package import path
                PackageImport = Path.GetDirectoryName(SourceFileName) ?? Package;
                PackageImport = PackageImport.Replace(GoRoot, "");
                PackageImport = PackageImport.Replace(GoPath, "");

                while (PackageImport.StartsWith(Path.DirectorySeparatorChar.ToString()) || PackageImport.StartsWith(Path.AltDirectorySeparatorChar.ToString()))
                {
                    PackageImport = PackageImport.Substring(1);
                }

                while (PackageImport.EndsWith(Path.DirectorySeparatorChar.ToString()) || PackageImport.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
                {
                    PackageImport = PackageImport.Substring(0, PackageImport.Length - 1);
                }

                int lastSlash;

                if (Path.IsPathRooted(PackageImport))
                {
                    // File converted was outside %GOPATH% and %GOROOT%
                    lastSlash = PackageImport.LastIndexOf('\\');

                    if (lastSlash > -1)
                    {
                        PackageImport = $"{PackageImport.Substring(lastSlash + 1)}";
                    }
                }

                PackageImport = $"{PackageImport.Replace('\\', '/')}";

                lastSlash = PackageImport.LastIndexOf('/');
                string package = SanitizedIdentifier(lastSlash > -1 ? PackageImport.Substring(lastSlash + 1) : PackageImport);

                if (!package.Equals(Package))
                {
                    AddWarning(context, $"Defined package clause \"{Package}\" does not match file path \"{SourceFileName}\"");
                    PackageImport = lastSlash > -1 ? $"{PackageImport.Substring(0, lastSlash)}.{Package}" : Package;
                }
            }
        }