Esempio n. 1
0
        private static void RemoveComments(TokenSet tokens)
        {
            TokenEnumerator enumerator = tokens.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Type == TokenType.Comment)
                {
                    enumerator.RemoveCurrent();
                }
                else
                {
                    RemoveComments(enumerator.Current.Children);
                }
            }
        }
Esempio n. 2
0
        private static void CreateTree_Grouping(TokenSet tokens)
        {
            //start with grouping constructs
            TokenEnumerator enumerator = tokens.GetEnumerator();

            while (enumerator.MoveNext())
            {
                //make sure this is the start of a group
                if (enumerator.Current.Type != TokenType.GroupBegin)
                {
                    continue;
                }

                //pull in all children
                Stack <Token> GroupStarters = new Stack <Token>();
                GroupStarters.Push(enumerator.Current);

                //push the group under its predecessor for functions...
                if (enumerator.Previous != null && enumerator.Previous.Children.Count == 0 &&
                    (enumerator.Previous.Type == TokenType.Identifier || enumerator.Previous.Type == TokenType.Unknown))
                {
                    enumerator.Previous.Children.Add(enumerator.Current);
                    enumerator.RemoveCurrent();
                }

                while (GroupStarters.Count > 0)
                {
                    enumerator.MoveNext();
                    if (!enumerator.IsValid)
                    {
                        throw new ApplicationException("Unclosed " + GroupStarters.Peek().Value);
                    }

                    Token child = enumerator.Current;
                    if (child == null)
                    {
                        throw new ApplicationException("Unclosed " + GroupStarters.Peek().Value);
                    }
                    enumerator.RemoveCurrent();

                    Token group = GroupStarters.Peek();
                    Token last  = group.Children.Count > 0 ? group.Children.Last : null;
                    if (last != null && last.Children.Count == 0 &&
                        (last.Type == TokenType.Identifier || last.Type == TokenType.Unknown) &&
                        (child.Type == TokenType.GroupBegin || child.Type == TokenType.CaseStatement))
                    {
                        //push the group under its predecessor for functions...
                        last.Children.Add(child);
                    }
                    else
                    {
                        group.Children.Add(child);
                    }

                    if (child.Type == TokenType.GroupBegin || child.Type == TokenType.CaseStatement)
                    {
                        GroupStarters.Push(child);
                    }
                    else if (child.Type == TokenType.GroupEnd)
                    {
                        GroupStarters.Pop();
                    }
                }
            }
        }