/// <summary> /// Initializes a new instance of the <see cref="YamlDocument"/> class. /// </summary> /// <param name="events">The events.</param> internal YamlDocument(EventReader events) { DocumentLoadingState state = new DocumentLoadingState(); events.Expect<DocumentStart>(); while (!events.Accept<DocumentEnd>()) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(events, state); if (RootNode is YamlAliasNode) { throw new YamlException(); } } state.ResolveAliases(); #if DEBUG foreach (var node in AllNodes) { if (node is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } #endif events.Expect<DocumentEnd>(); }
/// <summary> /// Initializes a new instance of the <see cref="YamlSequenceNode"/> class. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> internal YamlSequenceNode(EventReader events, DocumentLoadingState state) { SequenceStart sequence = events.Expect <SequenceStart>(); Load(sequence, state); bool hasUnresolvedAliases = false; while (!events.Accept <SequenceEnd>()) { YamlNode child = ParseNode(events, state); children.Add(child); hasUnresolvedAliases |= child is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } #if DEBUG else { foreach (var child in children) { if (child is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } } #endif events.Expect <SequenceEnd>(); }
private void Load(IParser parser, DocumentLoadingState state) { var sequence = parser.Expect <SequenceStart>(); Load(sequence, state); Style = sequence.Style; bool hasUnresolvedAliases = false; while (!parser.Accept <SequenceEnd>()) { var child = ParseNode(parser, state); children.Add(child); hasUnresolvedAliases |= child is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } #if DEBUG else { foreach (var child in children) { if (child is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } } #endif parser.Expect <SequenceEnd>(); }
/// <summary> /// Parses the node represented by the next event in <paramref name="events" />. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> /// <returns>Returns the node that has been parsed.</returns> static internal YamlNode ParseNode(EventReader events, DocumentLoadingState state, bool allowOverrideKeys) { if (events.Accept <Scalar>()) { return(new YamlScalarNode(events, state)); } if (events.Accept <SequenceStart>()) { return(new YamlSequenceNode(events, state, allowOverrideKeys)); } if (events.Accept <MappingStart>()) { return(new YamlMappingNode(events, state, allowOverrideKeys)); } if (events.Accept <AnchorAlias>()) { AnchorAlias alias = events.Expect <AnchorAlias>(); return(state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value)); } throw new ArgumentException("The current event is of an unsupported type.", "events"); }
/// <summary> /// Parses the node represented by the next event in <paramref name="parser" />. /// </summary> /// <returns>Returns the node that has been parsed.</returns> static internal YamlNode ParseNode(IParser parser, DocumentLoadingState state) { if (parser.Accept <Scalar>()) { return(new YamlScalarNode(parser, state)); } if (parser.Accept <SequenceStart>()) { return(new YamlSequenceNode(parser, state)); } if (parser.Accept <MappingStart>()) { return(new YamlMappingNode(parser, state)); } if (parser.Accept <AnchorAlias>()) { var alias = parser.Expect <AnchorAlias>(); return(state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value)); } throw new ArgumentException("The current event is of an unsupported type.", "events"); }
/// <summary> /// Initializes a new instance of the <see cref="YamlScalarNode"/> class. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> internal YamlScalarNode(EventReader events, DocumentLoadingState state) { Scalar scalar = events.Expect<Scalar>(); Load(scalar, state); Value = scalar.Value; Style = scalar.Style; }
private void Load(IParser parser, DocumentLoadingState state) { var mapping = parser.Consume <MappingStart>(); Load(mapping, state); Style = mapping.Style; bool hasUnresolvedAliases = false; while (!parser.TryConsume <MappingEnd>(out var _)) { var key = ParseNode(parser, state); var value = ParseNode(parser, state); try { children.Add(key, value); } catch (ArgumentException err) { throw new YamlException(key.Start, key.End, "Duplicate key", err); } hasUnresolvedAliases |= key is YamlAliasNode || value is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } }
/// <summary> /// Parses the node represented by the next event in <paramref name="events" />. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> /// <returns>Returns the node that has been parsed.</returns> static internal YamlNode ParseNode(EventReader events, DocumentLoadingState state) { if (events.Accept<Scalar>()) { return new YamlScalarNode(events, state); } if (events.Accept<SequenceStart>()) { return new YamlSequenceNode(events, state); } if (events.Accept<MappingStart>()) { return new YamlMappingNode(events, state); } if (events.Accept<AnchorAlias>()) { AnchorAlias alias = events.Expect<AnchorAlias>(); return state.GetNode(alias.Value, false, alias.Start, alias.End) ?? new YamlAliasNode(alias.Value); } throw new ArgumentException("The current event is of an unsupported type.", "events"); }
private void Load(IParser parser, DocumentLoadingState state) { MappingStart mappingStart = parser.Expect <MappingStart>(); Load(mappingStart, state); Style = mappingStart.Style; bool flag = false; while (!parser.Accept <MappingEnd>()) { YamlNode yamlNode = YamlNode.ParseNode(parser, state); YamlNode yamlNode2 = YamlNode.ParseNode(parser, state); try { children.Add(yamlNode, yamlNode2); } catch (ArgumentException innerException) { throw new YamlException(yamlNode.Start, yamlNode.End, "Duplicate key", innerException); } flag |= (yamlNode is YamlAliasNode || yamlNode2 is YamlAliasNode); } if (flag) { state.AddNodeWithUnresolvedAliases(this); } parser.Expect <MappingEnd>(); }
internal YamlMappingNode(EventReader events, DocumentLoadingState state) { this.children = new Dictionary <YamlNode, YamlNode>(); MappingStart yamlEvent = events.Expect <MappingStart>(); base.Load(yamlEvent, state); bool flag = false; while (!events.Accept <MappingEnd>()) { YamlNode key = ParseNode(events, state); YamlNode node2 = ParseNode(events, state); try { this.children.Add(key, node2); } catch (ArgumentException exception) { throw new YamlException(key.Start, key.End, "Duplicate key", exception); } flag |= (key is YamlAliasNode) || (node2 is YamlAliasNode); } if (flag) { state.AddNodeWithUnresolvedAliases(this); } events.Expect <MappingEnd>(); }
/// <summary> /// Initializes a new instance of the <see cref="YamlDocument"/> class. /// </summary> #pragma warning disable CS8618 // Non-nullable field is uninitialized. There is a guard that throws an exception if this happens (upstream bug: https://github.com/dotnet/roslyn/issues/44046). internal YamlDocument(IParser parser) #pragma warning restore CS8618 // Non-nullable field is uninitialized. { var state = new DocumentLoadingState(); parser.Consume <DocumentStart>(); while (!parser.TryConsume <DocumentEnd>(out var _)) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(parser, state); if (RootNode is YamlAliasNode) { throw new YamlException("A document cannot contain only an alias"); } } state.ResolveAliases(); if (RootNode == null) { // This should not happen unless the parser has a bug throw new ArgumentException("Atempted to parse an empty document"); } }
/// <summary> /// Initializes a new instance of the <see cref="YamlDocument"/> class. /// </summary> /// <param name="events">The events.</param> internal YamlDocument(EventReader events) { DocumentLoadingState state = new DocumentLoadingState(); events.Expect <DocumentStart>(); while (!events.Accept <DocumentEnd>()) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(events, state); if (RootNode is YamlAliasNode) { throw new YamlException(); } } state.ResolveAliases(); #if DEBUG foreach (var node in AllNodes) { if (node is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } #endif events.Expect <DocumentEnd>(); }
private void Load(EventReader events, DocumentLoadingState state) { Scalar scalar = events.Expect <Scalar>(); base.Load(scalar, state); Value = scalar.Value; Style = scalar.Style; }
/// <summary> /// Initializes a new instance of the <see cref="YamlScalarNode"/> class. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> internal YamlScalarNode(EventReader events, DocumentLoadingState state) { Scalar scalar = events.Expect <Scalar>(); Load(scalar, state); Value = scalar.Value; Style = scalar.Style; }
internal YamlScalarNode(EventReader events, DocumentLoadingState state) { Scalar yamlEvent = events.Expect <Scalar>(); base.Load(yamlEvent, state); this.Value = yamlEvent.Value; this.Style = yamlEvent.Style; }
private void Load(IParser parser, DocumentLoadingState state) { var scalar = parser.Consume <Scalar>(); Load(scalar, state); Value = scalar.Value; Style = scalar.Style; }
/// <summary> /// Resolves the aliases that could not be resolved when the node was created. /// </summary> /// <param name="state">The state of the document.</param> internal override void ResolveAliases(DocumentLoadingState state) { for (int i = 0; i < children.Count; ++i) { if (children[i] is YamlAliasNode) { children[i] = state.GetNode(children[i].Anchor, true, children[i].Start, children[i].End); } } }
internal override void ResolveAliases(DocumentLoadingState state) { for (int i = 0; i < this.children.Count; i++) { if (this.children[i] is YamlAliasNode) { this.children[i] = state.GetNode(this.children[i].Anchor, true, this.children[i].Start, this.children[i].End); } } }
/// <summary> /// Initializes a new instance of the <see cref="YamlMappingNode"/> class. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> internal YamlMappingNode(EventReader events, DocumentLoadingState state, bool overrideKeys) { MappingStart mapping = events.Expect <MappingStart>(); Load(mapping, state); bool hasUnresolvedAliases = false; while (!events.Accept <MappingEnd>()) { YamlNode key = ParseNode(events, state, overrideKeys); YamlNode value = ParseNode(events, state, overrideKeys); try { if (children.ContainsKey(key) && overrideKeys) { children[key] = value; } else { children.Add(key, value); } } catch (ArgumentException err) { throw new YamlException(key.Start, key.End, "Duplicate key", err); } hasUnresolvedAliases |= key is YamlAliasNode || value is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } #if DEBUG else { foreach (var child in children) { if (child.Key is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } if (child.Value is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } } #endif events.Expect <MappingEnd>(); }
/// <summary> /// Loads the specified event. /// </summary> /// <param name="yamlEvent">The event.</param> /// <param name="state">The state of the document.</param> internal void Load(NodeEvent yamlEvent, DocumentLoadingState state) { Tag = yamlEvent.Tag; if (yamlEvent.Anchor != null) { Anchor = yamlEvent.Anchor; state.AddAnchor(this); } Start = yamlEvent.Start; End = yamlEvent.End; }
private void Load(IParser parser, DocumentLoadingState state) { var mapping = parser.Expect <MappingStart>(); Load(mapping, state); Style = mapping.Style; bool hasUnresolvedAliases = false; while (!parser.Accept <MappingEnd>()) { var key = ParseNode(parser, state); var value = ParseNode(parser, state); try { children.Add(key, value); } catch (ArgumentException err) { throw new YamlException(key.Start, key.End, "Duplicate key", err); } hasUnresolvedAliases |= key is YamlAliasNode || value is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } #if DEBUG else { foreach (var child in children) { if (child.Key is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } if (child.Value is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } } #endif parser.Expect <MappingEnd>(); }
internal YamlDocument(EventReader events) { DocumentLoadingState state = new DocumentLoadingState(); events.Expect <DocumentStart>(); while (!events.Accept <DocumentEnd>()) { this.RootNode = YamlNode.ParseNode(events, state); if (this.RootNode is YamlAliasNode) { throw new YamlException(); } } state.ResolveAliases(); events.Expect <DocumentEnd>(); }
internal YamlDocument(IParser parser) { DocumentLoadingState documentLoadingState = new DocumentLoadingState(); parser.Expect <DocumentStart>(); while (!parser.Accept <DocumentEnd>()) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(parser, documentLoadingState); if (RootNode is YamlAliasNode) { throw new YamlException(); } } documentLoadingState.ResolveAliases(); parser.Expect <DocumentEnd>(); }
/// <summary> /// Resolves the aliases that could not be resolved when the node was created. /// </summary> /// <param name="state">The state of the document.</param> internal override void ResolveAliases(DocumentLoadingState state) { Dictionary <YamlNode, YamlNode>?keysToUpdate = null; Dictionary <YamlNode, YamlNode>?valuesToUpdate = null; foreach (var entry in children) { if (entry.Key is YamlAliasNode) { if (keysToUpdate == null) { keysToUpdate = new Dictionary <YamlNode, YamlNode>(); } // TODO: The representation model should be redesigned, because here the anchor could be null but that would be invalid YAML keysToUpdate.Add(entry.Key, state.GetNode(entry.Key.Anchor !, entry.Key.Start, entry.Key.End)); } if (entry.Value is YamlAliasNode) { if (valuesToUpdate == null) { valuesToUpdate = new Dictionary <YamlNode, YamlNode>(); } // TODO: The representation model should be redesigned, because here the anchor could be null but that would be invalid YAML valuesToUpdate.Add(entry.Key, state.GetNode(entry.Value.Anchor !, entry.Value.Start, entry.Value.End)); } } if (valuesToUpdate != null) { foreach (var entry in valuesToUpdate) { children[entry.Key] = entry.Value; } } if (keysToUpdate != null) { foreach (var entry in keysToUpdate) { YamlNode value = children[entry.Key]; children.Remove(entry.Key); children.Add(entry.Value, value); } } }
internal override void ResolveAliases(DocumentLoadingState state) { Dictionary <YamlNode, YamlNode> dictionary = null; Dictionary <YamlNode, YamlNode> dictionary2 = null; foreach (KeyValuePair <YamlNode, YamlNode> pair in this.children) { if (pair.Key is YamlAliasNode) { dictionary = new Dictionary <YamlNode, YamlNode> { { pair.Key, state.GetNode(pair.Key.Anchor, true, pair.Key.Start, pair.Key.End) } }; } if (pair.Value is YamlAliasNode) { dictionary2 = new Dictionary <YamlNode, YamlNode> { { pair.Key, state.GetNode(pair.Value.Anchor, true, pair.Value.Start, pair.Value.End) } }; } } if (dictionary2 != null) { foreach (KeyValuePair <YamlNode, YamlNode> pair2 in dictionary2) { this.children[pair2.Key] = pair2.Value; } } if (dictionary != null) { foreach (KeyValuePair <YamlNode, YamlNode> pair3 in dictionary) { YamlNode node = this.children[pair3.Key]; this.children.Remove(pair3.Key); this.children.Add(pair3.Value, node); } } }
private void Load(IParser parser, DocumentLoadingState state) { SequenceStart sequenceStart = parser.Expect <SequenceStart>(); Load(sequenceStart, state); Style = sequenceStart.Style; bool flag = false; while (!parser.Accept <SequenceEnd>()) { YamlNode yamlNode = YamlNode.ParseNode(parser, state); children.Add(yamlNode); flag |= (yamlNode is YamlAliasNode); } if (flag) { state.AddNodeWithUnresolvedAliases(this); } parser.Expect <SequenceEnd>(); }
internal YamlSequenceNode(EventReader events, DocumentLoadingState state) { this.children = new List <YamlNode>(); SequenceStart yamlEvent = events.Expect <SequenceStart>(); base.Load(yamlEvent, state); bool flag = false; while (!events.Accept <SequenceEnd>()) { YamlNode item = ParseNode(events, state); this.children.Add(item); flag |= item is YamlAliasNode; } if (flag) { state.AddNodeWithUnresolvedAliases(this); } events.Expect <SequenceEnd>(); }
/// <summary> /// Resolves the aliases that could not be resolved when the node was created. /// </summary> /// <param name="state">The state of the document.</param> internal override void ResolveAliases(DocumentLoadingState state) { Dictionary <YamlNode, YamlNode> keysToUpdate = null; Dictionary <YamlNode, YamlNode> valuesToUpdate = null; foreach (var entry in children) { if (entry.Key is YamlAliasNode) { if (keysToUpdate == null) { keysToUpdate = new Dictionary <YamlNode, YamlNode>(); } keysToUpdate.Add(entry.Key, state.GetNode(entry.Key.Anchor, true, entry.Key.Start, entry.Key.End)); } if (entry.Value is YamlAliasNode) { if (valuesToUpdate == null) { valuesToUpdate = new Dictionary <YamlNode, YamlNode>(); } valuesToUpdate.Add(entry.Key, state.GetNode(entry.Value.Anchor, true, entry.Value.Start, entry.Value.End)); } } if (valuesToUpdate != null) { foreach (var entry in valuesToUpdate) { children[entry.Key] = entry.Value; } } if (keysToUpdate != null) { foreach (var entry in keysToUpdate) { YamlNode value = children[entry.Key]; children.Remove(entry.Key); children.Add(entry.Value, value); } } }
internal override void ResolveAliases(DocumentLoadingState state) { Dictionary <YamlNode, YamlNode> dictionary = null; Dictionary <YamlNode, YamlNode> dictionary2 = null; foreach (KeyValuePair <YamlNode, YamlNode> child in children) { if (child.Key is YamlAliasNode) { if (dictionary == null) { dictionary = new Dictionary <YamlNode, YamlNode>(); } dictionary.Add(child.Key, state.GetNode(child.Key.Anchor, true, child.Key.Start, child.Key.End)); } if (child.Value is YamlAliasNode) { if (dictionary2 == null) { dictionary2 = new Dictionary <YamlNode, YamlNode>(); } dictionary2.Add(child.Key, state.GetNode(child.Value.Anchor, true, child.Value.Start, child.Value.End)); } } if (dictionary2 != null) { foreach (KeyValuePair <YamlNode, YamlNode> item in dictionary2) { children[item.Key] = item.Value; } } if (dictionary != null) { foreach (KeyValuePair <YamlNode, YamlNode> item2 in dictionary) { YamlNode value = children[item2.Key]; children.Remove(item2.Key); children.Add(item2.Value, value); } } }
private void Load(IParser parser, DocumentLoadingState state) { var sequence = parser.Consume <SequenceStart>(); Load(sequence, state); Style = sequence.Style; var hasUnresolvedAliases = false; while (!parser.TryConsume <SequenceEnd>(out var _)) { var child = ParseNode(parser, state); children.Add(child); hasUnresolvedAliases |= child is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } }
/// <summary> /// Initializes a new instance of the <see cref="YamlDocument"/> class. /// </summary> internal YamlDocument(IParser parser) { var state = new DocumentLoadingState(); parser.Expect<DocumentStart>(); while (!parser.Accept<DocumentEnd>()) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(parser, state); if (RootNode is YamlAliasNode) { throw new YamlException(); } } state.ResolveAliases(); parser.Expect<DocumentEnd>(); }
/// <summary> /// Initializes a new instance of the <see cref="YamlDocument"/> class. /// </summary> internal YamlDocument(IParser parser) { var state = new DocumentLoadingState(); parser.Consume <DocumentStart>(); while (!parser.TryConsume <DocumentEnd>(out var _)) { Debug.Assert(RootNode == null); RootNode = YamlNode.ParseNode(parser, state); if (RootNode is YamlAliasNode) { throw new YamlException("A document cannot contain only an alias"); } } state.ResolveAliases(); // Throw should not happen unless the parser has a bug RootNode = RootNode ?? throw new ArgumentException("Atempted to parse an empty document"); }
/// <summary> /// Parses the node represented by the next event in <paramref name="parser" />. /// </summary> /// <returns>Returns the node that has been parsed.</returns> static internal YamlNode ParseNode(IParser parser, DocumentLoadingState state) { if (parser.Accept <Scalar>(out var _)) { return(new YamlScalarNode(parser, state)); } if (parser.Accept <SequenceStart>(out var _)) { return(new YamlSequenceNode(parser, state)); } if (parser.Accept <MappingStart>(out var _)) { return(new YamlMappingNode(parser, state)); } if (parser.TryConsume <AnchorAlias>(out var alias)) { return(state.TryGetNode(alias.Value, out var node) ? node : new YamlAliasNode(alias.Value)); } throw new ArgumentException("The current event is of an unsupported type.", nameof(parser)); }
/// <summary> /// Resolves the aliases that could not be resolved when the node was created. /// </summary> /// <param name="state">The state of the document.</param> internal override void ResolveAliases(DocumentLoadingState state) { throw new NotSupportedException("Resolving an alias on a scalar node does not make sense"); }
/// <summary> /// Resolves the aliases that could not be resolved when the node was created. /// </summary> /// <param name="state">The state of the document.</param> internal abstract void ResolveAliases(DocumentLoadingState state);
/// <summary> /// Initializes a new instance of the <see cref="YamlScalarNode"/> class. /// </summary> internal YamlScalarNode(IParser parser, DocumentLoadingState state) { Load(parser, state); }
private void Load(IParser parser, DocumentLoadingState state) { var scalar = parser.Expect<Scalar>(); Load(scalar, state); Value = scalar.Value; Style = scalar.Style; }