public static Expression Resolve(string expression) { if (string.IsNullOrWhiteSpace(expression)) { return(null); } var lexer = new ExpressionLexer(expression); var resolve = new ExpressionResolve(lexer.GetToken()); return(resolve.ResolveMatchBegin()); }
/// <summary> /// 解析表达式块 /// </summary> /// <param name="e"></param> /// <returns></returns> private bool Resolve_Block(out Expression e) { if (Current.Value == "(") { var nCount = 0; List <ExpressionToken> tokens = new List <ExpressionToken>(); while (true) { if (Current == null) { throw new Exception("表达式解析失败,( )不匹配"); } if (Current.Value == "(") { nCount++; } else if (Current.Value == ")") { nCount--; } tokens.Add(Current); Read(); if (nCount == 0) { if (tokens.Count < 2) { throw new Exception("表达式解析失败,( )不匹配"); } tokens.RemoveAt(0); tokens.RemoveAt(tokens.Count - 1); if (tokens.Count == 0) { throw new Exception("表达式解析失败, 无效的( ), 没有任何意义"); } e = new ExpressionResolve(tokens.ToArray()).Resolve(); return(true); } } } e = null; return(false); }
/// <summary> /// 解析方法 /// </summary> /// <param name="e"></param> /// <returns></returns> private bool Resolve_Function(out Expression e) { e = null; if (Current.Type != ExpressionToken.eType.Identifier) { return(false); } if (Peek.Value != "(") { return(false); } var rFunction = new ExpressionFunction(Current.Value); // 所有参数列表 var rListToken = new List <ExpressionToken>(); var rListParameter = new List <ExpressionToken>(); Read(); var nCount = 0; while (true) { if (Current.Value == "(") { nCount++; } else if (Current.Value == ")") { nCount--; if (0 == nCount) { Read(); break; } } rListToken.Add(Current); Read(); } // 移除开始与结束的括号 rListToken.RemoveAt(0); rListToken.Add(new ExpressionToken(ExpressionToken.eType.End, null)); foreach (var rToken in rListToken) { if (rToken.Value == "(") { nCount++; } else if (rToken.Value == ")") { nCount--; } if ((rToken.Value == "," && nCount == 0) || rToken.Type == ExpressionToken.eType.End) { if (rListParameter.Count > 0) { var rResolve = new ExpressionResolve(rListParameter.ToArray()); var rExpression = rResolve.Resolve(); // 添加到参数列表中 rFunction.Parameter.Add(rExpression); rListParameter.Clear(); } } else { rListParameter.Add(rToken); } } if (nCount != 0) { throw new Exception("方法( )不匹配"); } e = rFunction; return(true); }