⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 token.cs

📁 字符串表达式的计算。本程序是一个Window Forms应用程序
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace EvaluationEngine.Parser
{
    public enum ParseState
    {
        Parse_State_Operand,            // the parser is looking for an operand
        Parse_State_Operator,           // the parser is looking for an operator
        Parse_State_Quote,              // the parser is looking for a quote
        Parse_State_OperandFunction,    // the parser is in an operand function
        Parse_State_Comment             // the parser is in a comment
    };

    /// <summary>
    /// This enumeration represents the current state of the iif[] short circuit
    /// </summary>
    public enum IIFShortCircuitState
    {
        ShortCircuit_Condition,         // We are currently in the condition parameter of the short citcuit
        ShortCircuit_True,              // We are in the true parameter of the iif[] operand
        ShortCircuit_False              // We are in the false parameter of the iif[] operand
    }

    public class Token
    {
        #region Local Variables

        private Parser.TokenItems tokenItems = null;
        private Parser.Variables variables = new Variables();

        private Support.ExQueue<TokenItem> rpn_queue = null;

        private string ruleSyntax = "";
        private string lastErrorMessage = "";

        private double tokenParseTime = 0;
        private double lastEvaluationTime = 0;  // populated by the evaluator

        private int charIndex = 0;  // that index of the character that is being analyzed.  This also tells us the position of the error.

        private string lastEvaluationResult = "";  // this value gets populated by the evaluator object

        private TokenGroup tokenGroup = null;  // a reference to the token group (if any)

        private bool anyAssignments = false;  // indicates if the rule syntax has any assignments

        #endregion

        #region Public Constructor

        public Token(string RuleSyntax)
        {
            tokenItems = new TokenItems(this);
            ruleSyntax = RuleSyntax.Trim();
            lastErrorMessage = "";

            
            GetTokens();
        }

        public Token(System.IO.FileInfo Filename)
        {
            tokenItems = new TokenItems(this);

            string ErrorMsg = "";
            if (Open(Filename, out ErrorMsg) == false)
            {
                throw new Exception(ErrorMsg);
            }
        }

        #endregion

        #region Public Properties

        public string RuleSyntax
        {
            get
            {
                return ruleSyntax;
            }
        }

        public string LastErrorMessage
        {
            get
            {
                return lastErrorMessage;
            }
            set
            {
                lastErrorMessage = value;
            }
        }

        public bool AnyErrors
        {
            get
            {
                return (String.IsNullOrEmpty(lastErrorMessage) == false);
            }
        }

        public Parser.TokenItems TokenItems
        {
            get
            {
                return tokenItems;
            }
        }

        public Support.ExQueue<TokenItem> RPNQueue
        {
            get
            {
                return rpn_queue;
            }
        }

        public double TokenParseTime
        {
            get
            {
                return tokenParseTime;
            }
        }

        public Parser.Variables Variables
        {
            get
            {
                return variables;
            }
        }

        /// <summary>
        /// that index of the character that is being analyzed.  This also tells us the position of the error.
        /// </summary>
        public int CharIndex
        {
            get
            {
                return charIndex;
            }
        }

        /// <summary>
        /// this value gets populated by the evaluator object
        /// </summary>
        public string LastEvaluationResult
        {
            get
            {
                return lastEvaluationResult;
            }
            set
            {
                lastEvaluationResult = value;
            }
        }

        /// <summary>
        /// This property is populated by the evaluator
        /// </summary>
        public double LastEvaluationTime
        {
            get
            {
                return lastEvaluationTime;
            }
            set
            {
                lastEvaluationTime = value;
            }
        }

        /// <summary>
        /// This token object may be part of a token group object.  This is a reference to the token group (if any)
        /// </summary>
        public TokenGroup TokenGroup
        {
            get
            {
                return tokenGroup;
            }
            set
            {
                tokenGroup = value;
            }
        }


        /// <summary>
        /// Indicates if the RuleSyntax has any assignments.
        /// </summary>
        public bool AnyAssignments
        {
            get
            {
                return anyAssignments;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Creates TokenItem object and adds them to the collection
        /// </summary>
        /// <param name="CurrentToken"></param>
        /// <param name="WaitForCreation"></param>
        /// <param name="NextParseState"></param>
        /// <returns></returns>
        private bool CreateTokenItem(string CurrentToken, bool WaitForCreation, bool InOperandFunction, out ParseState NextParseState, out bool IsError, out string ErrorMsg)
        {
            // initialize the outgoing variable
            NextParseState = ParseState.Parse_State_Comment;
            ErrorMsg = "";
            IsError = false; // assume that no error occur

            // check for an empty token
            if (String.IsNullOrEmpty(CurrentToken) == true) return false;

            // clean the token
            CurrentToken = CurrentToken.Trim();

            // check for an empty clean token
            if (String.IsNullOrEmpty(CurrentToken) == true) return false;

            // local variables
            string tempOperand = "";
            string tempOperator = "";

            // check if the token is an operator
            if (Support.DataTypeCheck.IsOperator(CurrentToken) == true)
            {

                // add the operator, but check the next character first

                // the problem is, the next character in the token
                // may also make an operator.  for example, if the
                // current token is < we need to see if the next token is =                                

                tempOperator = CurrentToken;
                switch (CurrentToken)
                {
                    case "<":
                        // see if the next character is = or >
                        try
                        {
                            if (charIndex < ruleSyntax.Length - 1)
                            {
                                char nextChar = ruleSyntax[charIndex]; //charIndex has already been incremented

                                if (nextChar == '=')
                                {
                                    tempOperator += '=';  // adjust the current token
                                    charIndex++; // cause the '=' to be skipped on the next iteration
                                }
                                else if (nextChar == '>')
                                {
                                    tempOperator += '>';  // adjust the current token
                                    charIndex++; // cause the '>' to be skipped on the next iteration
                                }
                            }
                        }
                        catch (Exception err)
                        {
                            IsError = true;
                            ErrorMsg = "Error while determining if the next character is <, location = 1, Error message = " + err.Message;
                            return false;
                        }

                        break;

                    case ">":
                        try
                        {
                            // see if the next character is =
                            if (charIndex < ruleSyntax.Length - 1)
                            {
                                char nextChar = ruleSyntax[charIndex];//charIndex has already been incremented

                                if (nextChar == '=')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -