public ParseTreeValue(TypeTokenPair valuePair)
        {
            _typeTokenPair         = valuePair;
            ParsesToConstantValue  = false;
            _exceedsValueTypeRange = null;
            _hashCode            = valuePair.Token.GetHashCode();
            _dateValue           = null;
            _stringConstant      = null;
            IsMismatchExpression = false;

            if (valuePair.ValueType.Equals(Tokens.Date))
            {
                ParsesToConstantValue = LetCoerce.TryCoerce(_typeTokenPair.Token, out _dateValue);
            }
            else if (valuePair.ValueType.Equals(Tokens.String) && IsStringConstant(valuePair.Token))
            {
                _stringConstant       = new StringLiteralExpression(new ConstantExpression(new StringValue(_typeTokenPair.Token)));
                ParsesToConstantValue = true;
            }
            else if (valuePair.ValueType.Equals(Tokens.String) &&
                     TryGetNonPrintingControlCharCompareToken(valuePair.Token, out _))
            {
                ParsesToConstantValue = true;
            }
        }
        public IParseTreeValue CreateDeclaredType(string expression, string declaredTypeName)
        {
            if (expression is null || declaredTypeName is null)
            {
                throw new ArgumentNullException();
            }

            if (ParseTreeValue.TryGetNonPrintingControlCharCompareToken(expression, out string comparableToken))
            {
                var charConversion = new TypeTokenPair(Tokens.String, comparableToken);
                return(ParseTreeValue.CreateValueType(charConversion));
            }

            var goalTypeTokenPair = new TypeTokenPair(declaredTypeName, null);
            var typeToken         = TypeTokenPair.ConformToType(declaredTypeName, expression);

            if (typeToken.HasValue)
            {
                if (LetCoerce.ExceedsValueTypeRange(typeToken.ValueType, typeToken.Token))
                {
                    return(ParseTreeValue.CreateOverflowExpression(expression, declaredTypeName));
                }
                return(ParseTreeValue.CreateValueType(typeToken));
            }
            return(ParseTreeValue.CreateExpression(new TypeTokenPair(declaredTypeName, expression)));
        }
Exemple #3
0
        public static bool TryParse(string inputString, out TypeTokenPair result)
        {
            result = new TypeTokenPair(null, inputString);
            if (inputString is null || inputString.Length == 0)
            {
                return(false);
            }

            if (IsDateLiteral(inputString))
            {
                result = ConformToType(Tokens.Date, inputString, false);
                if (result.HasValue)
                {
                    return(true);
                }
            }

            if (inputString.StartsWith("\"") && inputString.EndsWith("\""))
            {
                result = ConformToType(Tokens.String, inputString, false);
                return(true);
            }

            if (inputString.Equals(Tokens.True) || inputString.Equals(Tokens.False))
            {
                result = ConformToType(Tokens.Boolean, inputString, false);
                return(true);
            }

            if (inputString.Contains(".") || inputString.Count(ch => ch.Equals('E')) == 1)
            {
                result = ConformToType(Tokens.Double, inputString, false);
                if (result.HasValue)
                {
                    return(true);
                }
            }

            result = ConformToType(Tokens.Integer, inputString, false);
            if (result.HasValue)
            {
                return(true);
            }

            result = ConformToType(Tokens.Long, inputString, false);
            if (result.HasValue)
            {
                return(true);
            }

            result = ConformToType(Tokens.LongLong, inputString, false);
            if (result.HasValue)
            {
                result = new TypeTokenPair(Tokens.Double, result.Token);
                return(true);
            }

            return(false);
        }
        public static IParseTreeValue CreateExpression(TypeTokenPair typeToken)
        {
            var ptValue = new ParseTreeValue()
            {
                _typeTokenPair        = typeToken,
                ParsesToConstantValue = false,
            };

            return(ptValue);
        }
        public static IParseTreeValue CreateValueType(TypeTokenPair value)
        {
            if (value.ValueType.Equals(Tokens.Date) || value.ValueType.Equals(Tokens.String))
            {
                return(new ParseTreeValue(value));
            }

            var ptValue = new ParseTreeValue()
            {
                _typeTokenPair        = value,
                ParsesToConstantValue = true,
                IsOverflowExpression  = LetCoerce.ExceedsValueTypeRange(value.ValueType, value.Token),
            };

            return(ptValue);
        }
        public IParseTreeValue CreateDeclaredType(string expression, string declaredTypeName)
        {
            if (expression is null || declaredTypeName is null)
            {
                throw new ArgumentNullException();
            }

            var goalTypeTokenPair = new TypeTokenPair(declaredTypeName, null);
            var typeToken         = TypeTokenPair.ConformToType(declaredTypeName, expression);

            if (typeToken.HasValue)
            {
                if (LetCoerce.ExceedsValueTypeRange(typeToken.ValueType, typeToken.Token))
                {
                    return(ParseTreeValue.CreateOverflowExpression(expression, declaredTypeName));
                }
                return(ParseTreeValue.CreateValueType(typeToken));
            }
            return(ParseTreeValue.CreateExpression(new TypeTokenPair(declaredTypeName, expression)));
        }
        public IParseTreeValue Create(string valueToken)
        {
            if (valueToken is null || valueToken.Equals(string.Empty))
            {
                throw new ArgumentException();
            }

            if (HasTypeHint(valueToken, out string valueType))
            {
                var vToken = valueToken.Remove(valueToken.Length - 1);
                var conformedTypeTokenPair = TypeTokenPair.ConformToType(valueType, vToken);
                if (conformedTypeTokenPair.HasValue)
                {
                    return(ParseTreeValue.CreateValueType(conformedTypeTokenPair));
                }
                return(ParseTreeValue.CreateExpression(new TypeTokenPair(valueType, vToken)));
            }

            if (TypeTokenPair.TryParse(valueToken, out TypeTokenPair result))
            {
                return(ParseTreeValue.CreateValueType(result));
            }
            return(ParseTreeValue.CreateExpression(new TypeTokenPair(null, valueToken)));
        }