示例#1
0
文件: Emitter.cs 项目: TerabyteX/main
 private bool needMoreEvents()
 {
     if (_events.Count == 0) {
         return true;
     }
     _event = _events.Peek();
     if (_event is DocumentStartEvent) {
         return needEvents(1);
     } else if (_event is SequenceStartEvent) {
         return needEvents(2);
     } else if (_event is MappingStartEvent) {
         return needEvents(3);
     } else {
         return false;
     }
 }
示例#2
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);
        }
示例#3
0
文件: Emitter.cs 项目: TerabyteX/main
        public void Emit(YamlEvent/*!*/ @event)
        {
            _events.Enqueue(@event);
            while (!needMoreEvents()) {
                _event = _events.Dequeue();
                switch (_state) {
                    case EmitterState.STREAM_START:
                        EmitStreamStart();
                        break;
                    case EmitterState.FIRST_DOCUMENT_START:
                        EmitDocumentStart(true);
                        break;
                    case EmitterState.DOCUMENT_ROOT:
                        EmitDocumentRoot();
                        break;
                    case EmitterState.NOTHING:
                        EmitNothing();
                        break;
                    case EmitterState.DOCUMENT_START:
                        EmitDocumentStart(false);
                        break;
                    case EmitterState.DOCUMENT_END:
                        EmitDocumentEnd();
                        break;

                    case EmitterState.FIRST_FLOW_SEQUENCE_ITEM:
                        EmitFlowSequenceItem(true);
                        break;

                    case EmitterState.FLOW_SEQUENCE_ITEM:
                        EmitFlowSequenceItem(false);
                        break;

                    case EmitterState.FIRST_FLOW_MAPPING_KEY:
                        EmitFlowMappingKey(true);
                        break;

                    case EmitterState.FLOW_MAPPING_SIMPLE_VALUE:
                        EmitFlowMappingSimpleValue();
                        break;

                    case EmitterState.FLOW_MAPPING_VALUE:
                        EmitFlowMappingValue();
                        break;

                    case EmitterState.FLOW_MAPPING_KEY:
                        EmitFlowMappingKey(false);
                        break;

                    case EmitterState.BLOCK_SEQUENCE_ITEM:
                        if (_event is SequenceEndEvent) {
                            _indent = _indents.Pop();
                            _state = _states.Pop();
                        } else {
                            EmitBlockSequenceItem();
                        }
                        break;

                    case EmitterState.FIRST_BLOCK_SEQUENCE_ITEM:
                        EmitBlockSequenceItem();
                        break;

                    case EmitterState.BLOCK_MAPPING_KEY:
                        if (_event is MappingEndEvent) {
                            _indent = _indents.Pop();
                            _state = _states.Pop();
                        } else {
                            EmitBlockMappingKey();
                        }
                        break;

                    case EmitterState.FIRST_BLOCK_MAPPING_KEY:
                        EmitBlockMappingKey();
                        break;

                    case EmitterState.BLOCK_MAPPING_SIMPLE_VALUE:
                        EmitBlockMappingSimpleValue();
                        break;

                    case EmitterState.BLOCK_MAPPING_VALUE:
                        EmitBlockMappingValue();
                        break;

                    default:
                        Debug.Assert(false, "unreachable");
                        throw new InvalidOperationException("unreachable");
                }
                _event = null;
            }
        }