Пример #1
0
        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)));
        }
Пример #3
0
 private static bool TryParseAsDateLiteral(string valueString, out ComparableDateValue value)
 {
     value = default;
     if (IsDateLiteral(valueString))
     {
         return(LetCoerce.TryCoerce(valueString, out value));
     }
     return(false);
 }
Пример #4
0
        private static TypeTokenPair ConformTokenToDate(string valueToken, bool allowOverflow = false)
        {
            var goalValueType = Tokens.Date;

            if (LetCoerce.TryCoerce(valueToken, out ComparableDateValue dvComparable))
            {
                return(new TypeTokenPair(goalValueType, dvComparable.AsDateLiteral()));
            }
            return(new TypeTokenPair(goalValueType, null));
        }
Пример #5
0
        private static TypeTokenPair ConformTokenToBoolean(string valueToken, bool allowOverflow = false)
        {
            var goalValueType = Tokens.Boolean;

            if (LetCoerce.TryCoerce(valueToken, out bool newVal))
            {
                return(new TypeTokenPair(goalValueType, newVal.ToString(CultureInfo.InvariantCulture)));
            }
            return(new TypeTokenPair(goalValueType, null));
        }
Пример #6
0
        private static TypeTokenPair ConformTokenToLongLong(string valueToken, bool allowOverflow = false)
        {
            var goalValueType = Tokens.LongLong;

            if (LetCoerce.TryCoerce(valueToken, out long newVal) ||
                TryParseAsHexLiteral(valueToken, out newVal) ||
                TryParseAsOctalLiteral(valueToken, out newVal))
            {
                return(new TypeTokenPair(goalValueType, newVal.ToString(CultureInfo.InvariantCulture)));
            }

            return(new TypeTokenPair(goalValueType, null));
        }
Пример #7
0
        private static TypeTokenPair ConformTokenToCurrency(string valueToken, bool allowOverflow = true)
        {
            var goalValueType = Tokens.Currency;

            if (LetCoerce.TryCoerce(valueToken, out decimal newVal))
            {
                return(new TypeTokenPair(goalValueType, newVal.ToString(CultureInfo.InvariantCulture)));
            }

            if (allowOverflow && LetCoerce.TryCoerce(valueToken, out double overflowValue))
            {
                return(new TypeTokenPair(goalValueType, overflowValue.ToString(CultureInfo.InvariantCulture)));
            }
            return(new TypeTokenPair(goalValueType, null));
        }
Пример #8
0
        private static TypeTokenPair ConformTokenToByte(string valueToken, bool allowOverflow = true)
        {
            var goalValueType = Tokens.Byte;

            if (LetCoerce.TryCoerce(valueToken, out byte newVal))
            {
                return(new TypeTokenPair(goalValueType, newVal.ToString(CultureInfo.InvariantCulture)));
            }

            if (allowOverflow && LetCoerce.TryCoerce(valueToken, out long overflowValue))
            {
                return(new TypeTokenPair(goalValueType, overflowValue.ToString(CultureInfo.InvariantCulture)));
            }
            return(new TypeTokenPair(goalValueType, null));
        }
Пример #9
0
        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);
        }
Пример #10
0
        private static TypeTokenPair ConformTokenToInteger(string valueToken, bool allowOverflow = true)
        {
            var goalValueType = Tokens.Integer;

            if (LetCoerce.TryCoerce(valueToken, out short newVal) ||
                TryParseAsHexLiteral(valueToken, out newVal) ||
                TryParseAsOctalLiteral(valueToken, out newVal))
            {
                return(new TypeTokenPair(goalValueType, newVal.ToString(CultureInfo.InvariantCulture)));
            }

            if (allowOverflow && LetCoerce.TryCoerce(valueToken, out long overflowValue))
            {
                return(new TypeTokenPair(goalValueType, overflowValue.ToString(CultureInfo.InvariantCulture)));
            }

            return(new TypeTokenPair(goalValueType, null));
        }
        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)));
        }