コード例 #1
0
        /// <summary>
        /// 读取第一个表达式
        /// </summary>
        /// <returns>ReadResult</returns>
        private ReadResult ReadFirstExpression()
        {
            ReadResult result = ReadResult.Empty;

            var token = spResult.Next();

            // 只列出可以出现在最左边的
            switch (token.Type)
            {
            case TokenType.Operator:
                if (OperatorWord.UnaryExpressions.ContainsKey(token.Text))
                {
                    result.Expression = OperatorWord.UnaryExpressions[token.Text](ReadExpression(OperatorPriority.GetOperatorLevel(token.Text)).Expression);
                }
                else if (token.Is(OperatorWord.LeftBracket))
                {
                    result = ParseConvertType();
                }
                else if (token.Is(OperatorWord.Comma))
                {
                    result = ReadExpression();
                }
                else
                {
                    throw new ArgumentException(string.Format("Invalid Left Operator {0}", token.Text));
                }
                break;

            case TokenType.Identifier:
                result = ParseIdentifier(token);
                break;

            case TokenType.DigitValue:
                result.Expression = Expression.Constant(DigitValue.Parse(token.Text), DigitValue.GetType(token.Text));
                break;

            case TokenType.PackageValue:
                // 去掉首尾引号
                if (token.Text.StartsWith(PackageValue.String))
                {
                    result.Expression = Expression.Constant(token.Text.Substring(1, token.Text.Length - 2), PackageValue.All[PackageValue.String]);
                }
                else if (token.Text.StartsWith(PackageValue.Char))
                {
                    result.Expression = Expression.Constant(token.Text[1], PackageValue.All[PackageValue.Char]);
                }
                break;

            default:
                throw new ArgumentException(string.Format("Unsupported TokenType {0}", token.Type));
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// 获取当前Token
        /// </summary>
        /// <returns>当前Token</returns>
        private Token CurrentToken()
        {
            while (Char.IsWhiteSpace(currentChar))
            {
                NextChar();
            }

            TokenType type;
            int       tokenPos = currentPosition;

            if (OperatorChar.Contains(currentChar))
            {
                type = TokenType.Operator;
                string mark = currentChar.ToString();
                // 处理双操作符
                NextChar();
                if (OperatorChar.Contains(currentChar))
                {
                    mark = mark + currentChar.ToString();
                    if (OperatorWord.Contains(mark))
                    {
                        NextChar();
                    }
                }
            }
            else if (Char.IsLetter(currentChar) || currentChar == MarkChar.Parameter || currentChar == MarkChar.Underline)
            {
                type = TokenType.Identifier;
                do
                {
                    NextChar();
                } while (Char.IsLetterOrDigit(currentChar) || currentChar == MarkChar.Underline || currentChar == OperatorChar.Question);
            }
            else if (PackageValue.Contains(currentChar))
            {
                type = TokenType.PackageValue;
                char mark = currentChar;
                do
                {
                    NextChar();
                } while (currentChar != mark && currentPosition < sourceLength);

                if (currentPosition == sourceLength)
                {
                    throw new ArgumentException(string.Format("PackageValue not matched to paired {0}", mark));
                }

                NextChar();
            }
            else if (Char.IsDigit(currentChar))
            {
                type = TokenType.DigitValue;
                do
                {
                    NextChar();
                } while (Char.IsDigit(currentChar));

                // 数值尾标
                if (DigitValue.Contains(currentChar))
                {
                    NextChar();
                }
                else if (currentChar == MarkChar.Dot)
                {
                    NextChar();
                    ValidateDigit();
                    do
                    {
                        NextChar();
                    } while (Char.IsDigit(currentChar));

                    // 指数
                    if (MarkChar.Exponent.Contains(currentChar))
                    {
                        NextChar();
                        if (currentChar == MarkChar.Positive || currentChar == MarkChar.Negative)
                        {
                            NextChar();
                        }

                        ValidateDigit();

                        while (Char.IsDigit(currentChar))
                        {
                            NextChar();
                        }
                    }

                    // 数值尾标
                    if (DigitValue.Contains(currentChar))
                    {
                        NextChar();
                    }
                }
            }
            else if (currentChar == MarkChar.End)
            {
                type = TokenType.End;
            }
            else
            {
                throw new ArgumentException(string.Format("Unsupported Char {0}", currentChar));
            }

            var text = source.Substring(tokenPos, currentPosition - tokenPos);

            return(new Token {
                Type = type, Text = text, Index = tokenPos
            });
        }