Пример #1
0
 public Expression Create(Token token)
 {
     switch (token.TokenType)
     {
         case TokenType.Integer:
             return new IntegerExpression(token.Value, token.IsNegated);
         case TokenType.String:
             return new StringExpression(token.Value);
         case TokenType.Decimal:
             return new DecimalExpression(token.Value, token.IsNegated);
         case TokenType.Boolean:
             return new BooleanExpression(token.Value);
         case TokenType.ExcelAddress:
             return new ExcelAddressExpression(token.Value, _excelDataProvider, _parsingContext, token.IsNegated);
         case TokenType.InvalidReference:
             return new ExcelErrorExpression(token.Value, ExcelErrorValue.Create(eErrorType.Ref));
         case TokenType.NumericError:
             return new ExcelErrorExpression(token.Value, ExcelErrorValue.Create(eErrorType.Num));
         case TokenType.ValueDataTypeError:
             return new ExcelErrorExpression(token.Value, ExcelErrorValue.Create(eErrorType.Value));
         case TokenType.Null:
             return new ExcelErrorExpression(token.Value, ExcelErrorValue.Create(eErrorType.Null));
         case TokenType.NameValue:
             return new NamedValueExpression(token.Value, _parsingContext);
         default:
             return new StringExpression(token.Value);
     }
 }
 private void BuildUp(Token[] tokens, Expression parent)
 {
     while (_tokenIndex < tokens.Length)
     {
         var token = tokens[_tokenIndex];
         IOperator op = null;
         if (token.TokenType == TokenType.Operator && OperatorsDict.Instance.TryGetValue(token.Value, out op))
         {
             SetOperatorOnExpression(parent, op);
         }
         else if (token.TokenType == TokenType.Function)
         {
             BuildFunctionExpression(tokens, parent, token.Value);
         }
         else if (token.TokenType == TokenType.OpeningEnumerable)
         {
             _tokenIndex++;
             BuildEnumerableExpression(tokens, parent);
         }
         else if (token.TokenType == TokenType.OpeningParenthesis)
         {
             _tokenIndex++;
             BuildGroupExpression(tokens, parent);
             //if (parent is FunctionExpression)
             //{
             //    return;
             //}
         }
         else if (token.TokenType == TokenType.ClosingParenthesis || token.TokenType == TokenType.ClosingEnumerable)
         {
             break;
         }
         else if (token.TokenType == TokenType.Negator)
         {
             _negateNextExpression = true;
         }
         else if(token.TokenType == TokenType.Percent)
         {
             SetOperatorOnExpression(parent, Operator.Percent);
             if (parent == null)
             {
                 _graph.Add(ConstantExpressions.Percent);
             }
             else
             {
                 parent.AddChild(ConstantExpressions.Percent);
             }
         }
         else
         {
             CreateAndAppendExpression(ref parent, token);
         }
         _tokenIndex++;
     }
 }
Пример #3
0
 private void EnsureParenthesesAreWellFormed(Token token, AnalyzingContext context)
 {
     if (token.TokenType == TokenType.OpeningParenthesis)
     {
         context.NumberOfOpenedParentheses++;
     }
     else if (token.TokenType == TokenType.ClosingParenthesis)
     {
         context.NumberOfClosedParentheses++;
     }
 }
 public void ShouldReturnBooleanExpressionWhenTokenIsBoolean()
 {
     var token = new Token("true", TokenType.Boolean);
     var expression = _factory.Create(token);
     Assert.IsInstanceOfType(expression, typeof(BooleanExpression));
 }
Пример #5
0
 private void BuildGroupExpression(Token[] tokens, Expression parent)
 {
     if (parent == null)
     {
         _graph.Add(new GroupExpression(_negateNextExpression));
         _negateNextExpression = false;
         BuildUp(tokens, _graph.Current);
     }
     else
     {
         if (parent.IsGroupedExpression || parent is FunctionArgumentExpression)
         {
             var newGroupExpression = new GroupExpression(_negateNextExpression);
             _negateNextExpression = false;
             parent.AddChild(newGroupExpression);
             BuildUp(tokens, newGroupExpression);
         }
          BuildUp(tokens, parent);
     }
 }
Пример #6
0
 private void HandleFunctionArguments(Token[] tokens, Expression function)
 {
     _tokenIndex++;
     var token = tokens.ElementAt(_tokenIndex);
     if (token.TokenType != TokenType.OpeningParenthesis)
     {
         throw new ExcelErrorValueException(eErrorType.Value);
     }
     _tokenIndex++;
     BuildUp(tokens, function.Children.First());
 }
