示例#1
0
        public IEnumerable <String> Record(RecordDef r)
        {
            var Name = GetEscapedIdentifier(r.DefinitionName()) + GetGenericParameters(r.GenericParameters);

            foreach (var _Line in Combine(Begin(), GetXmlComment(r.Description)))
            {
                yield return(_Line);
            }
            yield return("[Record]");

            foreach (var _Line in Combine(Combine(Begin(), "public sealed class "), Name))
            {
                yield return(_Line);
            }
            yield return("{");

            foreach (var f in r.Fields)
            {
                foreach (var _Line in Combine(Begin(), GetXmlComment(f.Description)))
                {
                    yield return(_Line == "" ? "" : "    " + _Line);
                }
                foreach (var _Line in Combine(Combine(Combine(Combine(Combine(Begin(), "public "), GetTypeString(f.Type, r.NamespaceName())), " "), GetEscapedIdentifier(f.Name)), ";"))
                {
                    yield return(_Line == "" ? "" : "    " + _Line);
                }
            }
            yield return("}");
        }
示例#2
0
        public IEnumerable <String> Record(RecordDef r)
        {
            var Name = GetEscapedIdentifier(r.DefinitionName()) + GetGenericParameters(r.GenericParameters);

            yield return("#Record");

            foreach (var _Line in Combine(Combine(Combine(Begin(), "class "), Name), "(NamedTuple):"))
            {
                yield return(_Line);
            }
            foreach (var _Line in Combine(Combine(Begin(), "    "), GetXmlComment(r.Description)))
            {
                yield return(_Line);
            }
            yield return("");

            foreach (var f in r.Fields)
            {
                foreach (var _Line in Combine(Begin(), GetXmlComment(f.Description)))
                {
                    yield return(_Line == "" ? "" : "    " + _Line);
                }
                foreach (var _Line in Combine(Combine(Combine(Begin(), GetEscapedIdentifier(f.Name)), ": "), GetTypeString(f.Type, r.NamespaceName())))
                {
                    yield return(_Line == "" ? "" : "    " + _Line);
                }
            }
            if (r.Fields.Count == 0)
            {
                yield return("    " + "pass");
            }
        }
示例#3
0
文件: Cpp.cs 项目: blockspacer/niveum
        public IEnumerable <String> GetTypePredefinition(TypeDef t)
        {
            var    Name = t.DefinitionName();
            var    GenericParameterLine = GetGenericParameterLine(t.GenericParameters());
            String MetaType             = "class";

            if (t.OnPrimitive)
            {
                return(new List <String> {
                });
            }
            else if (t.OnAlias || t.OnRecord || t.OnTaggedUnion || t.OnClientCommand || t.OnServerCommand)
            {
                MetaType = "class";
            }
            else if (t.OnEnum)
            {
                return(new List <String> {
                });
            }
            if (t.OnClientCommand)
            {
                var c          = t.ClientCommand;
                var RequestRef = GetSuffixedTypeRef(c.Name, c.Version, "Request");
                var ReplyRef   = GetSuffixedTypeRef(c.Name, c.Version, "Reply");
                var Request    = new RecordDef {
                    Name = RequestRef.Name, Version = RequestRef.Version, GenericParameters = new List <VariableDef> {
                    }, Fields = c.OutParameters, Attributes = c.Attributes, Description = c.Description
                };
                var Reply = new TaggedUnionDef {
                    Name = ReplyRef.Name, Version = ReplyRef.Version, GenericParameters = new List <VariableDef> {
                    }, Alternatives = c.InParameters, Attributes = c.Attributes, Description = c.Description
                };
                return(TypePredefinition(Request.DefinitionName(), MetaType, t.GenericParameters()).Concat(TypePredefinition(Reply.DefinitionName(), MetaType, t.GenericParameters())));
            }
            else if (t.OnServerCommand)
            {
                var c        = t.ServerCommand;
                var EventRef = GetSuffixedTypeRef(c.Name, c.Version, "Event");
                var Event    = new RecordDef {
                    Name = EventRef.Name, Version = EventRef.Version, GenericParameters = new List <VariableDef> {
                    }, Fields = c.OutParameters, Attributes = c.Attributes, Description = c.Description
                };
                return(TypePredefinition(Event.DefinitionName(), MetaType, t.GenericParameters()));
            }
            else
            {
                return(TypePredefinition(Name, MetaType, t.GenericParameters()));
            }
        }
