StylesheetReader.cs 3.99 KB
Newer Older
Eric Domke's avatar
Eric Domke committed
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System.Collections.Generic;
using System.IO;
using System.Text;
using ExCSS.Model;

namespace ExCSS
{
    internal class StylesheetReader
    {
        private int _insertion;
        private readonly Stack<int> _collengths;
        private TextReader _reader;
        private readonly StringBuilder _buffer;
        private bool _lineWithReturn;

        StylesheetReader()
        {
            _buffer = new StringBuilder();
            _collengths = new Stack<int>();
            Column = 1;
            Line = 1;
        }

        internal StylesheetReader(string styleText) : this()
        {
            _reader = new StringReader(styleText);
            ReadCurrent();
        }

        internal StylesheetReader(Stream styleStream) : this()
        {
            _reader = new StreamReader(styleStream, true);
            ReadCurrent();
        }

        internal bool IsBeginning
        {
            get { return _insertion < 2; }
        }

        internal int Line { get; private set; }

        internal int Column { get; private set; }

        internal bool IsEnded { get; private set; }

        internal bool IsEnding
        {
            get { return Current == Specification.EndOfFile; }
        }

        internal char Current { get; private set; }

        internal char Next
        {
            get
            {
                Advance();

                return Current;
            }
        }

        internal char Previous
        {
            get
            {
                Back();

                return Current;
            }
        }

        internal void Advance()
        {
            if (!IsEnding)
            {
                AdvanceUnsafe();
            }
            else if (!IsEnded)
            {
                IsEnded = true;
            }
        }

        internal void Advance(int positions)
        {
            while (positions-- > 0 && !IsEnding)
            {
                AdvanceUnsafe();
            }
        }

        internal void Back()
        {
            IsEnded = false;

            if (!IsBeginning)
            {
                BackUnsafe();
            }
        }

        internal void Back(int positions)
        {
            IsEnded = false;

            while (positions-- > 0 && !IsBeginning)
            {
                BackUnsafe();
            }
        }

        private void ReadCurrent()
        {
            if (_insertion < _buffer.Length)
            {
                Current = _buffer[_insertion];
                _insertion++;
                return;
            }

            var nextPosition = _reader.Read();
            Current = nextPosition == -1 ? Specification.EndOfFile : (char)nextPosition;

            if (Current == Specification.CarriageReturn)
            {
                Current = Specification.LineFeed;
                _lineWithReturn = true;
            }
            else if (_lineWithReturn)
            {
                _lineWithReturn = false;

                if (Current == Specification.LineFeed)
                {
                    ReadCurrent();
                    return;
                }
            }

            _buffer.Append(Current);
            _insertion++;
        }

        private void AdvanceUnsafe()
        {
            if (Current.IsLineBreak())
            {
                _collengths.Push(Column);
                Column = 1;
                Line++;
            }
            else
            {
                Column++;
            }

            ReadCurrent();
        }

        private void BackUnsafe()
        {
            _insertion--;

            if (_insertion == 0)
            {
                Column = 0;
                Current = Specification.Null;
                return;
            }

            Current = _buffer[_insertion - 1];

            if (Current.IsLineBreak())
            {
                Column = _collengths.Count != 0 ? _collengths.Pop() : 1;
                Line--;
            }
            else
            {
                Column--;
            }
        }
    }
}