173
|
1 using System;
|
|
2 using Implab.Automaton.RegularExpressions;
|
|
3 using Implab.Automaton;
|
174
|
4 using System.Diagnostics;
|
173
|
5
|
|
6 namespace Implab.Formats {
|
|
7 public struct BufferScanner<TTag> {
|
|
8 readonly DFAStateDescriptor<TTag>[] m_dfa;
|
|
9 int m_state;
|
174
|
10 int m_pos;
|
173
|
11
|
174
|
12 public BufferScanner(DFAStateDescriptor<TTag>[] dfa, int initialState) {
|
173
|
13 m_dfa = dfa;
|
|
14 m_state = initialState;
|
|
15 }
|
|
16
|
|
17 public int Position {
|
174
|
18 get { return m_pos; }
|
173
|
19 }
|
|
20
|
|
21 /// <summary>
|
|
22 /// Scan this instance.
|
|
23 /// </summary>
|
|
24 /// <returns><c>true</c> - additional data required</returns>
|
174
|
25 public bool Scan(int[] buffer, int position, int length) {
|
|
26 var hi = position + length;
|
|
27 m_pos = position;
|
|
28
|
|
29 while (position < hi) {
|
|
30 var next = m_dfa[m_state].transitions[buffer[position]];
|
173
|
31 if (next == DFAConst.UNREACHABLE_STATE) {
|
|
32 if (m_dfa[m_state].final)
|
|
33 return false;
|
|
34
|
|
35 throw new ParserException(
|
|
36 String.Format(
|
174
|
37 "Unexpected symbol"
|
173
|
38 )
|
|
39 );
|
|
40 }
|
174
|
41 m_pos++;
|
173
|
42 m_state = next;
|
|
43 }
|
|
44
|
|
45 return true;
|
|
46 }
|
|
47
|
|
48 public void Eof() {
|
|
49 if (!m_dfa[m_state].final)
|
|
50 throw new ParserException(
|
|
51 String.Format(
|
174
|
52 "Unexpected EOF"
|
173
|
53 )
|
|
54 );
|
|
55 }
|
|
56
|
|
57 public TTag[] GetTokenTags() {
|
|
58 return m_dfa[m_state].tags;
|
|
59 }
|
|
60 }
|
|
61 }
|
|
62
|