private static void AdjustNode(IList <ContainerOrTerminalNode> parentChildren, int indexInParentChildren, CharacterPositionFinder finder) { var child = parentChildren[indexInParentChildren]; if (indexInParentChildren < parentChildren.Count - 1) { var nextSibling = parentChildren[indexInParentChildren + 1]; var indexBefore = finder.GetCharacterPosition(nextSibling.LocationSpan.Start) - 1; var newEndPos = finder.GetLineInfo(indexBefore); child.LocationSpan = new LocationSpan(child.LocationSpan.Start, newEndPos); } if (child is Container c) { var start = c.HeaderSpan.End + 1; AdjustChildren(c, finder); var end = finder.GetCharacterPosition(c.LocationSpan.End); c.FooterSpan = new CharacterSpan(start, end); } else if (child is TerminalNode t) { var start = finder.GetCharacterPosition(t.LocationSpan.Start); var end = finder.GetCharacterPosition(t.LocationSpan.End); t.Span = new CharacterSpan(start, end); } }
public static File ParseCore(string filePath, CharacterPositionFinder finder, Encoding encoding) { var text = SystemFile.ReadAllText(filePath, encoding); var lastCharacter = text.Length - 1; var file = new File { Name = filePath, FooterSpan = CharacterSpan.None, // there is no footer LocationSpan = lastCharacter >= 0 ? new LocationSpan(finder.GetLineInfo(0), finder.GetLineInfo(lastCharacter)) : new LocationSpan(LineInfo.None, LineInfo.None), }; try { var parser = new GherkinParser(); var document = parser.Parse(filePath); var root = new Container { Type = nameof(GherkinDocument), Name = string.Empty, LocationSpan = file.LocationSpan, HeaderSpan = new CharacterSpan(0, 0), // there is no header FooterSpan = new CharacterSpan(Math.Max(0, lastCharacter), lastCharacter), // there is no footer }; file.Children.Add(root); var feature = document.Feature; if (feature != null) { // get locations ordered so that we know the position of the feature var locations = document.Comments.Select(_ => _.Location).ToList(); locations.Add(feature.Location); var sortedLocations = locations.OrderBy(_ => _.Line).ThenBy(_ => _.Column).ToList(); var positionAfterFeature = sortedLocations.IndexOf(feature.Location) + 1; var location = positionAfterFeature < sortedLocations.Count - 1 ? GetLineInfo(locations[positionAfterFeature + 1]) : file.LocationSpan.End; var parsedChild = ParseFeature(feature, finder, location); root.Children.Add(parsedChild); } } catch (Exception ex) { // try to adjust location span to include full file content // but ignore empty files as parsing errors var lines = SystemFile.ReadLines(filePath).Count(); if (lines == 0) { file.LocationSpan = new LocationSpan(LineInfo.None, LineInfo.None); } else { file.ParsingErrors.Add(new ParsingError { ErrorMessage = ex.Message, Location = LineInfo.None, }); file.LocationSpan = new LocationSpan(new LineInfo(1, 0), new LineInfo(lines + 1, 0)); } } return(file); }