/// <summary> /// 合并块 /// </summary> private void CheckRegion() { FixStack <string> stack = new FixStack <string>(); stack.SetFix(string.Empty); foreach (CodeElement element in this._codeElements) { RegionElement region = element as RegionElement; if (region != null) { if (region.IsEnd) { stack.Pop(); } else { stack.Push(region.Name); } } else if (!element.Skip) { element.Region = stack.Current; } } }
/// <summary> /// 移除非条件范围的代码(参数来自CodeAnalyzerContext的上下文) /// </summary> private static void RemoveCondition() { CodeElement[] arr = CodeAnalyzerContext.Current.Words.ToArray(); List <CodeElement> elements = CodeAnalyzerContext.Current.Words; elements.Clear(); int keepCnt = 0; FixStack <bool> conditionStack = new FixStack <bool>(); conditionStack.SetFix(false); FixStack <List <string> > logicalStack = new FixStack <List <string> >(); string condition = string.Empty; foreach (CodeElement el in arr) { switch (el.ItemType) { case CodeItemType.Sharp_IF: el.Skip = true; if (conditionStack.Current) { keepCnt++; break; } SharpElement sharp = el as SharpElement; List <string> words = GetConditionWords(sharp.Name); conditionStack.Push(!CheckCondition(words)); if (conditionStack.Current) { keepCnt = 1; } else { if (!logicalStack.IsEmpty) { words.Insert(0, "&&"); } words.Insert(0, "("); words.Add(")"); condition += words.LinkToString(); logicalStack.Push(words); } break; case CodeItemType.Sharp_Else: el.Skip = true; if (keepCnt > 1) { break; } bool kp = conditionStack.Current; conditionStack.Pop(); conditionStack.Push(!kp); if (logicalStack.Current != null && logicalStack.Current.Count > 0) { logicalStack.Current.Insert(logicalStack.Current[0] == "&&" ? 1 : 0, "!"); } condition = string.Empty; foreach (List <string> cd in logicalStack.Stack) { condition += cd.LinkToString(); } break; case CodeItemType.Sharp_EndIf: el.Skip = true; keepCnt--; if (keepCnt > 0) { break; } logicalStack.Pop(); condition = string.Empty; foreach (List <string> cd in logicalStack.Stack) { condition += cd.LinkToString(); } conditionStack.Pop(); break; default: if (conditionStack.Current) { el.Skip = true; } else { if (!el.Skip) { el.Condition = condition; elements.Add(el); } } break; } } }