private bool ParseConsts(Unit unit, XmlElement elem, string comment)
        {
            var name = elem.GetAttribute("name");
            var type = elem.GetAttribute("type");

            if (String.IsNullOrEmpty(name))
            {
                return(false);
            }
            if (String.IsNullOrEmpty(type))
            {
                type = "int32";  // default type
            }
            var def = new ConstsDef {
                Name    = name,
                Type    = type,
                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;
                if (child.IsEmpty)
                {
                    continue;
                }
                switch (child.Name)
                {
                case "const":
                    if (ParseConstant(def, child, subComment) == false)
                    {
                        return(false);
                    }
                    break;

                default:
                    break;
                }
                subComment = null;
            }
            unit.Definitions.Add(def);
            return(true);
        }
Beispiel #2
0
 private static void PreprocessConsts(ConstsDef def)
 {
     if (def.Type == "string")
     {
         foreach (var constant in def.Constants)
         {
             constant.Value = "\"" + constant.Value + "\"";
         }
     }
 }
        private bool ParseConstant(ConstsDef def, XmlElement elem, string comment)
        {
            var name = elem.GetAttribute("name");

            if (String.IsNullOrEmpty(name))
            {
                return(false);
            }
            var element = new ConstsDef.Constant {
                Name    = name,
                Value   = elem.InnerText.Trim(),
                Comment = comment
            };

            def.Constants.Add(element);
            return(true);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public override void FormatConsts(ConstsDef def)
        {
            if (nativeTypes.ContainsKey(def.Type))
            {
                def.NativeType = nativeTypes[def.Type];
            }
            else
            {
                return;
            }

            PreprocessConsts(def);

            FormatComment(0, def.Comment);
            Out(0, "public static class {0}", def.Name);
            Out(0, "{");
            foreach (var constant in def.Constants)
            {
                FormatComment(1, constant.Comment);
                Indent(1);
                Writer.Write("public const {0} {1}", def.NativeType, constant.Name);
                if (!String.IsNullOrEmpty(constant.Value))
                {
                    Writer.Write(" = {0}", constant.Value);
                }
                Writer.Write(';');
                NewLine();
            }
            NewLine();
            Out(1, "private static ConstsInfo<{0}> info;", def.NativeType);
            NewLine();
            Out(1, "static {0}()", def.Name);
            Out(1, "{");
            Out(2, "info = new ConstsInfo<{0}>();", def.NativeType);
            foreach (var constant in def.Constants)
            {
                Out(2, "info.Add(\"{0}\", {1});", constant.Name, constant.Value);
            }
            Out(1, "}");
            NewLine();
            Out(1, "public static bool ContainsName(string name)", def.NativeType);
            Out(1, "{");
            Out(2, "return info.ContainsName(name);");
            Out(1, "}");
            NewLine();
            Out(1, "public static bool ContainsValue({0} value)", def.NativeType);
            Out(1, "{");
            Out(2, "return info.ContainsValue(value);");
            Out(1, "}");
            NewLine();
            Out(1, "public static string GetName({0} value)", def.NativeType);
            Out(1, "{");
            Out(2, "return info.GetName(value);");
            Out(1, "}");
            NewLine();
            Out(1, "public static {0} Parse(string name)", def.NativeType);
            Out(1, "{");
            Out(2, "return info.Parse(name);");
            Out(1, "}");
            NewLine();
            Out(1, "public static bool TryParse(string name, out {0} result)", def.NativeType);
            Out(1, "{");
            Out(2, "return info.TryParse(name, out result);");
            Out(1, "}");
            Out(0, "}");
        }
Beispiel #7
0
 public abstract void FormatConsts(ConstsDef def);