public GherkinEditorParser(IAstBuilder <GherkinDocument> astBuilder) : base(astBuilder)
 {
     _astBuilder = astBuilder as GherkinTokenTagBuilder;
 }
Exemple #2
0
        private GherkinTokenTag[] Parse(ITextSnapshot snapshot)
        {
            var currentLastResult = lastResult;

            if (currentLastResult != null && currentLastResult.SnapshotVersion == snapshot.Version.VersionNumber)
            {
                return(currentLastResult.Tags);
            }

            var fileContent = snapshot.GetText();

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var parserErrors = new List <ParserException>();

            var tokenTagBuilder = new GherkinTokenTagBuilder(snapshot);
            var parser          = new GherkinEditorParser(tokenTagBuilder);

            var reader = new StringReader(fileContent);

            try
            {
                parser.Parse(new TokenScanner(reader), new TokenMatcher(VsGherkinDialectProvider.Instance));
            }
            catch (CompositeParserException compositeParserException)
            {
                parserErrors.AddRange(compositeParserException.Errors);
            }
            catch (ParserException parserException)
            {
                parserErrors.Add(parserException);
            }
            catch (Exception ex)
            {
                //nop;
                Debug.WriteLine(ex);
            }


            var tokenTags = new List <GherkinTokenTag>();

            tokenTags.AddRange((GherkinTokenTag[])tokenTagBuilder.GetResult());
            tokenTags.AddRange(parserErrors.Select(e => new GherkinTokenTag(e, snapshot)));
            tokenTags.Sort((t1, t2) => t1.Span.Start.Position.CompareTo(t2.Span.Start.Position));

            stopwatch.Stop();
            Debug.WriteLine("Gherkin3: parsed v{0} on thread {1} in {2} ms", snapshot.Version.VersionNumber, Thread.CurrentThread.ManagedThreadId, stopwatch.ElapsedMilliseconds);

            var result = new ParsingResult(snapshot.Version.VersionNumber, tokenTags.ToArray());

            Thread.MemoryBarrier();
            lastResult = result;

            //TODO: only for the changed scope
            if (TagsChanged != null)
            {
                TagsChanged(this, new SnapshotSpanEventArgs(new SnapshotSpan(snapshot, 0, snapshot.Length)));
            }

            return(result.Tags);
        }