Esempio n. 1
0
 public ScriptParserRuntimeException(SourceSpan span, string message, List<LogMessage> parserMessages, Exception innerException) : base(span, message, innerException)
 {
     if (parserMessages == null) throw new ArgumentNullException(nameof(parserMessages));
     ParserMessages = parserMessages;
 }
Esempio n. 2
0
 public SourceSpan(ScribanSourceSpan span)
 {
     FileName = span.FileName;
     Start    = new TextPosition(span.Start);
     End      = new TextPosition(span.End);
 }
Esempio n. 3
0
 public ScriptRuntimeException(SourceSpan span, string message, Exception innerException) : base(message, innerException)
 {
     Span = span;
 }
Esempio n. 4
0
 public ScriptParserRuntimeException(SourceSpan span, string message, List<LogMessage> parserMessages) : this(span, message, parserMessages, null)
 {
 }
Esempio n. 5
0
        public static object ToObject(SourceSpan span, object value, Type destinationType)
        {
            if (destinationType == null) throw new ArgumentNullException(nameof(destinationType));
            if (value == null)
            {
                if (destinationType == typeof (bool))
                {
                    return false;
                }

                if (destinationType == typeof (string))
                {
                    return string.Empty;
                }

                // TODO: Couldn't we get a converter method that support null to -> 0?
                if (destinationType == typeof(int))
                {
                    return (int)0;
                }
                else if (destinationType == typeof(double))
                {
                    return (double)0.0;
                }
                else if (destinationType == typeof(float))
                {
                    return (float)0.0f;
                }
                else if (destinationType == typeof(long))
                {
                    return (long)0L;
                }

                return null;
            }

            var type = value.GetType();
            var typeInfo = type.GetTypeInfo();
            var destTypeInfo = destinationType.GetTypeInfo();

            if (destTypeInfo.IsAssignableFrom(typeInfo))
            {
                return value;
            }

            if (destinationType == typeof (string))
            {
                return ToString(span, value);
            }

            if (typeInfo.IsPrimitive && destTypeInfo.IsPrimitive)
            {
                try
                {
                    return Convert.ChangeType(value, destinationType, CultureInfo.InvariantCulture);
                }
                catch (FormatException ex)
                {
                    throw new ScriptRuntimeException(span, $"Unable to convert type [{value.GetType()}] to [{destinationType}]", ex);
                }
            }

            var customType = value as IScriptCustomType;
            if (customType != null)
            {
                object result;
                if (customType.TryConvertTo(destinationType, out result))
                {
                    return result;
                }
            }

            return value;
        }
Esempio n. 6
0
 public ScriptRuntimeException(SourceSpan span, string message) : base(message)
 {
     Span = span;
 }
Esempio n. 7
0
 public static double ToDouble(SourceSpan span, object value)
 {
     try
     {
         return Convert.ToDouble(value, CultureInfo.InvariantCulture);
     }
     catch (FormatException ex)
     {
         throw new ScriptRuntimeException(span, $"Unable to convert type [{value.GetType()}] to double", ex);
     }
 }
Esempio n. 8
0
        public static string ToString(SourceSpan span, object value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            if (value is bool)
            {
                return ((bool) value) ? "true" : "false";
            }

            if (value is string)
            {
                return (string) value;
            }

            if (value is IScriptCustomFunction)
            {
                return "<function>";
            }

            // Dump a script object
            var scriptObject = value as ScriptObject;
            if (scriptObject != null)
            {
                var result = new StringBuilder();
                result.Append("{");
                bool isFirst = true;
                foreach (var item in scriptObject)
                {
                    if (!isFirst)
                    {
                        result.Append(", ");
                    }
                    var keyPair = (KeyValuePair<string, object>) item;
                    result.Append(keyPair.Key);
                    result.Append(": ");
                    result.Append(ToString(span, keyPair.Value));
                    isFirst = false;
                }
                result.Append("}");
                return result.ToString();
            }

            var type = value.GetType();
            if (type.GetTypeInfo().IsPrimitive)
            {
                try
                {
                    return Convert.ToString(value, CultureInfo.InvariantCulture);
                }
                catch (FormatException ex)
                {
                    throw new ScriptRuntimeException(span, $"Unable to convert value of type [{value.GetType()}] to string", ex);
                }
            }

            // Dump an enumeration
            var enumerable = value as IEnumerable;
            if (enumerable != null)
            {
                var result = new StringBuilder();
                result.Append("[");
                bool isFirst = true;
                foreach (var item in enumerable)
                {
                    if (!isFirst)
                    {
                        result.Append(", ");
                    }
                    result.Append(ToString(span, item));
                    isFirst = false;
                }
                result.Append("]");
                return result.ToString();
            }

            return value.ToString();
        }
Esempio n. 9
0
 public LogMessage(ParserMessageType type, SourceSpan span, string message)
 {
     Type = type;
     Span = span;
     Message = message;
 }
Esempio n. 10
0
 private void LogError(SourceSpan span, string text)
 {
     Log(new LogMessage(ParserMessageType.Error, span, text));
 }
Esempio n. 11
0
 /// <summary>
 /// Writes an object value to the current <see cref="Output"/>.
 /// </summary>
 /// <param name="span">The span of the object to render.</param>
 /// <param name="textAsObject">The text as object.</param>
 public void Write(SourceSpan span, object textAsObject)
 {
     if (textAsObject == null)
     {
         return;
     }
     var text = ScriptValueConverter.ToString(span, textAsObject);
     Write(text);
 }