You could make a Parser, either with Regular Expressions or by looping the string and reading the relevant informations, as the following piece of code.
I only include the parser itself, as the rest can be inferred from the way it gets call or how it is used. You would still need to check at which position the Token starts but I guess that should be simple enough.
public class FormulaReader : IFormulaReader
{
protected static readonly IToken EQUALIZE = new Token(TokenType.EQUALIZER, "=");
protected static readonly IToken ADD = new Token(TokenType.MODIFIER, "+");
protected static readonly IToken SUBTRACT = new Token(TokenType.MODIFIER, "-");
protected static readonly IToken DIVIDE = new Token(TokenType.MODIFIER, "/");
protected static readonly IToken MULTIPLY = new Token(TokenType.MODIFIER, "*");
protected static readonly IToken GROUPSTART = new Token(TokenType.GROUPSTART, "(");
protected static readonly IToken GROUPEND = new Token(TokenType.GROUPEND, ")");
public IFormula Parse(string textRepresentation)
{
IList<IToken> tokenList = new List<IToken>()
{
EQUALIZE, ADD, SUBTRACT, DIVIDE, MULTIPLY, GROUPSTART, GROUPEND
};
IFormula formula = new Formula();
IToken currentToken = new Token(TokenType.IDENTIFIER, "");
bool equalizerFound = false;
for (int i = 0, len = textRepresentation.Length; i < len; i++)
{
string item = textRepresentation[i].ToString();
if (item == " ")
{
continue;
}
IToken selectedToken = (from t in tokenList where string.Equals(t.Text, item) select t).FirstOrDefault();
if (selectedToken == null)
{
currentToken.Text += item;
}
else
{
if (!string.IsNullOrWhiteSpace(currentToken.Text))
{
// contains an identifier
formula.Add(currentToken, equalizerFound);
}
if (selectedToken.TokenType != TokenType.EQUALIZER)
{
formula.Add(selectedToken, equalizerFound);
}
else
{
equalizerFound = true;
}
currentToken = new Token(TokenType.IDENTIFIER, "");
}
}
return formula;
}
}