annotate Implab/Automaton/Scanner.cs @ 168:8fb9c9507a26 ref20160224

sync
author cin
date Wed, 02 Mar 2016 19:59:16 +0300
parents e227e78d72e4
children 92d5278d1b10
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
1 using Implab;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
2 using System;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
3 using System.Collections.Generic;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
4 using System.IO;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
5 using Implab.Components;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
6
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
7 namespace Implab.Automaton {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
9 /// Базовый класс для разбора потока входных символов на токены.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
10 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 /// <remarks>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12 /// Сканнер имеет внутри буффер с симолами входного текста, по которому перемещаются два
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13 /// указателя, начала и конца токена, при перемещении искользуется ДКА для определения
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 /// конца токена и допустимости текущего символа.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15 /// </remarks>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16 public abstract class Scanner<TTag> : Disposable {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17 struct ScannerConfig {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18 public DFAStateDescriptior<TTag>[] states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 public int[] alphabetMap;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
20 public int initialState;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 Stack<ScannerConfig> m_defs = new Stack<ScannerConfig>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25 DFAStateDescriptior<TTag>[] m_states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26 int[] m_alphabetMap;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
27 int m_initialState;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29 protected DFAStateDescriptior<TTag> m_currentState;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30 int m_previewCode;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
32 protected int m_tokenLen;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33 protected int m_tokenOffset;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35 protected char[] m_buffer;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36 protected int m_bufferSize;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 protected int m_pointer;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39 TextReader m_reader;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 bool m_disposeReader;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 int m_chunkSize = 1024; // 1k
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 int m_limit = 10 * 1024 * 1024; // 10Mb
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
44 protected Scanner(DFAStateDescriptior<TTag>[] states, int[] alphabet, int initialState) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 Safe.ArgumentNotEmpty(states, "states");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46 Safe.ArgumentNotNull(alphabet, "alphabet");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 m_states = states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 m_alphabetMap = alphabet;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
50 m_initialState = initialState;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 Feed(new char[0]);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 /// Заполняет входными данными буффер.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 /// <param name="data">Данные для обработки.</param>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 /// <remarks>Копирование данных не происходит, переданный массив используется в
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 /// качестве входного буффера.</remarks>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 public void Feed(char[] data) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 Safe.ArgumentNotNull(data, "data");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64 Feed(data, data.Length);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
65 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
67 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
68 /// Заполняет буффур чтения входными данными.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
69 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
70 /// <param name="data">Данные для обработки.</param>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
71 /// <param name="length">Длина данных для обработки.</param>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
72 /// <remarks>Копирование данных не происходит, переданный массив используется в
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
73 /// качестве входного буффера.</remarks>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
74 public void Feed(char[] data, int length) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
75 Safe.ArgumentNotNull(data, "data");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
76 Safe.ArgumentInRange(length, 0, data.Length, "length");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
77 AssertNotDisposed();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
78
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
79 m_pointer = -1;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
80 m_buffer = data;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
81 m_bufferSize = length;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
82 Shift();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
83 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
84
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
85 public void Feed(TextReader reader, bool dispose) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
86 Safe.ArgumentNotNull(reader, "reader");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
87 AssertNotDisposed();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
88
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
89 if (m_reader != null && m_disposeReader)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
90 m_reader.Dispose();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
91
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
92 m_reader = reader;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
93 m_disposeReader = dispose;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
94 m_pointer = -1;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
95 m_buffer = new char[m_chunkSize];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
96 m_bufferSize = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
97 Shift();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
98 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
99
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
100 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
101 /// Получает текущий токен в виде строки.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
102 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
103 /// <returns></returns>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
104 protected string GetTokenValue() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
105 return new String(m_buffer, m_tokenOffset, m_tokenLen);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
106 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
107
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
108 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
109 /// Метки текущего токена, которые были назначены в регулярном выражении.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
110 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
111 protected TTag[] TokenTags {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
112 get {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
113 return m_currentState.tag;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
114 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
115 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
116
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
117 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
118 /// Признак конца данных
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
119 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
120 public bool EOF {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
121 get {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
122 return m_pointer >= m_bufferSize;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
123 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
124 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
125
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
126 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
127 /// Читает следующий токен, при этом <see cref="m_tokenOffset"/> указывает на начало токена,
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
128 /// <see cref="m_tokenLen"/> на длину токена, <see cref="m_buffer"/> - массив символов, в
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
129 /// котором находится токен.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
130 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
131 /// <returns><c>false</c> - достигнут конец данных, токен не прочитан.</returns>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
132 protected bool ReadTokenInternal() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
133 if (m_pointer >= m_bufferSize)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
134 return false;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
135
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
136 m_currentState = m_states[m_initialState];
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
137 m_tokenLen = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
138 m_tokenOffset = m_pointer;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
139 int nextState;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
140 do {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
141 nextState = m_currentState.transitions[m_previewCode];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
142 if (nextState == DFAConst.UNREACHABLE_STATE) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
143 if (m_currentState.final)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
144 return true;
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
145
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
146 throw new ParserException(
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
147 String.Format(
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
148 "Unexpected symbol '{0}', at pos {1}",
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
149 m_buffer[m_pointer],
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
150 Position
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
151 )
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
152 );
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
153 }
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
154 m_currentState = m_states[nextState];
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
155 m_tokenLen++;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
156
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
157 } while (Shift());
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
158
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
159 // END OF DATA
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
160 if (!m_currentState.final)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
161 throw new ParserException("Unexpected end of data");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
163 return true;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
164 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
165
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
166
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
167 bool Shift() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
168 m_pointer++;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
169
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
170 if (m_pointer >= m_bufferSize) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
171 if (!ReadNextChunk())
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
172 return false;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
173 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
174
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
175 m_previewCode = m_alphabetMap[m_buffer[m_pointer]];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
176
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
177 return true;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
178 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
179
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
180 bool ReadNextChunk() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
181 if (m_reader == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
182 return false;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
183
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
184 // extend buffer if nesessary
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
185 if (m_pointer + m_chunkSize > m_buffer.Length) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
186 // trim unused buffer head
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
187 var size = m_tokenLen + m_chunkSize;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
188 if (size >= m_limit)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
189 throw new ParserException(String.Format("Input buffer {0} bytes limit exceeded", m_limit));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
190 var temp = new char[size];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
191 Array.Copy(m_buffer, m_tokenOffset, temp, 0, m_tokenLen);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
192 m_pointer -= m_tokenOffset;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
193 m_bufferSize -= m_tokenOffset;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
194 m_tokenOffset = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
195 m_buffer = temp;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
196 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
197
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
198 var read = m_reader.Read(m_buffer, m_tokenLen, m_chunkSize);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
199 if (read == 0)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
200 return false;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
201
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
202 m_bufferSize += read;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
203
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
204 return true;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
205 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
206
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
207 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
208 /// Позиция сканнера во входном буфере
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
209 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
210 public int Position {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
211 get {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
212 return m_pointer + 1;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
213 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
214 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
215
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
216 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
217 /// Преключает внутренний ДКА на указанный, позволяет реализовать подобие захватывающей
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
218 /// группировки.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
219 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
220 /// <param name="states">Таблица состояний нового ДКА</param>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
221 /// <param name="alphabet">Таблица входных символов для нового ДКА</param>
165
e227e78d72e4 DFA refactoring
cin
parents: 164
diff changeset
222 /// <param name = "initialState"></param>
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
223 protected void Switch(DFAStateDescriptior<TTag>[] states, int[] alphabet, int initialState) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
224 Safe.ArgumentNotNull(states, "dfa");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
225
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
226 m_defs.Push(new ScannerConfig {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
227 states = m_states,
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
228 alphabetMap = m_alphabetMap,
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
229 initialState = m_initialState
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
230 });
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
231
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
232 m_states = states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
233 m_alphabetMap = alphabet;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
234 m_initialState = initialState;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
235
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
236 m_previewCode = m_alphabetMap[m_buffer[m_pointer]];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
237 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
238
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
239 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
240 /// Восстанавливает предыдущей ДКА сканнера.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
241 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
242 protected void Restore() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
243 if (m_defs.Count == 0)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
244 throw new InvalidOperationException();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
245 var prev = m_defs.Pop();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
246 m_states = prev.states;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
247 m_alphabetMap = prev.alphabetMap;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 162
diff changeset
248 m_initialState = prev.initialState;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
249 m_previewCode = m_alphabetMap[m_buffer[m_pointer]];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
250 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
251
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
252 protected override void Dispose(bool disposing) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
253 if (disposing) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
254 if (m_reader != null && m_disposeReader)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
255 m_reader.Dispose();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
256 m_buffer = null;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
257 m_bufferSize = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
258 m_pointer = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
259 m_tokenLen = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
260 m_tokenOffset = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
261 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
262 base.Dispose(disposing);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
263 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
264 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
265 }