comparison Implab/Automaton/Scanner.cs @ 164:ec35731ae299 ref20160224

Almost complete DFA refactoring
author cin
date Thu, 25 Feb 2016 02:11:13 +0300
parents 0526412bbb26
children e227e78d72e4
comparison
equal deleted inserted replaced
163:419aa51b04fd 164:ec35731ae299
15 /// </remarks> 15 /// </remarks>
16 public abstract class Scanner<TTag> : Disposable { 16 public abstract class Scanner<TTag> : Disposable {
17 struct ScannerConfig { 17 struct ScannerConfig {
18 public DFAStateDescriptior<TTag>[] states; 18 public DFAStateDescriptior<TTag>[] states;
19 public int[] alphabetMap; 19 public int[] alphabetMap;
20 public int initialState;
20 } 21 }
21 22
22 Stack<ScannerConfig> m_defs = new Stack<ScannerConfig>(); 23 Stack<ScannerConfig> m_defs = new Stack<ScannerConfig>();
23 24
24 DFAStateDescriptior<TTag>[] m_states; 25 DFAStateDescriptior<TTag>[] m_states;
25 int[] m_alphabetMap; 26 int[] m_alphabetMap;
27 int m_initialState;
26 28
27 protected DFAStateDescriptior<TTag> m_currentState; 29 protected DFAStateDescriptior<TTag> m_currentState;
28 int m_previewCode; 30 int m_previewCode;
29 31
30 protected int m_tokenLen = 0; 32 protected int m_tokenLen = 0;
37 TextReader m_reader; 39 TextReader m_reader;
38 bool m_disposeReader; 40 bool m_disposeReader;
39 int m_chunkSize = 1024; // 1k 41 int m_chunkSize = 1024; // 1k
40 int m_limit = 10 * 1024 * 1024; // 10Mb 42 int m_limit = 10 * 1024 * 1024; // 10Mb
41 43
42 protected Scanner(DFAStateDescriptior<TTag>[] states, int[] alphabet) { 44 protected Scanner(DFAStateDescriptior<TTag>[] states, int[] alphabet, int initialState) {
43 Safe.ArgumentNotEmpty(states, "states"); 45 Safe.ArgumentNotEmpty(states, "states");
44 Safe.ArgumentNotNull(alphabet, "alphabet"); 46 Safe.ArgumentNotNull(alphabet, "alphabet");
45 47
46 m_states = states; 48 m_states = states;
47 m_alphabetMap = alphabet; 49 m_alphabetMap = alphabet;
50 m_initialState = initialState;
48 51
49 Feed(new char[0]); 52 Feed(new char[0]);
50 } 53 }
51 54
52 /// <summary> 55 /// <summary>
128 /// <returns><c>false</c> - достигнут конец данных, токен не прочитан.</returns> 131 /// <returns><c>false</c> - достигнут конец данных, токен не прочитан.</returns>
129 protected bool ReadTokenInternal() { 132 protected bool ReadTokenInternal() {
130 if (m_pointer >= m_bufferSize) 133 if (m_pointer >= m_bufferSize)
131 return false; 134 return false;
132 135
133 m_currentState = m_states[DFADefinition.INITIAL_STATE]; 136 m_currentState = m_states[m_initialState];
134 m_tokenLen = 0; 137 m_tokenLen = 0;
135 m_tokenOffset = m_pointer; 138 m_tokenOffset = m_pointer;
136 int nextState; 139 int nextState;
137 do { 140 do {
138 nextState = m_currentState.transitions[m_previewCode]; 141 nextState = m_currentState.transitions[m_previewCode];
215 /// Преключает внутренний ДКА на указанный, позволяет реализовать подобие захватывающей 218 /// Преключает внутренний ДКА на указанный, позволяет реализовать подобие захватывающей
216 /// группировки. 219 /// группировки.
217 /// </summary> 220 /// </summary>
218 /// <param name="states">Таблица состояний нового ДКА</param> 221 /// <param name="states">Таблица состояний нового ДКА</param>
219 /// <param name="alphabet">Таблица входных символов для нового ДКА</param> 222 /// <param name="alphabet">Таблица входных символов для нового ДКА</param>
220 protected void Switch(DFAStateDescriptior<TTag>[] states, int[] alphabet) { 223 protected void Switch(DFAStateDescriptior<TTag>[] states, int[] alphabet, int initialState) {
221 Safe.ArgumentNotNull(states, "dfa"); 224 Safe.ArgumentNotNull(states, "dfa");
222 225
223 m_defs.Push(new ScannerConfig { 226 m_defs.Push(new ScannerConfig {
224 states = m_states, 227 states = m_states,
225 alphabetMap = m_alphabetMap 228 alphabetMap = m_alphabetMap,
229 initialState = m_initialState
226 }); 230 });
227 231
228 m_states = states; 232 m_states = states;
229 m_alphabetMap = alphabet; 233 m_alphabetMap = alphabet;
234 m_initialState = initialState;
230 235
231 m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; 236 m_previewCode = m_alphabetMap[m_buffer[m_pointer]];
232 } 237 }
233 238
234 /// <summary> 239 /// <summary>
238 if (m_defs.Count == 0) 243 if (m_defs.Count == 0)
239 throw new InvalidOperationException(); 244 throw new InvalidOperationException();
240 var prev = m_defs.Pop(); 245 var prev = m_defs.Pop();
241 m_states = prev.states; 246 m_states = prev.states;
242 m_alphabetMap = prev.alphabetMap; 247 m_alphabetMap = prev.alphabetMap;
248 m_initialState = prev.initialState;
243 m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; 249 m_previewCode = m_alphabetMap[m_buffer[m_pointer]];
244 } 250 }
245 251
246 protected override void Dispose(bool disposing) { 252 protected override void Dispose(bool disposing) {
247 if (disposing) { 253 if (disposing) {