public ConstantInferenceFailedException(RelinqScriptExpression root, ConstantExpression ce)
     : base(JSToCSharpExceptionType.ConstantInferenceFailed, root, ce)
 {
     Constant = ce.Content;
     TokenType = String.Format("{0}.{1} ({2})",  
         typeof(EcmaScriptV3Lexer).Name,
         ce.Token.Type.GetSymbolicName<EcmaScriptV3Parser>(),
         ce.Token.Type);
 }
Exemplo n.º 2
0
 public UnknownList(ConstantExpression constant)
     : base(constant)
 {
     if (!EcmaScriptV3Syntax.IsArray(constant.Token))
     {
         throw new ArgumentException(String.Format(
             "No way that constant expression '{0}' can represent " +
             "an object serialized as list.", this));
     }
 }
Exemplo n.º 3
0
 private void InferConstant(ConstantExpression ce, TypeInferenceCache cache)
 {
     var inferred = TypeInferenceConstants.InferType(ce);
     if (inferred != null)
     {
         cache.Add(ce, inferred);
     }
     else
     {
         throw new ConstantInferenceFailedException(Root, ce);
     }
 }
Exemplo n.º 4
0
 protected UnknownConstant(ConstantExpression constant)
 {
     Constant = constant;
 }
Exemplo n.º 5
0
 public static RelinqScriptType InferType(ConstantExpression ce)
 {
     if (ce.Token.Text == "null")
     {
         return new Null();
     }
     else if (ce.Token.Text == "false" || ce.Token.Text == "true")
     {
         return typeof(bool);
     }
     else
     {
         if (EcmaScriptV3Syntax.IsInteger(ce.Token))
         {
             try
             {
                 try
                 {
                     var value = (long)typeof(long).FromInvariantString(ce.Token.Text);
                     if (long.MinValue <= value && value < int.MinValue)
                     {
                         return typeof(long);
                     }
                     else if (int.MinValue <= value && value <= int.MaxValue)
                     {
                         return typeof(int);
                     }
                     else if (value == (long)int.MaxValue + 1)
                     {
                         var parentIsUnaryMinus = ce.Parent is OperatorExpression &&
                             ((OperatorExpression)ce.Parent).Type == OperatorType.UnaryMinus;
                         return parentIsUnaryMinus ? typeof(int) : typeof(long);
                     }
                     else if ((long)int.MaxValue + 1 < value && value <= uint.MaxValue)
                     {
                         return typeof(uint);
                     }
                     else if (uint.MaxValue < value && value <= long.MaxValue)
                     {
                         return typeof(long);
                     }
                     else
                     {
                         throw new ArgumentException("Something was overlooked: " +
                             "no way a long number can have value '" + value + "'.");
                     }
                 }
                 catch (OverflowException)
                 {
                     ulong value;
                     if (ulong.TryParse(ce.Token.Text, out value))
                     {
                         if (value == (ulong)long.MaxValue + 1)
                         {
                             var parentIsUnaryMinus = ce.Parent is OperatorExpression &&
                                 ((OperatorExpression)ce.Parent).Type == OperatorType.UnaryMinus;
                             return parentIsUnaryMinus ? typeof(long) : typeof(ulong);
                         }
                         else if ((ulong)long.MaxValue + 1 < value && value <= ulong.MaxValue)
                         {
                             return typeof(ulong);
                         }
                         else
                         {
                             throw new ArgumentException("Something was overlooked: " +
                                 "no way an ulong (but not long) number can have value '" + value + "'.");
                         }
                     }
                     else
                     {
                         throw;
                     }
                 }
             }
             catch (Exception)
             {
                 return null;
             }
         }
         else if (EcmaScriptV3Syntax.IsFloat(ce.Token))
         {
             try
             {
                 return typeof(double).FromInvariantString(ce.Token.Text).GetType();
             }
             catch (Exception)
             {
                 return null;
             }
         }
         else if (EcmaScriptV3Syntax.IsString(ce.Token))
         {
             return new UnknownStringizedObject(ce);
         }
         else if (EcmaScriptV3Syntax.IsObject(ce.Token))
         {
             return new UnknownPropertyBag(ce);
         }
         else if (EcmaScriptV3Syntax.IsArray(ce.Token))
         {
             return new UnknownList(ce);
         }
         else
         {
             return null;
         }
     }
 }
Exemplo n.º 6
0
        private LinqExpression CompileConstant(ConstantExpression ce, CompilationContext ctx)
        {
            Func<Type, LinqExpression> constFactory = clrType =>
                LinqExpression.Constant(JsonDeserializer.Deserialize(ce.Data, clrType), clrType);

            if (ctx.Types[ce] is ClrType)
            {
                return constFactory(((ClrType)ctx.Types[ce]).Type);
            }
            else if (ctx.Types[ce] is Null || ctx.Types[ce] is UnknownConstant)
            {
                return constFactory(AcquireExpectedTypeFromContext(ce, ctx));
            }
            else
            {
                throw new CSharpBuilderException(
                    JSToCSharpExceptionType.UnexpectedInferredAst, Ast, ce, ctx);
            }
        }