Пример #1
0
 private static void PreprocessConsts(ConstsDef def)
 {
     if (def.Type == "string")
     {
         foreach (var constant in def.Constants)
         {
             constant.Value = "\"" + constant.Value + "\"";
         }
     }
 }
Пример #2
0
        public override void FormatConsts(ConstsDef def)
        {
            if (nativeTypes.ContainsKey(def.Type))
            {
                def.NativeType = nativeTypes[def.Type];
            }
            else
            {
                return;
            }

            PreprocessConsts(def);

            FormatComments(0, def.Comments);
            Indent(0); Out.WriteLine("public static class {0}", def.Name);
            Indent(0); Out.WriteLine("{");
            foreach (var constant in def.Constants)
            {
                FormatComments(1, constant.Comments);
                Indent(1);
                Out.Write("public const {0} {1}", def.NativeType, constant.Name);
                if (!String.IsNullOrEmpty(constant.Value))
                {
                    Out.Write(" = {0};", constant.Value);
                }
                Out.WriteLine();
            }
            Out.WriteLine();
            Indent(1); Out.WriteLine("private static ConstsInfo<{0}> info;", def.NativeType);
            Out.WriteLine();
            Indent(1); Out.WriteLine("static {0}()", def.Name);
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("info = new ConstsInfo<{0}>();", def.NativeType);
            foreach (var constant in def.Constants)
            {
                Indent(2);
                Out.WriteLine("info.Add(\"{0}\", {1});", constant.Name, constant.Value);
            }
            Indent(1); Out.WriteLine("}");
            Out.WriteLine();
            Indent(1); Out.WriteLine("public static string GetName({0} value)", def.NativeType);
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("return info.GetName(value);");
            Indent(1); Out.WriteLine("}");
            Out.WriteLine();
            Indent(1); Out.WriteLine("public static {0} Parse(string name)", def.NativeType);
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("return info.Parse(name);");
            Indent(1); Out.WriteLine("}");
            Out.WriteLine();
            Indent(1); Out.WriteLine("public static bool TryParse(string name, out {0} result)", def.NativeType);
            Indent(1); Out.WriteLine("{");
            Indent(2); Out.WriteLine("return info.TryParse(name, out result);");
            Indent(1); Out.WriteLine("}");
            Out.WriteLine();
            Indent(0); Out.WriteLine("}");
        }
Пример #3
0
        private bool ParseConsts(Document doc, XmlElement elem, string comments)
        {
            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();
            def.Name = name;
            def.Type = type;
            def.Comments = comments;

            string subComments = null;
            var node = elem.FirstChild;
            for ( ; node != null; node = node.NextSibling)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    if (node.NodeType == XmlNodeType.Comment)
                    {
                        subComments = node.Value.Trim();
                    }
                    else
                    {
                        subComments = null;
                    }
                    continue;
                }
                var child = (XmlElement)node;
                if (child.IsEmpty)
                {
                    continue;
                }
                switch (child.Name)
                {
                    case "const":
                        if (ParseConstant(def, child, subComments) == false)
                        {
                            return false;
                        }
                        break;
                    default:
                        break;
                }
                subComments = null;
            }
            doc.Definitions.Add(def);
            return true;
        }
Пример #4
0
 public abstract void FormatConsts(ConstsDef def);
Пример #5
0
 private bool ParseConstant(ConstsDef def, XmlElement elem, string comments)
 {
     var name = elem.GetAttribute("name");
     if (String.IsNullOrEmpty(name))
     {
         return false;
     }
     var element = new ConstsDef.Constant();
     element.Name = name;
     element.Value = elem.InnerText.Trim();
     element.Comments = comments;
     def.Constants.Add(element);
     return true;
 }