Beispiel #1
0
        /// <summary>
        /// Verifies is a valid EcmaScript variable expression.
        /// </summary>
        /// <param name="varExpr">the variable expression</param>
        /// <returns>varExpr</returns>
        /// <remarks>
        /// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
        ///
        /// IdentifierName =
        ///		IdentifierStart | IdentifierName IdentifierPart
        /// IdentifierStart =
        ///		Letter | '$' | '_'
        /// IdentifierPart =
        ///		IdentifierStart | Digit
        /// </remarks>
        public static bool IsValidIdentifier(string varExpr, bool nested)
        {
            if (String.IsNullOrEmpty(varExpr))
            {
                return(false);
            }

            if (nested)
            {
                string[] parts = varExpr.Split('.');
                foreach (string part in parts)
                {
                    if (!EcmaScriptIdentifier.IsValidIdentifier(part, false))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            if (EcmaScriptIdentifier.IsReservedWord(varExpr))
            {
                return(false);
            }

            bool indentPart = false;

            foreach (char ch in varExpr)
            {
                if (indentPart && Char.IsDigit(ch))
                {
                    // digits are only allowed after first char
                    continue;
                }

                // can be start or part
                if (Char.IsLetter(ch) || ch == '_' || ch == '$')
                {
                    indentPart = true;
                    continue;
                }

                return(false);
            }

            return(true);
        }
        public static bool IsValidIdentifier(string varExpr, bool nested)
        {
            if (string.IsNullOrEmpty(varExpr))
            {
                return(false);
            }
            if (nested)
            {
                string[] array = varExpr.Split(new char[]
                {
                    '.'
                });
                string[] array2 = array;
                for (int i = 0; i < array2.Length; i++)
                {
                    string varExpr2 = array2[i];
                    if (!EcmaScriptIdentifier.IsValidIdentifier(varExpr2, false))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            if (EcmaScriptIdentifier.IsReservedWord(varExpr))
            {
                return(false);
            }
            bool flag = false;

            for (int j = 0; j < varExpr.Length; j++)
            {
                char c = varExpr[j];
                if (!flag || !char.IsDigit(c))
                {
                    if (!char.IsLetter(c) && c != '_' && c != '$')
                    {
                        return(false);
                    }
                    flag = true;
                }
            }
            return(true);
        }