internal void Parse() { Summarys.Clear(); List <Line> list = new List <Line>(); foreach (Line line in Lines) { if (line.Content.StartsWith("//")) { if (line.Content.Length > 2) { Summarys.Add(line.Content.Substring(2).Trim()); } } else { list.Add(line); } } ParseInfos(list); foreach (ProtoBufferField field in Message.Fields) { if (Equals(field)) { continue; } if (field.FieldNumber == FieldNumber) { throw new ProtoBufferException(string.Format("在文件名{0},message:{1},FieldNumber:{2}重复。{3}", Message.File.FileName, Message.Name, FieldNumber, list[0].ToString())); } if (Name.Equals(field.Name)) { throw new ProtoBufferException(string.Format("在文件名{0},message:{1},名称{2}重复,FieldNumber:{3}。{4}", Message.File.FileName, Message.Name, Name, FieldNumber, list[0].ToString())); } } }
/// <summary> /// 移除msg想关信息,只留下field相关信息 /// </summary> /// <returns></returns> private List <Line> ParseMsgElement() { List <Line> lines = new List <Line>(); bool hasBegin = false; foreach (Line line in Lines) { if (hasBegin) { if (line.Content.StartsWith("}")) { hasBegin = false; } else { lines.Add(line); } } else { if (line.Content.StartsWith("//")) { if (line.Content.Length > 2) { Summarys.Add(line.Content.Substring(2).Trim()); } } if (line.Content.Contains("{")) { //msg type and begin string[] strings = line.Content.Split(new string[] { " ", "{" }, StringSplitOptions.RemoveEmptyEntries); if (strings.Length != 2) { throw new ProtoBufferException(string.Format("message 解析出错:{0}", line.ToString())); } if (strings[0].Equals("message")) { DataType = DataType.Class; Name = strings[1]; hasBegin = true; } else if (strings[0].Equals("enum")) { DataType = DataType.Enum; Name = strings[1]; hasBegin = true; } else { throw new ProtoBufferException(string.Format("message 解析出错:{0}", line.ToString())); } for (int i = 0; i < Name.Length; i++) { if (i == 0) { if (!Char.IsUpper(Name[i])) { throw new ProtoBufferException(string.Format("Message命名不正确,第一个字母必须是大写。{0}", line.ToString())); } } else { if (!Char.IsLetter(Name[i])) { throw new ProtoBufferException(string.Format("Message命名不正确,出现了非字母字符。{0}", line.ToString())); } } } } } } if (hasBegin) { throw new ProtoBufferException("message 只有开头没有结尾"); } return(lines); }