示例#1
0
        private static ContainerOrTerminalNode ParseScenarioDefinition(ScenarioDefinition definition, CharacterPositionFinder finder, LineInfo locationAfterDefinition)
        {
            var start = GetLineInfo(definition.Location);
            var end   = locationAfterDefinition;

            var spanStart = finder.GetCharacterPosition(start);
            var spanEnd   = spanStart + finder.GetLineLength(start);

            var locationInside = definition.Steps.Select(_ => _.Location).OrderBy(_ => _.Line).ThenBy(_ => _.Column).FirstOrDefault();

            if (locationInside != null)
            {
                var lineInfo = GetLineInfo(locationInside);
                var position = finder.GetCharacterPosition(lineInfo) - 1;
                spanEnd = position;
            }

            var container = new Container
            {
                Type         = nameof(ScenarioDefinition),
                Name         = definition.Name,
                LocationSpan = new LocationSpan(start, end),
                HeaderSpan   = new CharacterSpan(spanStart, spanEnd),
                FooterSpan   = CharacterSpan.None,                   // TODO: FIX
            };

            container.Children.AddRange(ParseSteps(definition, finder, locationAfterDefinition));

            return(container);
        }
示例#2
0
        private static ContainerOrTerminalNode ParseTag(Tag tag, CharacterPositionFinder finder, LineInfo locationAfterDefinition)
        {
            var start = GetLineInfo(tag.Location);
            var end   = locationAfterDefinition;

            var spanStart = finder.GetCharacterPosition(start);
            var spanEnd   = spanStart + finder.GetLineLength(start);

            var container = new Container
            {
                Type         = nameof(Tag),
                Name         = tag.Name,
                LocationSpan = new LocationSpan(start, end),
                HeaderSpan   = new CharacterSpan(spanStart, spanEnd),
                FooterSpan   = CharacterSpan.None,                   // TODO: FIX
            };

            return(container);
        }
示例#3
0
        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);
        }