Ejemplo n.º 1
0
        private void ImportModulesFromPackage(FromImportStatement node, PackageImport packageImport)
        {
            var names   = node.Names;
            var asNames = node.AsNames;

            var importNames = names.Select(n => n.Name).ToArray();

            if (importNames.Length == 1 && importNames[0] == "*")
            {
                // TODO: Need tracking of previous imports to determine possible imports for namespace package
                MakeUnresolvedImport(packageImport.Name, node.Root);
                return;
            }

            foreach (var module in packageImport.Modules)
            {
                var index = importNames.IndexOf(module.Name, StringExtensions.EqualsOrdinal);
                if (index == -1)
                {
                    MakeUnresolvedImport(module.FullName, node.Root);
                    continue;
                }

                if (!ProjectState.Modules.TryImport(module.FullName, out var moduleReference))
                {
                    MakeUnresolvedImport(module.FullName, node.Root);
                    continue;
                }

                _unit.DeclaringModule.AddModuleReference(module.FullName, moduleReference);
                moduleReference.Module.Imported(_unit);

                var importedModule = moduleReference.Module;
                var variableName   = asNames[index]?.Name ?? importNames[index];
                AssignImportedModuleOrMember(variableName, importedModule, true, names[index], asNames[index]);
            }
        }
Ejemplo n.º 2
0
        public override void EnterPackageClause(GoParser.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))
            {
                if (m_targetFile.Length == 0)
                {
                    headerLevelComments = headerLevelComments.TrimStart();
                }

                m_targetFile.Append(headerLevelComments);

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

            if (!Options.ExcludeHeaderComments)
            {
                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");
        }