Пример #7
0
 private static bool TokenIsNegator(Token t)
 {
     return t == null
                 ||
                 t.TokenType == TokenType.Operator
                 ||
                 t.TokenType == TokenType.OpeningParenthesis
                 ||
                 t.TokenType == TokenType.Comma
                 ||
                 t.TokenType == TokenType.SemiColon
                 ||
                 t.TokenType == TokenType.OpeningEnumerable;
 }
 public void ShouldReturnIntegerExpressionWhenTokenIsInteger()
 {
     var token = new Token("2", TokenType.Integer);
     var expression = _factory.Create(token);
     Assert.IsInstanceOfType(expression, typeof(IntegerExpression));
 }
Пример #9
0
 private void BuildEnumerableExpression(Token[] tokens, Expression parent)
 {
     if (parent == null)
     {
         _graph.Add(new EnumerableExpression());
         BuildUp(tokens, _graph.Current);
     }
     else
     {
         var enumerableExpression = new EnumerableExpression();
         parent.AddChild(enumerableExpression);
         BuildUp(tokens, enumerableExpression);
     }
 }
Пример #10
0
 public void AddToken(Token token)
 {
     _result.Add(token);
 }
Пример #11
0
 public void ReplaceLastToken(Token newToken)
 {
     var count = _result.Count;
     if (count > 0)
     {
         _result.RemoveAt(count - 1);   
     }
     _result.Add(newToken);
 }
Пример #12
0
 private void EnsureStringsAreWellFormed(Token token, AnalyzingContext context)
 {
     if (!context.IsInString && token.TokenType == TokenType.String)
     {
         context.IsInString = true;
         context.OpenedStrings++;
     }
     else if (context.IsInString && token.TokenType == TokenType.String)
     {
         context.IsInString = false;
         context.ClosedStrings++;
     }
 }
Пример #13
0
        public UnrecognizedTokenException(Token token)
            : base( "Unrecognized token: " + token.Value)
        {

        }
Пример #14
0
 public void ReplaceLastToken(Token newToken)
 {
     if (_result.Count > 0)
     {
         _result.RemoveAt(_result.Count - 1);
     }
     _result.Add(newToken);
 }
Пример #15
0
 public void ShouldReturnDecimalExpressionWhenTokenIsDecimal()
 {
     var token = new Token("2.5", TokenType.Decimal);
     var expression = _factory.Create(token);
     Assert.IsInstanceOfType(expression, typeof(DecimalExpression));
 }
Пример #16
0
 private void CreateAndAppendExpression(ref Expression parent, Token token)
 {
     if (IsWaste(token)) return;
     if (parent != null && 
         (token.TokenType == TokenType.Comma || token.TokenType == TokenType.SemiColon))
     {
         parent = parent.PrepareForNextChild();
         return;
     }
     if (_negateNextExpression)
     {
         token.Negate();
         _negateNextExpression = false;
     }
     var expression = _expressionFactory.Create(token);
     if (parent == null)
     {
         _graph.Add(expression);
     }
     else
     {
         parent.AddChild(expression);
     }
 }
Пример #17
0
 public void ShouldReturnExcelRangeExpressionWhenTokenIsExcelAddress()
 {
     var token = new Token("A1", TokenType.ExcelAddress);
     var expression = _factory.Create(token);
     Assert.IsInstanceOfType(expression, typeof(ExcelAddressExpression));
 }
Пример #18
0
 private bool IsWaste(Token token)
 {
     if (token.TokenType == TokenType.String)
     {
         return true;
     }
     return false;
 }
Пример #19
0
 public void ShouldReturnNamedValueExpressionWhenTokenIsNamedValue()
 {
     var token = new Token("NamedValue", TokenType.NameValue);
     var expression = _factory.Create(token);
     Assert.IsInstanceOfType(expression, typeof(NamedValueExpression));
 }
Пример #20
0
 private void BuildFunctionExpression(Token[] tokens, Expression parent, string funcName)
 {
     if (parent == null)
     {
         _graph.Add(new FunctionExpression(funcName, _parsingContext, _negateNextExpression));
         _negateNextExpression = false;
         HandleFunctionArguments(tokens, _graph.Current);
     }
     else
     {
         var func = new FunctionExpression(funcName, _parsingContext, _negateNextExpression);
         _negateNextExpression = false;
         parent.AddChild(func);
         HandleFunctionArguments(tokens, func);
     }
 }
Пример #21
0
 private bool CharIsTokenSeparator(char c, out Token token)
 {
     var result = _tokenProvider.Tokens.ContainsKey(c.ToString());
     token = result ? token = _tokenProvider.Tokens[c.ToString()] : null;
     return result;
 }
Пример #22
0
 private static bool IsDoubleQuote(Token tokenSeparator, int formulaCharIndex, TokenizerContext context)
 {
     return tokenSeparator.TokenType == TokenType.String && formulaCharIndex + 1 < context.FormulaChars.Length && context.FormulaChars[formulaCharIndex + 1] == '\"';
 }