Ejemplo n.º 1
0
        internal override bool TryGetToken(ref string text, out TokenBase token, bool requireReturnValue = true)
        {
            token = null;
            bool inQuotes = false;
            int  brackets = 0;
            int  i        = 0;

            while (true)
            {
                if (i >= text.Length)
                {
                    return(false);
                }
                if (i > 0 && text[i] == '\'' && text[i - 1] != '\\')
                {
                    inQuotes = !inQuotes;
                }
                else if (!inQuotes)
                {
                    if (text[i] == '(')
                    {
                        ++brackets;
                    }
                    else if (text[i] == ')')
                    {
                        --brackets;
                        if (brackets == 0)
                        {
                            break;
                        }
                    }
                }
                ++i;
            }
            string temp = text.Substring(1, i - 1);

            var tuple = GetNameMatches(temp, null, null).Where(tup => tup.Item1 is Type).Reverse().FirstOrDefault();

            if (tuple == null)
            {
                return(false);
            }

            temp = text.Substring(i + 1).TrimStart();
            TokenBase valToken = null;

            if (parseTarget && !EquationTokenizer.TryGetValueToken(ref temp, out valToken))
            {
                return(false);
            }
            text  = temp;
            token = new TypeCastToken()
            {
                TargetType = tuple.Item1 as Type, Target = valToken
            };
            return(true);
        }
Ejemplo n.º 2
0
        }                                                                   // Allow non-assignments for arrays and ICollection<>
        internal override bool TryGetToken(ref string text, out TokenBase token)
        {
            token = null;
            if (!text.StartsWith("new"))
            {
                return(false);
            }
            string temp = text.Substring(3).TrimStart();

            var tuple = GetNameMatches(temp, null, null).Where(tup => tup.Item1 is Type).Reverse().FirstOrDefault();

            if (tuple == null)
            {
                return(false);
            }

            Type type = tuple.Item1 as Type;

            temp = tuple.Item2;

            temp = temp.TrimStart();
            if (temp.Length == 0)
            {
                return(false);
            }
            bool      array = temp[0] == '[';
            TokenBase args;
            List <ConstructorInfo> cons = null;

            if (type.IsArray)
            {
                type  = type.GetElementType();
                array = true;
                string s = "[]";
                new ArgumentListToken('[', ']').TryGetToken(ref s, out args);
            }
            else if (array)
            {
                if (!new ArgumentListToken('[', ']').TryGetToken(ref temp, out args))
                {
                    return(false);
                }
            }
            else
            {
                if (!new ArgumentListToken('(', ')').TryGetToken(ref temp, out args))
                {
                    return(false);
                }
                if ((args as ArgumentListToken).Arguments.Length > 0 || type.IsClass)
                {
                    cons = type.GetConstructors().Where(info => info.GetParameters().Length == (args as ArgumentListToken).Arguments.Length).ToList();
                    for (int i = cons.Count - 1; i >= 0; --i)
                    {
                        for (int j = 0; j < (args as ArgumentListToken).Arguments.Length; ++j)
                        {
                            TypeCastToken cast = (args as ArgumentListToken).Arguments[j] as TypeCastToken;
                            if (cast != null && !cons[i].GetParameters()[j].ParameterType.IsAssignableFrom(cast.TargetType))
                            {
                                cons.RemoveAt(j);
                                break;
                            }
                        }
                    }

                    if (cons.Count > 1)
                    {
                        throw new Exception("Ambiguous constructor call with " + (args as ArgumentListToken).Arguments.Length + " parameter(s) found for type " + type.FullName + ". Try using type casts to disambiguate the call.");
                    }
                    if (cons.Count == 0)
                    {
                        return(false);
                    }
                }
            }

            Type       genericType = null;
            TokenBase  inits       = null;
            string     str         = temp.TrimStart();
            MethodInfo method      = null;

            if (str.Length > 0 && str[0] == '{')
            {
                if (array && (args as ArgumentListToken).Arguments.Length != 0)
                {
                    throw new Exception("Array size arguments and array initializers cannot be used in conjunction.");
                }
                genericType = type.GetInterfaces().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection <>));
                if (array || genericType != null)
                {
                    if (!new ArgumentListToken('{', '}', true).TryGetToken(ref str, out inits))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!new ArgumentListToken('{', '}', type).TryGetToken(ref str, out inits))
                    {
                        return(false);
                    }
                }
                temp = str;
            }

            if (array)
            {
                if ((args as ArgumentListToken).Arguments.Length == 0 && inits == null)
                {
                    return(false);
                }
                token = new ConstructorToken()
                {
                    Arguments = args as ArgumentListToken, ArrayType = type, Initializers = inits as ArgumentListToken
                };
            }
            else if (genericType != null)
            {
                token = new ConstructorToken()
                {
                    Arguments = args as ArgumentListToken, ArrayType = genericType.GetGenericArguments()[0], Constructor = cons != null ? cons[0] : null, ConstructorType = type, Initializers = inits as ArgumentListToken
                }
            }
            ;
            else
            {
                token = new ConstructorToken()
                {
                    Arguments = args as ArgumentListToken, Constructor = cons != null ? cons[0] : null, ConstructorType = type, Initializers = inits as ArgumentListToken
                }
            };
            text = temp;
            return(true);
        }