예제 #1
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;
        }
예제 #2
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" );
            }
            IExpression exp = ParseAddExpression();
            string leftover = string.Empty;
            while ( CurrentToken.type != TokenType.EOF )
            {
                leftover += CurrentToken.value;
                Next();
            }
            if ( !string.IsNullOrEmpty( leftover ) )
            {
                throw new TrailingTokensException( "Trailing characters: " + leftover );
            }
            return exp;
        }