private void ProcessTableAndFieldNamespace(FbsStructure fbsStructure) { string targetNamespace = AppData.TargetNamespace; string namespaceName = fbsStructure.namespaceName; for (int i = 0; i < fbsStructure.tableStructures.Length; i++) { TableStructure tableStructure = fbsStructure.tableStructures[i]; if (string.IsNullOrEmpty(namespaceName)) { tableStructure.tableNameWithNamespace = $"global::{tableStructure.tableName}"; tableStructure.tableNameWithCSharpNamespace = $"{targetNamespace}.{tableStructure.tableName}"; } else { tableStructure.tableNameWithNamespace = $"global::{namespaceName}.{tableStructure.tableName}"; tableStructure.tableNameWithCSharpNamespace = $"{targetNamespace}.{namespaceName}.{tableStructure.tableName}"; } for (int j = 0; j < tableStructure.fieldInfos.Length; j++) { TableFieldInfo fieldInfo = tableStructure.fieldInfos[j]; fieldInfo.fieldTypeNameWithNameSpace = ConvertToCSharpTypeNameWithNamespaceName(fieldInfo.fieldTypeName, namespaceName); } } }
private void ParseNamespace(string line, ref int currentLineNum, ref FbsStructure fbsStructure) { //namespace 不能以数字开头.只能包含英文字符数字和_ const string validatePattern = @"namespace +[a-zA-Z_+][a-zA-Z0-9_]+ *;"; MatchCollection matchCollection = Regex.Matches(line, validatePattern); if (matchCollection.Count == 0) { return; } if (matchCollection.Count != 1) { throw new ParseFileException { errorMessage = $"解析namespace出错,{line} , at {currentLineNum}" }; } const string namespacePattern = @"[^namespace ][a-zA-Z_+][a-zA-Z0-9_]+"; matchCollection = Regex.Matches(line, namespacePattern); if (matchCollection.Count != 1) { throw new ParseFileException { errorMessage = $"解析namespace出错,{line} , at {currentLineNum}" }; } string namespaceName = matchCollection[0].Value.Trim(); fbsStructure.namespaceName = namespaceName; currentLineNum++; // Debug.WriteLine($"namespace :{namespaceName}"); }
public FbsStructure ParseFbsFileLines(string[] lines) { if (lines == null || lines.Length == 0) { throw new ParseFileException { errorMessage = "fbs文件解析失败:传入的文件没有内容" }; } FbsStructure fbsStructure = new FbsStructure(); int currentLineNum = 0; List <TableStructure> tableStructureList = new List <TableStructure>(); while (currentLineNum < lines.Length) { if (string.IsNullOrEmpty(fbsStructure.namespaceName)) { ParseNamespace(lines[currentLineNum], ref currentLineNum, ref fbsStructure); } ParseObject(lines, ref currentLineNum, ref tableStructureList); } fbsStructure.tableStructures = tableStructureList.ToArray(); // Debug.WriteLine(fbsStructure.ToString()); ProcessTableAndFieldNamespace(fbsStructure); return(fbsStructure); }