示例#4
0
        public Dictionary <String, IEnumerable <String> > GetPackageFiles(Schema Schema, String NamespaceName)
        {
            var NamespaceToClasses = new Dictionary <String, List <KeyValuePair <String, List <String> > > >();

            void AddClass(String ClassNamespaceName, String ClassName, IEnumerable <String> ClassContent)
            {
                if (!NamespaceToClasses.ContainsKey(ClassNamespaceName))
                {
                    NamespaceToClasses.Add(ClassNamespaceName, new List <KeyValuePair <String, List <String> > >());
                }
                NamespaceToClasses[ClassNamespaceName].Add(new KeyValuePair <String, List <String> >(ClassName, ClassContent.ToList()));
            }

            foreach (var c in Schema.Types)
            {
                if (c.OnPrimitive)
                {
                    if (c.VersionedName() == "Unit")
                    {
                        AddClass(c.NamespaceName(), "Unit", Primitive_Unit());
                    }
                    else if (c.VersionedName() == "Set")
                    {
                        AddClass(c.NamespaceName(), "Set", Primitive_Set());
                    }
                    else
                    {
                        var p = c.Primitive;
                        if (PrimitiveMapping.ContainsKey(p.VersionedName()))
                        {
                            var Name         = p.VersionedName();
                            var PlatformName = PrimitiveMapping[Name];
                            if (Name != PlatformName && p.GenericParameters.Count() == 0 && PlatformName != "Error")
                            {
                                AddClass(c.NamespaceName(), Name, Primitive(Name, PlatformName));
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                else if (c.OnAlias)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), Alias(c.Alias));
                }
                else if (c.OnRecord)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), Record(c.Record));
                }
                else if (c.OnTaggedUnion)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), TaggedUnion(c.TaggedUnion));
                }
                else if (c.OnEnum)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), Enum(c.Enum));
                }
                else if (c.OnClientCommand)
                {
                    var tc         = c.ClientCommand;
                    var RequestRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Request");
                    var Request    = new RecordDef {
                        Name = RequestRef.Name, Version = RequestRef.Version, GenericParameters = new List <VariableDef> {
                        }, Fields = tc.OutParameters, Attributes = tc.Attributes, Description = tc.Description
                    };
                    var ReplyRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Reply");
                    var Reply    = new TaggedUnionDef {
                        Name = ReplyRef.Name, Version = ReplyRef.Version, GenericParameters = new List <VariableDef> {
                        }, Alternatives = tc.InParameters, Attributes = tc.Attributes, Description = tc.Description
                    };
                    AddClass(c.NamespaceName(), Request.DefinitionName(), Record(Request));
                    AddClass(c.NamespaceName(), Reply.DefinitionName(), TaggedUnion(Reply));
                }
                else if (c.OnServerCommand)
                {
                    var tc       = c.ServerCommand;
                    var EventRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Event");
                    var Event    = new RecordDef {
                        Name = EventRef.Name, Version = EventRef.Version, GenericParameters = new List <VariableDef> {
                        }, Fields = tc.OutParameters, Attributes = tc.Attributes, Description = tc.Description
                    };
                    AddClass(c.NamespaceName(), Event.DefinitionName(), Record(Event));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            var scg    = Schema.GetSchemaClosureGenerator();
            var sc     = scg.GetClosure(Schema.TypeRefs.Concat(Schema.Types), new List <TypeSpec> {
            });
            var Tuples = sc.TypeSpecs.Where(t => t.OnTuple).ToList();

            foreach (var t in Tuples)
            {
                AddClass(NamespaceName, t.SimpleName(NamespaceName), Tuple(t, NamespaceName));
            }

            var Commands = Schema.Types.Where(t => t.OnClientCommand || t.OnServerCommand).Where(t => t.Version() == "").ToList();

            if (Commands.Count > 0)
            {
                AddClass(NamespaceName, "IApplicationClient", IApplicationClient(Commands, NamespaceName));
            }

            return(NamespaceToClasses.SelectMany(p => p.Value.Select(v => new KeyValuePair <String, IEnumerable <String> >(String.Join("/", p.Key.Split('.').Where(NamespacePart => NamespacePart != "").Select(NamespacePart => LowercaseCamelize(NamespacePart)).Concat(new String[] { v.Key })), WrapModule(p.Key, Schema.Imports, v.Value)))).ToDictionary(p => p.Key, p => p.Value));
        }
