public 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.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('/')) { while (Peek() != '\n' && !IsAtEnd()) { Advance(); } } else if (Match('*')) { while (!(Peek() != '*' && PeekNext() != '/') && !IsAtEnd()) { if (Peek() == '\n') { line++; } Advance(); } } else { AddToken(TokenType.SLASH); } break; case ' ': case '\r': case '\t': // Ignore whitespace. break; case '\n': line++; break; case '"': HandleString(); break; default: if (IsDigit(c)) { HandleNumber(); } else if (IsAlpha(c)) { Identifier(); } else { Lox.Error(line, "Unexpected Character."); } break; } }
private ParserErrorException Error(Token token, string message) { Lox.Error(token, message); return(new ParserErrorException()); }
private ParseError Error(Token token, string message) { Lox.Error(token, message); return(new ParseError()); }
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(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('/')) //We are inside of a comment discard the whole line { while (Peek() != '\n' && !IsAtEnd()) { Advance(); } } else { AddToken(SLASH); } break; case '"': ScanString(); break; case '\n': _line++; break; case ' ': case '\r': case '\t': // Ignore Whitespaces break; default: if (IsDigit(c)) { ScanNumber(); } else if (IsAlpha(c)) { ScanIdentifier(); } else { Lox.Error(_line, "Unexpected character."); } break; } }