コード例 #1
0
        public string GetSafeIdentifier(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(name);
            }

            // NOTE: "partial" differs in behavior on macOS vs. Windows, Windows reports "partial" as a valid identifier
            //	This check ensures the same output on both platforms
            switch (name)
            {
            case "partial": return(name);

            // `this` isn't in TypeNameUtilities.reserved_keywords; special-case.
            case "this": return("this_");
            }

            // In the ideal world, it should not be applied twice.
            // Sadly that is not true in reality, so we need to exclude non-symbols
            // when replacing the argument name with a valid identifier.
            // (ReturnValue.ToNative() takes an argument which could be either an expression or mere symbol.)
            if (name [name.Length - 1] != ')' && !name.Contains('.') && !name.StartsWith("@"))
            {
                if (!IdentifierValidator.IsValidIdentifier(name) ||
                    Array.BinarySearch(TypeNameUtilities.reserved_keywords, name) >= 0)
                {
                    name = name + "_";
                }
            }
            return(name.Replace('$', '_'));
        }
コード例 #2
0
        /// <summary>
        ///		Sets the value of a variable referenced within the expression prior
        ///		to evaluation. This override allows specifying the Type of the variable
        ///		instead of trying to introspect it. Also allows for passing null as the
        ///		value.
        /// </summary>
        /// <param name="name">
        ///		Name of the variable referenced within the expression.
        /// </param>
        /// <param name="value">
        ///		Value of the variable that should be used when evaluating the expression.
        /// </param>
        /// <param name="type">
        ///		The variable Type.
        /// </param>
        public void SetVariable(
            string name,
            object value,
            Type type)
        {
            if (variables.ContainsKey(name))
            {
                variables[name].Value = value;
            }
            else
            {
                if (IdentifierValidator.IsValidIdentifier(name))
                {
                    variables[name] = new Variable
                    {
                        Type  = type,
                        Value = value
                    };

                    initialized = false;
                }
                else
                {
                    throw new ArgumentException("Invalid value passed in for variable name. " +
                                                "Valid variable names must start with a letter or underscore, and not contain any whitespace.");
                }
            }
        }
コード例 #3
0
        public void IsValidIdentifier()
        {
            Assert.IsTrue(IdentifierValidator.IsValidIdentifier("name"));
            Assert.IsTrue(IdentifierValidator.IsValidIdentifier("Name_With_Underscores"));

            // Yes, this is "wrong" -- keywords aren't identifiers -- but the keyword check is done elsewhere.
            Assert.IsTrue(IdentifierValidator.IsValidIdentifier("true"));

            Assert.IsFalse(IdentifierValidator.IsValidIdentifier("name-with-hyphens and spaces"));
            Assert.IsFalse(IdentifierValidator.IsValidIdentifier("123"));
        }