Ejemplo n.º 1
0
		/// <summary>
		/// Initializes a new instance of the <see cref="IParser"/> class.
		/// </summary>
		/// <param name="input">The input where the YAML stream is to be read.</param>
		public Parser(TextReader input)
		{
			scanner = new Scanner(input);
		}
		/// <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))
					{
						if (previousTokenType == typeof (Key))
						{
							classificationType = _key;
						}
						else
						{
							// Decode the scalar
							var scalarToken = (Scalar) scanner.Current;
							var scalar = new SharpYaml.Events.Scalar(scalarToken.Value);
							switch (schema.GetDefaultTag(scalar))
							{
								case JsonSchema.BoolShortTag:
                                    classificationType = _bool;
                                    break;
                                case JsonSchema.FloatShortTag:
								case JsonSchema.IntShortTag:
									classificationType = _number;
									break;
								case SchemaBase.StrShortTag:
                                    classificationType = scalarToken.Style == ScalarStyle.DoubleQuoted || scalarToken.Style == ScalarStyle.SingleQuoted ? _string : _value;
									break;
								default:
									classificationType = _value;
									break;
							}
						}
						
					}
					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 && tokenLength > 0)
					{
						classifications.Add(
							new ClassificationSpan(
								new SnapshotSpan(
									span.Snapshot,
									new Span(span.Start + scanner.Current.Start.Index, tokenLength)
								),
								classificationType
							)
						);
					}
				}
			}
			catch (Exception ex)
			{
                Trace.WriteLine("Exception in AssetObjectEditor " + ex);
			}

			return classifications;
		}