BufferScanner.cs
62 lines
| 1.6 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r173 | using System; | ||
using Implab.Automaton.RegularExpressions; | ||||
using Implab.Automaton; | ||||
cin
|
r174 | using System.Diagnostics; | ||
cin
|
r173 | |||
namespace Implab.Formats { | ||||
public struct BufferScanner<TTag> { | ||||
readonly DFAStateDescriptor<TTag>[] m_dfa; | ||||
int m_state; | ||||
cin
|
r174 | int m_pos; | ||
cin
|
r173 | |||
cin
|
r174 | public BufferScanner(DFAStateDescriptor<TTag>[] dfa, int initialState) { | ||
cin
|
r173 | m_dfa = dfa; | ||
m_state = initialState; | ||||
} | ||||
public int Position { | ||||
cin
|
r174 | get { return m_pos; } | ||
cin
|
r173 | } | ||
/// <summary> | ||||
/// Scan this instance. | ||||
/// </summary> | ||||
/// <returns><c>true</c> - additional data required</returns> | ||||
cin
|
r174 | 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]]; | ||||
cin
|
r173 | if (next == DFAConst.UNREACHABLE_STATE) { | ||
if (m_dfa[m_state].final) | ||||
return false; | ||||
throw new ParserException( | ||||
String.Format( | ||||
cin
|
r174 | "Unexpected symbol" | ||
cin
|
r173 | ) | ||
); | ||||
} | ||||
cin
|
r174 | m_pos++; | ||
cin
|
r173 | m_state = next; | ||
} | ||||
return true; | ||||
} | ||||
public void Eof() { | ||||
if (!m_dfa[m_state].final) | ||||
throw new ParserException( | ||||
String.Format( | ||||
cin
|
r174 | "Unexpected EOF" | ||
cin
|
r173 | ) | ||
); | ||||
} | ||||
public TTag[] GetTokenTags() { | ||||
return m_dfa[m_state].tags; | ||||
} | ||||
} | ||||
} | ||||