Skip to content

Commit

Permalink
Merge pull request #33 from dsLeks/getNextToken-to-Lex
Browse files Browse the repository at this point in the history
[NFC] s/getNextToken/Lex/
  • Loading branch information
dsLeks authored Dec 27, 2023
2 parents ee1a3f3 + c9c7b4b commit b78c9fe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
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::getNextToken() {
Result<Token> Lexer::Lex() {
int ch = getNextChar();
uint32_t row = row_, col = col_;

Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char **argv) {
std::ifstream input(argv[1]);
xos::Lexer lexer(input);
while (1) {
xos::Result<xos::Token> tok = lexer.getNextToken();
xos::Result<xos::Token> tok = lexer.Lex();
if (tok.hasError()) {
std::cerr << tok.getError() << std::endl;
return 1;
Expand Down
4 changes: 2 additions & 2 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace xos {

Result<std::unique_ptr<ast::Str>> Parser::parseStr() {
Result<Token> res = lexer_.getNextToken();
Result<Token> res = lexer_.Lex();
if (res.hasError()) {
return res;
}
Expand All @@ -29,7 +29,7 @@ Result<std::unique_ptr<ast::Str>> Parser::parseStr() {
}

std::unique_ptr<ast::Out> Parser::parseOut() {
Result<Token> tok = lexer_.getNextToken();
Result<Token> tok = lexer_.Lex();
if (tok.hasError()) return nullptr;

if (tok.get().getKind() != Token::out) return nullptr;
Expand Down
26 changes: 13 additions & 13 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ TEST(Lexer, HelloWorld) {
std::stringstream ss(kHelloWorldStr);
xos::Lexer lexer(ss);

ASSERT_EQ(lexer.getNextToken().get(),
ASSERT_EQ(lexer.Lex().get(),
xos::Token(xos::Token::identifier, 1, 1, "main"));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::lparen, 1, 5));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::lparen, 1, 7));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::rparen, 1, 8));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::arrow, 1, 10));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::lparen, 1, 13));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::rparen, 1, 14));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::rparen, 1, 16));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::out, 2, 3));
ASSERT_EQ(lexer.getNextToken().get(),
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::lparen, 1, 5));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::lparen, 1, 7));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::rparen, 1, 8));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::arrow, 1, 10));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::lparen, 1, 13));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::rparen, 1, 14));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::rparen, 1, 16));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::out, 2, 3));
ASSERT_EQ(lexer.Lex().get(),
xos::Token(xos::Token::string, 2, 7, "Hello World"));
ASSERT_EQ(lexer.getNextToken().get(), xos::Token(xos::Token::eof, 2, 20));
ASSERT_EQ(lexer.Lex().get(), xos::Token(xos::Token::eof, 2, 20));
}

TEST(Lexer, UnfinishedArrowToken) {
std::stringstream ss("=");
xos::Lexer lexer(ss);

auto result = lexer.getNextToken();
auto result = lexer.Lex();
ASSERT_TRUE(result.hasError());
ASSERT_EQ(result.getError(), "Expected '=>' at row 1, col 1");
}
Expand All @@ -42,7 +42,7 @@ TEST(Lexer, UnhandledCharacter) {
std::stringstream ss("%");
xos::Lexer lexer(ss);

auto result = lexer.getNextToken();
auto result = lexer.Lex();
ASSERT_TRUE(result.hasError());
ASSERT_EQ(result.getError(), "Unhandled character '%'");
}
Expand Down
2 changes: 1 addition & 1 deletion xos.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Result {
class Lexer {
public:
Lexer(std::istream &input);
Result<Token> getNextToken();
Result<Token> Lex();

private:
int getNextChar();
Expand Down

0 comments on commit b78c9fe

Please sign in to comment.