예제 #1
0
 private void FormatEqualsTo(CellDef def)
 {
     Out(1, "protected override bool EqualsTo(Cell other)");
     Out(1, "{");
     Out(2, "if (!base.EqualsTo(other))");
     Out(2, "{");
     Out(3, "return false;");
     Out(2, "}");
     if (def.HasProperties)
     {
         Out(2, "{0} o = ({0})other;", def.Name);
         foreach (var property in def.Properties)
         {
             if (Types.IsCollective(property.TypeSpec.Type))
             {
                 Out(2, "if (!{0}.EqualsEx(o.{0}))", property.NativeName);
             }
             else
             {
                 Out(2, "if ({0} != o.{0})", property.NativeName);
             }
             Out(2, "{");
             Out(3, "return false;");
             Out(2, "}");
         }
     }
     Out(2, "return true;");
     Out(1, "}");
 }
예제 #2
0
        private static void PreprocessProperties(CellDef def)
        {
            int index = 0;

            foreach (var property in def.Properties)
            {
                property.Index = index++;

                property.NativeName = FirstToLower(property.Name) + "_";
                if (!def.IsLocal)
                {
                    property.Name = FirstToUpper(property.Name);
                }

                if (Types.IsPrimitive(property.TypeSpec.Type))
                {
                    if (String.IsNullOrEmpty(property.DefaultValue))
                    {
                        property.DefaultValue = defaultValues[property.TypeSpec.Type];
                    }
                    if (property.TypeSpec.Type == "string")
                    {
                        property.DefaultValue = "\"" + property.DefaultValue + "\"";
                    }
                }
                else
                {
                    property.DefaultValue = "null";
                }

                property.NativeType = FormatTypeSpec(property.TypeSpec);
            }
        }
예제 #3
0
        private void FormatGetHashCode(CellDef def)
        {
            Out(1, "public override int GetHashCode(Fingerprint fingerprint)");
            Out(1, "{");
            Out(2, "var hash = new Hash(base.GetHashCode(fingerprint));");
            if (def.HasProperties)
            {
                Out(2, "if (fingerprint.Length <= tag.Offset)");
                Out(2, "{");
                Out(3, "return hash.Code;");
                Out(2, "}");

                Out(2, "var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Out(2, "if (touched[{0}])", property.Index);
                    Out(2, "{");
                    Out(3, "hash.Update(tag.Offset + {0});", property.Index);
                    Out(3, "hash.Update({0});", property.NativeName);
                    Out(2, "}");
                }
            }
            Out(2, "return hash.Code;");
            Out(1, "}");
        }
예제 #4
0
        private void FormatGetType(CellDef def)
        {
            if (def.IsEvent)
            {
                Out(1, "public override int GetTypeId()");
                Out(1, "{");
                Out(2, "return tag.TypeId;");
                Out(1, "}");
                NewLine();
            }

            Out(1, "public override Cell.Tag GetTypeTag() ");
            Out(1, "{");
            Out(2, "return tag;");
            Out(1, "}");

            if (def.IsEvent)
            {
                NewLine();
                Out(1, "public override Func<Event> GetFactoryMethod()");
                Out(1, "{");
                Out(2, "return {0}.New;", def.Name);
                Out(1, "}");
            }
        }
예제 #5
0
        private void FormatIsEquivalent(CellDef def)
        {
            Out(1, "protected override bool IsEquivalent(Cell other, Fingerprint fingerprint)");
            Out(1, "{");
            Out(2, "if (!base.IsEquivalent(other, fingerprint))");
            Out(2, "{");
            Out(3, "return false;");
            Out(2, "}");
            if (def.HasProperties)
            {
                Out(2, "{0} o = ({0})other;", def.Name);
                Out(2, "var touched = new Capo<bool>(fingerprint, tag.Offset);");

                foreach (var property in def.Properties)
                {
                    Out(2, "if (touched[{0}])", property.Index);
                    Out(2, "{");
                    if (Types.IsCollective(property.TypeSpec.Type))
                    {
                        Out(3, "if (!{0}.EqualsEx(o.{0}))", property.NativeName);
                    }
                    else
                    {
                        Out(3, "if ({0} != o.{0})", property.NativeName);
                    }
                    Out(3, "{");
                    Out(4, "return false;");
                    Out(3, "}");
                    Out(2, "}");
                }
            }
            Out(2, "return true;");
            Out(1, "}");
        }
