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

Commit 18f5b5e

Browse files
committed
fix: store per lexer options instead of global
1 parent 61152b8 commit 18f5b5e

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

‎include/lbd/frontend/lexer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace frontend {
2626
size_t row = 1;
2727
size_t col = 1;
2828
const std::string filepath;
29+
const context::Options options;
2930

3031
[[nodiscard]] char peek() const;
3132

‎src/frontend/lexer.cpp‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <fstream>
66

77
namespace frontend {
8-
static context::Options optionsValue;
9-
108
char Lexer::peek() const {
119
return position < source.size() ? source[position] : '\0';
1210
}
@@ -34,22 +32,21 @@ namespace frontend {
3432
}
3533

3634
Lexer Lexer::fromFile(const std::string &filepath, const context::Options options) {
37-
std::ifstream fileStream(filepath);
38-
if (!fileStream) {
39-
optionsValue.logger.error({}, "IO error: could not open file ", filepath);
35+
std::ifstream inputFileStream(filepath);
36+
if (!inputFileStream) {
37+
options.logger.error({}, "IO error: could not open file ", filepath);
4038
}
41-
std::ostringstream stringStream;
42-
stringStream << fileStream.rdbuf();
43-
return Lexer(filepath, stringStream.str(), options);
39+
std::ostringstream outputStringStream;
40+
outputStringStream << inputFileStream.rdbuf();
41+
return Lexer(filepath, outputStringStream.str(), options);
4442
}
4543

4644
Lexer Lexer::fromRepl(const std::string &source, const context::Options options) {
4745
return Lexer("", source, options);
4846
}
4947

5048
Lexer::Lexer(std::string filepath, std::string source, const context::Options options) : source(std::move(source)),
51-
filepath(std::move(filepath)) {
52-
optionsValue = options;
49+
filepath(std::move(filepath)), options(options) {
5350
}
5451

5552
token::Token Lexer::nextToken() {
@@ -105,7 +102,7 @@ namespace frontend {
105102
}
106103
const std::string value = source.substr(start, position - start);
107104
if (currentCharacter != '"') {
108-
optionsValue.logger.error(currentLocation, "syntax error: unbalanced quote");
105+
options.logger.error(currentLocation, "syntax error: unbalanced quote");
109106
}
110107
get(); // Consume '"'.
111108
return {token::String{value}, currentLocation};
@@ -153,7 +150,7 @@ namespace frontend {
153150
default:
154151
break;
155152
}
156-
optionsValue.logger.error(currentLocation, "syntax error: unexpected character ", currentCharacter);
153+
options.logger.error(currentLocation, "syntax error: unexpected character ", currentCharacter);
157154
}
158155

159156
std::vector<token::Token> Lexer::lex() {

0 commit comments

Comments
 (0)