public
Fork of olsonjeffery/boolangstudio
Description: Boo language integration for Visual Studio 2008
Homepage: http://www.codeplex.com/BooLangStudio
Clone URL: git://github.com/jagregory/boolangstudio.git
boolangstudio / Source / BooLangService / StringParsing / BracketPairFinder.cs
100644 42 lines (34 sloc) 1.123 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections.Generic;
 
namespace Boo.BooLangService.StringParsing
{
    public class BracketPairFinder
    {
        private readonly BracketPairType type;
 
        public BracketPairFinder(BracketPairType type)
        {
            this.type = type;
        }
 
        public BracketPairCollection FindPairs(string value)
        {
            var pairs = new BracketPairCollection();
            var stack = new Stack<BracketPair>();
            var walker = new ExcludingStringLiteralsStringWalker();
 
            foreach (var position in walker.Iterate(value))
            {
                if (position.Character == type.Left)
                {
                    // starting new pair
                    stack.Push(new BracketPair { LeftIndex = position.Index });
                }
 
                if (position.Character == type.Right)
                {
                    // ending pair
                    var pair = stack.Pop();
 
                    pair.RightIndex = position.Index;
 
                    pairs.Add(pair);
                }
            }
 
            return pairs;
        }
    }
}