/// <summary>
        /// Gets the next token while within a variable 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;

            if ((result = PerenthesisToken.TryParse(internalLine, out token) && ((PerenthesisToken)token).PerenthesisType == PerenthesisTokenType.Closed))
            {
                state = new PerenthesisTokenParseState();
            }
            else if ((result = InfixFunctionToken.TryParse(internalLine, out token) || ArithmeticOperatorToken.TryParse(internalLine, out token) || BinaryOperatorToken.TryParse(internalLine, out token) || BooleanOperatorToken.TryParse(internalLine, out token) || AssignmentOperatorToken.TryParse(internalLine, out token)))
            {
                state = new InfixOperationParseState();
            }
            else if ((result = PostfixFunctionToken.TryParse(internalLine, out token)))
            {
                state = new PostfixFunctionParseState();
            }
            else if ((result = CommentToken.TryParse(internalLine, out token)))
            {
                state = new NullTokenParseState();
            }
            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);
        }
Exemplo 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 a perenthesis 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 == ((PerenthesisToken)lastToken).PerenthesisType))
            {
                state = new PerenthesisTokenParseState();
            }
            else if ((result = CommentToken.TryParse(internalLine, out token)))
            {
                state = new NullTokenParseState();
            }
            else if (((PerenthesisToken)lastToken).PerenthesisType == PerenthesisTokenType.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);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets a Token of type PerenthesisToken from the beginning of a line of text.
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public new static Token Parse(string line)
        {
            Token token = new NullToken();

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

                token = new PerenthesisToken(matchText);

                if (matchText.Equals("(", StringComparison.InvariantCultureIgnoreCase))
                {
                    ((PerenthesisToken)token)._perenthesisType = PerenthesisTokenType.Open;
                }
                else
                {
                    ((PerenthesisToken)token)._perenthesisType = PerenthesisTokenType.Closed;
                }
            }

            return(token);
        }
        /// <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);
        }