Inheritance: IScanner
コード例 #1
0
ファイル: ScannerTests.cs プロジェクト: ClockVapor/YamlDotNet
        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");
        }
コード例 #2
0
        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)
            {
            }
        }
コード例 #3
0
ファイル: Parser.cs プロジェクト: NemoNessuno/Opia
 /// <summary>
 /// Initializes a new instance of the <see cref="Parser"/> class.
 /// </summary>
 public Parser(Scanner scanner)
 {
     this.scanner = scanner;
 }
コード例 #4
0
 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");
 }
コード例 #5
0
ファイル: Parser.cs プロジェクト: igor-toporet/YamlDotNet
 /// <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);
 }
コード例 #6
0
		private void AssertSequenceOfTokensFrom(Scanner scanner, params Token[] tokens)
		{
			AssertPartialSequenceOfTokensFrom(scanner, tokens);
			scanner.MoveNext().Should().BeFalse("Found extra tokens");
		}
コード例 #7
0
ファイル: ScannerTests.cs プロジェクト: chipdude/yamldotnet
 private void AssertHasNext(Scanner scanner)
 {
     Assert.IsTrue(scanner.MoveNext(), "The scanner does not contain more tokens.");
 }
コード例 #8
0
ファイル: ScannerTests.cs プロジェクト: chipdude/yamldotnet
 private void AssertNext(Scanner scanner, Token expected)
 {
     AssertHasNext(scanner);
     AssertCurrent(scanner, expected);
 }
コード例 #9
0
ファイル: ScannerTests.cs プロジェクト: chipdude/yamldotnet
        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));
                }
            }
        }
コード例 #10
0
ファイル: ScannerTests.cs プロジェクト: chipdude/yamldotnet
 private void AssertDoesNotHaveNext(Scanner scanner)
 {
     Assert.IsFalse(scanner.MoveNext(), "The scanner should not contain more tokens.");
 }
コード例 #11
0
ファイル: YamlDotNetEditor.cs プロジェクト: roji/YamlDotNet
		/// <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;
		}
コード例 #12
0
 private void AssertHasNext(Scanner scanner)
 {
     Assert.True(scanner.MoveNext());
 }
コード例 #13
0
 private void AssertDoesNotHaveNext(Scanner scanner)
 {
     Assert.False(scanner.MoveNext());
 }
コード例 #14
0
        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);
                }
            }
        }
コード例 #15
0
ファイル: Parser.cs プロジェクト: DarkBoroda/YamlDotNet
 /// <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);
 }