예제 #6
0
 private void FormatMethods(CellDef def)
 {
     FormatStaticConstructor(def);
     NewLine();
     FormatConstructor(def);
     NewLine();
     FormatEqualsTo(def);
     NewLine();
     FormatGetHashCode(def);
     NewLine();
     FormatGetType(def);
     NewLine();
     FormatIsEquivalent(def);
     if (!def.IsLocal)
     {
         NewLine();
         FormatDeserialize(def);
         NewLine();
         FormatSerialize(def);
     }
     NewLine();
     FormatDescribe(def);
     if (!def.IsLocal)
     {
         NewLine();
         FormatInitializer(def);
     }
 }
        private bool ParseCellProperty(CellDef def, XmlElement elem, string comment)
        {
            var name = elem.GetAttribute("name");
            var type = elem.GetAttribute("type");

            if (String.IsNullOrEmpty(name))
            {
                return(false);
            }
            if (String.IsNullOrEmpty(type))
            {
                return(false);
            }
            var property = new CellDef.Property {
                Name         = name,
                DefaultValue = elem.InnerText.Trim(),
                Comment      = comment
            };

            def.Properties.Add(property);

            property.TypeSpec = Types.Parse(type);
            if (property.TypeSpec == null)
            {
                return(false);
            }

            return(true);
        }
예제 #8
0
        private void FormatProperties(CellDef def)
        {
            var leading = true;

            foreach (var property in def.Properties)
            {
                if (leading)
                {
                    leading = false;
                }
                else
                {
                    NewLine();
                }
                FormatComment(1, property.Comment);
                Out(1, "public {0} {1}", property.NativeType, property.Name);
                Out(1, "{");
                Out(2, "get {{ return {0}; }}", property.NativeName);
                Out(2, "set");
                Out(2, "{");
                Out(3, "fingerprint.Touch(tag.Offset + {0});", property.Index);
                Out(3, "{0} = value;", property.NativeName);
                Out(2, "}");
                Out(1, "}");
            }
        }
예제 #9
0
 private void FormatFields(CellDef def)
 {
     foreach (var property in def.Properties)
     {
         Out(1, "private {0} {1};", property.NativeType, property.NativeName);
     }
 }
예제 #10
0
        public override void FormatCell(CellDef def)
        {
            def.BaseClass = def.Base;
            if (String.IsNullOrEmpty(def.BaseClass))
            {
                def.BaseClass = (def.IsEvent ? "Event" : "Cell");
            }
            PreprocessProperties(def);

            FormatComment(0, def.Comment);
            Out(0, "public class {0} : {1}", def.Name, def.BaseClass);
            Out(0, "{");
            Out(1, "protected static new readonly Tag tag;");
            NewLine();
            if (def.IsEvent)
            {
                Out(1, "public static new int TypeId { get { return tag.TypeId; } }");
                NewLine();
            }
            FormatFields(def);
            if (def.HasProperties)
            {
                NewLine();
            }
            FormatProperties(def);
            if (def.HasProperties)
            {
                NewLine();
            }
            FormatMethods(def);
            Out(0, "}");
        }
예제 #11
0
 private void FormatInitializer(CellDef def)
 {
     Out(1, "private void Initialize()");
     Out(1, "{");
     foreach (var property in def.Properties)
     {
         Out(2, "{0} = {1};", property.NativeName, property.DefaultValue);
     }
     Out(1, "}");
 }
예제 #12
0
 private void FormatDescribe(CellDef def)
 {
     Out(1, "protected override void Describe(StringBuilder stringBuilder)");
     Out(1, "{");
     Out(2, "base.Describe(stringBuilder);");
     foreach (var property in def.Properties)
     {
         Out(2, "stringBuilder.AppendFormat(\" {0}:{{0}}\", {1}.ToStringEx());",
             property.Name, property.NativeName);
     }
     Out(1, "}");
 }
예제 #13
0
        private void FormatSerialize(CellDef def)
        {
            // GetLength
            Out(1, "public override int GetLength(Type targetType, ref bool flag)");
            Out(1, "{");
            Out(2, "int length = base.GetLength(targetType, ref flag);");
            Out(2, "if (!flag) { return length; }");
            if (def.HasProperties)
            {
                Out(2, "var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Out(2, "if (touched[{0}])", property.Index);
                    Out(2, "{");
                    Out(3, "length += Serializer.GetLength({0});", property.NativeName);
                    Out(2, "}");
                }
            }
            Out(2, "if (targetType != null && targetType == typeof({0}))", def.Name);
            Out(2, "{");
            Out(3, "flag = false;");
            Out(2, "}");
            Out(2, "return length;");
            Out(1, "}");

            // Serialize(Serializer)
            NewLine();
            Out(1, "public override void Serialize(Serializer serializer,");
            Out(2, "Type targetType, ref bool flag)");
            Out(1, "{");
            Out(2, "base.Serialize(serializer, targetType, ref flag);");
            Out(2, "if (!flag) { return; }");
            if (def.HasProperties)
            {
                Out(2, "var touched = new Capo<bool>(fingerprint, tag.Offset);");
                foreach (var property in def.Properties)
                {
                    Out(2, "if (touched[{0}])", property.Index);
                    Out(2, "{");
                    Out(3, "serializer.Write({0});", property.NativeName);
                    Out(2, "}");
                }
            }
            Out(2, "if (targetType != null && targetType == typeof({0}))", def.Name);
            Out(2, "{");
            Out(3, "flag = false;");
            Out(2, "}");
            Out(1, "}");
        }
