173
|
1 using System;
|
|
2 using Implab.Components;
|
175
|
3 using System.Diagnostics;
|
|
4 using Implab.Automaton;
|
176
|
5 using System.Text;
|
173
|
6
|
|
7 namespace Implab.Formats {
|
176
|
8 public abstract class TextScanner : Disposable {
|
|
9 readonly int m_bufferMax;
|
|
10 readonly int m_chunkSize;
|
173
|
11
|
176
|
12 char[] m_buffer;
|
174
|
13 int m_bufferOffset;
|
175
|
14 int m_bufferSize;
|
176
|
15 int m_tokenOffset;
|
173
|
16 int m_tokenLength;
|
174
|
17
|
176
|
18 /// <summary>
|
177
|
19 /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class.
|
176
|
20 /// </summary>
|
|
21 /// <param name="bufferMax">Buffer max.</param>
|
|
22 /// <param name="chunkSize">Chunk size.</param>
|
|
23 protected TextScanner(int bufferMax, int chunkSize) {
|
|
24 Debug.Assert(m_chunkSize <= m_bufferMax);
|
|
25
|
|
26 m_bufferMax = bufferMax;
|
|
27 m_chunkSize = chunkSize;
|
|
28 }
|
173
|
29
|
176
|
30 /// <summary>
|
177
|
31 /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class.
|
176
|
32 /// </summary>
|
|
33 /// <param name="buffer">Buffer.</param>
|
|
34 protected TextScanner(char[] buffer) {
|
|
35 if (buffer != null) {
|
|
36 m_buffer = buffer;
|
|
37 m_bufferSize = buffer.Length;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 /// <summary>
|
|
42 /// (hungry) Reads the next token.
|
|
43 /// </summary>
|
|
44 /// <returns><c>true</c>, if token internal was read, <c>false</c> if there is no more tokens in the stream.</returns>
|
|
45 /// <param name="dfa">The transition map for the automaton</param>
|
|
46 /// <param name="final">Final states of the automaton.</param>
|
|
47 /// <param name="tags">Tags.</param>
|
|
48 /// <param name="state">The initial state for the automaton.</param>
|
177
|
49 /// <param name="alphabet"></param>
|
|
50 /// <param name = "tag"></param>
|
|
51 internal bool ReadToken<TTag>(int[,] dfa, bool[] final, TTag[][] tags, int state, int[] alphabet, out TTag[] tag) {
|
176
|
52 Safe.ArgumentNotNull();
|
|
53 m_tokenLength = 0;
|
|
54
|
|
55 var maxSymbol = alphabet.Length - 1;
|
174
|
56
|
175
|
57 do {
|
176
|
58 // after the next chunk is read the offset in the buffer may change
|
|
59 int pos = m_bufferOffset + m_tokenLength;
|
|
60
|
177
|
61 while (pos < m_bufferSize) {
|
175
|
62 var ch = m_buffer[pos];
|
176
|
63
|
178
|
64 state = dfa[state, ch > maxSymbol ? AutomatonConst.UNCLASSIFIED_INPUT : alphabet[ch]];
|
|
65 if (state == AutomatonConst.UNREACHABLE_STATE)
|
175
|
66 break;
|
176
|
67
|
|
68 pos++;
|
175
|
69 }
|
176
|
70
|
|
71 m_tokenLength = pos - m_bufferOffset;
|
178
|
72 } while (state != AutomatonConst.UNREACHABLE_STATE && Feed());
|
176
|
73
|
|
74 m_tokenOffset = m_bufferOffset;
|
|
75 m_bufferOffset += m_tokenLength;
|
174
|
76
|
176
|
77 if (final[state]) {
|
|
78 tag = tags[state];
|
|
79 return true;
|
177
|
80 }
|
|
81
|
|
82 if (m_bufferOffset == m_bufferSize) {
|
|
83 if (m_tokenLength == 0) //EOF
|
176
|
84 return false;
|
|
85
|
177
|
86 throw new ParserException();
|
|
87 }
|
|
88
|
|
89 throw new ParserException(String.Format("Unexpected symbol '{0}'", m_buffer[m_bufferOffset]));
|
176
|
90
|
|
91 }
|
173
|
92
|
176
|
93 protected void Feed(char[] buffer, int offset, int length) {
|
|
94 m_buffer = buffer;
|
|
95 m_bufferOffset = offset;
|
|
96 m_bufferSize = offset + length;
|
173
|
97 }
|
|
98
|
176
|
99 protected bool Feed() {
|
|
100 if (m_chunkSize <= 0)
|
|
101 return false;
|
|
102
|
|
103 if (m_buffer != null) {
|
|
104 var free = m_buffer.Length - m_bufferSize;
|
|
105
|
|
106 if (free < m_chunkSize) {
|
|
107 free += m_chunkSize;
|
|
108 var used = m_bufferSize - m_bufferOffset;
|
|
109 var size = used + free;
|
|
110
|
|
111 if (size > m_bufferMax)
|
177
|
112 throw new ParserException(String.Format("The buffer limit ({0} Kb) is reached", m_bufferMax/1024));
|
176
|
113
|
|
114 var temp = new char[size];
|
175
|
115
|
176
|
116 var read = Read(temp, used, m_chunkSize);
|
|
117 if (read == 0)
|
|
118 return false;
|
|
119
|
|
120 Array.Copy(m_buffer, m_bufferOffset, temp, 0, used);
|
|
121
|
|
122 m_bufferOffset = 0;
|
|
123 m_bufferSize = used + read;
|
|
124 m_buffer = temp;
|
|
125 }
|
|
126 } else {
|
|
127 Debug.Assert(m_bufferOffset == 0);
|
|
128 m_buffer = new char[m_chunkSize];
|
|
129 m_bufferSize = Read(m_buffer, 0, m_chunkSize);
|
|
130 return (m_bufferSize != 0);
|
|
131 }
|
175
|
132 }
|
|
133
|
|
134 protected abstract int Read(char[] buffer, int offset, int size);
|
173
|
135
|
176
|
136 public string GetTokenValue() {
|
|
137 return new String(m_buffer, m_tokenOffset, m_tokenLength);
|
173
|
138 }
|
|
139
|
176
|
140 public void CopyTokenTo(char[] buffer, int offset) {
|
|
141 m_buffer.CopyTo(buffer, offset);
|
|
142 }
|
|
143
|
|
144 public void CopyTokenTo(StringBuilder sb) {
|
|
145 sb.Append(m_buffer, m_tokenOffset, m_tokenLength);
|
|
146 }
|
175
|
147
|
173
|
148 }
|
|
149 }
|
|
150
|