int brackets = 0;
final GeneratedFormulaParserTokenManager tokenParser = new GeneratedFormulaParserTokenManager(new JavaCharStream(new StringReader(formula), 1, 1));
Token token = tokenParser.getNextToken();
while (token.kind != GeneratedFormulaParserConstants.EOF)
{
final FormulaToken formulaToken;
images.add(token.image);
final String upper = token.image.toUpperCase();
if (parserNames.containsKey(upper))
{
if ("(".equals(token.image))
{
brackets++;
}
else if (")".equals(token.image))
{
--brackets;
}
final FormulaOpCodeMapEntry opCode = (FormulaOpCodeMapEntry) parserNames.get(upper);
formulaToken = opCode.Token;
}
else if (token.kind == GeneratedFormulaParserConstants.WHITESPACE)
{
final FormulaOpCodeMapEntry opCode = (FormulaOpCodeMapEntry) specialOpCodes.get(FormulaMapGroupSpecialOffset.SPACES);
formulaToken = opCode.Token;
}
else
{
formulaToken = new FormulaToken();
formulaToken.OpCode = opCodePush.Token.OpCode;
formulaToken.Data = new Any(Type.STRING, token.image);
}
tokens.add(formulaToken);
token = tokenParser.getNextToken();
}
if (brackets > 0)
{
final FormulaOpCodeMapEntry opCode = (FormulaOpCodeMapEntry) parserNames.get(")");
while (brackets-- != 0)
{
formula = formula.concat(")");
images.add(")");
tokens.add(opCode.Token);
}
}
parser.parse(formula);
}
catch (ParseException ex)
{
boolean found = false;
// error occured so all token must be bad
for (int i = 0; i < tokens.size(); i++)
{
if (!found && ex.currentToken != null && images.get(i).equals(ex.currentToken.image))
{
found = true;
}
if (found)
{
final FormulaToken dest = new FormulaToken();
dest.OpCode = ((FormulaOpCodeMapEntry) specialOpCodes.get(FormulaMapGroupSpecialOffset.BAD)).Token.OpCode;
dest.Data = new Any(Type.STRING, images.get(i));
tokens.remove(i);
tokens.add(i, dest);
}