public override void Parse(string context) { var lines = context.SplitAndTrim('\n'); var firstLine = lines[0]; //解析定义 var field = new LuaFieldNode(); field.Parse(firstLine); propertyName = field.fieldName.Substring(1); memberType = field.memberType; initStatementNode = field.initStatementNode; //解析get与set var getFuncBody = context.FindBetween($"get{propertyName}()", $"end {LuaDocumentNode.Lua_FUNC_END}"); var getFuncLines = from line in getFuncBody.SplitAndTrim('\n') where !string.IsNullOrEmpty(line.Trim()) select new LuaScriptStatementNode(line); getStatementNodes.AddRange(getFuncLines); context = context.FindAfter($"end {LuaDocumentNode.Lua_FUNC_END}"); var setFuncBody = context.FindBetween($"set{propertyName}({propertyName})", $"end {LuaDocumentNode.Lua_FUNC_END}"); var setFuncLines = (from line in setFuncBody.Split('\n') where !string.IsNullOrEmpty(line.Trim()) select new LuaScriptStatementNode(line)).ToArray(); setStatementNodes.AddRange(setFuncLines); }
/// <summary> /// 解析 /// </summary> /// <param name="context"></param> public void Parse(string context) { //requires RequireNodes.Clear(); if (context.Contains(LUA_REQUIRES_TAG)) { var requireContext = context.FindBetween($"{LUA_REQUIRES_TAG}", $"{LUA_REQUIRES_TAG} end"); var lines = from line in requireContext.Split('\n') where !string.IsNullOrEmpty(line.Trim()) select line; lines.Foreach((line) => { var requireNode = new LuaRequireNode(); requireNode.Parse(line); RequireNodes.Add(requireNode); }); } //model ModelNode = null; if (context.Contains(LUA_MODEL_TAG)) { var modelContext = context.FindBetween($"{LUA_MODEL_TAG}", $"{LUA_MODEL_TAG} end"); ModelNode = new LuaModelNode(); ModelNode.Parse(modelContext); } //class ClassName = null; ClassInitStatement = null; if (context.Contains(LUA_CLASSDEINE_TAG)) { var classDefineContext = context.FindBetween($"{LUA_CLASSDEINE_TAG}", $"{LUA_CLASSDEINE_TAG} end"); var splits = classDefineContext.Split('='); ClassName = splits[0].Trim(); ClassInitStatement = splits[1].Trim() == "{}" ? null : new LuaScriptStatementNode(splits[1].Trim()); } //fields FieldNodes.Clear(); if (context.Contains(LUA_FIELDS_TAG)) { var fieldsContext = context.FindBetween($"{LUA_FIELDS_TAG}", $"{LUA_FIELDS_TAG} end"); while (true) { if (string.IsNullOrEmpty(fieldsContext) || !fieldsContext.Contains(LUA_NEXT_TAG)) { break; } var endPosition = fieldsContext.IndexOf(LUA_NEXT_TAG); var fieldContext = fieldsContext.Substring(0, endPosition).Trim(); var fieldNode = new LuaFieldNode(); FieldNodes.Add(fieldNode); fieldNode.Parse(fieldContext); fieldsContext = fieldsContext.FindAfter(LUA_NEXT_TAG).Trim(); } } //properties PropertyNodes.Clear(); if (context.Contains(LUA_PROPERTIES_TAG)) { var propertiesContext = context.FindBetween($"{LUA_PROPERTIES_TAG}", $"{LUA_PROPERTIES_TAG} end"); while (true) { if (string.IsNullOrEmpty(propertiesContext) || !propertiesContext.Contains(LUA_NEXT_TAG)) { break; } var endPosition = propertiesContext.IndexOf(LUA_NEXT_TAG); var propertyContext = propertiesContext.Substring(0, endPosition).Trim(); var propertyNode = new LuaPropertyNode(); PropertyNodes.Add(propertyNode); propertyNode.Parse(propertyContext); propertiesContext = propertiesContext.FindAfter(LUA_NEXT_TAG).Trim(); } } //functions FunctionNodes.Clear(); if (context.Contains(LUA_FUNCTIONS_TAG)) { var functionsContext = context.FindBetween($"{LUA_FUNCTIONS_TAG}", $"{LUA_FUNCTIONS_TAG} end"); int i = 0; while (true) { i++; if (i > 100) { break; } if (string.IsNullOrEmpty(functionsContext) || !functionsContext.Contains(LUA_NEXT_TAG) || i > 100) { break; } var endPosition = functionsContext.IndexOf(LUA_NEXT_TAG); var functionContext = functionsContext.Substring(0, endPosition).Trim(); var functionNode = new LuaFunctionNode(); FunctionNodes.Add(functionNode); functionNode.Parse(functionContext); functionsContext = functionsContext.FindAfter(LUA_NEXT_TAG).Trim(); } } //statements StatementNodes.Clear(); if (context.Contains(LUA_STATEMENTS_TAG)) { var statementsContext = context.FindBetween($"{LUA_STATEMENTS_TAG}", $"{LUA_STATEMENTS_TAG} end"); var lines = from line in statementsContext.SplitAndTrim('\n') where string.IsNullOrEmpty(line.Trim()) select new LuaScriptStatementNode(line); StatementNodes.AddRange(lines); } //custom CustomScript = null; if (context.Contains(LUA_CUSTOM_TAG)) { var customContext = context.FindBetween($"{LUA_CUSTOM_TAG}", $"{LUA_CUSTOM_TAG} end"); CustomScript = customContext.Trim(); } }