Ejemplo n.º 1
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        /// <summary>
        ///     Parse a string representation of a parametric function into an
        ///     expression tree that can be later evaluated.
        /// </summary>
        /// <param name="function">The function to parse</param>
        /// <returns>An expression tree representing the function parsed</returns>
        public static IExpression Parse(string function)
        {
            _tokenizer = new Tokenizer(function);
            _currentToken = new Token("", TokenType.None);

            if (!Next())
            {
                throw new InvalidExpressionException("Cannot parse an empty function");
            }
            var exp = ParseAddExpression();
            var leftover = string.Empty;
            while (_currentToken.Type != TokenType.Eof)
            {
                leftover += _currentToken.Value;
                Next();
            }
            if (!string.IsNullOrEmpty(leftover))
            {
                throw new TrailingTokensException("Trailing characters: " + leftover);
            }
            return exp;
        }
Ejemplo n.º 2
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        /// <summary>
        ///     Assign the next token in the queue to CurrentToken
        /// </summary>
        /// <returns>
        ///     true if there are still more tokens in the queue,
        ///     false if we have looked at all available tokens already
        /// </returns>
        private static bool Next()
        {
            if (_currentToken.Type == TokenType.Eof)
            {
                throw new OutOfTokensException("Parsed past the end of the function");
            }
            _currentToken = _tokenizer.Next();

            return _currentToken.Type != TokenType.Eof;
        }