예제 #14
0
 private void FormatDeserialize(CellDef def)
 {
     // Deserialize(Deserializer)
     Out(1, "public override void Deserialize(Deserializer deserializer)");
     Out(1, "{");
     Out(2, "base.Deserialize(deserializer);");
     if (def.HasProperties)
     {
         Out(2, "var touched = new Capo<bool>(fingerprint, tag.Offset);");
         foreach (var property in def.Properties)
         {
             Out(2, "if (touched[{0}])", property.Index);
             Out(2, "{");
             Out(3, "deserializer.Read(out {0});", property.NativeName);
             Out(2, "}");
         }
     }
     Out(1, "}");
 }
예제 #15
0
 private void FormatConstructor(CellDef def)
 {
     Out(1, "public {0}()", def.Name);
     Out(2, ": base(tag.NumProps)");
     Out(1, "{");
     if (!def.IsLocal)
     {
         Out(2, "Initialize();");
     }
     Out(1, "}");
     NewLine();
     Out(1, "protected {0}(int length)", def.Name);
     Out(2, ": base(length + tag.NumProps)");
     Out(1, "{");
     if (!def.IsLocal)
     {
         Out(2, "Initialize();");
     }
     Out(1, "}");
 }
예제 #16
0
        private void FormatStaticConstructor(CellDef def)
        {
            string baseTag = def.Base;

            if (String.IsNullOrEmpty(baseTag))
            {
                baseTag = (def.IsEvent ? "Event.tag" : "null");
            }
            else
            {
                baseTag += ".tag";
            }
            Out(1, "static {0}()", def.Name);
            Out(1, "{");
            Indent(2);
            Writer.Write("tag = new Tag({0}, typeof({1}), {2}", baseTag, def.Name,
                         def.Properties.Count);
            if (def.IsEvent)
            {
                int    i;
                string s = ((EventDef)def).Id;
                Writer.WriteLine(",");
                Writer.Write("                    ");
                if (!Int32.TryParse(s, out i))
                {
                    Writer.Write("(int)");
                }
                Writer.Write("{0}", s);
            }
            Writer.WriteLine(");");
            Out(1, "}");

            if (def.IsEvent)
            {
                NewLine();
                Out(1, "public new static {0} New()", def.Name);
                Out(1, "{");
                Out(2, "return new {0}();", def.Name);
                Out(1, "}");
            }
        }
예제 #17
0
        private Unit Normalize(Root root)
        {
            Unit unit = new Unit {
                Namespace = root.Namespace
            };

            if (root.References != null)
            {
                for (int i = 0; i < root.References.Count; ++i)
                {
                    var r = root.References[i];
                    if (r.GetType() == typeof(NamespaceRef))
                    {
                        var reference = new Reference {
                            Target = r.Target
                        };
                        unit.References.Add(reference);
                    }
                }
            }

            if (root.Definitions != null)
            {
                for (int i = 0; i < root.Definitions.Count; ++i)
                {
                    var def = root.Definitions[i];
                    if (def is Cell)
                    {
                        bool    isEvent    = (def.GetType() == typeof(Event));
                        Cell    c          = (Cell)def;
                        CellDef definition = (isEvent ? new EventDef() : new CellDef());

                        definition.Name = c.Name;
                        definition.Base = c.Base;

                        if (!String.IsNullOrEmpty(c.Local) && c.Local.ToLower() == "true")
                        {
                            definition.IsLocal = true;
                        }
                        if (isEvent)
                        {
                            ((EventDef)definition).Id = ((Event)c).Id;
                        }

                        if (c.Properties != null)
                        {
                            for (int j = 0; j < c.Properties.Count; ++j)
                            {
                                var p        = c.Properties[j];
                                var property = new CellDef.Property {
                                    Name         = p.Name,
                                    TypeSpec     = Types.Parse(p.Type),
                                    DefaultValue = p.Default
                                };
                                definition.Properties.Add(property);
                            }
                        }

                        unit.Definitions.Add(definition);
                    }
                    else if (def.GetType() == typeof(Consts))
                    {
                        var c          = (Consts)def;
                        var definition = new ConstsDef {
                            Name = c.Name
                        };

                        var type = c.Type;
                        if (String.IsNullOrEmpty(type))
                        {
                            type = "int32";
                        }
                        definition.Type = type;

                        if (c.Elements != null)
                        {
                            for (int j = 0; j < c.Elements.Count; ++j)
                            {
                                var e        = c.Elements[j];
                                var constant = new ConstsDef.Constant {
                                    Name  = e.Name,
                                    Value = e.Value
                                };
                                definition.Constants.Add(constant);
                            }
                        }

                        unit.Definitions.Add(definition);
                    }
                }
            }

            return(unit);
        }
