Example #1
0
        public bool HasAttr(string name)
        {
            if (attrs.Count == 0)
            {
                return(false);
            }
            IDLAttr a = attrs.Find(attr => attr.attr_name == name);

            if (a != null)
            {
                return(true);
            }
            return(false);
        }
Example #2
0
        public string[] GetAttrParams(string name)
        {
            if (attrs.Count == 0)
            {
                return(null);
            }

            IDLAttr a = attrs.Find(attr => attr.attr_name == name);

            if (a != null)
            {
                return(a.attr_params);
            }
            return(null);
        }
Example #3
0
        public string[] attr_params; //参数按逗号拆分

        public static IDLAttr ParseAttr(string line)
        {
            line = line.Substring(line.IndexOf('[') + 1, line.IndexOf(']') - line.IndexOf('[') - 1);
            if (string.IsNullOrEmpty(line))
            {
                return(null);
            }
            IDLAttr idlattr = new IDLAttr();

            //处理带参数的属性
            string[] ss = line.Split('(');
            idlattr.attr_name = ss[0];

            if (ss.Length > 1)
            {
                idlattr.attr_param  = ss[1].Split(')')[0];
                idlattr.attr_params = idlattr.attr_param.Split(',');
            }
            return(idlattr);
        }
Example #4
0
        public static IDLMeta Parse(string idl)
        {
            IDLMeta  meta    = new IDLMeta();
            IDLClass m_class = null;
            IDLEnum  m_enum  = null;

            string[] lines    = File.ReadAllLines(idl);
            string   metaname = new string(Path.GetFileNameWithoutExtension(idl).TakeWhile(c => c != '.').ToArray());

            meta.meta_file_path = Path.Combine(Path.GetDirectoryName(idl), metaname).Replace('\\', '/');
            meta.meta_name      = CodeGenHelper.NameMangling(metaname, NameManglingType.AaBb);
            string     comment      = null;
            IDLAttr    attr         = null;
            ParseState m_parseState = ParseState.End;

            for (int i = 0; i < lines.Count(); i++)
            {
                //注释
                string cc = parseComment(ref lines[i]);
                if (!string.IsNullOrEmpty(cc))
                {
                    comment = cc;
                }
                //空行
                if (Regex.IsMatch(lines[i], @"^\s*$"))
                {
                    continue;
                }
                //特性
                if (Regex.IsMatch(lines[i], @"^\s*\[.*\]\s*$"))
                {
                    attr = IDLAttr.ParseAttr(lines[i]);
                    continue;
                }
                //变量
                Match match = Regex.Match(lines[i], @"^\s*set\s+(\w+)\s*=\s*(\w+)\s*;\s*$");
                if (match.Success)
                {
                    string key = match.Groups[1].Value;
                    string val = match.Groups[2].Value;
                    meta.meta_variant.Add(key, val);
                    continue;
                }
                //引用
                match = Regex.Match(lines[i], @"^\s*using\s+(\w+)\s*;\s*$");
                if (match.Success)
                {
                    IDLUsing u = new IDLUsing();
                    u.comment    = comment;
                    u.using_name = match.Groups[1].Value;
                    u.Meta       = meta;
                    meta.meta_using.Add(u);
                    comment = null;
                    continue;
                }

                //结束
                if (Regex.IsMatch(lines[i], @"^\s*};?\s*$"))
                {
                    if (m_parseState == ParseState.End)
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }
                    if (m_parseState == ParseState.BeginClass)
                    {
                        meta.meta_class.Add(m_class.class_name, m_class);
                        m_class = null;
                    }
                    else if (m_parseState == ParseState.BeginEnum)
                    {
                        meta.meta_enum.Add(m_enum.enum_name, m_enum);
                        m_enum = null;
                    }
                    m_parseState = ParseState.End;

                    continue;
                }
                //开始
                if (Regex.IsMatch(lines[i], @"^\s*{\s*$"))
                {
                    if (m_parseState != ParseState.BeginClass && m_parseState != ParseState.BeginEnum)
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }
                    continue;
                }
                //枚举开始
                match = Regex.Match(lines[i], @"^\s*enum\s*(\w+)\s*{?\s*$");
                if (match.Success)
                {
                    if (m_parseState != ParseState.End)
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }
                    m_parseState     = ParseState.BeginEnum;
                    m_enum           = new IDLEnum();
                    m_enum.comment   = comment;
                    m_enum.enum_name = match.Groups[1].Value;
                    m_enum.Meta      = meta;
                    attr             = null;//用完清空
                    comment          = null;
                    continue;
                }

                //结构体开始
                match = Regex.Match(lines[i], @"^\s*(struct|class)\s*(\w+)\s*{?\s*$");
                if (match.Success)
                {
                    if (m_parseState != ParseState.End)
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }
                    m_parseState       = ParseState.BeginClass;
                    m_class            = new IDLClass();
                    m_class.comment    = comment;
                    m_class.class_name = match.Groups[2].Value;
                    m_class.Meta       = meta;
                    if (attr != null)
                    {
                        m_class.attrs.Add(attr);
                    }
                    if (attr != null && attr.attr_name == "root")
                    {
                        meta.root_class_name = m_class.class_name;
                    }
                    attr    = null;//用完清空
                    comment = null;
                    continue;
                }


                //结构体
                if (m_parseState == ParseState.BeginClass)
                {
                    parseGenericType(ref lines[i]);
                    string def = parseDefaultValue(ref lines[i]);
                    Match  m   = Regex.Match(lines[i], @"\s*(\S+)\s+(\w+)\s*;\s*$");
                    if (m.Success == false)
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }

                    IDLClassField field = new IDLClassField();
                    field.comment       = comment;
                    field.type_name     = m.Groups[1].Value;
                    field.field_name    = m.Groups[2].Value;
                    field.default_value = def;
                    if (attr != null)
                    {
                        field.attrs.Add(attr);
                    }
                    field.Class = m_class;
                    if (!ParseFieldType(meta, field.type_name, field.field_type, true))
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }
                    m_class.fieldList.Add(field);
                    attr    = null;//用完清空
                    comment = null;
                    continue;
                }

                //枚举
                if (m_parseState == ParseState.BeginEnum)
                {
                    Match m = Regex.Match(lines[i], @"\s*(\w+)\s*=\s*(\w+)\s*,\s*$");
                    if (m.Success == false)
                    {
                        throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
                    }

                    IDLEnumField item = new IDLEnumField();
                    item.comment    = comment;
                    item.item_name  = m.Groups[1].Value;
                    item.item_value = m.Groups[2].Value;
                    item.Enum       = m_enum;
                    m_enum.enum_fields.Add(item.item_name, item);
                    attr    = null;//用完清空
                    comment = null;
                    continue;
                }

                throw new Exception($"idl文件错误:第{i + 1}行,{lines[i]}");
            }
            return(meta);
        }