private FormulaTree Interpret2(IList<IFormulaToken> tokens)
{
// valid tokens
if (tokens == null)
{
SetParsingError(null, AppResourcesHelper.Get("FormulaInterpreter_NullOrEmpty"));
return null;
}
// interpret syntax
var tokens2 = SetOrigin(tokens).ToList();
var tokens3 = tokens2.AsEnumerable();
tokens3 = InterpretBrackets(tokens3);
tokens3 = InterpretNumbers(tokens3, tokens2);
tokens3 = InterpretNonParameter(tokens3);
tokens3 = InterpretFunctions(tokens3);
tokens3 = InterpretMinusTokenForward(tokens3);
tokens3 = InterpretOperators(tokens3, tokens2);
var formula = tokens3.Cast<FormulaTree>().FirstOrDefault();
if (IsCancellationRequested) return null;
// valid formula
if (formula == null)
{
SetParsingError(
source: Range.FromLength(0, tokens.Count),
message: AppResourcesHelper.Get("FormulaInterpreter_NullOrEmpty"));
return null;
}
// special case *) should find parsing error at *
var formulaRange = GetOrigin(formula);
if (formulaRange.Length < tokens.Count)
{
var nextIndex = formulaRange.End;
var nextToken = tokens2.First(token => GetOrigin(token).Start == nextIndex);
var nextParenthesis = nextToken as FormulaTokenParenthesis;
if (nextParenthesis != null && nextParenthesis.IsClosing)
{
SetParsingError(
source: nextParenthesis,
message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_UnmatchedClosingParenthesis"));
}
else if (nextToken is FormulaTokenParameterSeparator)
{
SetParsingError(
source: nextToken,
message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_NonArgumentParameterSeparator"));
}
else
{
SetParsingError(
source: Range.Empty(GetOrigin(formula).End),
message: AppResourcesHelper.Get("FormulaInterpreter_DoubleValue"));
}
return null;
}
// interpret semantics
try
{
// validate evaluation constraints like sin(True + 4)
var isNumber = formula.IsNumber();
}
catch (SemanticsErrorException ex)
{
var selection = ex.Node == null ? Range.Empty(0) : GetOrigin(ex.Node);
SetParsingError2(selection, ex.Message);
return null;
}
return formula;
}