Beispiel #1
0
 public void PushToken(Token token)
 {
     if (token != null)
         this.tokenStack.Push(token);
 }
Beispiel #2
0
        private Token NextString()
        {
            string text = string.Empty;

            char ch;

            try
            {
                ch = this.NextChar();

                while (ch != '"')
                {
                    if (ch == '\\')
                        ch = this.NextChar();

                    text += ch;
                    ch = this.NextChar();
                }
            }
            catch (EndOfInputException)
            {
                throw new LexerException("Unclosed string");
            }

            Token token = new Token() { TokenType = TokenType.String, Value = text };

            return token;
        }
Beispiel #3
0
        private Token NextSymbol(char firstChar)
        {
            string name;

            name = firstChar.ToString();

            char ch;

            try
            {
                ch = this.NextChar();

                while (!char.IsWhiteSpace(ch) && !char.IsControl(ch) && SeparatorCharacters.IndexOf(ch) < 0)
                {
                    name += ch;
                    ch = this.NextChar();
                }

                this.PushChar(ch);
            }
            catch (EndOfInputException)
            {
            }

            Token token = new Token() { TokenType = TokenType.Symbol, Value = name };

            return token;
        }
Beispiel #4
0
        private Token NextMacro(char firstChar)
        {
            string name = firstChar.ToString();

            char ch;

            try
            {
                ch = this.NextChar();

                if (!char.IsWhiteSpace(ch))
                {
                    string newname = name + ch.ToString();
                    if (macroBicharacters.Contains(newname))
                        name = newname;
                    else
                        this.PushChar(ch);
                }
                else
                {
                    this.PushChar(ch);
                }
            }
            catch (EndOfInputException)
            {
            }

            Token token = new Token() { TokenType = TokenType.Macro, Value = name };

            return token;
        }
Beispiel #5
0
        private Token NextKeyword()
        {
            string name = string.Empty;

            char ch;

            try
            {
                ch = this.NextChar();

                while (!char.IsWhiteSpace(ch) && !char.IsControl(ch) && SeparatorCharacters.IndexOf(ch) < 0 && ch != '.')
                {
                    name += ch;
                    ch = this.NextChar();
                }

                this.PushChar(ch);
            }
            catch (EndOfInputException)
            {
            }

            Token token = new Token() { TokenType = TokenType.Keyword, Value = name };

            return token;
        }
Beispiel #6
0
        private Token NextInteger(char firstChar)
        {
            string integer;

            integer = new string(firstChar, 1);

            char ch;

            try
            {
                ch = this.NextChar();

                while (char.IsDigit(ch))
                {
                    integer += ch;
                    ch = this.NextChar();
                }

                this.PushChar(ch);
            }
            catch (EndOfInputException)
            {
            }

            Token token = new Token() { TokenType = TokenType.Integer, Value = integer };

            return token;
        }
Beispiel #7
0
        private Token NextCharacter()
        {
            Token token = new Token() { TokenType = TokenType.Character, Value = this.NextChar().ToString() };

            try
            {
                char ch = this.NextChar();

                if (!char.IsWhiteSpace(ch) && SeparatorCharacters.IndexOf(ch) == -1)
                    throw new LexerException("Invalid character");

                this.PushChar(ch);
            }
            catch (EndOfInputException)
            {
            }

            return token;
        }
Beispiel #8
0
        public void PushToken()
        {
            Lexer lexer = new Lexer(string.Empty);

            Token token = new Token { Value = "foo", TokenType = TokenType.Symbol };

            lexer.PushToken(token);

            Token retrieved = lexer.NextToken();

            Assert.IsNotNull(retrieved);
            Assert.AreEqual("foo", retrieved.Value);
            Assert.AreEqual(TokenType.Symbol, retrieved.TokenType);
        }