private void ScanToken() { var 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('/')) { // A comment goes until the end of the line. while (Peek() != '\n' && !AtEnd()) { Advance(); } } 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 (IsAlpha(c)) { Identifier(); } else { _lox.Error(_line, "Unexpected character."); } break; } }
private Option <Token> ScanToken() { char c = Advance(); switch (c) { case '(': return(GenerateToken(TokenType.LeftParen).Some()); case ')': return(GenerateToken(TokenType.RightParen).Some()); case '{': return(GenerateToken(TokenType.LeftBrace).Some()); case '}': return(GenerateToken(TokenType.RightBrace).Some()); case ',': return(GenerateToken(TokenType.Comma).Some()); case '.': return(GenerateToken(TokenType.Dot).Some()); case '-': return(GenerateToken(TokenType.Minus).Some()); case '+': return(GenerateToken(TokenType.Plus).Some()); case ';': return(GenerateToken(TokenType.SemiColon).Some()); case '*': return(GenerateToken(TokenType.Star).Some()); case '?': return(GenerateToken(TokenType.Question).Some()); case ':': return(GenerateToken(TokenType.Colon).Some()); // operators case '!': return(GenerateToken(Match('=') ? TokenType.BangEqual : TokenType.Bang).Some()); case '=': return(GenerateToken(Match('=') ? TokenType.EqualEqual : TokenType.Equal).Some()); case '<': return(GenerateToken(Match('=') ? TokenType.LessEqual : TokenType.Less).Some()); case '>': return(GenerateToken(Match('=') ? TokenType.GreaterEqual : TokenType.Greater).Some()); case '/': if (Match('/')) { while (Peek() != '\n' && !IsAtEnd()) { Advance(); } } else if (Match('*')) { MultiLine(); } else { return(GenerateToken(TokenType.Slash).Some()); } break; // whitespace case ' ': case '\r': case '\t': break; case '\n': _line++; break; case '"': return(String().Some()); // unrecognized character default: if (IsDigit(c)) { return(Number().Some()); } else if (IsAlpha(c)) { return(Identifier().Some()); } else { Lox.Error(_line, "Unexpected character."); break; } } return(Option.None <Token>()); }
private void ScanToken() { char c = Advance(); switch (c) { case '(': AddToken(LeftParen); break; case ')': AddToken(RightParen); break; case '{': AddToken(LeftBrace); break; case '}': AddToken(RightBrace); 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('=') ? BangEqual : Bang); break; case '=': AddToken(Match('=') ? EqualEqual : Equal); break; case '<': AddToken(Match('=') ? LessEqual : Less); break; case '>': AddToken(Match('=') ? GreaterEqual : Greater); break; case '/': if (Match('/')) { while (Peek() != '\n' && !IsAtEnd()) { Advance(); } } else { AddToken(Slash); } break; case ' ': case '\r': case '\t': 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; } }
private static ParseException Error(Token token, string message) { Lox.Error(token, message); return(new ParseException()); }