/// <summary>
        /// Gets a Token of type LastResultToken from the beginning of a line of text. The resulting token
        /// needs to be a value token, like BooleanToken or NumberToken.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public new static Token Parse(string line)
        {
            Token token = new NullToken();

            if (LastResultToken.__constantPattern.IsMatch(line))
            {
                string matchText = LastResultToken.__constantPattern.Matches(line)[0].Value;

                if (matchText.Equals("$", StringComparison.InvariantCultureIgnoreCase))
                {
                    token = new LastResultToken(CalculatorContext.GetInstance().GetLastResult(), matchText);

                    Token intermediateToken = new NullToken();

                    if (NumberToken.TryParse(token.ToString(), out intermediateToken, true) ||
                        BooleanToken.TryParse(token.ToString(), out intermediateToken, true))
                    {
                        token = intermediateToken;
                    }
                }
            }

            return(token);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the next token while in the set container token parse state.
        /// </summary>
        /// <param name="internalLine"></param>
        /// <param name="token"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state)
        {
            bool  result    = false;
            Token lastToken = CalculatorContext.GetInstance()[CalculatorContext.LastToken];

            if ((result = SetContainerToken.TryParse(internalLine, out token) && ((SetContainerToken)token).SetContainerType == ((SetContainerToken)lastToken).SetContainerType))
            {
                state = new SetContainerTokenParseState();
            }
            else if ((result = CommentToken.TryParse(internalLine, out token)))
            {
                state = new NullTokenParseState();
            }
            else if (((SetContainerToken)lastToken).SetContainerType == SetContainerTokenType.Open)
            {
                if ((result = PrefixFunctionToken.TryParse(internalLine, out token)))
                {
                    state = new PrefixFunctionTokenParseState();
                }
                else if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token) || LastResultToken.TryParse(internalLine, out token)))
                {
                    state = new ValueTokenParseState();
                }
                else if ((result = VariableToken.TryParse(internalLine, out token)))
                {
                    state = new VariableTokenParseState();
                }
                else
                {
                    state = new InvalidTokenParseState();
                }
            }
            else if ((result = InfixFunctionToken.TryParse(internalLine, out token) || ArithmeticOperatorToken.TryParse(internalLine, out token) || BinaryOperatorToken.TryParse(internalLine, out token) || BooleanOperatorToken.TryParse(internalLine, out token)))
            {
                state = new InfixOperationParseState();
            }
            else if ((result = PostfixFunctionToken.TryParse(internalLine, out token)))
            {
                state = new PostfixFunctionParseState();
            }
            else
            {
                state = new InvalidTokenParseState();
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the next token while within ta prefix function token parse state.
        /// </summary>
        /// <param name="internalLine"></param>
        /// <param name="token"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state)
        {
            bool  result    = false;
            Token lastToken = CalculatorContext.GetInstance()[CalculatorContext.LastToken];

            if ((result = (PerenthesisToken.TryParse(internalLine, out token) && ((PerenthesisToken)token).PerenthesisType == PerenthesisTokenType.Open)))
            {
                state = new PerenthesisTokenParseState();
            }
            else if (lastToken.ToString().Equals("!", StringComparison.InvariantCultureIgnoreCase))
            {
                if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token)))
                {
                    state = new ValueTokenParseState();
                }
                else if ((result = VariableToken.TryParse(internalLine, out token)))
                {
                    state = new VariableTokenParseState();
                }
                else
                {
                    state = new InvalidTokenParseState();
                }
            }
            else
            {
                state = new InvalidTokenParseState();
            }

            return(result);
        }
        /// <summary>
        /// Gets the next token while within an infix operation parse state.
        /// </summary>
        /// <param name="internalLine"></param>
        /// <param name="token"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state)
        {
            bool result = false;

            if ((result = PrefixFunctionToken.TryParse(internalLine, out token)))
            {
                state = new PrefixFunctionTokenParseState();
            }
            else if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token) || LastResultToken.TryParse(internalLine, out token)))
            {
                state = new ValueTokenParseState();
            }
            else if ((result = VariableToken.TryParse(internalLine, out token)))
            {
                state = new VariableTokenParseState();
            }
            else if ((result = (PerenthesisToken.TryParse(internalLine, out token) && ((PerenthesisToken)token).PerenthesisType == PerenthesisTokenType.Open)))
            {
                state = new PerenthesisTokenParseState();
            }
            else if ((result = CommentToken.TryParse(internalLine, out token)))
            {
                state = new NullTokenParseState();
            }
            else
            {
                state = new InvalidTokenParseState();
            }

            return(result);
        }
        /// <summary>
        /// Gets the next Token if the last token was a NullToken. It first tries to get a normal,
        /// valid first token. If that fails, then it checks for a normal secondary token, and if
        /// that passes then the context LastResultIsImplied is set to true.
        /// </summary>
        /// <param name="internalLine"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state)
        {
            bool result = false;

            if ((result = PerenthesisToken.TryParse(internalLine, out token)))
            {
                state = new PerenthesisTokenParseState();
            }
            else if ((result = PrefixFunctionToken.TryParse(internalLine, out token)))
            {
                state = new PrefixFunctionTokenParseState();
            }
            else if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token) || LastResultToken.TryParse(internalLine, out token)))
            {
                state = new ValueTokenParseState();
            }
            else if ((result = VariableToken.TryParse(internalLine, out token)))
            {
                state = new VariableTokenParseState();
            }
            else if ((result = InfixFunctionToken.TryParse(internalLine, out token) || ArithmeticOperatorToken.TryParse(internalLine, out token) || BinaryOperatorToken.TryParse(internalLine, out token) || BooleanOperatorToken.TryParse(internalLine, out token)))
            {
                state = new InfixOperationParseState();
                CalculatorContext.GetInstance().LastResultIsImplied = true;
            }
            else if ((result = PostfixFunctionToken.TryParse(internalLine, out token)))
            {
                state = new PostfixFunctionParseState();
                CalculatorContext.GetInstance().LastResultIsImplied = true;
            }
            else
            {
                state = new InvalidTokenParseState();
            }

            return(result);
        }