Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created May 25, 2011 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ElemarJR/991825 to your computer and use it in GitHub Desktop.
Save ElemarJR/991825 to your computer and use it in GitHub Desktop.
public IEnumerable<Token> Scan(IEnumerable<char> source)
{
var currentState = StatesTable.States[StatesTable.InitialState];
string currentValue = "";
var enumerator = source.GetEnumerator();
bool @continue = enumerator.MoveNext();
while (@continue)
{
var c = enumerator.Current;
if (currentState.GoTo.ContainsKey(c))
{
currentState = StatesTable.States[currentState.GoTo[c]];
currentValue += c;
@continue = enumerator.MoveNext();
if (!@continue)
{
if (currentState.ResultingTokenId != null)
{
var result = new Token(currentState.ResultingTokenId, currentValue);
yield return result;
currentState = StatesTable.States[StatesTable.InitialState];
currentValue = "";
}
else
{
throw new ArgumentException("Invalid source", "source");
}
}
}
else if (currentState.ResultingTokenId != null)
{
var result = new Token(currentState.ResultingTokenId, currentValue);
yield return result;
currentState = StatesTable.States[StatesTable.InitialState];
currentValue = "";
}
else
{
throw new ArgumentException("Invalid source", "source");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment