Beispiel #1
0
 private ParseError error(Token token, String message)
 {
     Lox.error(token, message);
     return(new ParseError());
 }
Beispiel #2
0
        private void ScanToken()
        {
            var c = Advance();

            switch (c)
            {
            case '(': AddToken(LEFT_PAREN); break;

            case ')': AddToken(RIGHT_PAREN); break;

            case '{': AddToken(LEFT_BRACE); break;

            case '}': AddToken(RIGHT_BRACE); break;

            case ',': AddToken(COMMA); break;

            case '.': AddToken(DOT); break;

            case '-': AddToken(MINUS); break;

            case '+': AddToken(PLUS); break;

            case ';': AddToken(SEMICOLON); break;

            case '*': AddToken(STAR); break;

            case '!':
                AddToken(Match('=') ? BANG_EQUAL : BANG);
                break;

            case '=':
                AddToken(Match('=') ? EQUAL_EQUAL : EQUAL);
                break;

            case '<':
                AddToken(Match('=') ? LESS_EQUAL : LESS);
                break;

            case '>':
                AddToken(Match('=') ? GREATER_EQUAL : GREATER);
                break;

            case '/':
                if (Match('/'))
                {
                    // A comment goes until the end of the line.
                    while (Peek() != '\n' && !IsAtEnd())
                    {
                        Advance();
                    }
                }
                else
                {
                    AddToken(SLASH);
                }
                break;

            case ' ':
            case '\r':
            case '\t':
                // Ignore whitespace.
                break;

            case '\n':
                _line++;
                break;

            case '"': String(); break;

            default:
                if (IsDigit(c))
                {
                    Number();
                }
                else if (IsAlpha(c))
                {
                    Identifier();
                }
                else
                {
                    Lox.Error(_line, "Unexpected character.");
                }
                break;
            }
        }
Beispiel #3
0
        private void scanToken()
        {
            char c = advance();

            switch (c)
            {
            case '(': addToken(TokenType.LEFT_PAREN); break;

            case ')': addToken(TokenType.RIGHT_PAREN); break;

            case '{': addToken(TokenType.LEFT_BRACE); break;

            case '}': addToken(TokenType.RIGHT_BRACE); break;

            case ',': addToken(TokenType.COMMA); break;

            case '.': addToken(TokenType.DOT); break;

            case '-': addToken(TokenType.MINUS); break;

            case '+': addToken(TokenType.PLUS); break;

            case ';': addToken(TokenType.SEMICOLON); break;

            case '*': addToken(TokenType.STAR); break;

            case '!': addToken(match('=') ? TokenType.BANG_EQUAL : TokenType.BANG); break;

            case '<': addToken(match('=') ? TokenType.LESS_EQUAL : TokenType.LESS); break;

            case '>': addToken(match('=') ? TokenType.GREATER_EQUAL : TokenType.GREATER); break;

            case '=': addToken(match('=') ? TokenType.EQUAL_EQUAL : TokenType.EQUAL); break;

            case '/':
                if (match('/'))
                {
                    while (peek() != '\n' && !isAtEnd())
                    {
                        advance();
                    }
                }
                else
                {
                    addToken(TokenType.SLASH);
                }
                break;     // todo

            case '"':
                int closingPos = closingMatchPos('"');
                if (closingPos > 0)
                {
                    string literal = source.Substring(current, closingPos - 1);
                    addToken(TokenType.STRING, literal);
                    current += closingPos;
                }
                else
                {
                    Lox.Error(line, $"Missing closing \"");
                }

                break;

            case 'f':
                // If we get an f, check if we get a n followed by a space
                if (match('n'))
                {
                    if (match(' '))
                    {
                        addToken(TokenType.FUN);
                    }
                }
                break;

            case '\t': break;

            case '\r': break;

            case '\n': line++; break;

            case ' ': break;

            default: Lox.Error(line, $"Unexpected character {c}:{(byte)c}"); break;
            }
        }
Beispiel #4
0
        private void ScanToken()
        {
            char c = Advance();

            switch (c)
            {
            case '(': AddToken(LEFT_PAREN); break;

            case ')': AddToken(RIGHT_PAREN); break;

            case '{': AddToken(LEFT_BRACE); break;

            case '}': AddToken(RIGHT_BRACE); break;

            case ',': AddToken(COMMA); break;

            case '.': AddToken(DOT); break;

            case '-': AddToken(MINUS); break;

            case '+': AddToken(PLUS); break;

            case ';': AddToken(SEMICOLON); break;

            case '*': AddToken(STAR); break;

            case ':': AddToken(COLON); break;

            case '?': AddToken(QUESTION_MARK); break;

            case '!': AddToken(Match('=') ? BANG_EQUAL : BANG); break;

            case '=': AddToken(Match('=') ? EQUAL_EQUAL : EQUAL); break;

            case '<': AddToken(Match('=') ? LESS_EQUAL : LESS); break;

            case '>': AddToken(Match('=') ? GREATER_EQUAL : GREATER); break;

            case '/':
                if (Match('/'))
                {
                    // eat the whole line
                    while (Peek() != '\n' && !IsAtEnd())
                    {
                        Advance();
                    }
                }
                else if (Match('*'))
                {
                    ParseBlockComment();
                }
                else
                {
                    AddToken(SLASH);
                }
                break;

            case ' ':
            case '\r':
            case '\t':
                // ignore whitespace
                break;

            case '\n':
                line++;
                break;

            case '"': ParseString(); break;

            default:
                if (IsDigit(c))
                {
                    ParseNumber();
                }
                else if (IsAlpha(c))
                {
                    ParseIdentifier();
                }
                else
                {
                    Lox.Error(line, "Unexpected character.");
                }
                break;
            }
        }
Beispiel #5
0
 private ParseError Error(Token token, string msg)
 {
     Lox.Error(token, msg);
     return(new ParseError(token, msg));
 }
Beispiel #6
0
 private static ParseError Error(Token token, string message)
 {
     Lox.Error(token, message);
     return(new ParseError());
 }