-
-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using YamlDotNet.Core.Events; | ||
|
||
namespace YamlDotNet.Core | ||
{ | ||
/// <summary> | ||
/// Simple implementation of <see cref="IParser"/> that implements merging: http://yaml.org/type/merge.html | ||
/// </summary> | ||
public sealed class MergingParser : IParser | ||
{ | ||
private readonly List<ParsingEvent> _allEvents = new List<ParsingEvent>(); | ||
private readonly IParser _innerParser; | ||
private int _currentIndex = -1; | ||
|
||
public MergingParser(IParser innerParser) | ||
{ | ||
_innerParser = innerParser; | ||
} | ||
|
||
public ParsingEvent Current { get; private set; } | ||
|
||
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; | ||
} | ||
|
||
private IEnumerable<ParsingEvent> GetMappingEvents(string mappingAlias) | ||
{ | ||
var nesting = 0; | ||
return _allEvents | ||
.SkipWhile(e => | ||
{ | ||
var mappingStart = e as MappingStart; | ||
return mappingStart == null || mappingStart.Anchor != mappingAlias; | ||
}) | ||
.Skip(1) | ||
.TakeWhile(e => (nesting += e.NestingIncrease) >= 0) | ||
.ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters