private void ScanToken() { char c = Advance(); switch (c) { case '(': AddToken(TokenType.LeftParen); break; case ')': AddToken(TokenType.RightParen); break; case '{': AddToken(TokenType.LeftBrace); break; case '}': AddToken(TokenType.RightBrace); 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.BangEqual : TokenType.Bang); break; case '=': AddToken(Match('=') ? TokenType.EqualEqual : TokenType.Equal); break; case '<': AddToken(Match('=') ? TokenType.LessEqual : TokenType.Less); break; case '>': AddToken(Match('=') ? TokenType.GreaterEqual : TokenType.Greater); break; case '/': if (Match('/')) { // a comment goes until the end of the line while (Peek() != '\n' && !IsAtEnd()) { Advance(); } } else { AddToken(TokenType.Slash); } break; case ' ': case '\r': case '\t': break; case '\n': line++; break; case '"': ParseString(); break; default: if (IsDigit(c)) { ParseNumber(); } else if (IsAlpha(c)) { ParseIdentifier(); } else { Lox.Error(line, string.Format("Unexpected character '{0}'.", c)); } break; } }
private void ScanToken() { char c = Advance(); switch (c) { // Single char tokens. 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.EQUAL_EQUAL : TokenType.EQUAL); break; case '<': AddToken(Match('=') ? TokenType.LESS_EQUAL : TokenType.LESS); break; case '>': AddToken(Match('=') ? TokenType.GREATER_EQUAL : TokenType.GREATER); break; case '/': if (Match('/')) { // A comment goes until the end of the line. // Use peek so we dont consume the newline token since we want it to go to its switch-case below while (Peek() != '\n' && !IsAtEnd()) { Advance(); } // Dont add the comment as token. } else { AddToken(TokenType.SLASH); } break; case ' ': case '\r': case '\t': // Ignore whitespace. break; case '\n': line++; break; case '"': String(); break; default: if (IsDigit(c)) { Number(); } else if (IsAlphabetical(c)) { Identifier(); } Lox.Error(line, "Unexpected character."); break; } }
private ParseError Error(Token token, string message) { Lox.Error(token, message); return(new ParseError()); }