Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Forked from ElemarJR/scan.cs
Created May 25, 2011 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanplopes/991831 to your computer and use it in GitHub Desktop.
Save juanplopes/991831 to your computer and use it in GitHub Desktop.
public IEnumerable<Token> Scan(IEnumerable<char> source)
{
var currentState = StatesTable.InitialStage;
var currentValue = "";
Func<Token> markPartialStop = () =>
{
if (currentState.IsStop)
{
var result = new Token(currentState.ResultingTokenId, currentValue);
currentState = StatesTable.InitialState;
currentValue = "";
return result;
}
else
throw new ArgumentException("Invalid source", "source");
};
foreach (var symbol in source)
{
if (currentState.Accepts(symbol))
{
currentState = StatesTable.Accept(currentState.ChangeTo(symbol));
currentValue += c;
}
else yield return markPartialStop();
}
yield return markPartialStop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment