Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add token peeking to lexer #42

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int Lexer::getNextChar() {
return ch;
}

Result<Token> Lexer::Lex() {
Result<Token> Lexer::LexImpl() {
int ch = getNextChar();
pos_t row = row_, col = col_;

Expand Down
18 changes: 18 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ TEST(Lexer, HelloWorld) {
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::eof, 2, 20));
}

TEST(Lexer, Peek) {
std::stringstream ss(kHelloWorldStr);
xos::Lexer lexer(ss);

xos::Token expected(xos::Token::identifier, 1, 1, "main");

// We can peek as much as we want and see the same token.
ASSERT_EQ(lexer.Peek().get(), expected);
ASSERT_EQ(lexer.Peek().get(), expected);
ASSERT_EQ(lexer.Peek().get(), expected);
ASSERT_EQ(lexer.Lex().get(), expected);

xos::Token lparen(xos::Token::lparen, 1, 5);
ASSERT_EQ(lexer.Peek().get(), lparen);
ASSERT_EQ(lexer.Peek().get(), lparen);
ASSERT_EQ(lexer.Peek().get(), lparen);
}

TEST(Lexer, UnfinishedArrowToken) {
std::stringstream ss("=");
xos::Lexer lexer(ss);
Expand Down
24 changes: 23 additions & 1 deletion xos.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Token {
number,
};

Token() = default;
Token(Kind kind, pos_t row, pos_t col, std::string val)
: kind_(kind), start_{row, col}, val_(val) {}
Token(Kind kind, pos_t row, pos_t col) : kind_(kind), start_{row, col} {}
Expand Down Expand Up @@ -128,16 +129,37 @@ class Result {
class Lexer {
public:
Lexer(std::istream &input);
Result<Token> Lex();

Result<Token> Lex() {
if (has_lookahead_token_) {
has_lookahead_token_ = false;
return lookahead_token_;
}
return LexImpl();
}

Result<Token> Peek() {
if (!has_lookahead_token_) {
auto next = LexImpl();
if (next.hasError()) return next;

has_lookahead_token_ = true;
lookahead_token_ = next.get();
}
return lookahead_token_;
}

private:
int getNextChar();
Result<Token> LexImpl();

std::istream &input_;
pos_t row_ = 1, col_ = 0;

bool has_lookahead_ = false;
int lookahead_;
bool has_lookahead_token_ = false;
Token lookahead_token_;
};

namespace ast {
Expand Down
Loading