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); } parser.Expect <MappingEnd>(); }
/// <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"); }
private void Load(IParser parser, DocumentLoadingState state) { var scalar = parser.Expect <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); } } }
/// <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; }
/// <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); } } }
/// <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>(); }
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); } parser.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) { throw new NotSupportedException("Resolving an alias on an alias node does not make sense"); }
/// <summary> /// Initializes a new instance of the <see cref="YamlMappingNode"/> class. /// </summary> internal YamlMappingNode(IParser parser, DocumentLoadingState state) { Load(parser, state); }
/// <summary> /// Initializes a new instance of the <see cref="YamlScalarNode"/> class. /// </summary> internal YamlScalarNode(IParser parser, DocumentLoadingState state) { Load(parser, state); }
/// <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="YamlSequenceNode"/> class. /// </summary> internal YamlSequenceNode(IParser parser, DocumentLoadingState state) { Load(parser, state); }