示例#5
0
        public Dictionary <String, IEnumerable <String> > GetPackageFiles(Schema Schema, String NamespaceName)
        {
            var NamespaceToClasses = new Dictionary <String, List <KeyValuePair <String, List <String> > > >();

            void AddClass(String ClassNamespaceName, String ClassName, IEnumerable <String> ClassContent)
            {
                if (!NamespaceToClasses.ContainsKey(ClassNamespaceName))
                {
                    NamespaceToClasses.Add(ClassNamespaceName, new List <KeyValuePair <String, List <String> > >());
                }
                NamespaceToClasses[ClassNamespaceName].Add(new KeyValuePair <String, List <String> >(ClassName, ClassContent.ToList()));
            }

            AddClass("niveum.lang", "Record", Attribute_Record());
            AddClass("niveum.lang", "Alias", Attribute_Alias());
            AddClass("niveum.lang", "TaggedUnion", Attribute_TaggedUnion());
            AddClass("niveum.lang", "Tag", Attribute_Tag());
            AddClass("niveum.lang", "Tuple", Attribute_Tuple());
            AddClass("niveum.lang", "Unit", Primitive_Unit());

            foreach (var c in Schema.Types)
            {
                if (c.OnPrimitive)
                {
                    continue;
                }
                else if (c.OnAlias)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), Alias(c.Alias));
                }
                else if (c.OnRecord)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), Record(c.Record));
                }
                else if (c.OnTaggedUnion)
                {
                    var tu      = c.TaggedUnion;
                    var TagName = GetSuffixedTypeName(tu.Name, tu.Version, "Tag", tu.NamespaceName());
                    AddClass(c.NamespaceName(), TagName, TaggedUnionTag(c.TaggedUnion));
                    AddClass(c.NamespaceName(), c.DefinitionName(), TaggedUnion(c.TaggedUnion));
                }
                else if (c.OnEnum)
                {
                    AddClass(c.NamespaceName(), c.DefinitionName(), Enum(c.Enum));
                }
                else if (c.OnClientCommand)
                {
                    var tc         = c.ClientCommand;
                    var RequestRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Request");
                    var Request    = new RecordDef {
                        Name = RequestRef.Name, Version = RequestRef.Version, GenericParameters = new List <VariableDef> {
                        }, Fields = tc.OutParameters, Attributes = tc.Attributes, Description = tc.Description
                    };
                    var ReplyRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Reply");
                    var Reply    = new TaggedUnionDef {
                        Name = ReplyRef.Name, Version = ReplyRef.Version, GenericParameters = new List <VariableDef> {
                        }, Alternatives = tc.InParameters, Attributes = tc.Attributes, Description = tc.Description
                    };
                    var ReplyTagName = GetSuffixedTypeName(Reply.Name, Reply.Version, "Tag", tc.NamespaceName());
                    AddClass(c.NamespaceName(), Request.DefinitionName(), Record(Request));
                    AddClass(c.NamespaceName(), ReplyTagName, TaggedUnionTag(Reply));
                    AddClass(c.NamespaceName(), Reply.DefinitionName(), TaggedUnion(Reply));
                }
                else if (c.OnServerCommand)
                {
                    var tc       = c.ServerCommand;
                    var EventRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Event");
                    var Event    = new RecordDef {
                        Name = EventRef.Name, Version = EventRef.Version, GenericParameters = new List <VariableDef> {
                        }, Fields = tc.OutParameters, Attributes = tc.Attributes, Description = tc.Description
                    };
                    AddClass(c.NamespaceName(), Event.DefinitionName(), Record(Event));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            var scg              = Schema.GetSchemaClosureGenerator();
            var sc               = scg.GetClosure(Schema.TypeRefs.Concat(Schema.Types), new List <TypeSpec> {
            });
            var Tuples           = sc.TypeSpecs.Where(t => t.OnTuple).ToList();
            var GenericTypeSpecs = sc.TypeSpecs.Where(t => t.OnGenericTypeSpec).ToList();

            foreach (var t in Tuples)
            {
                AddClass(NamespaceName, t.SimpleName(NamespaceName), Tuple(t, NamespaceName));
            }

            var GenericOptionalTypes = Schema.TypeRefs.Concat(Schema.Types).Where(t => t.NameMatches("Optional")).ToList();

            if (GenericOptionalTypes.Count > 0)
            {
                var GenericOptionalType = new TaggedUnionDef
                {
                    Name = new List <String> {
                        "Optional"
                    },
                    Version           = "",
                    GenericParameters = new List <VariableDef> {
                        new VariableDef {
                            Name = "T", Type = TypeSpec.CreateTypeRef(new TypeRef {
                                Name = new List <String> {
                                    "Type"
                                }, Version = ""
                            }), Attributes = new List <KeyValuePair <String, List <String> > > {
                            }, Description = ""
                        }
                    },
                    Alternatives = new List <VariableDef>
                    {
                        new VariableDef {
                            Name = "None", Type = TypeSpec.CreateTypeRef(new TypeRef {
                                Name = new List <String> {
                                    "Unit"
                                }, Version = ""
                            }), Attributes = new List <KeyValuePair <String, List <String> > > {
                            }, Description = ""
                        },
                        new VariableDef {
                            Name = "Some", Type = TypeSpec.CreateGenericParameterRef("T"), Attributes = new List <KeyValuePair <String, List <String> > > {
                            }, Description = ""
                        }
                    },
                    Attributes  = new List <KeyValuePair <String, List <String> > > {
                    },
                    Description = ""
                };
                {
                    var TagName = GetSuffixedTypeName(GenericOptionalType.Name, GenericOptionalType.Version, "Tag", GenericOptionalType.NamespaceName());
                    AddClass(NamespaceName, TagName, TaggedUnionTag(GenericOptionalType));
                }
                foreach (var gts in GenericTypeSpecs)
                {
                    if (gts.GenericTypeSpec.TypeSpec.OnTypeRef && gts.GenericTypeSpec.TypeSpec.TypeRef.NameMatches("Optional") && gts.GenericTypeSpec.ParameterValues.Count == 1)
                    {
                        var ElementType  = gts.GenericTypeSpec.ParameterValues.Single();
                        var Name         = "Opt" + ElementType.SimpleName(NamespaceName);
                        var Alternatives = GenericOptionalType.Alternatives.Select(a => new VariableDef {
                            Name = a.Name, Type = a.Type.OnGenericParameterRef ? ElementType : a.Type, Attributes = a.Attributes, Description = a.Description
                        }).ToList();
                        var tu = new TaggedUnionDef {
                            Name = new List <String> {
                                Name
                            }, Version = "", GenericParameters = new List <VariableDef> {
                            }, Alternatives = Alternatives, Attributes = GenericOptionalType.Attributes, Description = GenericOptionalType.Description
                        };
                        AddClass(NamespaceName, Name, TaggedUnion(tu, "OptionalTag"));
                    }
                }
            }

            return(NamespaceToClasses.SelectMany(p => p.Value.Select(v => new KeyValuePair <String, IEnumerable <String> >(String.Join("/", p.Key.Split('.').Where(NamespacePart => NamespacePart != "").Select(NamespacePart => LowercaseCamelize(NamespacePart)).Concat(new String[] { v.Key })), WrapModule(p.Key, Schema.Imports, v.Value)))).ToDictionary(p => p.Key, p => p.Value));
        }