Inheritance: CollectionNode
Exemplo n.º 1
0
        public object ConstructScalar(Node node)
        {
            ScalarNode scalar = node as ScalarNode;

            if (scalar == null)
            {
                MappingNode mapNode = node as MappingNode;
                if (mapNode != null)
                {
                    foreach (KeyValuePair <Node, Node> entry in mapNode.Nodes)
                    {
                        if ("tag:yaml.org,2002:value" == entry.Key.Tag)
                        {
                            return(ConstructScalar(entry.Value));
                        }
                    }
                }
                throw new ConstructorException("expected a scalar or mapping node, but found: " + node);
            }
            string value = scalar.Value;

            if (value.Length > 1 && value[0] == ':' && scalar.Style == '\0')
            {
                return(SymbolTable.StringToId(value.Substring(1)));
            }
            return(value);
        }
Exemplo n.º 2
0
        public static Range ConstructRubyRange(IConstructor /*!*/ 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));
                    }
                }
            }
            return(new Range(ctor.Scope.RubyContext, begin, end, excludeEnd));
        }
Exemplo n.º 3
0
                public static object CreateMap([NotNull]BlockParam/*!*/ block, Out/*!*/ self, [DefaultProtocol]MutableString taguri, object yamlStyle) {
                    var rep = self._representer;
                    var map = new MappingNode(rep.ToTag(taguri), new Dictionary<Node, Node>(), RubyYaml.ToYamlFlowStyle(yamlStyle));
                    
                    object blockResult;
                    if (block.Yield(map, out blockResult)) {
                        return blockResult;
                    }

                    return map;
                }
Exemplo n.º 4
0
                public static object CreateMap([NotNull] BlockParam /*!*/ block, Out /*!*/ self, [DefaultProtocol] MutableString taguri, object yamlStyle)
                {
                    var rep = self._representer;
                    var map = new MappingNode(rep.ToTag(taguri), new Dictionary <Node, Node>(), RubyYaml.ToYamlFlowStyle(yamlStyle));

                    object blockResult;

                    if (block.Yield(map, out blockResult))
                    {
                        return(blockResult);
                    }

                    return(map);
                }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public object ConstructPairs(Node mappingNode)
        {
            MappingNode map = mappingNode as MappingNode;

            if (map == null)
            {
                throw new ConstructorException("expected a mapping node, but found: " + mappingNode);
            }

            List <object[]> result = new List <object[]>();

            foreach (KeyValuePair <Node, Node> entry in map.Nodes)
            {
                result.Add(new object[] { ConstructObject(entry.Key), ConstructObject(entry.Value) });
            }
            return(result);
        }
Exemplo n.º 7
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");
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns the value of the scalar.
        /// </summary>
        public string ConstructScalar(Node /*!*/ node)
        {
            ScalarNode scalar = node as ScalarNode;

            if (scalar == null)
            {
                MappingNode mapNode = node as MappingNode;
                if (mapNode != null)
                {
                    foreach (KeyValuePair <Node, Node> entry in mapNode.Nodes)
                    {
                        if (entry.Key.Tag == "tag:yaml.org,2002:value")
                        {
                            return(ConstructScalar(entry.Value));
                        }
                    }
                }
                throw new ConstructorException("expected a scalar or mapping node, but found: " + node);
            }
            return(scalar.Value);
        }
