Beispiel #1
0
        public CodeBlock Parse(ITextSnapshot snapshot, AbortCheck abort)
        {
            CodeBlock root = new CodeBlock(null, BlockType.Root, null, new SnapshotSpan(snapshot, 0, snapshot.Length), 0, 0);

            CodeBlock parent = root;

            Stack <CodeBlock> blockOpenings = new Stack <CodeBlock>();

            bool          leadingWhitespace = true;
            int           statementStart    = -1;
            StringBuilder currentStatement  = new StringBuilder();

            SnapshotFilter filter = new SnapshotFilter(snapshot);

            while (filter.Next())
            {
                int  position = filter.Position;
                char c        = filter.Character;

                if (statementStart == -1)
                {
                    statementStart = position;
                }
                else if (leadingWhitespace)
                {
                    leadingWhitespace = char.IsWhiteSpace(c);
                    statementStart    = position;
                }

                if (!filter.InQuote)
                {
                    if (c == '{')
                    {
                        CodeBlock child = CreateCodeBlock(parent, currentStatement, new SnapshotSpan(snapshot, position, 0), statementStart, blockOpenings.Count + 1);

                        blockOpenings.Push(child);

                        parent = child;
                    }
                    else if (c == '}')
                    {
                        if (blockOpenings.Count > 0)
                        {
                            CodeBlock child = blockOpenings.Pop();
                            child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, position + 1)));

                            parent = child.Parent;
                        }
                    }
                }

                if (filter.EOS)
                {
                    currentStatement.Remove(0, currentStatement.Length);
                    statementStart    = -1;
                    leadingWhitespace = true;
                }
                else
                {
                    currentStatement.Append(c);
                }

                if (abort())
                {
                    return(null);
                }
            }

            while (blockOpenings.Count > 0)
            {
                CodeBlock child = blockOpenings.Pop();
                child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, snapshot.Length)));
            }

            return(root);
        }
Beispiel #2
0
        public Task <CodeBlock> ParseAsync(ITextSnapshot snapshot, CancellationToken token)
        {
            CodeBlock root = new CodeBlock(null, BlockType.Root, null, new SnapshotSpan(snapshot, 0, snapshot.Length), 0, 0);

            CodeBlock parent = root;

            Stack <CodeBlock> blockOpenings = new Stack <CodeBlock>();

            bool          leadingWhitespace = true;
            int           statementStart    = 0;
            StringBuilder currentStatement  = new StringBuilder();
            StringBuilder filteredStatement = new StringBuilder();

            SnapshotFilter filter = new SnapshotFilter(snapshot);

            while (filter.Next())
            {
                int  position = filter.Position;
                char c        = filter.Character;

                if (leadingWhitespace)
                {
                    leadingWhitespace = char.IsWhiteSpace(c);
                    statementStart    = position;
                }

                if (!filter.InQuote)
                {
                    if (c == '{')
                    {
                        CodeBlock child = CreateCodeBlock(parent, currentStatement, filteredStatement, new SnapshotSpan(snapshot, position, 0), statementStart, blockOpenings.Count + 1);

                        blockOpenings.Push(child);

                        parent = child;
                    }
                    else if (c == '}')
                    {
                        if (blockOpenings.Count > 0)
                        {
                            CodeBlock child = blockOpenings.Pop();
                            child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, position + 1)));

                            parent = child.Parent;
                        }
                    }
                }

                if (filter.EOS)
                {
                    currentStatement.Length  = 0;
                    filteredStatement.Length = 0;
                    leadingWhitespace        = true;
                }
                else
                {
                    AppendCharacter(currentStatement, c);
                    if (!filter.InQuote)
                    {
                        AppendCharacter(filteredStatement, c);
                    }
                }

                if (token.IsCancellationRequested)
                {
                    return(null);
                }
            }

            while (blockOpenings.Count > 0)
            {
                CodeBlock child = blockOpenings.Pop();
                child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, snapshot.Length)));
            }

            return(Task.FromResult <CodeBlock>(root));
        }
