상속: IronRuby.StandardLibrary.Yaml.Constructor
예제 #1
0
        private static object ConstructRubyDate(RubyConstructor /*!*/ ctor, Node node)
        {
            ScalarNode scalar = node as ScalarNode;

            if (scalar == null)
            {
                throw new ConstructorException("Can only contruct timestamp from scalar node.");
            }

            Match match = BaseConstructor.YmdRegex.Match(scalar.Value);

            if (match.Success)
            {
                int year_ymd  = Int32.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
                int month_ymd = Int32.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
                int day_ymd   = Int32.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);

                RubyModule module;
                if (ctor.GlobalScope.Context.TryGetModule(ctor.GlobalScope, "Date", out module))
                {
                    return(ctor._newSite.Target(ctor._newSite, module, year_ymd, month_ymd, day_ymd));
                }
                else
                {
                    throw new ConstructorException("Date class not found.");
                }
            }
            throw new ConstructorException("Invalid tag:yaml.org,2002:timestamp#ymd value.");
        }
예제 #2
0
        public static object LoadStream(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo,
                                        UnaryOpStorage /*!*/ newStorage, BinaryOpStorage /*!*/ addStorage, RubyScope /*!*/ scope,
                                        RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc = MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io));

            // TODO: only if io was converted to a string:
            io = CreateDefaultStream(newStorage, scope, self);

            AddDocumentsToStream(addStorage, rc, io);
            return(io);
        }
예제 #3
0
        public static object LoadStream(RubyScope /*!*/ scope, RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc          = MakeConstructor(scope, CheckYamlPort(io));
            object          streamClass = RubyUtils.GetConstant(scope, self, _Stream, false);
            object          stream      = _New.Target(_New, scope.RubyContext, streamClass as RubyModule, null);

            foreach (object doc in rc)
            {
                _Add.Target(_Add, scope.RubyContext, stream, doc);
            }
            return(stream);
        }
예제 #4
0
        private static object ParseObject(RubyConstructor /*!*/ ctor, string /*!*/ value)
        {
            Composer composer = RubyYaml.MakeComposer(new StringReader(value), ctor.Encoding);

            if (composer.CheckNode())
            {
                return(ctor.ConstructObject(composer.GetNode()));
            }
            else
            {
                throw new ConstructorException("Invalid YAML element: " + value);
            }
        }
예제 #5
0
        public static object AddDomainType(RubyContext /*!*/ context, BlockParam /*!*/ block, RubyModule /*!*/ self,
                                           MutableString /*!*/ domainAndDate, RubyRegex /*!*/ typeRegex)
        {
            if (block == null)
            {
                throw RubyExceptions.NoBlockGiven();
            }
            MutableString tag = MutableString.Create("tag:").
                                Append(domainAndDate).Append(":").
                                Append(typeRegex.GetPattern());

            RubyConstructor.AddExternalMultiConstructor(tag.ConvertToString(), block);
            return(null);
        }
예제 #6
0
        private static object ConstructRubyStruct(RubyConstructor /*!*/ ctor, string /*!*/ structName, Node /*!*/ node)
        {
            MappingNode mapping = node as MappingNode;

            if (mapping == null)
            {
                throw new ConstructorException("can only construct struct from mapping node");
            }

            if (structName.Length == 0)
            {
                // TODO:
                throw new NotSupportedException("anonymous structs not supported");
            }

            RubyContext context = ctor.GlobalScope.Context;
            RubyModule  module;

            // TODO: MRI calls "members" on an arbitrary object

            // MRI checks Struct first, then falls back to Object
            if (!context.TryGetModule(ctor.GlobalScope, "Struct::" + structName, out module) &&
                !context.TryGetModule(ctor.GlobalScope, structName, out module))
            {
                throw RubyExceptions.CreateTypeError("Undefined struct `{0}'", structName);
            }

            RubyClass cls = module as RubyClass;

            if (cls == null)
            {
                throw RubyExceptions.CreateTypeError("`{0}' is not a class", structName);
            }

            RubyStruct newStruct = RubyStruct.Create(cls);

            foreach (var pair in ctor.ConstructMapping(mapping))
            {
                var attributeName = pair.Key as MutableString;
                int index;

                // TODO: encoding
                if (attributeName != null && newStruct.TryGetIndex(attributeName.ToString(), out index))
                {
                    newStruct[index] = pair.Value;
                }
            }
            return(newStruct);
        }
