public void ClearEmpty(AnalyzeBlock root) { foreach (var unit in root.ResetElements()) { if (unit.IsEmpty) { continue; } var block = unit as AnalyzeBlock; if (block != null) { block.Parent = root; ClearEmpty(block); if (block.Elements.Count == 0) { continue; } block.Release(); root.Append(block.Elements.Count == 1 ? block.Elements[0] : unit); } else { root.Append(unit); } } }
private static void LinkBarket(AnalyzeBlock root) { AnalyzeUnitBase pre = null; AnalyzeBlock cur = null; // CodeItemType pretype = CodeItemType.None; foreach (var unit in root.ResetElements()) { var block = unit as AnalyzeBlock; if (block == null) { var word = (WordUnit)unit; pre = word.IsPunctuate || word.IsKeyWord ? null : unit; root.Append(unit); //pretype = CodeItemType.None; continue; } LinkBarket(block); switch (block.ItemType) { case CodeItemType.Brackets21: //case CodeItemType.Brackets31: //if (pretype == CodeItemType.None || cur == null) { if (pre == null) { root.Append(unit); break; } if (pre != cur) { cur?.Release(); cur = new AnalyzeBlock(); var wd = pre as WordUnit; cur.Primary = wd ?? ((AnalyzeBlock)pre).Primary; root.Elements.Remove(pre); cur.Elements.Add(pre); cur.ItemRace = CodeItemRace.Value; cur.ItemFamily = CodeItemFamily.ValueSentence; cur.ItemType = CodeItemType.Brackets21 == block.ItemType ? CodeItemType.Call : CodeItemType.Table_Child; root.Elements.Add(cur); pre = cur; } unit.Name = "_call_"; cur.Elements.Add(unit); } //root.Elements.Add(block); //pretype = block.ItemType; continue; } root.Append(unit); pre = unit; } cur?.Release(); }
/// <summary> /// 区域组合 /// </summary> /// <returns></returns> public void MergeRange(AnalyzeBlock root, int level) { var cur = root; Stack <AnalyzeBlock> stack = new Stack <AnalyzeBlock>(); foreach (var u in root.ResetElements()) { if (u.IsEmpty) { continue; } var unit = u; var child = unit as AnalyzeBlock; if (child != null) { if (!unit.IsLock && !unit.IsUnit) { MergeRange((AnalyzeBlock)unit, level); } cur.Append(unit); continue; } var word = unit as WordUnit; if (unit.JoinLevel == level) { switch (unit.JoinFeature) { case JoinFeature.RangeOpen: stack.Push(cur); var block = new AnalyzeBlock { Parent = cur, Primary = word }; cur.Append(block); cur.JoinFeature = JoinFeature.BeforeBy; cur = block; break; case JoinFeature.RangeClose: cur.Append(unit); if (stack.Count == 0) { cur.IsError = true; cur = root; } else { cur = stack.Pop(); } continue; } } cur.Append(unit); } }
/// <summary> /// For组合 /// </summary> /// <returns></returns> public void MergeFor(AnalyzeBlock root) { var cur = root; Stack <AnalyzeBlock> stack = new Stack <AnalyzeBlock>(); int step = 0; foreach (var u in root.ResetElements()) { if (u.IsEmpty) { continue; } var unit = u; var child = unit as AnalyzeBlock; if (child == null) { var word = (WordUnit)unit; if (unit.ItemFamily == CodeItemFamily.Iterator && word.ItemType == CodeItemType.Key_For) { stack.Push(cur); var block = new AnalyzeBlock { Parent = cur, Primary = word, ItemRace = CodeItemRace.Range, ItemFamily = CodeItemFamily.IteratorRange, ItemType = CodeItemType.Key_For }; cur.Append(block); cur = block; step = 1; } else if (step > 0) { step++; if (word.ItemType == CodeItemType.Key_In) { cur.ItemType = CodeItemType.Key_Foreach; } } cur.Append(unit); } else { if (!unit.IsLock && !unit.IsUnit) { MergeFor((AnalyzeBlock)unit); } cur.Append(unit); if (step == 0) { continue; } step++; if (unit.Word != "do") { continue; } if (cur.ItemType != CodeItemType.Key_Foreach) { cur.Elements[1].ValueType = LuaDataTypeItem.NumberValue; } else { cur.Elements[1].ValueLink = cur.Elements[cur.Elements.Count - 2]; } if (stack.Count == 0) { cur.IsError = true; cur = root; } else { cur = stack.Pop(); } step = 0; } } }
/// <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); }
/// <summary> /// 串联 /// </summary> /// <param name="root"></param> /// <param name="level"></param> /// <returns></returns> public void ConnectBlock(AnalyzeBlock root, int level) { AnalyzeUnitBase pre = null; var step = 0; AnalyzeBlock cur = root; foreach (var unit in root.ResetElements()) { if (unit.IsEmpty) { continue; } var block = unit as AnalyzeBlock; if (block != null) { if (!unit.IsLock && !unit.IsUnit) { ConnectBlock(block, level); } pre = block; } else { if (unit.JoinLevel == level && unit.JoinFeature == JoinFeature.Connect) { if (step == 0) { cur = new AnalyzeBlock { Primary = (WordUnit)unit }; if (pre != null) { cur.Append(pre); root.Elements.RemoveAt(root.Elements.Count - 1); } else { cur.IsError = true; } root.Append(cur); } step = 1; cur.Append(unit); continue; } pre = ((WordUnit)unit).IsPunctuate || ((WordUnit)unit).IsKeyWord ? null : unit; } switch (step) { case 0: root.Append(unit); break; case 1: step = 2; cur.Append(unit); break; default: step = 0; root.Append(unit); break; } } }