public virtual Focus Emit(ParsingEvent parsingEvent) { if (parsingEvent is SequenceStart) { return(new SequenceFocus(this)); } else if (parsingEvent is MappingStart) { return(new MappingFocus(this)); } else if (parsingEvent is Scalar) { var scalar = (Scalar)parsingEvent; if (scalar.Style == ScalarStyle.DoubleQuoted) { return(Add(new JValue(scalar.Value))); } else { return(Add(JValue.Parse(scalar.Value))); } } else { throw new ApplicationException($"Unexpected {parsingEvent.GetType().Name}"); } }
public bool MoveNext() { if (_currentIndex < 0) { while (_innerParser.MoveNext()) { _allEvents.Add(_innerParser.Current); } for (int i = _allEvents.Count - 2; i >= 0; --i) { var merge = _allEvents[i] as Scalar; if (merge != null && merge.Value == "<<") { var anchorAlias = _allEvents[i + 1] as AnchorAlias; if (anchorAlias != null) { var mergedEvents = GetMappingEvents(anchorAlias.Value); _allEvents.RemoveRange(i, 2); _allEvents.InsertRange(i, mergedEvents); } } } } var nextIndex = _currentIndex + 1; if (nextIndex < _allEvents.Count) { Current = _allEvents[nextIndex]; _currentIndex = nextIndex; return(true); } return(false); }
internal static void DeserializeHelper(Type tItem, IParser parser, Func <IParser, Type, object> nestedObjectDeserializer, IList result, bool canUpdate) { parser.Expect <SequenceStart>(); while (!parser.Accept <SequenceEnd>()) { ParsingEvent current = parser.Current; object obj = nestedObjectDeserializer(parser, tItem); IValuePromise valuePromise = obj as IValuePromise; if (valuePromise == null) { result.Add(TypeConverter.ChangeType(obj, tItem)); } else { if (!canUpdate) { throw new ForwardAnchorNotSupportedException(current.Start, current.End, "Forward alias references are not allowed because this type does not implement IList<>"); } int index = result.Add((!tItem.IsValueType()) ? null : Activator.CreateInstance(tItem)); valuePromise.ValueAvailable += delegate(object v) { result[index] = TypeConverter.ChangeType(v, tItem); }; } } parser.Expect <SequenceEnd>(); }
protected void AssertEvent(ParsingEvent expected, ParsingEvent actual, int eventNumber) { actual.GetType().Should().Be(expected.GetType(), "Parse event {0} is not of the expected type.", eventNumber); foreach (var property in expected.GetType().GetTypeInfo().GetProperties()) { if (property.PropertyType == typeof(Mark) || !property.CanRead) { continue; } var value = property.GetValue(actual, null); var expectedValue = property.GetValue(expected, null); if (expectedValue is IEnumerable enumerable && !(expectedValue is string)) { if (expectedValue is ICollection expectedCollection && value is ICollection valueCollection) { var expectedCount = expectedCollection.Count; var valueCount = valueCollection.Count; valueCount.Should().Be(expectedCount, "Compared size of collections in property {0} in parse event {1}", property.Name, eventNumber); } var values = ((IEnumerable)value).GetEnumerator(); var expectedValues = enumerable.GetEnumerator(); while (expectedValues.MoveNext()) { values.MoveNext().Should().BeTrue("Property {0} in parse event {1} had too few elements", property.Name, eventNumber); values.Current.Should().Be(expectedValues.Current, "Compared element in property {0} in parse event {1}", property.Name, eventNumber); } values.MoveNext().Should().BeFalse("Property {0} in parse event {1} had too many elements", property.Name, eventNumber); }
public void Emit(ParsingEvent @event) { if (@event is MappingStart mapping) { @event = new MappingStart(mapping.Anchor, mapping.Tag, false, mapping.Style, mapping.Start, mapping.End); } _next.Emit(@event); }
private IEnumerable <ParsingEvent> Wrap(IEnumerable <ParsingEvent> events, ParsingEvent start, ParsingEvent end) { yield return(start); foreach (var @event in events) { yield return(@event); } yield return(end); }
public void Emit(ParsingEvent @event) { var mapping = @event as MappingStart; if (mapping != null) { @event = new MappingStart(mapping.Anchor, mapping.Tag, false, mapping.Style, mapping.Start, mapping.End); } next.Emit(@event); }
public bool MoveNext() { if (++position < parsingEvents.Count) { current = parsingEvents[position]; return(true); } return(false); }
public ParsingEvent Clone(ParsingEvent e) { e.Accept(this); if (clonedEvent == null) { throw new InvalidOperationException($"Could not clone event of type '{e.Type}'"); } return(clonedEvent); }
public IYamlNode Process(ParsingEvent ev) { IYamlNode?classifiedNode = null; if (stack.TopIsSequence() && (ev is MappingStart || ev is SequenceStart || ev is Scalar)) { stack.TopSequenceIncrementIndex(); } switch (ev) { case MappingStart ms: classifiedNode = new YamlNode <MappingStart>(ms, stack.GetPath()); stack.Push(YamlStructure.Mapping); break; case MappingEnd me: stack.Pop(); classifiedNode = new YamlNode <MappingEnd>(me, stack.GetPath()); stack.TopMappingKeyEnd(); break; case SequenceStart ss: classifiedNode = new YamlNode <SequenceStart>(ss, stack.GetPath()); stack.Push(YamlStructure.Sequence); break; case SequenceEnd se: stack.Pop(); classifiedNode = new YamlNode <SequenceEnd>(se, stack.GetPath()); stack.TopMappingKeyEnd(); break; case Scalar sc: if (stack.TopIsMappingExpectingKey()) { // This is a map key stack.TopMappingKeyStart(sc.Value); } else { // This is a value in a map or sequence classifiedNode = new YamlNode <Scalar>(sc, stack.GetPath()); stack.TopMappingKeyEnd(); } break; case Comment c: classifiedNode = new YamlNode <Comment>(c, stack.GetPath()); break; } return(classifiedNode ?? new YamlNode <ParsingEvent>(ev, stack.GetPath())); }
public override Focus Emit(ParsingEvent parsingEvent) { if (parsingEvent is SequenceEnd) { return(Parent.Add(_sequence)); } else { return(base.Emit(parsingEvent)); } }
public void Emit(ParsingEvent @event) { if (@event is DocumentEnd documentEnd) { inner.Emit(new DocumentEnd(true)); } else { inner.Emit(@event); } }
private void AssertCurrent(IParser parser, ParsingEvent expected) { Dump.WriteLine(expected.GetType().Name); Assert.True(expected.GetType().IsAssignableFrom(parser.Current.GetType()), string.Format("The event is not of the expected type. Exprected: {0}, Actual: {1}", expected.GetType().Name, parser.Current.GetType().Name)); foreach (var property in expected.GetType().GetProperties()) { if (property.PropertyType != typeof(Mark) && property.CanRead) { var value = property.GetValue(parser.Current, null); var expectedValue = property.GetValue(expected, null); // Todo: what does GetTypeCode do and is it necessary? if (expectedValue != null && Type.GetTypeCode(expectedValue.GetType()) == TypeCode.Object && expectedValue is IEnumerable) { Dump.Write("\t{0} = {{", property.Name); var isFirst = true; foreach (var item in (IEnumerable)value) { if (isFirst) { isFirst = false; } else { Dump.Write(", "); } Dump.Write(item); } Dump.WriteLine("}"); if (expectedValue is ICollection && value is ICollection) { Assert.Equal(((ICollection)expectedValue).Count, ((ICollection)value).Count); } var values = ((IEnumerable)value).GetEnumerator(); var expectedValues = ((IEnumerable)expectedValue).GetEnumerator(); while (expectedValues.MoveNext()) { Assert.True(values.MoveNext()); Assert.Equal(expectedValues.Current, values.Current); } Assert.False(values.MoveNext()); } else { Dump.WriteLine("\t{0} = {1}", property.Name, value); Assert.Equal(expectedValue, value); } } } }
public void Emit(ParsingEvent @event) { // Ignore some events if (@event is StreamStart || @event is StreamEnd || @event is DocumentStart || @event is DocumentEnd) { return; } parsingEvents.Add(@event); }
public void Emit(ParsingEvent @event) { if (@event is DocumentEnd) { // Prevents the "..." document end characters from being added to the end of the file _inner.Emit(new DocumentEnd(isImplicit: true)); } else { _inner.Emit(@event); } }
private void AssertCurrent(Parser parser, ParsingEvent expected) { Console.WriteLine(expected.GetType().Name); Assert.IsTrue(expected.GetType().IsAssignableFrom(parser.Current.GetType()), "The event is not of the expected type."); foreach (var property in expected.GetType().GetProperties()) { if (property.PropertyType != typeof(Mark) && property.CanRead) { object value = property.GetValue(parser.Current, null); object expectedValue = property.GetValue(expected, null); if (expectedValue != null && Type.GetTypeCode(expectedValue.GetType()) == TypeCode.Object && expectedValue is IEnumerable) { Console.Write("\t{0} = {{", property.Name); bool isFirst = true; foreach (var item in (IEnumerable)value) { if (isFirst) { isFirst = false; } else { Console.Write(", "); } Console.Write(item); } Console.WriteLine("}"); if (expectedValue is ICollection && value is ICollection) { Assert.AreEqual(((ICollection)expectedValue).Count, ((ICollection)value).Count, "The collection does not contain the correct number of items."); } IEnumerator values = ((IEnumerable)value).GetEnumerator(); IEnumerator expectedValues = ((IEnumerable)expectedValue).GetEnumerator(); while (expectedValues.MoveNext()) { Assert.IsTrue(values.MoveNext(), "The property does not contain enough items."); Assert.AreEqual(expectedValues.Current, values.Current, string.Format("The property '{0}' is incorrect.", property.Name)); } Assert.IsFalse(values.MoveNext(), "The property contains too many items."); } else { Console.WriteLine("\t{0} = {1}", property.Name, value); Assert.AreEqual(expectedValue, value, string.Format("The property '{0}' is incorrect.", property.Name)); } } } }
public T Expect <T>() where T : ParsingEvent { T local = this.Allow <T>(); if (local != null) { return(local); } ParsingEvent current = this.parser.Current; object[] args = new object[] { typeof(T).Name, current.GetType().Name, current.Start }; throw new YamlException(current.Start, current.End, string.Format(CultureInfo.InvariantCulture, "Expected '{0}', got '{1}' (at {2}).", args)); }
public void Enqueue(ParsingEvent @event) { EventType type = @event.Type; if (type == EventType.StreamStart || type == EventType.DocumentStart) { highPriorityEvents.Enqueue(@event); } else { normalPriorityEvents.Enqueue(@event); } }
public override Focus Emit(ParsingEvent parsingEvent) { if (parsingEvent is Scalar) { return(new PropertyFocus(this, ((Scalar)parsingEvent).Value)); } else if (parsingEvent is MappingEnd) { return(Parent.Add(_mapping)); } throw new ApplicationException($"Unexpected {parsingEvent.GetType().Name}"); }
public bool MoveNext() { if (this.state == ParserState.StreamEnd) { this.current = null; return(false); } if (this.pendingEvents.Count == 0) { this.pendingEvents.Enqueue(this.StateMachine()); } this.current = this.pendingEvents.Dequeue(); return(true); }
public bool MoveNext() { if (state == ParserState.StreamEnd) { currentEvent = null; return(false); } if (pendingEvents.Count == 0) { pendingEvents.Enqueue(StateMachine()); } currentEvent = pendingEvents.Dequeue(); return(true); }
public void ACommentAsTheFirstEventAddsANewLine() { var events = new ParsingEvent[] { StandaloneComment("Top comment"), Scalar("first").ImplicitPlain, }; var yaml = EmittedTextFrom(StreamedDocumentWith(events)); yaml.Should() .Contain("# Top comment") .And.Contain("first") .And.NotContain("# Top commentfirst"); }
private void AssertEvent(ParsingEvent expected, ParsingEvent actual, int eventNumber) { actual.GetType().Should().Be(expected.GetType(), "Parse event {0} is not of the expected type.", eventNumber); foreach (var property in expected.GetType().GetProperties()) { if (property.PropertyType == typeof(Mark) || !property.CanRead) { continue; } var value = property.GetValue(actual, null); var expectedValue = property.GetValue(expected, null); if (expectedValue is IEnumerable && !(expectedValue is string)) { Dump.Write("\t{0} = {{", property.Name); Dump.Write(string.Join(", ", (IEnumerable)value)); Dump.WriteLine("}"); if (expectedValue is ICollection && value is ICollection) { var expectedCount = ((ICollection)expectedValue).Count; var valueCount = ((ICollection)value).Count; valueCount.Should().Be(expectedCount, "Compared size of collections in property {0} in parse event {1}", property.Name, eventNumber); } var values = ((IEnumerable)value).GetEnumerator(); var expectedValues = ((IEnumerable)expectedValue).GetEnumerator(); while (expectedValues.MoveNext()) { values.MoveNext().Should().BeTrue("Property {0} in parse event {1} had too few elements", property.Name, eventNumber); values.Current.Should().Be(expectedValues.Current, "Compared element in property {0} in parse event {1}", property.Name, eventNumber); } values.MoveNext().Should().BeFalse("Property {0} in parse event {1} had too many elements", property.Name, eventNumber); } else { Dump.WriteLine("\t{0} = {1}", property.Name, value); value.Should().Be(expectedValue, "Compared property {0} in parse event {1}", property.Name, eventNumber); } } }
public void Process(ParsingEvent ev) { if (ev.NestingIncrease > 0 && (ev is MappingStart ms && ms.Style != MappingStyle.Flow || ev is SequenceStart ss && ss.Style != SequenceStyle.Flow)) { var startColumnChange = ev.Start.Column - lastNestingChangeColumn; if (IndentDoesNotCrashYamlDotNetEmitter(startColumnChange)) { indents.Add(startColumnChange); } lastNestingChangeColumn = ev.Start.Column; } if (ev.NestingIncrease < 1 && ev.Start.Column < lastNestingChangeColumn) { lastNestingChangeColumn = ev.Start.Column; } }
public override void Emit(ParsingEvent parsingEvent) { events.Add(parsingEvent); }
private void AssertNext(Parser parser, ParsingEvent expected) { AssertHasNext(parser); AssertCurrent(parser, expected); }
public void Emit(ParsingEvent evnt) { Events.Add(evnt); }
public bool MoveNext() { if (_currentIndex < 0) { while (_innerParser.MoveNext()) { _allEvents.Add(_innerParser.Current); } for (int i = _allEvents.Count - 2; i >= 0; --i) { var merge = _allEvents[i] as Scalar; if (merge != null && merge.Value == "<<") { var anchorAlias = _allEvents[i + 1] as AnchorAlias; if (anchorAlias != null) { var mergedEvents = GetMappingEvents(anchorAlias.Value); _allEvents.RemoveRange(i, 2); _allEvents.InsertRange(i, mergedEvents); continue; } var sequence = _allEvents[i + 1] as SequenceStart; if (sequence != null) { var mergedEvents = new List <IEnumerable <ParsingEvent> >(); var sequenceEndFound = false; for (var itemIndex = i + 2; itemIndex < _allEvents.Count; ++itemIndex) { anchorAlias = _allEvents[itemIndex] as AnchorAlias; if (anchorAlias != null) { mergedEvents.Add(GetMappingEvents(anchorAlias.Value)); continue; } if (_allEvents[itemIndex] is SequenceEnd) { _allEvents.RemoveRange(i, itemIndex - i + 1); _allEvents.InsertRange(i, mergedEvents.SelectMany(e => e)); sequenceEndFound = true; break; } } if (sequenceEndFound) { continue; } } throw new SemanticErrorException(merge.Start, merge.End, "Unrecognized merge key pattern"); } } } var nextIndex = _currentIndex + 1; if (nextIndex < _allEvents.Count) { Current = _allEvents[nextIndex]; _currentIndex = nextIndex; return(true); } return(false); }
void IParsingEventVisitor.Visit(MappingEnd e) { clonedEvent = new MappingEnd(e.Start, e.End); }
void IParsingEventVisitor.Visit(MappingStart e) { clonedEvent = new MappingStart(null, e.Tag, e.IsImplicit, e.Style, e.Start, e.End); }