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);
}
}