Skip to content

Commit

Permalink
Split the .peek() method into its next-char and lookahead cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Mar 28, 2024
1 parent 6f74e4f commit 106e516
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/asm/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ struct LexerState {

~LexerState();

int peek(uint8_t distance);
int peekChar();
int peekCharAhead();

std::shared_ptr<char[]> makeSharedCaptureBufPtr() const {
return std::shared_ptr<char[]>(captureBuf, captureBuf->data());
Expand Down
37 changes: 31 additions & 6 deletions src/asm/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,37 @@ static std::shared_ptr<std::string> readMacroArg(char name) {
}
}

int LexerState::peek(uint8_t distance) {
int LexerState::peekChar() {
// This is `.peekCharAhead()` modified for zero lookahead distance
for (Expansion &exp : expansions) {
if (exp.offset < exp.size())
return (uint8_t)(*exp.contents)[exp.offset];
}

if (auto *view = std::get_if<ViewedContent>(&content); view) {
if (view->offset < view->span.size)
return (uint8_t)view->span.ptr[view->offset];
} else {
assert(std::holds_alternative<BufferedContent>(content));
auto &cbuf = std::get<BufferedContent>(content);
if (cbuf.size == 0)
cbuf.refill();
assert(cbuf.offset < LEXER_BUF_SIZE);
if (cbuf.size > 0)
return (uint8_t)cbuf.buf[cbuf.offset];
}

// If there aren't enough chars, give up
return EOF;
}

int LexerState::peekCharAhead() {
// We only need one character of lookahead, for macro arguments
assert(distance == 0 || distance == 1);
uint8_t distance = 1;

for (Expansion &exp : expansions) {
// An expansion that has reached its end will have `exp.offset` == `exp.size()`,
// and `.peek()` will continue with its parent
// and `.peekCharAhead()` will continue with its parent
assert(exp.offset <= exp.size());
if (exp.offset + distance < exp.size())
return (uint8_t)(*exp.contents)[exp.offset + distance];
Expand All @@ -708,10 +732,11 @@ int LexerState::peek(uint8_t distance) {
auto &cbuf = std::get<BufferedContent>(content);
assert(distance < LEXER_BUF_SIZE);
if (cbuf.size <= distance)
cbuf.refill(); // Buffer isn't full enough, read some chars in
cbuf.refill();
if (cbuf.size > distance)
return (uint8_t)cbuf.buf[(cbuf.offset + distance) % LEXER_BUF_SIZE];
}

// If there aren't enough chars, give up
return EOF;
}
Expand All @@ -721,7 +746,7 @@ static void shiftChar();
static std::shared_ptr<std::string> readInterpolation(size_t depth);

static int peek() {
int c = lexerState->peek(0);
int c = lexerState->peekChar();

if (lexerState->macroArgScanDistance > 0)
return c;
Expand All @@ -731,7 +756,7 @@ static int peek() {
if (c == '\\' && !lexerState->disableMacroArgs) {
// If character is a backslash, check for a macro arg
lexerState->macroArgScanDistance++;
c = lexerState->peek(1);
c = lexerState->peekCharAhead();
if (isMacroChar(c)) {
shiftChar();
shiftChar();
Expand Down

0 comments on commit 106e516

Please sign in to comment.