mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use std::string for lexer state paths
This commit is contained in:
@@ -65,7 +65,7 @@ struct BufferedLexerState {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct LexerState {
|
struct LexerState {
|
||||||
char const *path;
|
std::string path;
|
||||||
|
|
||||||
LexerMode mode;
|
LexerMode mode;
|
||||||
bool atLineStart;
|
bool atLineStart;
|
||||||
@@ -115,7 +115,7 @@ static inline void lexer_SetGfxDigits(char const digits[4]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// `path` is referenced, but not held onto..!
|
// `path` is referenced, but not held onto..!
|
||||||
bool lexer_OpenFile(LexerState &state, char const *path);
|
bool lexer_OpenFile(LexerState &state, std::string const &path);
|
||||||
void lexer_OpenFileView(
|
void lexer_OpenFileView(
|
||||||
LexerState &state, char const *path, char const *buf, size_t size, uint32_t lineNo
|
LexerState &state, char const *path, char const *buf, size_t size, uint32_t lineNo
|
||||||
);
|
);
|
||||||
@@ -144,7 +144,6 @@ struct String {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void lexer_CheckRecursionDepth();
|
void lexer_CheckRecursionDepth();
|
||||||
char const *lexer_GetFileName();
|
|
||||||
uint32_t lexer_GetLineNo();
|
uint32_t lexer_GetLineNo();
|
||||||
uint32_t lexer_GetColNo();
|
uint32_t lexer_GetColNo();
|
||||||
void lexer_DumpStringExpansions();
|
void lexer_DumpStringExpansions();
|
||||||
|
|||||||
@@ -265,7 +265,7 @@ void fstk_RunInclude(std::string const &path) {
|
|||||||
uint32_t uniqueID = contextStack.top().uniqueID;
|
uint32_t uniqueID = contextStack.top().uniqueID;
|
||||||
|
|
||||||
Context &context = newContext(*fileInfo);
|
Context &context = newContext(*fileInfo);
|
||||||
if (!lexer_OpenFile(context.lexerState, fileInfo->name().c_str()))
|
if (!lexer_OpenFile(context.lexerState, fileInfo->name()))
|
||||||
fatalerror("Failed to set up lexer for file include\n");
|
fatalerror("Failed to set up lexer for file include\n");
|
||||||
lexer_SetStateAtEOL(&context.lexerState);
|
lexer_SetStateAtEOL(&context.lexerState);
|
||||||
// We're back at top-level, so most things are reset,
|
// We're back at top-level, so most things are reset,
|
||||||
@@ -293,7 +293,7 @@ static void runPreIncludeFile() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Context &context = newContext(*fileInfo);
|
Context &context = newContext(*fileInfo);
|
||||||
if (!lexer_OpenFile(context.lexerState, fileInfo->name().c_str()))
|
if (!lexer_OpenFile(context.lexerState, fileInfo->name()))
|
||||||
fatalerror("Failed to set up lexer for file include\n");
|
fatalerror("Failed to set up lexer for file include\n");
|
||||||
lexer_SetState(&context.lexerState);
|
lexer_SetState(&context.lexerState);
|
||||||
// We're back at top-level, so most things are reset
|
// We're back at top-level, so most things are reset
|
||||||
@@ -447,11 +447,11 @@ void fstk_NewRecursionDepth(size_t newDepth) {
|
|||||||
|
|
||||||
void fstk_Init(std::string const &mainPath, size_t maxDepth) {
|
void fstk_Init(std::string const &mainPath, size_t maxDepth) {
|
||||||
Context &context = contextStack.emplace();
|
Context &context = contextStack.emplace();
|
||||||
if (!lexer_OpenFile(context.lexerState, mainPath.c_str()))
|
if (!lexer_OpenFile(context.lexerState, mainPath))
|
||||||
fatalerror("Failed to open main file\n");
|
fatalerror("Failed to open main file\n");
|
||||||
lexer_SetState(&context.lexerState);
|
lexer_SetState(&context.lexerState);
|
||||||
|
|
||||||
context.fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, lexer_GetFileName());
|
context.fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, context.lexerState.path);
|
||||||
if (!context.fileInfo)
|
if (!context.fileInfo)
|
||||||
fatalerror("Failed to allocate memory for main file info: %s\n", strerror(errno));
|
fatalerror("Failed to allocate memory for main file info: %s\n", strerror(errno));
|
||||||
// lineNo and nbReptIters are unused on the top-level context
|
// lineNo and nbReptIters are unused on the top-level context
|
||||||
|
|||||||
@@ -48,10 +48,10 @@
|
|||||||
|
|
||||||
#define MAP_FAILED nullptr
|
#define MAP_FAILED nullptr
|
||||||
|
|
||||||
static void mapFile(void *&mappingAddr, int fd, char const *path, size_t) {
|
static void mapFile(void *&mappingAddr, int fd, std::string const &path, size_t) {
|
||||||
mappingAddr = MAP_FAILED;
|
mappingAddr = MAP_FAILED;
|
||||||
if (HANDLE file = CreateFileA(
|
if (HANDLE file = CreateFileA(
|
||||||
path,
|
path.c_str(),
|
||||||
GENERIC_READ,
|
GENERIC_READ,
|
||||||
FILE_SHARE_READ,
|
FILE_SHARE_READ,
|
||||||
nullptr,
|
nullptr,
|
||||||
@@ -76,14 +76,14 @@ static int munmap(void *mappingAddr, size_t) {
|
|||||||
#else // defined(_MSC_VER) || defined(__MINGW32__)
|
#else // defined(_MSC_VER) || defined(__MINGW32__)
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
static void mapFile(void *&mappingAddr, int fd, char const *path, size_t size) {
|
static void mapFile(void *&mappingAddr, int fd, std::string const &path, size_t size) {
|
||||||
mappingAddr = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
mappingAddr = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
if (mappingAddr == MAP_FAILED && errno == ENOTSUP) {
|
if (mappingAddr == MAP_FAILED && errno == ENOTSUP) {
|
||||||
// The implementation may not support MAP_PRIVATE; try again with MAP_SHARED
|
// The implementation may not support MAP_PRIVATE; try again with MAP_SHARED
|
||||||
// instead, offering, I believe, weaker guarantees about external modifications to
|
// instead, offering, I believe, weaker guarantees about external modifications to
|
||||||
// the file while reading it. That's still better than not opening it at all, though.
|
// the file while reading it. That's still better than not opening it at all, though.
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf("mmap(%s, MAP_PRIVATE) failed, retrying with MAP_SHARED\n", path);
|
printf("mmap(%s, MAP_PRIVATE) failed, retrying with MAP_SHARED\n", path.c_str());
|
||||||
mappingAddr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
|
mappingAddr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -378,23 +378,23 @@ void lexer_ReachELSEBlock() {
|
|||||||
lexerState->ifStack.front().reachedElseBlock = true;
|
lexerState->ifStack.front().reachedElseBlock = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool lexer_OpenFile(LexerState &state, char const *path) {
|
bool lexer_OpenFile(LexerState &state, std::string const &path) {
|
||||||
if (!strcmp(path, "-")) {
|
if (path == "-") {
|
||||||
state.path = "<stdin>";
|
state.path = "<stdin>";
|
||||||
state.content = BufferedLexerState{.fd = STDIN_FILENO, .index = 0, .buf = {}, .nbChars = 0};
|
state.content = BufferedLexerState{.fd = STDIN_FILENO, .index = 0, .buf = {}, .nbChars = 0};
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf("Opening stdin\n");
|
printf("Opening stdin\n");
|
||||||
} else {
|
} else {
|
||||||
struct stat fileInfo;
|
struct stat fileInfo;
|
||||||
if (stat(path, &fileInfo) != 0) {
|
if (stat(path.c_str(), &fileInfo) != 0) {
|
||||||
error("Failed to stat file \"%s\": %s\n", path, strerror(errno));
|
error("Failed to stat file \"%s\": %s\n", path.c_str(), strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
state.path = path;
|
state.path = path;
|
||||||
|
|
||||||
int fd = open(path, O_RDONLY);
|
int fd = open(path.c_str(), O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
error("Failed to open file \"%s\": %s\n", path, strerror(errno));
|
error("Failed to open file \"%s\": %s\n", path.c_str(), strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +403,7 @@ bool lexer_OpenFile(LexerState &state, char const *path) {
|
|||||||
if (fileInfo.st_size > 0) {
|
if (fileInfo.st_size > 0) {
|
||||||
// Try using `mmap` for better performance
|
// Try using `mmap` for better performance
|
||||||
void *mappingAddr;
|
void *mappingAddr;
|
||||||
mapFile(mappingAddr, fd, state.path, fileInfo.st_size);
|
mapFile(mappingAddr, fd, path, fileInfo.st_size);
|
||||||
|
|
||||||
if (mappingAddr != MAP_FAILED) {
|
if (mappingAddr != MAP_FAILED) {
|
||||||
close(fd);
|
close(fd);
|
||||||
@@ -414,7 +414,7 @@ bool lexer_OpenFile(LexerState &state, char const *path) {
|
|||||||
.isReferenced = false,
|
.isReferenced = false,
|
||||||
};
|
};
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf("File \"%s\" is mmap()ped\n", path);
|
printf("File \"%s\" is mmap()ped\n", path.c_str());
|
||||||
isMmapped = true;
|
isMmapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -424,9 +424,11 @@ bool lexer_OpenFile(LexerState &state, char const *path) {
|
|||||||
state.content = BufferedLexerState{.fd = fd, .index = 0, .buf = {}, .nbChars = 0};
|
state.content = BufferedLexerState{.fd = fd, .index = 0, .buf = {}, .nbChars = 0};
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
if (fileInfo.st_size == 0) {
|
if (fileInfo.st_size == 0) {
|
||||||
printf("File \"%s\" is empty\n", path);
|
printf("File \"%s\" is empty\n", path.c_str());
|
||||||
} else {
|
} else {
|
||||||
printf("File \"%s\" is opened; errno reports: %s\n", path, strerror(errno));
|
printf(
|
||||||
|
"File \"%s\" is opened; errno reports: %s\n", path.c_str(), strerror(errno)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -628,7 +630,7 @@ static size_t readInternal(BufferedLexerState &cbuf, size_t bufIndex, size_t nbC
|
|||||||
ssize_t nbReadChars = read(cbuf.fd, &cbuf.buf[bufIndex], nbChars);
|
ssize_t nbReadChars = read(cbuf.fd, &cbuf.buf[bufIndex], nbChars);
|
||||||
|
|
||||||
if (nbReadChars == -1)
|
if (nbReadChars == -1)
|
||||||
fatalerror("Error while reading \"%s\": %s\n", lexerState->path, strerror(errno));
|
fatalerror("Error while reading \"%s\": %s\n", lexerState->path.c_str(), strerror(errno));
|
||||||
|
|
||||||
// `nbReadChars` cannot be negative, so it's fine to cast to `size_t`
|
// `nbReadChars` cannot be negative, so it's fine to cast to `size_t`
|
||||||
return (size_t)nbReadChars;
|
return (size_t)nbReadChars;
|
||||||
@@ -815,10 +817,6 @@ static void handleCRLF(int c) {
|
|||||||
|
|
||||||
// "Services" provided by the lexer to the rest of the program
|
// "Services" provided by the lexer to the rest of the program
|
||||||
|
|
||||||
char const *lexer_GetFileName() {
|
|
||||||
return lexerState ? lexerState->path : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t lexer_GetLineNo() {
|
uint32_t lexer_GetLineNo() {
|
||||||
return lexerState->lineNo;
|
return lexerState->lineNo;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user