public static TokenType[] GetExpectedTokens(int state) { var parser = new GherkinEditorParser(new GherkinTokenTagBuilder(null)) { StopAtFirstError = true }; try { parser.MatchToken(state, new Token(null, new Location()), new ParserContext() { //Builder = new NullAstBuilder(), Errors = new List <ParserException>(), TokenMatcher = new AllFalseTokenMatcher(), TokenQueue = new Queue <Token>(), TokenScanner = new NullTokenScanner() }); } catch (UnexpectedEOFException ex) { return(ex.ExpectedTokenTypes.Select(type => (TokenType)Enum.Parse(typeof(TokenType), type.TrimStart('#'))).ToArray()); } return(new TokenType[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); }