mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Use std::variant for lexer mmap/buffer state (#1328)
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <deque>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "platform.hpp" // SSIZE_MAX
|
||||
@@ -44,15 +45,18 @@ struct IfStackEntry {
|
||||
};
|
||||
|
||||
struct MmappedLexerState {
|
||||
union {
|
||||
char const *unreferenced;
|
||||
char *referenced; // Non-`const` only so it can be `munmap()`ped
|
||||
} ptr;
|
||||
char *ptr;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
bool isReferenced; // If a macro in this file requires not unmapping it
|
||||
};
|
||||
|
||||
struct ViewedLexerState {
|
||||
char const *ptr;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
struct BufferedLexerState {
|
||||
int fd;
|
||||
size_t index; // Read index into the buffer
|
||||
@@ -63,16 +67,6 @@ struct BufferedLexerState {
|
||||
struct LexerState {
|
||||
char const *path;
|
||||
|
||||
// mmap()-dependent IO state
|
||||
bool isMmapped;
|
||||
union {
|
||||
MmappedLexerState mmap; // If mmap()ed
|
||||
BufferedLexerState cbuf; // Otherwise
|
||||
};
|
||||
|
||||
// Common state
|
||||
bool isFile;
|
||||
|
||||
enum LexerMode mode;
|
||||
bool atLineStart;
|
||||
uint32_t lineNo;
|
||||
@@ -90,6 +84,13 @@ struct LexerState {
|
||||
size_t macroArgScanDistance; // Max distance already scanned for macro args
|
||||
bool expandStrings;
|
||||
std::deque<Expansion> expansions; // Front is the innermost current expansion
|
||||
|
||||
std::variant<
|
||||
std::monostate,
|
||||
MmappedLexerState,
|
||||
ViewedLexerState,
|
||||
BufferedLexerState
|
||||
> content;
|
||||
};
|
||||
|
||||
extern LexerState *lexerState;
|
||||
|
||||
Reference in New Issue
Block a user