Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 7650000

Browse files
committed
chore: new named-factory methods for lexer construction
1 parent c5c7717 commit 7650000

5 files changed

Lines changed: 30 additions & 30 deletions

File tree

‎include/lbd/frontend/lexer.h‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,25 @@
77
#include <vector>
88

99
namespace frontend {
10-
struct FromFile {
11-
};
12-
13-
struct FromRepl {
14-
};
15-
1610
struct Lexer {
17-
// TODO: Make factory instead of these constructors.
18-
Lexer(const std::string &filepath, FromFile, global::Options options_ = {});
11+
static Lexer fromFile(const std::string &filepath, global::Options options = {});
12+
13+
static Lexer fromRepl(const std::string &source, global::Options options = {});
1914

20-
Lexer(std::string str, FromRepl, global::Options options_ = {});
15+
// TODO: Add fromStdin
2116

2217
token::Token nextToken();
2318

24-
// TODO: Find a better name for this.
25-
std::vector<token::Token> lexAll();
19+
std::vector<token::Token> lex();
2620

2721
private:
22+
explicit Lexer(std::string filepath, std::string source, global::Options options = {});
23+
2824
std::string source;
2925
size_t position = 0;
3026
size_t row = 1;
3127
size_t col = 1;
32-
std::string filepath;
28+
const std::string filepath;
3329

3430
[[nodiscard]] char peek() const;
3531

‎src/frontend/lexer.cpp‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ namespace frontend {
3333
return {row, col, filepath};
3434
}
3535

36-
Lexer::Lexer(const std::string &filepath, FromFile, global::Options options_) : filepath(filepath) {
37-
std::ifstream ifs(filepath);
38-
optionsValue = options_;
39-
if (!ifs) {
36+
Lexer Lexer::fromFile(const std::string &filepath, const global::Options options) {
37+
std::ifstream fileStream(filepath);
38+
if (!fileStream) {
4039
optionsValue.logger.error({}, "IO error: could not open file ", filepath);
4140
}
42-
std::ostringstream ss;
43-
ss << ifs.rdbuf();
44-
source = ss.str();
41+
std::ostringstream stringStream;
42+
stringStream << fileStream.rdbuf();
43+
return Lexer(filepath, stringStream.str(), options);
4544
}
4645

47-
Lexer::Lexer(std::string str, FromRepl, const global::Options options_) : source(std::move(str)) {
48-
optionsValue = options_;
46+
Lexer Lexer::fromRepl(const std::string &source, const global::Options options) {
47+
return Lexer("", source, options);
48+
}
49+
50+
Lexer::Lexer(std::string filepath, std::string source, const global::Options options) : source(std::move(source)),
51+
filepath(std::move(filepath)) {
52+
optionsValue = options;
4953
}
5054

5155
token::Token Lexer::nextToken() {
@@ -152,7 +156,7 @@ namespace frontend {
152156
optionsValue.logger.error(currentLocation, "syntax error: unexpected character ", currentCharacter);
153157
}
154158

155-
std::vector<token::Token> Lexer::lexAll() {
159+
std::vector<token::Token> Lexer::lex() {
156160
std::vector<token::Token> tokens;
157161
while (true) {
158162
token::Token token = nextToken();

‎src/frontend/parser.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ namespace frontend {
152152
return;
153153
}
154154
loadedFiles.insert(absolutePath);
155-
auto lexerValue = Lexer(filepath, FromFile{}, optionsValue);
156-
const auto tokens = lexerValue.lexAll();
155+
auto lexerValue = Lexer::fromFile(filepath, optionsValue);
156+
const auto tokens = lexerValue.lex();
157157
if (optionsValue.debug) {
158158
for (const auto &token: tokens) {
159159
optionsValue.logger.debug(token);

‎src/main.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ int main(const int argc, char **argv) {
2020
repl::loop(debug);
2121
} else {
2222
// Lex
23-
auto lexerValue = frontend::Lexer(*filepath, frontend::FromFile{});
24-
const std::vector<frontend::token::Token> tokens = lexerValue.lexAll();
23+
auto lexerValue = frontend::Lexer::fromFile(*filepath);
24+
const std::vector<frontend::token::Token> tokens = lexerValue.lex();
2525
if (debug) {
2626
for (const frontend::token::Token &token: tokens) {
2727
std::cout << token << std::endl;

‎src/repl.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ namespace repl {
2525
subOptions.logger.error({}, "IO error: filepath ", filepath, " does not exist");
2626
}
2727

28-
frontend::Lexer lexer(filepath, frontend::FromFile{}, subOptions);
29-
const auto tokens = lexer.lexAll();
28+
frontend::Lexer lexer = frontend::Lexer::fromFile(filepath, subOptions);
29+
const auto tokens = lexer.lex();
3030
if (subOptions.debug) {
3131
for (const auto &token: tokens) {
3232
subOptions.logger.debug(token);
@@ -204,8 +204,8 @@ namespace repl {
204204
buffer.clear();
205205

206206
// Lex
207-
frontend::Lexer lexerValue(line, frontend::FromRepl{}, optionsValue);
208-
const std::vector<frontend::token::Token> tokens = lexerValue.lexAll();
207+
frontend::Lexer lexerValue = frontend::Lexer::fromRepl(line, optionsValue);
208+
const std::vector<frontend::token::Token> tokens = lexerValue.lex();
209209
if (optionsValue.debug) {
210210
for (const auto &tok: tokens) {
211211
optionsValue.logger.debug(tok);

0 commit comments

Comments
 (0)