public ExpressErrorException(string errMsg, int line = 0, int col = 0, int col2 = 0, IExpressPart errExp = null) : base(errMsg) { ErrerLine = line; ErrorCol = col; ErrorCol2 = col2; ErrorExpress = errExp; }
/// <summary> /// 后序遍历 /// </summary> public IEnumerable <IExpressPart> BackForeach() { if (listBackForeach != null) { int len = listBackForeach.Length; for (int i = 0; i < len; i++) { yield return(listBackForeach[i]); } yield break; } if (this.RightTree != null) { foreach (var node in this.RightTree.BackForeach()) { yield return(node); } //RightTree.BackForeach(); } if (this.LeftTree != null) { foreach (var node in this.LeftTree.BackForeach()) { yield return(node); } //LeftTree.BackForeach(); } IExpressPart o1 = (IExpressPart)this.TreeNode; if (o1 is CalExpress) { //Console.WriteLine((o1 as CalExpress).Express); yield return(o1); } else if (o1 is CalSign) { //Console.WriteLine((o1 as CalSign).SignName); yield return(o1); } }
/// <summary> /// 分解过程,每次分解一行代码 /// </summary> /// <param name="express"></param> /// <returns></returns> private IExpressPart[] ResolveCalStep(string express, int line) { if (string.IsNullOrWhiteSpace(express)) { return(null); } //express = new Regex(@";;{1,}").Replace(express, ";"); //这里有问题,比如((5+3)/2) //express = TrimBrackets(express); List <IExpressPart> arry = new List <IExpressPart>(); int ifcount = 0; int bracket = 0; int pointStart = 0; int elsePosit = 0; int thenPosit = 0; int modelID = 0; IExpressPart ep = null; int expressLen = express.Length; var expressCharArray = express.ToArray(); for (int i = 0; i <= expressLen; i++) { if (i == expressLen || (bracket == 0 && i > 0 && !IsCanGroup(expressCharArray[i - 1], expressCharArray[i])) ) { string es = express.Substring(pointStart, i - pointStart); es = es.TrimBrackets(); //检查是否是保留字 //if (CalSignFactory.IsProtectWord(es)) //{ // throw new ExpressErrorException(es + "是保留字!"); //} if (i < expressLen && es[0] == '\'' && expressCharArray[i] == '\'' && ifcount == 0) { i++; StringSign cexp = new StringSign(es.Trim('\''), ++modelID); cexp.StartIndex = pointStart; cexp.EndIndex = i; cexp.CodeLine = line; arry.Add(cexp); } else if (es[0] == '\'' && expressCharArray[i] != '\'' && ifcount == 0) { throw new ExpressErrorException("字符串分析错误"); } else if (es.Equals("if", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 0) { ep = new ConditionSign(); modelID += 2; ep.CodeLine = line; ep.ModeID = modelID; ep.StartIndex = pointStart; ep.EndIndex = i; arry.Add(ep); } ifcount++; } //else if (es.ToLower() == "then") else if (es.Equals("then", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 1) { //先加上左边的条件 if (i - 6 - ep.EndIndex <= 0) { throw new ExpressErrorException("if缺少条件表达式!", line, ep.EndIndex, i, ep); } CalExpress cexp = new CalExpress(express.Substring(ep.EndIndex + 1, i - 6 - ep.EndIndex).TrimBrackets(), ep.ModeID - 1); cexp.StartIndex = ep.EndIndex + 1; cexp.EndIndex = i - 4; cexp.CodeLine = line; arry.Add(cexp); thenPosit = i - 4; } } //else if (es.ToLower() == "else") else if (es.Equals("else", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 0) { throw new ExpressErrorException("表达式错误,else缺少if条件!", line, elsePosit, i, ep); } if (thenPosit == 0) { throw new ExpressErrorException("表达式错误,else前要有then!", line, thenPosit, i, ep); } if (ifcount == 1) { elsePosit = i - 4; } } //else if (es.ToLower() == "end") else if (es.Equals("end", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 1 && thenPosit == 0) { throw new ExpressErrorException("if 表达式错误,缺少then!", line, i, i + 3, ep); } ifcount--; if (ifcount == 0) { if (elsePosit > 0) { modelID += 2; ElseSign elseSign = new ElseSign(); elseSign.StartIndex = elsePosit; elseSign.EndIndex = elsePosit + 4; elseSign.ModeID = modelID; elseSign.CodeLine = line; arry.Add(elseSign); //左边为真条件 if (elsePosit - thenPosit - 6 <= 0) { throw new ExpressErrorException("if else 表达式中缺少条件为真的表达式!", line, thenPosit - 1, thenPosit, ep); } CalExpress cexp0 = new CalExpress(express.Substring(thenPosit + 5, elsePosit - thenPosit - 6).Trim().TrimBrackets(), modelID - 1); cexp0.StartIndex = thenPosit + 5; cexp0.EndIndex = elsePosit - thenPosit - 6; cexp0.CodeLine = line; arry.Add(cexp0); if (i - elsePosit - 9 <= 0) { throw new ExpressErrorException("if else 表达式中缺少条件为假的表达式!", line, elsePosit + 4, elsePosit + 5, ep); } //右边为假条件 CalExpress cexp = new CalExpress(express.Substring(elsePosit + 5, i - elsePosit - 9).Trim().TrimBrackets(), ++modelID); cexp.StartIndex = elsePosit + 5; cexp.EndIndex = i - 4; cexp.CodeLine = line; arry.Add(cexp); } else { //加上右边的操作 if (i - thenPosit - 9 <= 0) { throw new ExpressErrorException("if表达式中缺少条件表达式!", line, thenPosit + 4, thenPosit + 5, ep); } //右边条件 CalExpress cexp = new CalExpress(express.Substring(thenPosit + 5, i - thenPosit - 9).TrimBrackets(), ++modelID); cexp.StartIndex = thenPosit + 5; cexp.EndIndex = i - 4; arry.Add(cexp); } } } else if (!string.IsNullOrWhiteSpace(es) && es != ";" && ifcount == 0) { CalSign cs; if (CalSignFactory.TryCreateSign(es, this.CalCurrent, out cs)) { cs.StartIndex = pointStart; cs.EndIndex = i; if (cs is FunSign) { FunSign fs = cs as FunSign; arry.Add(new CalExpress(fs.FunName, ++modelID)); fs.ModeID = ++modelID; arry.Add(fs); if (!string.IsNullOrWhiteSpace(fs.ParamString)) { arry.Add(new CalExpress(fs.ParamString, ++modelID)); } } else { cs.ModeID = ++modelID; arry.Add(cs); } } else { CalExpress cexp = new CalExpress(es, ++modelID); cexp.StartIndex = pointStart; cexp.EndIndex = i; arry.Add(cexp); } } pointStart = i; } if (i < expressLen) { if (expressCharArray[i] == '(') { bracket++; } else if (expressCharArray[i] == ')') { bracket--; } } if (i == expressLen && bracket > 0) { throw new ExpressErrorException("表达式错误,缺少右)!", line, expressLen, expressLen + 1, ep); } if (i == expressLen && ifcount > 0) { throw new ExpressErrorException("if表达式错误!", line, expressLen, expressLen + 1, ep); } } return(arry.OrderBy(c => c.ModeID).ToArray()); }