/// <summary> /// 括号块组合 /// </summary> /// <returns></returns> public void MergeBracket(AnalyzeBlock root) { var cur = root; Stack <CodeItemType> stack = new Stack <CodeItemType>(); foreach (var unit in root.ResetElements()) { if (unit.IsEmpty) { continue; } var word = unit as WordUnit; if (word == null) { cur.Append(unit); continue; } if (word.IsSpace) { continue; } AnalyzeBlock block; switch (unit.ItemType) { case CodeItemType.Brackets31: var vw = new WordUnit('.') { Start = unit.Start, End = unit.End, IsReplenish = true, IsKeyWord = true, PrimaryLevel = 1, JoinLevel = 1, JoinFeature = JoinFeature.Connect }; vw.SetRace(CodeItemRace.Sentence, CodeItemFamily.Separator, CodeItemType.Separator_Dot); cur.Append(vw); stack.Push(unit.ItemType + 1); block = new AnalyzeBlock { Parent = cur, Primary = word }; cur.Append(block); cur = block; break; case CodeItemType.Brackets21: case CodeItemType.Brackets41: stack.Push(unit.ItemType + 1); block = new AnalyzeBlock { Parent = cur, Primary = word }; cur.Append(block); cur = block; break; case CodeItemType.Brackets22: case CodeItemType.Brackets32: case CodeItemType.Brackets42: if (stack.Count > 0 && stack.Peek() == unit.ItemType) { stack.Pop(); unit.IsError = true; cur.Append(unit); cur.Release(); if (cur.Parent == null) { cur.IsError = true; } else { cur = cur.Parent; } continue; } break; } cur.Append(word); } LinkBarket(root); }
private void CheckPunctuate(WordUnit word, ref WordUnit pre) { switch (word.Char) { #region 其它 case '#': word.IsKeyWord = true; word.PrimaryLevel = 33; word.JoinLevel = 2; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.Front; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Operator, CodeItemType.StringLen); pre = null; break; case ';': word.IsKeyWord = true; word.SetRace(CodeItemRace.Control, CodeItemFamily.Control, CodeItemType.Separator_Semicolon); pre = null; break; default: pre = null; break; #endregion #region 扩号区域 case '{': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 2; word.ValueType = LuaDataTypeItem.TableDefinition; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Brackets41); pre = null; break; case '}': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 2; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Brackets42); pre = null; break; case '(': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 3; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Brackets21); pre = null; break; case ')': word.IsKeyWord = true; word.JoinLevel = 3; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Brackets22); pre = null; break; case '[': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 1; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Brackets31); pre = null; break; case ']': word.IsKeyWord = true; word.JoinLevel = 1; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Brackets32); pre = null; break; #endregion #region 算术计算 case '+': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 5; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compute, CodeItemType.Compute_Add); pre = null; break; case '-': word.IsKeyWord = true; word.JoinLevel = 5; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compute, CodeItemType.Compute_Sub); pre = null; break; case '*': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 4; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compute, CodeItemType.Compute_Mulit); pre = null; break; case '/': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 4; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compute, CodeItemType.Compute_Div); pre = null; break; case '%': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 4; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compute, CodeItemType.Compute_Mod); pre = null; break; case '^': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 3; word.ValueType = LuaDataTypeItem.NumberValue; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compute, CodeItemType.Compute_Exp); pre = null; break; #endregion #region 连接符 case ',': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 3; word.JoinFeature = JoinFeature.Connect; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Separator, CodeItemType.Separator_Comma); pre = null; break; case ':': word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 1; word.JoinFeature = JoinFeature.Connect; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Separator, CodeItemType.Separator_Colon); pre = null; break; case '.': if (pre == null || pre.FirstChar != '.') { word.IsKeyWord = true; word.PrimaryLevel = 1; word.JoinLevel = 1; word.JoinFeature = JoinFeature.Connect; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Separator, CodeItemType.Separator_Dot); pre = word; } else if (pre.Chars.Count == 1) { pre.Append(word); word.Clear(); pre.JoinLevel = 2; pre.ValueType = LuaDataTypeItem.StringValue; pre.SetRace(CodeItemRace.Sentence, CodeItemFamily.Separator, CodeItemType.Separator_StringJoin); } else { pre.Append(word); word.Clear(); word.IsKeyWord = false; pre.JoinLevel = -1; pre.ValueType = LuaDataTypeItem.TableDefinition; pre.JoinFeature = JoinFeature.None; pre.SetRace(CodeItemRace.Variable, CodeItemFamily.Variable, CodeItemType.Variable_Arg); pre = null; } break; #endregion #region 逻辑比较 case '>': word.IsKeyWord = true; word.PrimaryLevel = 1; word.ValueType = LuaDataTypeItem.BoolValue; word.JoinLevel = 6; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Compare_Greater); pre = word; break; case '<': word.IsKeyWord = true; word.PrimaryLevel = 1; word.ValueType = LuaDataTypeItem.BoolValue; word.JoinLevel = 6; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Compare_Less); pre = word; break; case '~': pre = word; break; case '=': bool canLink = false; if (pre != null) { switch (pre.Char) { case '>': canLink = true; pre.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Compare_GreaterEqual); break; case '<': canLink = true; pre.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Compare_LessEqual); break; case '~': canLink = true; pre.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Compare_NotEqual); break; case '=': canLink = true; pre.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Compare_Equal); break; } } if (canLink) { pre.Append(word); word.Clear(); pre.PrimaryLevel = 1; pre.ValueType = LuaDataTypeItem.BoolValue; pre.JoinLevel = 6; pre.JoinFeature = JoinFeature.TowWay; pre = null; } else { word.PrimaryLevel = 1; word.IsKeyWord = true; word.JoinLevel = 9; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.SetValue, CodeItemType.Separator_Equal); pre = word; } break; #endregion } }
private void CheckWord(WordUnit word) { if (char.IsNumber(word.Chars[0])) { word.ValueType = LuaDataTypeItem.NumberValue; word.SetRace(CodeItemRace.Value, CodeItemFamily.Constant, CodeItemType.Number); return; } if (!_keyWords.Contains(word.Word)) { word.Name = word.Word; word.SetRace(CodeItemRace.Variable, CodeItemFamily.Variable); return; } word.IsKeyWord = true; switch (word.Word) { default: word.SetRace(CodeItemRace.None, CodeItemFamily.KeyWord); break; case "nil": word.ValueType = LuaDataTypeItem.Nil; word.SetRace(CodeItemRace.Value, CodeItemFamily.Constant, CodeItemType.Value_Null); break; case "true": word.ValueType = LuaDataTypeItem.BoolValue; word.SetRace(CodeItemRace.Value, CodeItemFamily.Constant, CodeItemType.Value_True); break; case "false": word.ValueType = LuaDataTypeItem.BoolValue; word.SetRace(CodeItemRace.Value, CodeItemFamily.Constant, CodeItemType.Value_False); break; case "and": word.JoinLevel = 7; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Logical, CodeItemType.Key_And); break; case "or": word.JoinLevel = 8; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Logical, CodeItemType.Key_Or); break; case "not": word.JoinLevel = 2; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.Front; word.SetRace(CodeItemRace.Sentence, CodeItemFamily.Compare, CodeItemType.Key_Not); break; case "local": word.JoinLevel = 2; word.JoinFeature = JoinFeature.Front; word.PrimaryLevel = 33; word.SetRace(CodeItemRace.Control, CodeItemFamily.Scope, CodeItemType.Key_Local); break; case "break": word.SetRace(CodeItemRace.Control, CodeItemFamily.Control, CodeItemType.Key_Break); break; case "return": word.JoinLevel = 2; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.Front; word.SetRace(CodeItemRace.Control, CodeItemFamily.Control, CodeItemType.Key_Return); break; case "for": word.JoinLevel = 12; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.RangeOpen; word.SetRace(CodeItemRace.Range, CodeItemFamily.Iterator, CodeItemType.Key_For); break; case "in": word.JoinLevel = 9; word.JoinFeature = JoinFeature.TowWay; word.SetRace(CodeItemRace.Range, CodeItemFamily.Control, CodeItemType.Key_In); break; case "if": word.JoinLevel = 11; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.RangeOpen; word.SetRace(CodeItemRace.Range, CodeItemFamily.Condition, CodeItemType.Key_If); break; case "while": word.JoinLevel = 11; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.RangeOpen; word.SetRace(CodeItemRace.Range, CodeItemFamily.Condition, CodeItemType.Key_While); break; case "else": word.PrimaryLevel = 3; word.JoinFeature = JoinFeature.RangeShift; word.SetRace(CodeItemRace.Range, CodeItemFamily.Condition, CodeItemType.Key_Else); break; case "elseif": word.PrimaryLevel = 2; word.JoinFeature = JoinFeature.RangeShift; word.SetRace(CodeItemRace.Range, CodeItemFamily.Condition, CodeItemType.Key_Elseif); break; case "then": word.JoinFeature = JoinFeature.RangeShift; word.SetRace(CodeItemRace.Range, CodeItemFamily.Control, CodeItemType.Key_Then); break; case "go": word.JoinFeature = JoinFeature.RangeShift; word.SetRace(CodeItemRace.Range, CodeItemFamily.Control, CodeItemType.Key_Go); break; case "repeat": word.JoinLevel = 10; word.JoinFeature = JoinFeature.RangeOpen; word.SetRace(CodeItemRace.Range, CodeItemFamily.Condition, CodeItemType.Key_Repeat); break; case "until": word.JoinLevel = 10; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.RangeClose; word.SetRace(CodeItemRace.Range, CodeItemFamily.Condition, CodeItemType.Key_Until); break; case "function": word.JoinLevel = 11; word.PrimaryLevel = 1; word.JoinFeature = JoinFeature.RangeOpen; word.SetRace(CodeItemRace.Range, CodeItemFamily.FunctionRange, CodeItemType.Key_Function); break; case "do": word.JoinLevel = 11; word.PrimaryLevel = 2; word.JoinFeature = JoinFeature.RangeOpen; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Key_Do); break; case "end": word.JoinLevel = 11; word.JoinFeature = JoinFeature.RangeClose; word.SetRace(CodeItemRace.Range, CodeItemFamily.Range, CodeItemType.Key_End); break; } }