Beispiel #3
0
        public Task<CodeBlock> ParseAsync(ITextSnapshot snapshot, CancellationToken token)
        {
            CodeBlock root = new CodeBlock(null, BlockType.Root, null, new SnapshotSpan(snapshot, 0, snapshot.Length), 0, 0);

            CodeBlock parent = root;

            Stack<CodeBlock> blockOpenings = new Stack<CodeBlock>();

            bool leadingWhitespace = true;
            int statementStart = 0;
            StringBuilder currentStatement = new StringBuilder();
            StringBuilder filteredStatement = new StringBuilder();

            SnapshotFilter filter = new SnapshotFilter(snapshot);
            while (filter.Next())
            {
                int position = filter.Position;
                char c = filter.Character;

                if (leadingWhitespace)
                {
                    leadingWhitespace = char.IsWhiteSpace(c);
                    statementStart = position;
                }

                if (!filter.InQuote)
                {
                    if (c == '{')
                    {
                        CodeBlock child = CreateCodeBlock(parent, currentStatement, filteredStatement, new SnapshotSpan(snapshot, position, 0), statementStart, blockOpenings.Count + 1);

                        blockOpenings.Push(child);

                        parent = child;
                    }
                    else if (c == '}')
                    {
                        if (blockOpenings.Count > 0)
                        {
                            CodeBlock child = blockOpenings.Pop();
                            child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, position + 1)));

                            parent = child.Parent;
                        }
                    }
                }

                if (filter.EOS)
                {
                    currentStatement.Length = 0;
                    filteredStatement.Length = 0;
                    leadingWhitespace = true;
                }
                else
                {
                    AppendCharacter(currentStatement, c);
                    if (!filter.InQuote)
                        AppendCharacter(filteredStatement, c);
                }

                if (token.IsCancellationRequested)
                    return null;
            }

            while (blockOpenings.Count > 0)
            {
                CodeBlock child = blockOpenings.Pop();
                child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, snapshot.Length)));
            }

            return Task.FromResult<CodeBlock>(root);
        }
        public CodeBlock Parse(ITextSnapshot snapshot, AbortCheck abort)
        {
            CodeBlock root = new CodeBlock(null, BlockType.Root, null, new SnapshotSpan(snapshot, 0, snapshot.Length), 0, 0);

            CodeBlock parent = root;

            Stack<CodeBlock> blockOpenings = new Stack<CodeBlock>();

            bool leadingWhitespace = true;
            int statementStart = -1;
            StringBuilder currentStatement = new StringBuilder();

            SnapshotFilter filter = new SnapshotFilter(snapshot);
            while (filter.Next())
            {
                int position = filter.Position;
                char c = filter.Character;

                if (statementStart == -1)
                    statementStart = position;
                else if (leadingWhitespace)
                {
                    leadingWhitespace = char.IsWhiteSpace(c);
                    statementStart = position;
                }

                if (!filter.InQuote)
                {
                    if (c == '{')
                    {
                        CodeBlock child = CreateCodeBlock(parent, currentStatement, new SnapshotSpan(snapshot, position, 0), statementStart, blockOpenings.Count + 1);

                        blockOpenings.Push(child);

                        parent = child;
                    }
                    else if (c == '}')
                    {
                        if (blockOpenings.Count > 0)
                        {
                            CodeBlock child = blockOpenings.Pop();
                            child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, position + 1)));

                            parent = child.Parent;
                        }
                    }
                }

                if (filter.EOS)
                {
                    currentStatement.Remove(0, currentStatement.Length);
                    statementStart = -1;
                    leadingWhitespace = true;
                }
                else
                    currentStatement.Append(c);

                if (abort())
                    return null;
            }

            while (blockOpenings.Count > 0)
            {
                CodeBlock child = blockOpenings.Pop();
                child.SetSpan(new SnapshotSpan(snapshot, Span.FromBounds(child.Span.Start, snapshot.Length)));
            }

            return root;
        }