Пример #1
0
        public static BamlNode Parse(BamlDocument document, CancellationToken token)
        {
            Debug.Assert(document.Count > 0 && document[0].Type == BamlRecordType.DocumentStart);

            BamlBlockNode current = null;
            var           stack   = new Stack <BamlBlockNode>();

            for (int i = 0; i < document.Count; i++)
            {
                token.ThrowIfCancellationRequested();

                if (IsHeader(document[i]))
                {
                    var prev = current;

                    current = new BamlBlockNode {
                        Header = document[i]
                    };

                    if (!(prev is null))
                    {
                        prev.Children.Add(current);
                        current.Parent = prev;
                        stack.Push(prev);
                    }
                }
                else if (IsFooter(document[i]))
                {
                    if (current is null)
                    {
                        throw new Exception("Unexpected footer.");
                    }

                    while (!IsMatch(current.Header, document[i]))
                    {
                        // End record can be omited (sometimes).
                        if (stack.Count > 0)
                        {
                            current = stack.Pop();
                        }
                    }
                    current.Footer = document[i];
                    if (stack.Count > 0)
                    {
                        current = stack.Pop();
                    }
                }
                else
                {
                    current.Children.Add(new BamlRecordNode(document[i])
                    {
                        Parent = current
                    });
                }
            }
            Debug.Assert(stack.Count == 0);
            return(current);
        }
Пример #2
0
		void BuildNodeMap(BamlBlockNode node, RecursionCounter counter) {
			if (node == null || !counter.Increment())
				return;

			NodeMap[node.Header] = node;

			foreach (var child in node.Children) {
				var childBlock = child as BamlBlockNode;
				if (childBlock != null)
					BuildNodeMap(childBlock, counter);
			}

			counter.Decrement();
		}
Пример #3
0
		public static void ProcessChildren(XamlContext ctx, BamlBlockNode node, BamlElement nodeElem) {
			ctx.XmlNs.PushScope(nodeElem);
			if (nodeElem.Xaml.Element != null)
				nodeElem.Xaml.Element.AddAnnotation(ctx.XmlNs.CurrentScope);
			foreach (var child in node.Children) {
				var handler = LookupHandler(child.Type);
				if (handler == null) {
					Debug.WriteLine("BAML Handler {0} not implemented.", child.Type);
					continue;
				}
				var elem = handler.Translate(ctx, (BamlNode)child, nodeElem);
				if (elem != null) {
					nodeElem.Children.Add(elem);
					elem.Parent = nodeElem;
				}

				ctx.CancellationToken.ThrowIfCancellationRequested();
			}
			ctx.XmlNs.PopScope();
		}
Пример #4
0
		public static BamlNode Parse(BamlDocument document, CancellationToken token) {
			Debug.Assert(document.Count > 0 && document[0].Type == BamlRecordType.DocumentStart);

			BamlBlockNode current = null;
			var stack = new Stack<BamlBlockNode>();

			for (int i = 0; i < document.Count; i++) {
				token.ThrowIfCancellationRequested();

				if (IsHeader(document[i])) {
					var prev = current;

					current = new BamlBlockNode {
						Header = document[i]
					};

					if (prev != null) {
						prev.Children.Add(current);
						current.Parent = prev;
						stack.Push(prev);
					}
				}
				else if (IsFooter(document[i])) {
					if (current == null)
						throw new Exception("Unexpected footer.");

					while (!IsMatch(current.Header, document[i])) {
						// End record can be omited (sometimes).
						if (stack.Count > 0)
							current = stack.Pop();
					}
					current.Footer = document[i];
					if (stack.Count > 0)
						current = stack.Pop();
				}
				else
					current.Children.Add(new BamlRecordNode(document[i]) { Parent = current });
			}
			Debug.Assert(stack.Count == 0);
			return current;
		}