예제 #7
0
        /// <summary>
        /// Returns MutableString or RubySymbol.
        /// </summary>
        private static object ConstructRubyString(RubyConstructor /*!*/ ctor, Node /*!*/ node)
        {
            ScalarNode scalar = (ScalarNode)node;
            string     value  = ctor.ConstructScalar(node);

            if (value == null)
            {
                return(null);
            }

            if (value.Length > 1 && value[0] == ':' && scalar.Style == ScalarQuotingStyle.None)
            {
                return(ctor.GlobalScope.Context.CreateAsciiSymbol(value.Substring(1)));
            }

            return(MutableString.CreateMutable(value, ctor.RubyEncoding));
        }
예제 #8
0
        private static object ConstructPrivateObject(RubyConstructor /*!*/ ctor, string className, Node node)
        {
            MappingNode mapping = node as MappingNode;

            if (mapping == null)
            {
                throw new ConstructorException("can only construct private type from mapping node");
            }
            RubyModule      module;
            RubyGlobalScope globalScope = ctor.GlobalScope;

            if (globalScope.Context.TryGetModule(globalScope, className, out module))
            {
                if (!module.IsClass)
                {
                    throw new ConstructorException("Cannot construct module");
                }
                Hash values = ctor.ConstructMapping(mapping);
                // TODO: call allocate here:

                object         result = null;
                RubyMethodInfo method = module.GetMethod("yaml_initialize") as RubyMethodInfo;
                if (method != null)
                {
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module);
                    ctor._yamlInitializeSite.Target(ctor._yamlInitializeSite, result, className, values);
                }
                else
                {
                    var dict = new Dictionary <string, object>(values.Count);
                    foreach (var kvp in EnumerateAttributes(globalScope.Context, values))
                    {
                        dict.Add(kvp.Key, kvp.Value);
                    }
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module, dict);
                }
                return(result);
            }
            else
            {
                //TODO: YAML::Object
                throw new NotImplementedError("YAML::Object is not implemented yet");
            }
        }
