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

📄 token.cs

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

                            // add the closed parenthesis and keep looking for the operator
                            tokenItems.Add(new TokenItem(")", TokenType.Token_Close_Parenthesis, false));
                            #endregion
                        }
                        else if (c == '[')
                        {
                            #region Operand Function Handling
                            // we found an open parenthesis while searching for an operator....that's a problem
                            lastErrorMessage = "Error in rule syntax: Found an open square parenthesis while searching for an operator";
                            return;
                            #endregion
                        }
                        else if (c == ']')
                        {
                            #region operand Function Handling
                            // we found an closed parenthesis while searching for an operator....that's a problem
                            lastErrorMessage = "Error in rule syntax: Found a closed square parenthesis while searching for an operator";
                            return;
                            #endregion
                        }
                        else if (c == ',')
                        {
                            #region Comma Handling
                            // we found a comma while searching for an operator....that's a problem
                            lastErrorMessage = "Error in rule syntax: Found a comma while searching for an operator";
                            return;
                            #endregion
                        }
                        else
                        {
                            #region Other Handling
                            // try and create the new token
                            currentToken += c;
                            tokenCreated = CreateTokenItem(currentToken, true, false, out tempParseState, out isError, out lastErrorMessage);
                            if (isError == true) return;
                            if (tokenCreated == true)
                            {
                                // the new tokens were created

                                // reset the current token
                                currentToken = "";

                                // set the next parse state
                                parseState = tempParseState;

                            }
                            #endregion
                        }
                        #endregion
                        break;

                    case ParseState.Parse_State_Quote:
                        #region Parse_State_Quote
                        if (c == '"')
                        {
                            #region Quote Handling
                            // we found the end of the qoute
                            quoteMatch--;
                            currentToken += c;
                            tokenCreated = CreateTokenItem(currentToken, false, false, out tempParseState, out isError, out lastErrorMessage);
                            if (isError == true) return;
                            if (tokenCreated == true)
                            {
                                // set the next parse state
                                parseState = tempParseState;

                                // clear the current token
                                currentToken = "";
                            }
                            #endregion
                        }
                        else
                        {
                            currentToken += c;
                        }

                        #endregion
                        break;
                }


            } while (charIndex < ruleSyntax.Length);


            #region Final Token Handling

            // see if we have a current token that needs to be processed.
            currentToken = currentToken.Trim();

            if (String.IsNullOrEmpty(currentToken) == false)
            {
                // we have a token that needs to be processed
                if (currentToken == "(")
                    parenthesisMatch++;
                else if (currentToken == ")")
                    parenthesisMatch--;
                else if (currentToken == "[")
                    squareMatch++;
                else if (currentToken == "]")
                    squareMatch--;
                else if (currentToken == "~")
                    tildaMatch--;
                else if (currentToken == "\"")
                    quoteMatch--;

                switch (parseState)
                {
                    case ParseState.Parse_State_Operand:
                        CreateTokenItem(currentToken, false, false, out tempParseState, out isError, out lastErrorMessage);
                        if (isError == true) return;
                        break;

                    case ParseState.Parse_State_Operator:
                        // there should never be an operator at the end of the rule syntax
                        lastErrorMessage = "Error in Rule Syntax: A rule cannot end with an operator.";
                        break;

                    case ParseState.Parse_State_Quote:
                        // we are looking for a closing quote
                        if (currentToken != "\"")
                        {
                            lastErrorMessage = "Error in RuleSyntax: Double quote mismatch.";
                            return;
                        }
                        else
                        {
                            // add the token as an operand
                            tokenItems.Add(new TokenItem(currentToken, TokenType.Token_Operand, TokenDataType.Token_DataType_String, false));
                        }
                        break;

                    case ParseState.Parse_State_OperandFunction:
                        break;

                }
            }

            #endregion

            #region RuleSyntax Validation Checks

            if (parenthesisMatch != 0)
            {
                lastErrorMessage = "Error in RuleSyntax: There is a parenthesis mismatch.";
                return;
            }

            if (squareMatch != 0)
            {
                lastErrorMessage = "Error in RuleSyntax: There is an operand function mismatch.";
                return;
            }

            if (operandFunctionDepth > 0)
            {
                lastErrorMessage = "Error in RuleSyntax: There is an operand function mismatch error...Operand function depth is not zero.";
                return;
            }

            if (tildaMatch != 0)
            {
                lastErrorMessage = "Error in RuleSyntax: There is a comment mismatch.";
                return;
            }

            if (quoteMatch != 0)
            {
                lastErrorMessage = "Error in RuleSyntax: There is a quote mismatch.";
                return;
            }

            if (charIndex < ruleSyntax.Length)
            {
                lastErrorMessage = "Error in RuleSyntax: There was a problem parsing the rule...some of the tokens were not found.";
                return;
            }

            #endregion

            // create the RPN Stack
            if (this.AnyErrors == false) MakeRPNQueue();

            // check that we have tokens in out RPN Queue
            if (rpn_queue == null)
            {
                lastErrorMessage = "Error in RuleSyntax: There was a problem creating the RPN queue.";
                return;
            }

            if (rpn_queue.Count == 0)
            {
                lastErrorMessage = "Error in RuleSyntax: There was a problem creating the RPN queue.";
                return;
            }

            // stop the timer
            parseTime.Stop();
            tokenParseTime = parseTime.Elapsed.TotalMilliseconds;

        }

        /// <summary>
        /// This is called by the constructor.  This new version includes support for assignment :=
        /// </summary>
        private void GetTokens_old_2()
        {
            // local variables
            ParseState parseState = ParseState.Parse_State_Operand;  // start be searching for an operand
            ParseState nextParseState = ParseState.Parse_State_Comment; // the next parse state after we have processed a comment            
            ParseState opFuncParseState = ParseState.Parse_State_Operand; // The parse state within the operand function
            ParseState tempParseState = ParseState.Parse_State_Operand; // temporary variable to hold a parse state
            string currentToken = "";


            int parenthesisMatch = 0;  // the number of open and close parenthesis should match
            int squareMatch = 0;  // the number of open and close square parenthesis should match
            int tildaMatch = 0; // the open and close tildas should match
            int quoteMatch = 0; // the open and close quotes should match
            int assignmentStartCount = 0;  // the number of times an assignment start operator was found;
            int assignmentStopCount = 0;   // the number of times an assignment stop operator was found
            bool isError = false;
            bool tokenCreated = false;

            int operandFunctionDepth = 0;  // operand function can contain operand function...This is the depth of the operand functions

            // make sure we have some syntax to parse
            if (String.IsNullOrEmpty(ruleSyntax) == true) return;

            // create a stop watch to time the parse
            System.Diagnostics.Stopwatch parseTime = System.Diagnostics.Stopwatch.StartNew();

            // start at the first character
            charIndex = 0;

            do
            {
                //  double check the exit condition, just in case
                if (charIndex >= ruleSyntax.Length) break;

                // the character to be processed
                char c = ruleSyntax[charIndex];

                // Increment the counter for the next character
                charIndex++;

                System.Diagnostics.Debug.WriteLine("c = " + c.ToString() + "\tcurrentToken = " + currentToken + "\tparse state = " + parseState.ToString() + "\tOp Parse State = " + opFuncParseState.ToString());

                #region New Line and Tab check
                // check for new line and tab
                if ((c == '\n') || (c == '\t'))
                {
                    // new lines and tabs are always ignored.
                    c = ' ';  // force the new line or tab to be a space and continue processing.
                }
                #endregion

                #region Comment Checking

                // check if we are in a comment
                if (parseState == ParseState.Parse_State_Comment)
                {
                    if (c == '~')
                    {
                        // we found the end of our comment, continue with what we were looking for
                        parseState = nextParseState;
                        tildaMatch--;
                    }

                    // continue with the next item in the loop
                    continue;
                }
                else
                {
                    if (c == '~')
                    {
                        // we found the start of a comment
                        tildaMatch++;
                        nextParseState = parseState;  // save the parse state
                        parseState = ParseState.Parse_State_Comment;
                        continue; // continue with the next item in the loop
                    }
                }
                #endregion

     

⌨️ 快捷键说明

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