public void CommentsAreCorrectlyMarked() { var sut = new Scanner(Yaml.ReaderForText(@" - first # Comment on first item "), skipComments: false); while(sut.MoveNext()) { var comment = sut.Current as Comment; if(comment != null) { Assert.Equal(8, comment.Start.Index); Assert.Equal(31, comment.End.Index); return; } } Assert.True(false, "Did not find a comment"); }
public void SetSource(string source, int offset) { if(offset > 0) { source = source.Substring(offset); } Scanner scanner = new Scanner(new StringReader(source)); try { int currentOffset = 0; Token previous = null; while(scanner.MoveNext()) { Token token = scanner.Current; Debug.WriteLine(token.GetType().Name); if(token.Start.Index != token.End.Index) { TokenInfoParsed(token, TokenType.String, ColorIndex.Text); currentOffset = token.End.Index; previous = token; } } } catch(YamlException) { } }
/// <summary> /// Initializes a new instance of the <see cref="Parser"/> class. /// </summary> public Parser(Scanner scanner) { this.scanner = scanner; }
private void AssertSequenceOfTokensFrom(Scanner scanner, params Token[] tokens) { var tokenNumber = 1; foreach (var expected in tokens) { scanner.MoveNext().Should().BeTrue("Missing token number {0}", tokenNumber); AssertToken(expected, scanner.Current, tokenNumber); tokenNumber++; } scanner.MoveNext().Should().BeFalse("Found extra tokens"); }
/// <summary> /// Initializes a new instance of the <see cref="Parser"/> class. /// </summary> /// <param name="input">The input where the YAML stream is to be read.</param> public Parser(TextReader input) { scanner = new Scanner(input); }
private void AssertSequenceOfTokensFrom(Scanner scanner, params Token[] tokens) { AssertPartialSequenceOfTokensFrom(scanner, tokens); scanner.MoveNext().Should().BeFalse("Found extra tokens"); }
private void AssertHasNext(Scanner scanner) { Assert.IsTrue(scanner.MoveNext(), "The scanner does not contain more tokens."); }
private void AssertNext(Scanner scanner, Token expected) { AssertHasNext(scanner); AssertCurrent(scanner, expected); }
private void AssertCurrent(Scanner scanner, Token expected) { Console.WriteLine(expected.GetType().Name); Assert.IsNotNull(scanner.Current, "The current token is null."); Assert.IsTrue(expected.GetType().IsAssignableFrom(scanner.Current.GetType()), "The token is not of the expected type."); foreach (var property in expected.GetType().GetProperties()) { if(property.PropertyType != typeof(Mark) && property.CanRead) { object value = property.GetValue(scanner.Current, null); Console.WriteLine("\t{0} = {1}", property.Name, value); Assert.AreEqual(property.GetValue(expected, null), value, string.Format("The property '{0}' is incorrect.", property.Name)); } } }
private void AssertDoesNotHaveNext(Scanner scanner) { Assert.IsFalse(scanner.MoveNext(), "The scanner should not contain more tokens."); }
/// <summary> /// This method scans the given SnapshotSpan for potential matches for this classification. /// In this instance, it classifies everything and returns each span as a new ClassificationSpan. /// </summary> /// <param name="trackingSpan">The span currently being classified</param> /// <returns>A list of ClassificationSpans that represent spans identified to be of this classification</returns> public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span) { var classifications = new List<ClassificationSpan>(); var text = span.GetText(); var commentIndex = text.IndexOf('#'); if (commentIndex >= 0) { classifications.Add( new ClassificationSpan( new SnapshotSpan( span.Snapshot, new Span(span.Start + commentIndex, span.Length - commentIndex) ), _comment ) ); text = text.Substring(0, commentIndex); } var match = Regex.Match(text, @"^( *(\t+))+"); if (match.Success) { foreach (Capture capture in match.Groups[2].Captures) { classifications.Add( new ClassificationSpan( new SnapshotSpan( span.Snapshot, new Span(span.Start + capture.Index, capture.Length) ), _tab ) ); } } try { var scanner = new Scanner(new StringReader(text)); Type previousTokenType = null; while (scanner.MoveNext()) { IClassificationType classificationType = null; var currentTokenType = scanner.Current.GetType(); var tokenLength = scanner.Current.End.Index - scanner.Current.Start.Index; if (currentTokenType == typeof(Anchor)) { classificationType = _anchor; } else if (currentTokenType == typeof(AnchorAlias)) { classificationType = _alias; } else if (currentTokenType == typeof(Scalar)) { classificationType = previousTokenType == typeof(Key) ? _key : _value; } else if (currentTokenType == typeof(Tag)) { classificationType = _tag; } else if (currentTokenType == typeof(TagDirective)) { classificationType = _directive; } else if (currentTokenType == typeof(VersionDirective)) { classificationType = _directive; } else if (tokenLength > 0) { classificationType = _symbol; } previousTokenType = currentTokenType; if (classificationType != null) { classifications.Add( new ClassificationSpan( new SnapshotSpan( span.Snapshot, new Span(span.Start + scanner.Current.Start.Index, tokenLength) ), classificationType ) ); } } } catch { } return classifications; }
private void AssertHasNext(Scanner scanner) { Assert.True(scanner.MoveNext()); }
private void AssertDoesNotHaveNext(Scanner scanner) { Assert.False(scanner.MoveNext()); }
private void AssertCurrent(Scanner scanner, Token expected) { Console.WriteLine(expected.GetType().Name); Assert.NotNull(scanner.Current); Assert.True(expected.GetType().IsAssignableFrom(scanner.Current.GetType())); foreach (var property in expected.GetType().GetProperties()) { if(property.PropertyType != typeof(Mark) && property.CanRead) { object value = property.GetValue(scanner.Current, null); Console.WriteLine("\t{0} = {1}", property.Name, value); Assert.Equal(property.GetValue(expected, null), value); } } }