Mercurial > pub > ImplabNet
diff Implab/Automaton/Scanner.cs @ 172:92d5278d1b10 ref20160224
Working on text scanner
author | cin |
---|---|
date | Mon, 14 Mar 2016 01:19:38 +0300 |
parents | e227e78d72e4 |
children | 983df35b3ca1 |
line wrap: on
line diff
--- a/Implab/Automaton/Scanner.cs Thu Mar 10 01:19:33 2016 +0300 +++ b/Implab/Automaton/Scanner.cs Mon Mar 14 01:19:38 2016 +0300 @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using Implab.Components; +using Implab.Automaton.RegularExpressions; namespace Implab.Automaton { /// <summary> @@ -14,19 +15,23 @@ /// конца токена и допустимости текущего символа. /// </remarks> public abstract class Scanner<TTag> : Disposable { - struct ScannerConfig { - public DFAStateDescriptior<TTag>[] states; - public int[] alphabetMap; - public int initialState; + protected struct ScannerConfig { + public readonly DFAStateDescriptor<TTag>[] states; + public readonly int[] alphabet; + public readonly int initialState; + + public ScannerConfig(DFAStateDescriptor<TTag>[] states, int[] alphabet, int initialState) { + this.initialState = initialState; + this.alphabet = alphabet; + this.states = states; + } } Stack<ScannerConfig> m_defs = new Stack<ScannerConfig>(); - DFAStateDescriptior<TTag>[] m_states; - int[] m_alphabetMap; - int m_initialState; + ScannerConfig m_config; - protected DFAStateDescriptior<TTag> m_currentState; + protected DFAStateDescriptor<TTag> m_currentState; int m_previewCode; protected int m_tokenLen; @@ -41,15 +46,11 @@ int m_chunkSize = 1024; // 1k int m_limit = 10 * 1024 * 1024; // 10Mb - protected Scanner(DFAStateDescriptior<TTag>[] states, int[] alphabet, int initialState) { - Safe.ArgumentNotEmpty(states, "states"); - Safe.ArgumentNotNull(alphabet, "alphabet"); + protected Scanner(ScannerConfig config) { + Safe.ArgumentNotEmpty(config.states, "config.states"); + Safe.ArgumentNotNull(config.alphabet, "config.alphabet"); - m_states = states; - m_alphabetMap = alphabet; - m_initialState = initialState; - - Feed(new char[0]); + m_config = config; } /// <summary> @@ -110,7 +111,7 @@ /// </summary> protected TTag[] TokenTags { get { - return m_currentState.tag; + return m_currentState.tags; } } @@ -133,7 +134,7 @@ if (m_pointer >= m_bufferSize) return false; - m_currentState = m_states[m_initialState]; + m_currentState = m_config.states[m_config.initialState]; m_tokenLen = 0; m_tokenOffset = m_pointer; int nextState; @@ -151,7 +152,7 @@ ) ); } - m_currentState = m_states[nextState]; + m_currentState = m_config.states[nextState]; m_tokenLen++; } while (Shift()); @@ -172,7 +173,7 @@ return false; } - m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; + m_previewCode = m_config.alphabet[m_buffer[m_pointer]]; return true; } @@ -217,23 +218,14 @@ /// Преключает внутренний ДКА на указанный, позволяет реализовать подобие захватывающей /// группировки. /// </summary> - /// <param name="states">Таблица состояний нового ДКА</param> - /// <param name="alphabet">Таблица входных символов для нового ДКА</param> - /// <param name = "initialState"></param> - protected void Switch(DFAStateDescriptior<TTag>[] states, int[] alphabet, int initialState) { - Safe.ArgumentNotNull(states, "dfa"); + /// <param name = "config"></param> + protected void Switch(ScannerConfig config) { + Safe.ArgumentNotNull(config.states, "config.states"); - m_defs.Push(new ScannerConfig { - states = m_states, - alphabetMap = m_alphabetMap, - initialState = m_initialState - }); + m_defs.Push(m_config); + m_config = config; - m_states = states; - m_alphabetMap = alphabet; - m_initialState = initialState; - - m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; + m_previewCode = m_config.alphabet[m_buffer[m_pointer]]; } /// <summary> @@ -242,11 +234,9 @@ protected void Restore() { if (m_defs.Count == 0) throw new InvalidOperationException(); - var prev = m_defs.Pop(); - m_states = prev.states; - m_alphabetMap = prev.alphabetMap; - m_initialState = prev.initialState; - m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; + m_config = m_defs.Pop(); + + m_previewCode = m_config.alphabet[m_buffer[m_pointer]]; } protected override void Dispose(bool disposing) {