예제 #18
0
 public abstract void FormatCell(CellDef def);
예제 #19
0
        private Unit Normalize(Document doc)
        {
            Unit unit = new Unit {
                Namespace = doc.Namespace
            };

            if (doc.References != null)
            {
                for (int i = 0; i < doc.References.Count; ++i)
                {
                    var r = doc.References[i];
                    if (!String.IsNullOrEmpty(r.Type) && r.Type.ToLower() == "namespace")
                    {
                        var reference = new Reference {
                            Target = r.Target
                        };
                        unit.References.Add(reference);
                    }
                }
            }

            if (doc.Definitions != null)
            {
                for (int i = 0; i < doc.Definitions.Count; ++i)
                {
                    var def = doc.Definitions[i];
                    if (def.Class == "cell" || def.Class == "event")
                    {
                        bool    isEvent    = (def.Class == "event");
                        CellDef definition = (isEvent ? new EventDef() : new CellDef());

                        definition.Name = def.Name;
                        definition.Base = def.Base;

                        if (!String.IsNullOrEmpty(def.Local) && def.Local.ToLower() == "true")
                        {
                            definition.IsLocal = true;
                        }
                        if (isEvent)
                        {
                            ((EventDef)definition).Id = def.Id;
                        }

                        if (def.Properties != null)
                        {
                            for (int j = 0; j < def.Properties.Count; ++j)
                            {
                                var p        = def.Properties[j];
                                var property = new CellDef.Property {
                                    Name         = p.Name,
                                    TypeSpec     = Types.Parse(p.Type),
                                    DefaultValue = p.Default
                                };
                                definition.Properties.Add(property);
                            }
                        }

                        unit.Definitions.Add(definition);
                    }
                    else if (def.Class == "consts")
                    {
                        var definition = new ConstsDef {
                            Name = def.Name
                        };

                        var type = def.Type;
                        if (String.IsNullOrEmpty(type))
                        {
                            type = "int32";
                        }
                        definition.Type = type;

                        if (def.Elements != null)
                        {
                            for (int j = 0; j < def.Elements.Count; ++j)
                            {
                                var e        = def.Elements[j];
                                var constant = new ConstsDef.Constant {
                                    Name  = e.Name,
                                    Value = e.Value
                                };
                                definition.Constants.Add(constant);
                            }
                        }

                        unit.Definitions.Add(definition);
                    }
                }
            }

            return(unit);
        }
        private bool ParseCell(Unit unit, XmlElement elem, string comment)
        {
            var name = elem.GetAttribute("name");

            if (String.IsNullOrEmpty(name))
            {
                return(false);
            }
            var isEvent = (elem.Name == "event");
            var id      = elem.GetAttribute("id");

            if (isEvent && String.IsNullOrEmpty(id))
            {
                return(false);
            }
            CellDef def = (isEvent ? new EventDef() : new CellDef());

            def.Name = name;
            if (isEvent)
            {
                ((EventDef)def).Id = id;
            }
            def.Base = elem.GetAttribute("base");
            var local = elem.GetAttribute("local");

            if (!String.IsNullOrEmpty(local) && local.EndsWith("rue"))
            {
                def.IsLocal = true;
            }
            def.Comment = comment;

            string subComment = null;
            var    node       = elem.FirstChild;

            for ( ; node != null; node = node.NextSibling)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    if (node.NodeType == XmlNodeType.Comment)
                    {
                        subComment = node.Value.Trim();
                    }
                    else
                    {
                        subComment = null;
                    }
                    continue;
                }
                var child = (XmlElement)node;
                switch (child.Name)
                {
                case "property":
                    if (ParseCellProperty(def, child, subComment) == false)
                    {
                        return(false);
                    }
                    break;

                default:
                    break;
                }
                subComment = null;
            }
            unit.Definitions.Add(def);
            return(true);
        }