예제 #9
0
        public static object EachDocument(RubyScope /*!*/ scope, BlockParam block, RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc = MakeConstructor(scope, CheckYamlPort(io));

            if (block == null && rc.CheckData())
            {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (object obj in rc)
            {
                object result;
                if (block.Yield(obj, out result))
                {
                    return(result);
                }
            }
            return(null);
        }
예제 #10
0
        private static RubyRegex /*!*/ ConstructRubyRegexp(RubyConstructor /*!*/ ctor, Node /*!*/ node)
        {
            ScalarNode scalar = node as ScalarNode;

            if (node == null)
            {
                throw RubyExceptions.CreateTypeError("Can only create regex from scalar node");
            }
            Match match = _regexPattern.Match(scalar.Value);

            if (!match.Success)
            {
                throw new ConstructorException("Invalid Regular expression: \"" + scalar.Value + "\"");
            }
            RubyRegexOptions options = new RubyRegexOptions();

            foreach (char c in match.Groups["opts"].Value)
            {
                switch (c)
                {
                case 'i': options |= RubyRegexOptions.IgnoreCase; break;

                case 'x': options |= RubyRegexOptions.Extended; break;

                case 'm': options |= RubyRegexOptions.Multiline; break;

                case 'o': break;

                case 'n': options |= RubyRegexOptions.FIXED; break;

                case 'e': options |= RubyRegexOptions.EUC; break;

                case 's': options |= RubyRegexOptions.SJIS; break;

                case 'u': options |= RubyRegexOptions.UTF8; break;

                default:
                    throw new ConstructorException("Unknown regular expression option: '" + c + "'");
                }
            }
            // TODO: encoding (ignore kcode on 1.9, string enc?):
            return(new RubyRegex(MutableString.CreateMutable(match.Groups["expr"].Value, RubyEncoding.UTF8), options));
        }
예제 #11
0
        public static object EachDocument(ConversionStorage <MutableString> /*!*/ toStr, RespondToStorage /*!*/ respondTo,
                                          RubyScope /*!*/ scope, BlockParam block, RubyModule /*!*/ self, object io)
        {
            RubyConstructor rc = MakeConstructor(scope.GlobalScope, GetStream(toStr, respondTo, io));

            if (block == null && rc.CheckData())
            {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (object obj in rc)
            {
                object result;
                if (block.Yield(obj, out result))
                {
                    return(result);
                }
            }
            return(null);
        }
예제 #12
0
 private static object ConstructPrivateObject(RubyConstructor/*!*/ ctor, string className, Node node) {
     MappingNode mapping = node as MappingNode;
     if (mapping == null) {
         throw new ConstructorException("can only construct private type from mapping node");
     }
     RubyModule module;
     RubyGlobalScope globalScope = ctor.GlobalScope;
     if (globalScope.Context.TryGetModule(globalScope, className, out module)) {
         if (!module.IsClass) {
             throw new ConstructorException("Cannot construct module");
         }
         Hash values = ctor.ConstructMapping(mapping);
         RubyMethodInfo method = module.GetMethod("yaml_initialize") as RubyMethodInfo;
         if (method != null) {
             object result = RubyUtils.CreateObject((RubyClass)module);
             ctor._yamlInitializeSite.Target(ctor._yamlInitializeSite, result, className, values);
             return result;
         } else {
             return RubyUtils.CreateObject((RubyClass)module, values, true);
         }
     } else {
         //TODO: YAML::Object
         throw new NotImplementedError("YAML::Object is not implemented yet");
     }
 }
예제 #13
0
        private static object ConstructRubyStruct(RubyConstructor/*!*/ ctor, string className, Node node) {
            MappingNode mapping = node as MappingNode;
            if (mapping == null) {
                throw new ConstructorException("can only construct struct from mapping node");
            }

            RubyContext context = ctor.GlobalScope.Context;
            RubyModule module;
            RubyClass cls;
            if (context.TryGetModule(ctor.GlobalScope, className, out module)) {
                cls = module as RubyClass;
                if (cls == null) {
                    throw new ConstructorException("Struct type name must be Ruby class");
                }
            } else {
                RubyModule structModule = context.GetModule(typeof(RubyStruct));
                cls = RubyUtils.GetConstant(ctor.GlobalScope, structModule, className, false) as RubyClass;
                if (cls == null) {
                    throw new ConstructorException(String.Format("Cannot find struct class \"{0}\"", className));
                }
            }

            RubyStruct newStruct = RubyStruct.Create(cls);
            foreach (var pair in ctor.ConstructMapping(mapping)) {
                RubyStructOps.SetValue(newStruct, SymbolTable.StringToId(pair.Key.ToString()), pair.Value);        
            }
            return newStruct;
        }
예제 #14
0
        private static Range ConstructRubyRange(RubyConstructor/*!*/ ctor, Node node) {
            object begin = null;
            object end = null;
            bool excludeEnd = false;
            ScalarNode scalar = node as ScalarNode;                        
            if (scalar != null) {
                string value = scalar.Value;                
                int dotsIdx;
                if ((dotsIdx = value.IndexOf("...")) != -1) {
                    begin = ParseObject(ctor, value.Substring(0, dotsIdx));                
                    end = ParseObject(ctor, value.Substring(dotsIdx + 3));
                    excludeEnd = true;
                } else if ((dotsIdx = value.IndexOf("..")) != -1) {
                    begin = ParseObject(ctor, value.Substring(0, dotsIdx));
                    end = ParseObject(ctor, value.Substring(dotsIdx + 2));
                } else {
                    throw new ConstructorException("Invalid Range: " + value);
                }
            } else {
                MappingNode mapping = node as MappingNode;
                if (mapping == null) {
                    throw new ConstructorException("Invalid Range: " + node);    
                }
                foreach (KeyValuePair<Node, Node> n in mapping.Nodes) {
                    string key = ctor.ConstructScalar(n.Key).ToString();
                    switch (key) {
                        case "begin":
                            begin = ctor.ConstructObject(n.Value);
                            break;
                        case "end":
                            end = ctor.ConstructObject(n.Value);
                            break;
                        case "excl":
                            TryConstructYamlBool(ctor, n.Value, out excludeEnd);
                            break;
                        default:
                            throw new ConstructorException(string.Format("'{0}' is not allowed as an instance variable name for class Range", key));
                    }
                }                
            }

            var comparisonStorage = new BinaryOpStorage(ctor.GlobalScope.Context);
            return new Range(comparisonStorage, ctor.GlobalScope.Context, begin, end, excludeEnd);            
        }
예제 #15
0
 private static RubyRegex ConstructRubyRegexp(RubyConstructor/*!*/ ctor, Node node) {
     ScalarNode scalar = node as ScalarNode;
     if (node == null) {
         throw RubyExceptions.CreateTypeError("Can only create regex from scalar node");
     }                        
     Match match = _regexPattern.Match(scalar.Value);
     if (!match.Success) {
         throw new ConstructorException("Invalid Regular expression: \"" + scalar.Value + "\"");
     }
     RubyRegexOptions options = new RubyRegexOptions();
     foreach (char c in match.Groups["opts"].Value) {
         switch (c) {
             case 'i': options |= RubyRegexOptions.IgnoreCase; break;
             case 'x': options |= RubyRegexOptions.Extended; break;
             case 'm': options |= RubyRegexOptions.Multiline; break;
             case 'o': break;
             case 'n': options |= RubyRegexOptions.FIXED; break;
             case 'e': options |= RubyRegexOptions.EUC; break;
             case 's': options |= RubyRegexOptions.SJIS; break;
             case 'u': options |= RubyRegexOptions.UTF8; break;
             default:
                 throw new ConstructorException("Unknown regular expression option: '" + c + "'");
         }
     }            
     // TODO: encoding (ignore kcode on 1.9, string enc?):
     return new RubyRegex(MutableString.CreateMutable(match.Groups["expr"].Value, RubyEncoding.UTF8), options);            
 }
예제 #16
0
 private static object ConstructRubyScalar(RubyConstructor/*!*/ ctor, Node node) {
     object value = ctor.ConstructScalar(node);
     if (value == null) {
         return value;
     }
     string str = value as string;
     if (str != null) {
         return MutableString.Create(str, RubyEncoding.UTF8);
     }
     return value;
 }
예제 #17
0
 private static object ParseObject(RubyConstructor/*!*/ ctor, string value) {
     Composer composer = RubyYaml.MakeComposer(new StringReader(value));
     if (composer.CheckNode()) {
         return ctor.ConstructObject(composer.GetNode());
     } else {
         throw new ConstructorException("Invalid YAML element: " + value);
     }
 }
예제 #18
0
 private static object ConstructRubySymbol(RubyConstructor/*!*/ ctor, Node/*!*/ node) {
     return ctor.GlobalScope.Context.CreateAsciiSymbol(((ScalarNode)node).Value);
 }
예제 #19
0
        private static object ConstructRubyStruct(RubyConstructor/*!*/ ctor, string/*!*/ structName, Node/*!*/ node) {
            MappingNode mapping = node as MappingNode;
            if (mapping == null) {
                throw new ConstructorException("can only construct struct from mapping node");
            }

            if (structName.Length == 0) {
                // TODO:
                throw new NotSupportedException("anonymous structs not supported");
            }

            RubyContext context = ctor.GlobalScope.Context;
            RubyModule module;

            // TODO: MRI calls "members" on an arbitrary object
            
            // MRI checks Struct first, then falls back to Object
            if (!context.TryGetModule(ctor.GlobalScope, "Struct::" + structName, out module) && 
                !context.TryGetModule(ctor.GlobalScope, structName, out module)) {
                throw RubyExceptions.CreateTypeError("Undefined struct `{0}'", structName);
            }

            RubyClass cls = module as RubyClass;
            if (cls == null) {
                throw RubyExceptions.CreateTypeError("`{0}' is not a class", structName);
            }

            RubyStruct newStruct = RubyStruct.Create(cls);
            foreach (var pair in ctor.ConstructMapping(mapping)) {
                var attributeName = pair.Key as MutableString;
                int index;

                // TODO: encoding
                if (attributeName != null && newStruct.TryGetIndex(attributeName.ToString(), out index)) {
                    newStruct[index] = pair.Value;
                }
            }
            return newStruct;
        }
예제 #20
0
        /// <summary>
        /// Returns MutableString or RubySymbol.
        /// </summary>
        private static object ConstructRubyString(RubyConstructor/*!*/ ctor, Node/*!*/ node) {
            ScalarNode scalar = (ScalarNode)node;
            string value = ctor.ConstructScalar(node);

            if (value == null) {
                return null;
            }

            if (value.Length > 1 && value[0] == ':' && scalar.Style == ScalarQuotingStyle.None) {
                return ctor.GlobalScope.Context.CreateAsciiSymbol(value.Substring(1));
            }

            return MutableString.CreateMutable(value, ctor.RubyEncoding);
        }
예제 #21
0
        private static object ConstructPrivateObject(RubyConstructor/*!*/ ctor, string className, Node node)
        {
            MappingNode mapping = node as MappingNode;
            if (mapping == null) {
                throw new ConstructorException("can only construct private type from mapping node");
            }
            RubyModule module;
            RubyGlobalScope globalScope = ctor.GlobalScope;
            if (globalScope.Context.TryGetModule(globalScope, className, out module)) {
                if (!module.IsClass) {
                    throw new ConstructorException("Cannot construct module");
                }
                Hash values = ctor.ConstructMapping(mapping);
                // TODO: call allocate here:

                object result = null;
                RubyMethodInfo method = module.GetMethod("yaml_initialize") as RubyMethodInfo;
                if (method != null) {
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module);
                    ctor._yamlInitializeSite.Target(ctor._yamlInitializeSite, result, className, values);
                } else {
                    var dict = new Dictionary<string, object>(values.Count);
                    foreach (var kvp in EnumerateAttributes(globalScope.Context, values)) {
                        dict.Add(kvp.Key, kvp.Value);
                    }
                    result = RubyMarshal.Utils.CreateObjectAndSetIvars((RubyClass)module, dict);
                }
                return result;
            } else {
                //TODO: YAML::Object
                throw new NotImplementedError("YAML::Object is not implemented yet");
            }
        }
예제 #22
0
 private static object ConstructRubySymbol(RubyConstructor /*!*/ ctor, Node /*!*/ node)
 {
     return(ctor.GlobalScope.Context.CreateAsciiSymbol(((ScalarNode)node).Value));
 }
예제 #23
0
 private static MutableString ConstructRubyBinary(RubyConstructor/*!*/ ctor, Node node) {
     return MutableString.CreateBinary(BaseConstructor.ConstructYamlBinary(ctor, node));
 }
예제 #24
0
        private static Range /*!*/ ConstructRubyRange(RubyConstructor /*!*/ ctor, Node /*!*/ node)
        {
            object     begin      = null;
            object     end        = null;
            bool       excludeEnd = false;
            ScalarNode scalar     = node as ScalarNode;

            if (scalar != null)
            {
                string value = scalar.Value;
                int    dotsIdx;
                if ((dotsIdx = value.IndexOf("...", StringComparison.Ordinal)) != -1)
                {
                    begin      = ParseObject(ctor, value.Substring(0, dotsIdx));
                    end        = ParseObject(ctor, value.Substring(dotsIdx + 3));
                    excludeEnd = true;
                }
                else if ((dotsIdx = value.IndexOf("..", StringComparison.Ordinal)) != -1)
                {
                    begin = ParseObject(ctor, value.Substring(0, dotsIdx));
                    end   = ParseObject(ctor, value.Substring(dotsIdx + 2));
                }
                else
                {
                    throw new ConstructorException("Invalid Range: " + value);
                }
            }
            else
            {
                MappingNode mapping = node as MappingNode;
                if (mapping == null)
                {
                    throw new ConstructorException("Invalid Range: " + node);
                }
                foreach (KeyValuePair <Node, Node> n in mapping.Nodes)
                {
                    string key = ctor.ConstructScalar(n.Key).ToString();
                    switch (key)
                    {
                    case "begin":
                        begin = ctor.ConstructObject(n.Value);
                        break;

                    case "end":
                        end = ctor.ConstructObject(n.Value);
                        break;

                    case "excl":
                        if (!TryConstructYamlBool(ctor, n.Value, out excludeEnd))
                        {
                            throw new ConstructorException("Invalid Range: " + node);
                        }
                        break;

                    default:
                        throw new ConstructorException(String.Format("'{0}' is not allowed as an instance variable name for class Range", key));
                    }
                }
            }

            var comparisonStorage = new BinaryOpStorage(ctor.GlobalScope.Context);

            return(new Range(comparisonStorage, ctor.GlobalScope.Context, begin, end, excludeEnd));
        }
예제 #25
0
        private static object ConstructRubyTimestampYMD(RubyConstructor/*!*/ ctor, Node node) {
            ScalarNode scalar = node as ScalarNode;
            if (scalar == null) {
                throw new ConstructorException("Can only contruct timestamp from scalar node.");
            }

            Match match = BaseConstructor.YmdRegex.Match(scalar.Value);
            if (match.Success) {
                int year_ymd = Int32.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
                int month_ymd = Int32.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
                int day_ymd = Int32.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture);

                RubyModule module;
                if (ctor.GlobalScope.Context.TryGetModule(ctor.GlobalScope, "Date", out module)) {
                    return ctor._newSite.Target(ctor._newSite, module, year_ymd, month_ymd, day_ymd);
                } else {
                    throw new ConstructorException("Date class not found.");
                }
            }
            throw new ConstructorException("Invalid tag:yaml.org,2002:timestamp#ymd value.");
        }
예제 #26
0
 private static MutableString /*!*/ ConstructRubyBinary(RubyConstructor /*!*/ ctor, Node /*!*/ node)
 {
     return(MutableString.CreateBinary(BaseConstructor.ConstructYamlBinary(ctor, node)));
 }