Beispiel #1
0
        public Token GetNextToken()
        {
            if(!_currentChar.HasValue)
            {
                return new Token(Token.EOI);
            }

            if(Char.IsWhiteSpace(_currentChar.Value))
            {
                AdvancePos();
                return GetNextToken();
            }

            if(char.IsDigit(_currentChar.Value))
            {
                StringBuilder buffer = new StringBuilder();
                while(_currentChar.HasValue && Char.IsDigit(_currentChar.Value))
                {
                    buffer.Append(_currentChar.Value);
                    AdvancePos();
                }
                return new Token(Token.INTEGER, buffer.ToString());
            }

            Token token = null;
            switch(_currentChar.Value)
            {
                case '+':
                    token = new Token(Token.ADD);
                    break;
                case '-':
                    token = new Token(Token.SUBTRACT);
                    break;
                case '/':
                    token = new Token(Token.DIVIDE);
                    break;
                case '*':
                    token = new Token(Token.MULTIPLY);
                    break;
                case '(':
                    token = new Token(Token.LPAREN);
                    break;
                case ')':
                    token = new Token(Token.RPAREN);
                    break;
            }

            if(token != null)
            {
                token.Source = _currentChar.Value.ToString();
                AdvancePos();
                return token;
            }
            else
            {
                throw new InvalidOperationException($"Unknown character {_currentChar.Value}");
            }
        }
Beispiel #2
0
 public void Match(int tokenType)
 {
     if(_currentToken.Type == tokenType)
     {
         _currentToken = _lexer.GetNextToken();
     }
     else
     {
         throw new ArgumentException($"Invalid token type for this position of the equation. Expected {tokenType} but are parsing {_currentToken.Type}");
     }
 }
Beispiel #3
0
        public Token GetNextToken()
        {
            if(!_currentChar.HasValue)
            {
                return new Token(TokenType.EOF);
            }

            if(Char.IsWhiteSpace(_currentChar.Value))
            {
                AdvanceChar();
                return GetNextToken();
            }

            if(Char.IsDigit(_currentChar.Value))
            {
                string integerValue = string.Empty;
                while(_currentChar.HasValue && Char.IsNumber(_currentChar.Value))
                {
                    integerValue += _currentChar.ToString();
                    AdvanceChar();
                }
                return new Token(TokenType.INTEGER, integerValue);
            }

            Token knownToken = null;
            switch(_currentChar)
            {
                case '+':
                    knownToken = new Token(TokenType.PLUS, "+");
                    break;
                case '-':
                    knownToken = new Token(TokenType.MINUS, "-");
                    break;
                case '/':
                    knownToken = new Token(TokenType.DIVIDE, "/");
                    break;
                case '*':
                    knownToken = new Token(TokenType.MULTIPLY, "*");
                    break;
                case '(':
                    knownToken = new Token(TokenType.LPARAM, "(");
                    break;
                case ')':
                    knownToken = new Token(TokenType.RPARAM, ")");
                    break;
                default:
                    throw new ArgumentException($"Unable to parse {_currentChar.Value}");
            }

            AdvanceChar();
            return knownToken;
        }
Beispiel #4
0
 public Parser(Lexer lexer)
 {
     _lexer = lexer;
     _currentToken = _lexer.GetNextToken();
 }