Created
May 25, 2011 20:13
-
-
Save ElemarJR/991825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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