Exemple #1
0
		public static StyleRule ParseTree(BaseTree tree)
		{
			var rule = new StyleRule();

			foreach (BaseTree child in tree.Children)
			{
				switch(child.Text)
				{
					case "PROPERTY":
						rule.Properties.Add(StyleProperty.ParseTree(child));
						break;
					case "RULE":
						rule.Rules.Add(StyleRule.ParseTree(child));
						break;
					case "SELECTORGROUP":
						foreach (BaseTree selectorChild in child.Children)
						{
							rule.Selectors.Add(StyleSelector.ParseTree(selectorChild));
						}
						break;
					case "MIXIN":
						rule.Mixins.AddRange(ParseMixins(child));
						break;
				}
			}
			return rule;
		}
Exemple #2
0
		private static IEnumerable<StyleSelector> ParseMixins(BaseTree child)
		{
			foreach(BaseTree selectorgroup in child.Children)
			{
				foreach(BaseTree selector in selectorgroup.Children)
					yield return StyleSelector.ParseTree(selector);
			}
		}
Exemple #3
0
        private static void DumpSubTree(BaseTree tree, int level, StreamWriter sw)
        {
            var text = tree.Text ?? "nil";
            sw.WriteLine(new String(' ', level * 2) + text);

            foreach(BaseTree subTree in tree.Children ?? new BaseTree[0])
            {
                DumpSubTree(subTree, level + 1, sw);
            }
        }
Exemple #4
0
        private static void DumpTree(BaseTree tree)
        {
            if (!Directory.Exists(@"d:\ast-dumps\"))
                return;

            using(var sw = new StreamWriter(@"d:\ast-dumps\"+ Guid.NewGuid() + ".dump"))
            {
                DumpSubTree(tree, 0, sw);
            }
        }
Exemple #5
0
        public virtual void SanityCheckParentAndChildIndexes(ITree parent, int i)
        {
            if (parent != this.Parent)
            {
                throw new InvalidOperationException("parents don't match; expected " + parent + " found " + this.Parent);
            }
            if (i != this.ChildIndex)
            {
                throw new InvalidOperationException("child indexes don't match; expected " + i + " found " + this.ChildIndex);
            }
            int n = this.ChildCount;

            for (int c = 0; c < n; c++)
            {
                BaseTree child = (BaseTree)this.GetChild(c);
                child.SanityCheckParentAndChildIndexes(this, c);
            }
        }
Exemple #6
0
		public static StyleProperty ParseTree(BaseTree tree)
		{
			var property = new StyleProperty {Name = ((BaseTree) tree.Children[0]).Text};

			for(var i=1; i<tree.ChildCount;i++)
			{
				var node = tree.GetChild(i);
				if(node.Text == "EXPR")
				{
					property.Values.Add(StyleExpression.ParseExpression(node.GetChild(0)));
				}
				else
				{
					property.Values.Add(new LiteralExpression(node.Text));
				}
			}

			return property;
		}
		private StyleRule ParseRule(BaseTree tree)
		{
			var rule = new StyleRule();

			foreach (BaseTree child in tree.Children)
			{
				switch (child.Text)
				{
					case "PROPERTY":
						rule.Properties.Add(ParseProperty(child));
						break;
					case "RULE":
						rule.Rules.Add(ParseRule(child));
						break;
					case "SELECTOR":
						rule.Selectors.Add(ParseSelector(child));
						break;
					case "MIXIN":
						rule.Mixins.Add(ParseSelector(child));
						break;
				}
			}
			return rule;
		}
		private StyleDocument ParseDocument(BaseTree tree)
		{
			var document = new StyleDocument();

			if (tree.IsNil)
			{
				foreach (BaseTree child in tree.Children)
				{
					switch (child.Text)
					{
						case "VAR":
							var variable = ParseVariable(child);
							document.AddVariable(variable);
							break;
						case "RULE":
							var rule = ParseRule(child);
							document.AddRule(rule);
							break;
					}
				}
			}

			return document;
		}
Exemple #9
0
        public virtual void ReplaceChildren(int startChildIndex, int stopChildIndex, object t)
        {
            if (startChildIndex < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (stopChildIndex < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (t == null)
            {
                throw new ArgumentNullException(nameof(t));
            }
            if (stopChildIndex < startChildIndex)
            {
                throw new ArgumentException();
            }
            if (this.Children == null)
            {
                throw new ArgumentException("indexes invalid; no children in list");
            }
            int           num1  = stopChildIndex - startChildIndex + 1;
            ITree         tree1 = (ITree)t;
            IList <ITree> treeList;

            if (tree1.IsNil)
            {
                BaseTree baseTree = tree1 as BaseTree;
                if (baseTree != null && baseTree.Children != null)
                {
                    treeList = baseTree.Children;
                }
                else
                {
                    treeList = this.CreateChildrenList();
                    int childCount = tree1.ChildCount;
                    for (int i = 0; i < childCount; ++i)
                    {
                        treeList.Add(tree1.GetChild(i));
                    }
                }
            }
            else
            {
                treeList = (IList <ITree>) new List <ITree>(1);
                treeList.Add(tree1);
            }
            int count1 = treeList.Count;
            int count2 = treeList.Count;
            int num2   = num1 - count1;

            if (num2 == 0)
            {
                int index1 = 0;
                for (int index2 = startChildIndex; index2 <= stopChildIndex; ++index2)
                {
                    ITree tree2 = treeList[index1];
                    this.Children[index2] = tree2;
                    tree2.Parent          = (ITree)this;
                    tree2.ChildIndex      = index2;
                    ++index1;
                }
            }
            else if (num2 > 0)
            {
                for (int index = 0; index < count2; ++index)
                {
                    this.Children[startChildIndex + index] = treeList[index];
                }
                int index1 = startChildIndex + count2;
                for (int index2 = index1; index2 <= stopChildIndex; ++index2)
                {
                    this.Children.RemoveAt(index1);
                }
                this.FreshenParentAndChildIndexes(startChildIndex);
            }
            else
            {
                for (int index = 0; index < num1; ++index)
                {
                    this.Children[startChildIndex + index] = treeList[index];
                }
                for (int index = num1; index < count1; ++index)
                {
                    this.Children.Insert(startChildIndex + index, treeList[index]);
                }
                this.FreshenParentAndChildIndexes(startChildIndex);
            }
        }
Exemple #10
0
        /** <summary>
         *  Delete children from start to stop and replace with t even if t is
         *  a list (nil-root tree).  num of children can increase or decrease.
         *  For huge child lists, inserting children can force walking rest of
         *  children to set their childindex; could be slow.
         *  </summary>
         */
        public virtual void ReplaceChildren(int startChildIndex, int stopChildIndex, object t)
        {
            /*
             * System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+
             *                 " with "+((BaseTree)t).toStringTree());
             * System.out.println("in="+toStringTree());
             */
            if (children == null)
            {
                throw new ArgumentException("indexes invalid; no children in list");
            }
            int          replacingHowMany = stopChildIndex - startChildIndex + 1;
            int          replacingWithHowMany;
            ITree        newTree     = (ITree)t;
            List <ITree> newChildren = null;

            // normalize to a list of children to add: newChildren
            if (newTree.IsNil)
            {
                BaseTree baseTree = newTree as BaseTree;
                if (baseTree != null)
                {
                    newChildren = baseTree.children;
                }
                else
                {
                    newChildren = CreateChildrenList();
                    int n = newTree.ChildCount;
                    for (int i = 0; i < n; i++)
                    {
                        newChildren.Add(newTree.GetChild(i));
                    }
                }
            }
            else
            {
                newChildren = new List <ITree>(1);
                newChildren.Add(newTree);
            }
            replacingWithHowMany = newChildren.Count;
            int numNewChildren = newChildren.Count;
            int delta          = replacingHowMany - replacingWithHowMany;

            // if same number of nodes, do direct replace
            if (delta == 0)
            {
                int j = 0; // index into new children
                for (int i = startChildIndex; i <= stopChildIndex; i++)
                {
                    ITree child = newChildren[j];
                    children[i]      = child;
                    child.Parent     = this;
                    child.ChildIndex = i;
                    j++;
                }
            }
            else if (delta > 0)
            {
                // fewer new nodes than there were
                // set children and then delete extra
                for (int j = 0; j < numNewChildren; j++)
                {
                    children[startChildIndex + j] = newChildren[j];
                }
                int indexToDelete = startChildIndex + numNewChildren;
                for (int c = indexToDelete; c <= stopChildIndex; c++)
                {
                    // delete same index, shifting everybody down each time
                    children.RemoveAt(indexToDelete);
                }
                FreshenParentAndChildIndexes(startChildIndex);
            }
            else
            {
                // more new nodes than were there before
                // fill in as many children as we can (replacingHowMany) w/o moving data
                for (int j = 0; j < replacingHowMany; j++)
                {
                    children[startChildIndex + j] = newChildren[j];
                }
                int numToInsert = replacingWithHowMany - replacingHowMany;
                for (int j = replacingHowMany; j < replacingWithHowMany; j++)
                {
                    children.Insert(startChildIndex + j, newChildren[j]);
                }
                FreshenParentAndChildIndexes(startChildIndex);
            }
            //System.out.println("out="+toStringTree());
        }
Exemple #11
0
 public AntlrRuntime_BaseTreeDebugView(BaseTree tree)
 {
     this._tree = tree;
 }
		private StyleSelector ParseSelector(BaseTree child)
		{
			var selectors = new List<string>();
			foreach (BaseTree selector in child.Children)
			{
				switch(selector.Text)
				{
					case "CLASS":
						selectors.Add("." + selector.GetChild(0).Text);
						break;
					case "ID":
						selectors.Add("#" + selector.GetChild(0).Text);
						break;
					case "TAG":
						selectors.Add(selector.GetChild(0).Text);
						break;

				}
			}
			return new StyleSelector { Name = string.Join(" ", selectors.ToArray()) };
		}
Exemple #13
0
 /**
  * @deprecated use #setChildWithTokens(int,LinkedListTree), damnit
  */
 public void setChild(int index, BaseTree child)
 {
     base.SetChild(index, child);
     ((LinkedListTree)child).Parent = this;
 }
Exemple #14
0
        /// <summary>
        /// Delete children from start to stop and replace with t even if t is
        /// a list (nil-root tree).
        /// </summary>
        /// <remarks>
        /// Number of children can increase or decrease.
        /// For huge child lists, inserting children can force walking rest of
        /// children to set their childindex; could be slow.
        /// </remarks>
        public virtual void ReplaceChildren(int startChildIndex, int stopChildIndex, object t)
        {
            /*
             * Console.Out.WriteLine("replaceChildren "+startChildIndex+", "+stopChildIndex+
             *                                 " with "+((BaseTree)t).ToStringTree());
             * Console.Out.WriteLine("in="+ToStringTree());
             */
            if (children == null)
            {
                throw new ArgumentException("indexes invalid; no children in list");
            }
            int      replacingHowMany = stopChildIndex - startChildIndex + 1;
            int      replacingWithHowMany;
            BaseTree newTree = (BaseTree)t;
            IList    newChildren;

            // normalize to a list of children to add: newChildren
            if (newTree.IsNil)
            {
                newChildren = newTree.Children;
            }
            else
            {
                newChildren = new ArrayList(1);
                newChildren.Add(newTree);
            }
            replacingWithHowMany = newChildren.Count;
            int numNewChildren = newChildren.Count;
            int delta          = replacingHowMany - replacingWithHowMany;

            // if same number of nodes, do direct replace
            if (delta == 0)
            {
                int j = 0;                 // index into new children
                for (int i = startChildIndex; i <= stopChildIndex; i++)
                {
                    BaseTree child = (BaseTree)newChildren[j];
                    children[i]      = child;
                    child.Parent     = this;
                    child.ChildIndex = i;
                    j++;
                }
            }
            else if (delta > 0)
            {             // fewer new nodes than there were
                // set children and then delete extra
                for (int j = 0; j < numNewChildren; j++)
                {
                    children[startChildIndex + j] = newChildren[j];
                }
                int indexToDelete = startChildIndex + numNewChildren;
                for (int c = indexToDelete; c <= stopChildIndex; c++)
                {
                    // delete same index, shifting everybody down each time
                    children.RemoveAt(indexToDelete);
                }
                FreshenParentAndChildIndexes(startChildIndex);
            }
            else
            {             // more new nodes than were there before
                // fill in as many children as we can (replacingHowMany) w/o moving data
                int replacedSoFar;
                for (replacedSoFar = 0; replacedSoFar < replacingHowMany; replacedSoFar++)
                {
                    children[startChildIndex + replacedSoFar] = newChildren[replacedSoFar];
                }
                // replacedSoFar has correct index for children to add
                for ( ; replacedSoFar < replacingWithHowMany; replacedSoFar++)
                {
                    children.Insert(startChildIndex + replacedSoFar, newChildren[replacedSoFar]);
                }
                FreshenParentAndChildIndexes(startChildIndex);
            }
            //Console.Out.WriteLine("out="+ToStringTree());
        }
 public AntlrRuntime_BaseTreeDebugView(BaseTree tree)
 {
     _tree = tree;
 }
Exemple #16
0
		private void Load(BaseTree tree)
		{
			if (tree.IsNil)
			{
				foreach (BaseTree child in tree.Children)
				{
					Load(child);
				}
			}
			else
			{
				switch (tree.Text)
				{
					case "VAR":
						var variable = StyleVariable.ParseTree(tree);
						AddVariable(variable);
						break;
					case "RULE":
						var rule = StyleRule.ParseTree(tree);
						AddRule(rule);
						break;
				}
			}			
		}