/
Lexer.Core.cs
45 lines (40 loc) · 1.09 KB
/
Lexer.Core.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Collections.Generic;
namespace RegularExpressionDataGenerator
{
public class Lexer : IContext
{
Stack<IState> _states;
IState _currentState;
IEnumerator<char> _characters;
public IEnumerable<IToken> Tokenize(string expression)
{
_states = new Stack<IState>();
_currentState = new LiteralState();
_characters = expression.GetEnumerator();
while (_characters.MoveNext())
{
var token = _currentState.Handle(this);
if (token != null)
{
yield return token;
}
}
}
public void ToState(IState state)
{
_states.Push(_currentState);
_currentState = state;
}
public void EndState()
{
_currentState = _states.Pop();
}
public char Current
{
get
{
return _characters.Current;
}
}
}
}