Esempio n. 1
0
        public static Token Scan()
        {
            //去除空白
            for (; !EofReached; ReadChar())
            {
                if (peek == ' ' || peek == '\t')
                {
                }
                else if (peek == '\r')
                {
                    ReadChar();
                    ++line;
                }
                else
                {
                    break;
                }
            }
            if (EofReached)
            {
                return(null);
            }

            //检测注释
            if (peek == '/')
            {
                ReadChar();
                switch (peek)
                {
                case '/': for (; ; ReadChar())
                    {
                        if (peek == '\r' || peek == '\uffff')
                        {
                            return(Scan());
                        }
                    }

                case '*':
                    for (ReadChar(); ; ReadChar())
                    {
                        switch (peek)
                        {
                        case '\r':
                            line++;
                            ReadChar();
                            break;

                        case '*':
                            ReadChar();
                            if (peek == '/')
                            {
                                ReadChar();
                                return(Scan());
                            }
                            break;

                        case '\uffff':
                            return(Scan());
                        }
                    }
                }
                return(new Token('/'));
            }

            //操作符
            switch (peek)
            {
            case '&':
                return(ReadChar('&') ? Word.And : new Token('&'));

            case '|':
                return(ReadChar('|') ? Word.Or : new Token('|'));

            case '=':
                return(ReadChar('=') ? Word.Equal : new Token('='));

            case '!':
                return(ReadChar('=') ? Word.NotEqual : new Token('!'));

            case '<':
                return(ReadChar('=') ? Word.Less : new Token('<'));

            case '>':
                return(ReadChar('=') ? Word.Greater : new Token('>'));

            case '"':
                var s = "";
                ReadChar();
                for (; ; ReadChar())
                {
                    switch (peek)
                    {
                    case '"':
                    {
                        ReadChar();
                        return(new String(s));
                    }

                    case '\n':
                    {
                        //Error("应输入\"\"\"");
                        ReadChar();
                        return(new String(s));
                    }
                    }
                    s = s + peek;
                }
            }

            //分析数字
            if (char.IsDigit(peek))
            {
                var val = 0;
                do
                {
                    val = 10 * val + (peek - '0'); ReadChar();
                } while (char.IsDigit(peek));
                if (peek != '.')
                {
                    return(new Int(val));
                }

                float float_val = val;
                for (float d = 10; ; d *= 10)
                {
                    ReadChar();
                    if (!char.IsDigit(peek))
                    {
                        break;
                    }
                    float_val += (peek - 48) / d;
                }
                return(new Float(float_val));
            }

            //分析标识符
            if (char.IsLetter(peek))
            {
                var b = new StringBuilder();
                do
                {
                    b.Append(peek);
                    ReadChar();
                } while (char.IsLetterOrDigit(peek));

                var s = b.ToString();
                if (key_words.ContainsKey(s))
                {
                    return(key_words[s]);
                }
                return(key_words[s] = new Word(s, Tag.ID));
            }

            //其他符合
            var tok = new Token(peek);

            if (!EofReached)
            {
                peek = ' ';
            }
            return(tok);
        }
Esempio n. 2
0
 private static void Reserve(Word word) => key_words.Add(word.lexeme, word);