Exemplo n.º 1
0
        public static bool IsValid(string expression, VariableChecking variableChecking, List <IConstant> constants, ref HashSet <string> initializedVariables, out HashSet <string> uninitializedVariables, out string error)
        {
            uninitializedVariables = new HashSet <string>();

            string _error = null;
            var    ne     = new NumericExpression();

            if (constants != null)
            {
                ne.Constants.AddRange(constants);
            }
            ne.UndefinedVariableFound += (s, e) => e.Handled = true; // variables are allowed
            ne.UndefinedFunctionFound += (s, e) => e.Handled = true; // functions are allowed
            try
            {
                var pe = ne.Parse(expression);
                TraverseTree(pe.ExpressionTree, initializedVariables, uninitializedVariables);
                if (variableChecking == VariableChecking.None)
                {
                    uninitializedVariables.Clear();
                }
                if (uninitializedVariables.Count > 0)
                {
                    error = "Uninitialized variables used in expression.";
                    return(false);
                }
                else
                {
                    error = null;
                    return(true);
                }
            }
            catch (ArgumentException e)
            {
                if (_error == null)
                {
                    _error = string.Format("Error parsing expression. {0}", e.Message);
                }
            }
            catch (Exception e)
            {
                _error = string.Format("Unexpected error while parsing expression. {0}", e.Message);
            }
            error = _error;
            return(false);
        }
        public static bool IsValid(string expression, VariableChecking variableChecking, List<IConstant> constants, ref HashSet<string> initializedVariables, out HashSet<string> uninitializedVariables, out string error)
        {
            uninitializedVariables = new HashSet<string>();

            string _error = null;
            var ne = new NumericExpression();
            if (constants != null)
            {
                ne.Constants.AddRange(constants);
            }
            ne.UndefinedVariableFound += (s, e) => e.Handled = true; // variables are allowed
            ne.UndefinedFunctionFound += (s, e) => e.Handled = true; // functions are allowed
            try
            {
                var pe = ne.Parse(expression);
                TraverseTree(pe.ExpressionTree, initializedVariables, uninitializedVariables);
                if (variableChecking == VariableChecking.None)
                {
                    uninitializedVariables.Clear();
                }
                if (uninitializedVariables.Count > 0)
                {
                    error = "Uninitialized variables used in expression.";
                    return false;
                }
                else
                {
                    error = null;
                    return true;
                }
            }
            catch (ArgumentException e)
            {
                if (_error == null)
                {
                    _error = string.Format("Error parsing expression. {0}", e.Message);
                }
            }
            catch (Exception e)
            {
                _error = string.Format("Unexpected error while parsing expression. {0}", e.Message);
            }
            error = _error;
            return false;
        }