AK.ValidityChecker.CheckValidity C# (CSharp) Method

CheckValidity() public static method

public static CheckValidity ( string expression ) : void
expression string
return void
        public static void CheckValidity(string expression)
        {
            bool inStringParam = false;
            int parenthesisDepth = 0;
            int l = expression.Length;
            for (int i=0;i<l;i++)
            {
                var x = expression[i];
                if (inStringParam)
                {
                    if (x != '\'' || (x == '\'' && expression[i-1] == '\\'))
                    {
                        continue;
                    }
                }
                switch (x) {
                    case '(':
                        parenthesisDepth++;
                        if (i>0 && !CanPrecede(expression[i-1],expression[i]))
                        {
                            UnityEngine.Debug.Log(expression[i]);
                            UnityEngine.Debug.Log(expression[i-1]);
                            ThrowSyntaxErrorAt(expression,i);
                        }
                        break;
                    case ')':
                        if (parenthesisDepth == 0) {
                            throw new ESSyntaxErrorException("Parenthesis mismatch.");
                        }
                        if (i>0 && !CanPrecede(expression[i-1],expression[i])) {
                            ThrowSyntaxErrorAt(expression,i);
                        }
                        parenthesisDepth--;
                        break;
                    case '/':
                    case '*':
                    case '+':
                    case '^':
                    case '-':
                        if (i==l-1)
                            ThrowSyntaxErrorAt(expression,i);
                        if (i==0 && !(x=='-' || x=='+') )
                            ThrowSyntaxErrorAt(expression,i);
                        if (!CanBeBeginOfRValue(expression[i+1])) {
                            ThrowSyntaxErrorAt(expression,i);
                        }
                        if (i>0 && !CanPrecede(expression[i-1],expression[i])) {
                            ThrowSyntaxErrorAt(expression,i);
                        }
                        if ( (x == '+' || x=='-') && i < l-2) {
                            if ( (expression[i+2]=='+' || expression[i+2]=='-') && (expression[i+1]=='+' || expression[i+1]=='-') ) {
                                ThrowSyntaxErrorAt(expression,i);
                            }
                        }
                        break;
                    case ',':
                        if (i==l-1)
                            ThrowSyntaxErrorAt(expression,i);
                        if (!CanBeBeginOfRValue(expression[i+1])) {
                            ThrowSyntaxErrorAt(expression,i);
                        }
                        break;
                    case '\'':
                        if (!inStringParam)
                        {
                            if (i == 0 || (i>0 && !CanPrecede(expression[i-1],expression[i])))
                            {
                                ThrowSyntaxErrorAt(expression,i);
                            }
                        }
                        else
                        {
                            if (i < l-1 && (expression[i+1] != ')' && expression[i+1] != ','))
                            {
                                ThrowSyntaxErrorAt(expression,i);
                            }
                        }
                        inStringParam = !inStringParam;
                        break;
                    case '.':
                        if (i==l-1)
                            ThrowSyntaxErrorAt(expression,i);
                        if (! (expression[i+1] >= '0' && expression[i+1] <= '9') )
                            ThrowSyntaxErrorAt(expression,i);
                        break;
                    default:
                        if (x >= '0' && x<= '9')
                            break;
                        if (x >= 'a' && x<= 'z')
                            break;
                        if (x >= 'A' && x<= 'Z')
                            break;
                        if (x == '_')
                            break;
                        throw new ESInvalidCharacterException(expression.Substring(i,1));
                }
            }
            if (parenthesisDepth > 0) {
                throw new ESSyntaxErrorException("Parenthesis mismatch.");
            }
            if (inStringParam) {
                throw new ESSyntaxErrorException("String parameter not ending.");
            }
            var error = CheckNamesAndConstants(expression);
            if (error != null)
            {
                throw new ESInvalidNameException(error);
            }
        }

Usage Example

Example #1
0
        public Expression SymbolicateExpression(string formula, string[] localVariables = null)
        {
            Expression newExpression = new Expression();

            if (localVariables != null)
            {
                foreach (var localVariableName in localVariables)
                {
                    if (localVariableName[0] == '$')
                    {
                        newExpression.SetVariable(localVariableName.Substring(1, localVariableName.Length - 1).Trim(), "");
                    }
                    else
                    {
                        newExpression.SetVariable(localVariableName.Trim(), 0.0);
                    }
                }
            }

            formula = SolverTools.RemoveWhiteSpace(formula);

            // Check validity
            try
            {
                ValidityChecker.CheckValidity(formula);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

            Symbol s = Symbolicate(formula, 0, formula.Length, newExpression);

            newExpression.root = s;
            return(newExpression);
        }