Use std::variant for lexer mmap/buffer state (#1328)

This commit is contained in:
Sylvie
2024-03-03 18:45:50 -05:00
committed by GitHub
parent 6b67c82b94
commit 8cf446b14c
2 changed files with 178 additions and 136 deletions

View File

@@ -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;