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

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