Exemplo n.º 9
0
        public static object ConstructRubyStruct(IConstructor ctor, string className, Node node)
        {
            MappingNode mapping = node as MappingNode;

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

            RubyScope  scope = ctor.Scope;
            RubyModule module;
            RubyClass  cls;

            if (scope.RubyContext.TryGetModule(scope.GlobalScope, className, out module))
            {
                cls = module as RubyClass;
                if (cls == null)
                {
                    throw new ConstructorException("Struct type name must be Ruby class");
                }
            }
            else
            {
                RubyModule structModule = scope.RubyContext.GetModule(typeof(RubyStruct));
                cls = RubyUtils.GetConstant(scope, 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);
        }
Exemplo n.º 10
0
        public static object ConstructPrivateObject(IConstructor 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;
            RubyScope  scope = ctor.Scope;

            if (scope.RubyContext.TryGetModule(scope.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);
                    _YamlInitialize.Target(_YamlInitialize, scope.RubyContext, 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");
            }
        }
Exemplo n.º 11
0
        private Node ComposeNode(Node parent, object index)
        {
            Node      result;
            YamlEvent @event    = _parser.PeekEvent();
            NodeEvent nodeEvent = @event as NodeEvent;

            string anchor = (nodeEvent != null) ? nodeEvent.Anchor : null;

            if (nodeEvent is AliasEvent)
            {
                _parser.GetEvent();
                if (!_anchors.TryGetValue(anchor, out result))
                {
                    throw new ComposerException("found undefined alias: " + anchor);
                }
                return(result);
            }

            result = null;
            //_resolver.descendResolver(parent, index);
            if (@event is ScalarEvent)
            {
                ScalarEvent ev  = (ScalarEvent)_parser.GetEvent();
                string      tag = ev.Tag;
                if (tag == null || tag == "!")
                {
                    tag = Resolver.Resolve(typeof(ScalarNode), ev.Value, ev.Implicit);
                }
                result = new ScalarNode(tag, ev.Value, ev.Style);
                if (null != anchor)
                {
                    AddAnchor(anchor, result);
                }
            }
            else if (@event is SequenceStartEvent)
            {
                SequenceStartEvent start = (SequenceStartEvent)_parser.GetEvent();
                string             tag   = start.Tag;
                if (tag == null || tag == "!")
                {
                    tag = Resolver.Resolve(typeof(SequenceNode), null, start.Implicit);
                }
                SequenceNode seqResult = new SequenceNode(tag, new List <Node>(), start.FlowStyle);
                result = seqResult;
                if (null != anchor)
                {
                    AddAnchor(anchor, seqResult);
                }
                int ix = 0;
                while (!(_parser.PeekEvent() is SequenceEndEvent))
                {
                    seqResult.Nodes.Add(ComposeNode(seqResult, ix++));
                }
                _parser.GetEvent();
            }
            else if (@event is MappingStartEvent)
            {
                MappingStartEvent start = (MappingStartEvent)_parser.GetEvent();
                string            tag   = start.Tag;
                if (tag == null || tag == "!")
                {
                    tag = Resolver.Resolve(typeof(MappingNode), null, start.Implicit);
                }
                MappingNode mapResult = new MappingNode(tag, new Dictionary <Node, Node>(), start.FlowStyle);
                result = mapResult;
                if (null != anchor)
                {
                    AddAnchor(anchor, result);
                }
                while (!(_parser.PeekEvent() is MappingEndEvent))
                {
                    YamlEvent key      = _parser.PeekEvent();
                    Node      itemKey  = ComposeNode(mapResult, null);
                    Node      composed = ComposeNode(mapResult, itemKey);
                    if (!mapResult.Nodes.ContainsKey(itemKey))
                    {
                        mapResult.Nodes.Add(itemKey, composed);
                    }
                }
                _parser.GetEvent();
            }
            //_resolver.ascendResolver();
            return(result);
        }
Exemplo n.º 12
0
        //TODO: remove Ruby-specific stuff from this layer
        public Hash ConstructMapping(Node mappingNode)
        {
            MappingNode map = mappingNode as MappingNode;

            if (map == null)
            {
                throw new ConstructorException("expected a mapping node, but found: " + mappingNode);
            }
            Hash mapping            = new Hash(_globalScope.Context);
            LinkedList <Hash> merge = null;

            foreach (KeyValuePair <Node, Node> entry in map.Nodes)
            {
                Node key_v   = entry.Key;
                Node value_v = entry.Value;

                if (key_v.Tag == "tag:yaml.org,2002:merge")
                {
                    if (merge != null)
                    {
                        throw new ConstructorException("while constructing a mapping: found duplicate merge key");
                    }
                    SequenceNode sequence;
                    merge = new LinkedList <Hash>();
                    if (value_v is MappingNode)
                    {
                        merge.AddLast(ConstructMapping(value_v));
                    }
                    else if ((sequence = value_v as SequenceNode) != null)
                    {
                        foreach (Node subNode in sequence.Nodes)
                        {
                            if (!(subNode is MappingNode))
                            {
                                throw new ConstructorException("while constructing a mapping: expected a mapping for merging, but found: " + subNode);
                            }
                            merge.AddFirst(ConstructMapping(subNode));
                        }
                    }
                    else
                    {
                        throw new ConstructorException("while constructing a mapping: expected a mapping or list of mappings for merging, but found: " + value_v);
                    }
                }
                else if (key_v.Tag == "tag:yaml.org,2002:value")
                {
                    if (mapping.ContainsKey("="))
                    {
                        throw new ConstructorException("while construction a mapping: found duplicate value key");
                    }
                    mapping.Add("=", ConstructObject(value_v));
                }
                else
                {
                    object   kk       = ConstructObject(key_v);
                    object   vv       = ConstructObject(value_v);
                    LinkNode linkNode = vv as LinkNode;
                    if (linkNode != null)
                    {
                        AddFixer(linkNode.Linked, delegate(Node node, object real) {
                            IDictionaryOps.SetElement(_globalScope.Context, mapping, kk, real);
                        });
                    }
                    IDictionaryOps.SetElement(_globalScope.Context, mapping, kk, vv);
                }
            }
            if (null != merge)
            {
                merge.AddLast(mapping);
                mapping = new Hash(_globalScope.Context);
                foreach (Hash m in merge)
                {
                    foreach (KeyValuePair <object, object> e in m)
                    {
                        IDictionaryOps.SetElement(_globalScope.Context, mapping, e.Key, e.Value);
                    }
                }
            }
            return(mapping);
        }
Exemplo n.º 13
0
 public static object GetValue(MappingNode/*!*/ self) {
     return self.Nodes;
 }
Exemplo n.º 14
0
 public static void Add(YamlCallSiteStorage/*!*/ siteStorage, MappingNode/*!*/ self, object key, object value) {
     RubyRepresenter rep = new RubyRepresenter(siteStorage);
     self.Nodes.Add(rep.RepresentItem(key), rep.RepresentItem(value));
 }
Exemplo n.º 15
0
 public static object SetStyle(MappingNode/*!*/ self, object value) {
     self.FlowStyle = RubyYaml.ToYamlFlowStyle(value);
     return value;
 }
Exemplo n.º 16
0
 public static object GetValue(MappingNode /*!*/ self)
 {
     return(self.Nodes);
 }
Exemplo n.º 17
0
                public static void Add(YamlCallSiteStorage /*!*/ siteStorage, MappingNode /*!*/ self, object key, object value)
                {
                    RubyRepresenter rep = new RubyRepresenter(siteStorage);

                    self.Nodes.Add(rep.RepresentItem(key), rep.RepresentItem(value));
                }
Exemplo n.º 18
0
 public static object SetStyle(MappingNode /*!*/ self, object value)
 {
     self.FlowStyle = RubyYaml.ToYamlFlowStyle(value);
     return(value);
 }
Exemplo n.º 19
0
        private Node ComposeNode(Node parent, object index) {
            Node result;
            YamlEvent @event = _parser.PeekEvent();
            NodeEvent nodeEvent = @event as NodeEvent;

            string anchor = (nodeEvent != null) ? nodeEvent.Anchor : null;

            if (nodeEvent is AliasEvent) {
                _parser.GetEvent();
                if (!_anchors.TryGetValue(anchor, out result)) {
                    throw new ComposerException("found undefined alias: " + anchor);
                }
                return result;
            }

            result = null;
            //_resolver.descendResolver(parent, index);
            if (@event is ScalarEvent) {
                ScalarEvent ev = (ScalarEvent)_parser.GetEvent();

                string tag = ev.Tag;
                if (ev.Type == ScalarValueType.Unknown) {
                    Debug.Assert(tag == null || tag == "!");
                    tag = ResolverScanner.Recognize(ev.Value) ?? Tags.Str;
                }

                result = new ScalarNode(tag, ev.Value, ev.Style);
                if (anchor != null) {
                    AddAnchor(anchor, result);
                }
            } else if (@event is SequenceStartEvent) {
                SequenceStartEvent start = (SequenceStartEvent)_parser.GetEvent();
                SequenceNode seqResult = new SequenceNode(start.Tag != "!" ? start.Tag : null, new List<Node>(), start.FlowStyle);
                result = seqResult;
                if (anchor != null) {
                    AddAnchor(anchor, seqResult);
                }
                int ix = 0;
                while (!(_parser.PeekEvent() is SequenceEndEvent)) {
                    seqResult.Nodes.Add(ComposeNode(seqResult, ix++));
                }
                _parser.GetEvent();
            } else if (@event is MappingStartEvent) {
                MappingStartEvent start = (MappingStartEvent)_parser.GetEvent();
                MappingNode mapResult = new MappingNode(start.Tag != "!" ? start.Tag : null, new Dictionary<Node, Node>(), start.FlowStyle);
                result = mapResult;
                if (anchor != null) {
                    AddAnchor(anchor, result);
                }
                while (!(_parser.PeekEvent() is MappingEndEvent)) {
                    YamlEvent key = _parser.PeekEvent();
                    Node itemKey = ComposeNode(mapResult, key);
                    Node composed = ComposeNode(mapResult, itemKey);
                    if (!mapResult.Nodes.ContainsKey(itemKey)) {
                        mapResult.Nodes.Add(itemKey, composed);
                    }
                }
                _parser.GetEvent();
            }
            //_resolver.ascendResolver();
            return result;
        }