diff Implab/Formats/BufferScanner.cs @ 174:983df35b3ca1 ref20160224

sync
author cin
date Fri, 18 Mar 2016 18:10:30 +0300
parents ecfece82ca11
children
line wrap: on
line diff
--- a/Implab/Formats/BufferScanner.cs	Tue Mar 15 02:11:06 2016 +0300
+++ b/Implab/Formats/BufferScanner.cs	Fri Mar 18 18:10:30 2016 +0300
@@ -1,125 +1,45 @@
 using System;
 using Implab.Automaton.RegularExpressions;
 using Implab.Automaton;
+using System.Diagnostics;
 
 namespace Implab.Formats {
     public struct BufferScanner<TTag> {
-        char[] m_buffer;
-        int m_offset;
-        int m_position;
-        int m_hi;
-
-        readonly int m_chunk;
-        readonly int m_limit;
-
         readonly DFAStateDescriptor<TTag>[] m_dfa;
         int m_state;
+        int m_pos;
 
-        public BufferScanner(DFAStateDescriptor<TTag>[] dfa, int initialState, int chunk, int limit) {
+        public BufferScanner(DFAStateDescriptor<TTag>[] dfa, int initialState) {
             m_dfa = dfa;
             m_state = initialState;
-            m_chunk = chunk;
-            m_limit = limit;
-            m_buffer = null;
-            m_offset = 0;
-            m_position = 0;
-            m_hi = 0;
-        }
-
-        public char[] Buffer {
-            get {
-                return m_buffer;
-            }
-        }
-
-        public int HiMark {
-            get {
-                return m_hi;
-            }
         }
 
         public int Position {
-            get {
-                return m_position;
-            }
-        }
-
-        public int Length {
-            get {
-                return m_hi - m_position;
-            }
-        }
-
-        public int TokenOffset {
-            get {
-                return m_offset;
-            }
-        }
-
-        public int TokenLength {
-            get {
-                return m_position - m_offset;
-            }
-        }
-
-        public void Init(char[] buffer, int position, int length) {
-            m_buffer = buffer;
-            m_position = position;
-            m_offset = position;
-            m_hi = position + length;
-        }
-
-        public int Extend() {
-            // free space
-            var free = m_buffer.Length - m_hi;
-
-            // if the buffer have enough free space
-            if (free > 0)
-                return free;
-
-            // effective size of the buffer
-            var size = m_buffer.Length - m_offset;
-                
-            // calculate the new size
-            int grow = Math.Min(m_limit - size, m_chunk);
-            if (grow <= 0)
-                throw new ParserException(String.Format("Input buffer {0} bytes limit exceeded", m_limit));
-
-            var temp = new char[size + grow];
-            Array.Copy(m_buffer, m_offset, temp, 0, m_hi - m_offset);
-            m_position -= m_offset;
-            m_hi -= m_offset;
-            m_offset = 0;
-            m_buffer = temp;
-
-            return free + grow;
-        }
-
-        public void RaiseMark(int size) {
-            m_hi += size;
+            get { return m_pos; }
         }
 
         /// <summary>
         /// Scan this instance.
         /// </summary>
         /// <returns><c>true</c> - additional data required</returns>
-        public bool Scan() {
-            while (m_position < m_hi) {
-                var ch = m_buffer[m_position];
-                var next = m_dfa[m_state].transitions[(int)ch];
+        public bool Scan(int[] buffer, int position, int length) {
+            var hi = position + length;
+            m_pos = position;
+
+            while (position < hi) {
+                var next = m_dfa[m_state].transitions[buffer[position]];
                 if (next == DFAConst.UNREACHABLE_STATE) {
                     if (m_dfa[m_state].final)
                         return false;
 
                     throw new ParserException(
                         String.Format(
-                            "Unexpected token '{0}'",
-                            new string(m_buffer, m_offset, m_position - m_offset)
+                            "Unexpected symbol"
                         )
                     );
                 }
+                m_pos++;
                 m_state = next;
-                m_position++;
             }
 
             return true;
@@ -129,8 +49,7 @@
             if (!m_dfa[m_state].final)
                 throw new ParserException(
                     String.Format(
-                        "Unexpected token '{0}'",
-                        new string(m_buffer, m_offset, m_position - m_offset)
+                        "Unexpected EOF"
                     )
                 );
         }