Example #1
0
 public PB_Field(PB_Type type, string name, int index, bool repeated)
 {
     this.type     = type;
     this.name     = name;
     this.index    = index;
     this.repeated = repeated;
 }
Example #2
0
        public static PB_Message Parse(string protoDef)
        {
            protoDef = protoDef.Trim();
            if (!protoDef.StartsWith("message", System.StringComparison.OrdinalIgnoreCase))
            {
                return(Error(protoDef, "Invalid proto head"));
            }

            int indexL = protoDef.IndexOf('{');
            int indexR = protoDef.IndexOf('}');

            // Message ID
            string strId = protoDef.Substring(LEN_MSG, indexL - LEN_MSG - 1).Trim();
            int    id    = 0;

            if (!int.TryParse(strId, out id))
            {
                return(Error(strId, "Invalid Message ID"));
            }

            PB_Message message = new PB_Message();
            // Message Fields
            string strFields = protoDef.Substring(indexL + 1, indexR - indexL - 2).Trim();

            string[] fields = strFields.Split(SPLIT_FIELD, System.StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < fields.Length; ++i)
            {
                string[] segs     = fields[i].Split(SPLIT_SEG, System.StringSplitOptions.RemoveEmptyEntries);
                bool     repeated = segs[0].EndsWith("[]");
                string   strType  = repeated ? segs[0].Substring(0, segs[0].Length - 2) : segs[0];
                string   name     = segs[1];
                string   strIndex = segs[2];
                PB_Type  type;
                try {
                    type = (PB_Type)PB_Type.Parse(typeof(PB_Type), strType, true);
                } catch (System.Exception e) {
                    return(Error(fields[i], e.Message));
                }
                int index = 0;
                if (!int.TryParse(strIndex, out index))
                {
                    return(Error(fields[i], "Invalid index."));
                }
                message.Add(new PB_Field(type, name, index, repeated));
            }
            message.Sort();
            return(message);
        }