示例#1
0
        public static string BuildEnumHeader(EnumDecl decl, Dictionary <string, string> includePath)
        {
            var writer = new CodeWriter();

            var settings = GeneratorSettingsProvider.Get(decl.Module.ModuleName);

            writer.AppendLines(settings.Copyright);
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine("#pragma once");
            writer.AppendNewLineWithoutIndent();

            // includes
            writer.AppendLine("#include <dot/system/enum.hpp>");
            // pure data

            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"namespace {settings.Namespace}");
            writer.AppendLine("{");
            writer.PushIndent();
            BuildEnumDeclaration(decl, writer);
            writer.PopIndent();
            writer.AppendLine("}");

            return(writer.ToString());
        }
        public static string BuildDataSource(TypeDecl decl, Dictionary <string, string> declSet)
        {
            var writer = new CodeWriter();

            var settings = GeneratorSettingsProvider.Get(decl.Module.ModuleName);

            writer.AppendLines(settings.Copyright);
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"#include <{settings.Namespace}/precompiled.hpp>");
            writer.AppendLine($"#include <{settings.Namespace}/implement.hpp>");
            writer.AppendLine($"#include <{declSet[decl.Name]}/{decl.Name.Underscore()}_data.hpp>");
            writer.AppendLine($"#include <dc/platform/context/context_base.hpp>");

            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"namespace {settings.Namespace}");
            writer.AppendLine("{");
            writer.PushIndent();
            BuildClassImplementation(decl, writer);
            writer.PopIndent();
            writer.AppendLine("}");

            return(writer.ToString());
        }
        public static string BuildDataHeader(TypeDecl decl, Dictionary <string, string> declSet)
        {
            var writer = new CodeWriter();

            var settings = GeneratorSettingsProvider.Get(decl.Module.ModuleName);

            writer.AppendLines(settings.Copyright);
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine("#pragma once");
            writer.AppendNewLineWithoutIndent();

            // includes
            var includes = IncludesProvider.ForDataHeader(decl, declSet);

            foreach (string include in includes)
            {
                writer.AppendLine(include);
            }
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"namespace {settings.Namespace}");
            writer.AppendLine("{");
            writer.PushIndent();
            BuildClassDeclaration(decl, writer);
            writer.PopIndent();
            writer.AppendLine("}");

            return(writer.ToString());
        }
示例#4
0
        public static string BuildEnumSource(EnumDecl decl, Dictionary <string, string> includePath)
        {
            var writer   = new CodeWriter();
            var settings = GeneratorSettingsProvider.Get(decl.Module.ModuleName);

            writer.AppendLines(settings.Copyright);
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"#include <{settings.Namespace}/precompiled.hpp>");
            writer.AppendLine($"#include <{settings.Namespace}/implement.hpp>");
            writer.AppendLine($"#include <{includePath[decl.Name]}/{decl.Name.Underscore()}.hpp>");
            writer.AppendNewLineWithoutIndent();

            writer.AppendLine($"namespace {settings.Namespace}");
            writer.AppendLine("{");
            writer.PushIndent();
            BuildEnumImplementation(decl, writer);
            writer.PopIndent();
            writer.AppendLine("}");

            return(writer.ToString());
        }
        /// <summary>
        /// Generate python classes from declaration.
        /// </summary>
        public static string Build(TypeDecl decl, List <IDecl> declarations)
        {
            var writer = new CodeWriter();

            string name = decl.Name;

            // Determine if we are inside datacentric package
            // based on module name. This affects the imports
            // and namespace use.
            bool insideDc = PyExtensions.GetPackage(decl) == "datacentric";

            // If not generating for DataCentric package, use dc. namespace
            // in front of datacentric types, otherwise use no prefix
            string dcNamespacePrefix = insideDc ? "" : "dc.";

            PythonImportsBuilder.WriteImports(decl, declarations, writer);

            writer.AppendNewLineWithoutIndent();
            writer.AppendNewLineWithoutIndent();

            // Get base classes for current declaration
            List <string> bases = new List <string>();

            if (decl.Keys.Any())
            {
                bases.Add(dcNamespacePrefix + "Record");
            }
            else if (decl.Inherit != null)
            {
                // Full package name and short namespace of the parent class,
                // or null if there is no parent
                bool   parentClassInDifferentModule = !PyExtensions.IsPackageEquals(decl, decl.Inherit);
                string parentPackage = PyExtensions.GetPackage(decl.Inherit);
                string parentClassNamespacePrefix =
                    parentClassInDifferentModule ? PyExtensions.GetAlias(parentPackage) + "." : "";
                bases.Add(parentClassNamespacePrefix + decl.Inherit.Name);
            }
            else
            {
                bases.Add(dcNamespacePrefix + "Data");
            }

            if (decl.Kind == TypeKind.Abstract)
            {
                bases.Add("ABC");
            }

            // Python 3.8:
            // if (decl.Kind == TypeKind.Final)
            // writer.AppendLine("@final");
            writer.AppendLine("@attr.s(slots=True, auto_attribs=True)");
            writer.AppendLine($"class {name}({string.Join(", ", bases)}):");
            writer.PushIndent();
            writer.AppendLines(CommentHelper.PyComment(decl.Comment));

            writer.AppendNewLineWithoutIndent();
            if (!decl.Elements.Any())
            {
                writer.AppendLine("pass");
            }

            foreach (var element in decl.Elements)
            {
                // TODO: Should be replaced with callable with specific format instead of skipping
                string skipRepresentation = element.Vector == YesNo.Y ? ", repr=False" : "";

                writer.AppendLine($"{element.Name.Underscore()}: {GetTypeHint(decl, element)} = attr.ib(default=None, kw_only=True{skipRepresentation}{GetMetaData(element)})");
                writer.AppendLines(CommentHelper.PyComment(element.Comment));
                if (element != decl.Elements.Last())
                {
                    writer.AppendNewLineWithoutIndent();
                }
            }

            // Add to_key and create_key() methods
            if (decl.Keys.Any())
            {
                var keyElements = decl.Elements.Where(e => decl.Keys.Contains(e.Name)).ToList();

                writer.AppendNewLineWithoutIndent();
                writer.AppendLine("def to_key(self) -> str:");
                writer.PushIndent();
                writer.AppendLine(CommentHelper.PyComment($"Get {decl.Name} key."));
                writer.AppendLines($"return '{decl.Name}='{GetToKeyArgs(decl.Name, keyElements, true)}");
                writer.PopIndent();

                writer.AppendNewLineWithoutIndent();

                var    namedParams       = keyElements.Select(e => $"{e.Name.Underscore()}: {GetTypeHint(decl, e)}").ToList();
                var    joinedNamedParams = string.Join(", ", namedParams);
                string start             = "def create_key(";

                // Check if tokens should be separated by new line
                if (4 + start.Length + joinedNamedParams.Length > 120)
                {
                    var indent = new string(' ', start.Length);
                    joinedNamedParams = string.Join("," + Environment.NewLine + indent, namedParams);
                }

                writer.AppendLine("@classmethod");
                writer.AppendLines($"def create_key(cls, *, {joinedNamedParams}) -> str:");

                writer.PushIndent();
                writer.AppendLine(CommentHelper.PyComment($"Create {decl.Name} key."));
                writer.AppendLines($"return '{decl.Name}='{GetToKeyArgs(decl.Name, keyElements, false)}");
                writer.PopIndent();
            }

            if (decl.Declare != null)
            {
                writer.AppendNewLineWithoutIndent();
                WriteMethods(decl, writer);
            }

            // Class end
            writer.PopIndent();

            return(writer.ToString());
        }