mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Factor out a single consumeChar function
This and `peek` are the only functions that should call the low-level `peekChar` and `peekCharAhead` methods.
This commit is contained in:
@@ -780,6 +780,20 @@ static void shiftChar() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool consumeChar(int c) {
|
||||||
|
// This is meant to be called when the "extra" behavior of `peek()` is not wanted,
|
||||||
|
// e.g. painting the peeked-at character "blue".
|
||||||
|
if (lexerState->peekChar() != c) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment `lexerState->expansionScanDistance` to prevent `shiftChar()` from calling
|
||||||
|
// `peek()` and to balance its decrement.
|
||||||
|
++lexerState->expansionScanDistance;
|
||||||
|
shiftChar();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static int bumpChar() {
|
static int bumpChar() {
|
||||||
int c = peek();
|
int c = peek();
|
||||||
if (c != EOF) {
|
if (c != EOF) {
|
||||||
@@ -1214,15 +1228,9 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth) {
|
|||||||
FormatSpec fmt{};
|
FormatSpec fmt{};
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// If `lexerState->disableExpansions` is false, `peek()` will expand nested interpolations
|
// Use `consumeChar()` since `peek()` might expand nested interpolations and recursively
|
||||||
// and recursively call `readInterpolation()`, which can cause stack overflow.
|
// call `readInterpolation()`, which can cause stack overflow.
|
||||||
// `lexerState->peekChar()` lets `readInterpolation()` handle its own nested expansions,
|
if (consumeChar('{')) {
|
||||||
// increasing `depth` each time.
|
|
||||||
if (lexerState->peekChar() == '{') {
|
|
||||||
// Increment `lexerState->expansionScanDistance` to prevent `shiftChar()` from calling
|
|
||||||
// `peek()` and to balance its decrement.
|
|
||||||
++lexerState->expansionScanDistance;
|
|
||||||
shiftChar();
|
|
||||||
if (std::shared_ptr<std::string> str = readInterpolation(depth + 1); str) {
|
if (std::shared_ptr<std::string> str = readInterpolation(depth + 1); str) {
|
||||||
beginExpansion(str, *str);
|
beginExpansion(str, *str);
|
||||||
}
|
}
|
||||||
@@ -1428,17 +1436,13 @@ static void readString(std::string &str, bool rawString) {
|
|||||||
str += '"';
|
str += '"';
|
||||||
}
|
}
|
||||||
shiftChar();
|
shiftChar();
|
||||||
// `peek()` would mark the third character here as "painted blue" whether or not it is a
|
// Use `consumeChar()` since `peek()` would mark the third character here as "painted blue"
|
||||||
// third quote, which would incorrectly prevent expansions right after an empty string "".
|
// whether or not it is a third quote, which would incorrectly prevent expansions right
|
||||||
// `lexerState->peekChar()` avoids this, and is okay since expansions are disabled here.
|
// after an empty string "".
|
||||||
if (lexerState->peekChar() != '"') {
|
if (!consumeChar('"')) {
|
||||||
// "" is an empty string, skip the loop
|
// "" is an empty string, skip the loop
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Increment `lexerState->expansionScanDistance` to prevent `shiftChar()` from calling
|
|
||||||
// `peek()` and to balance its decrement.
|
|
||||||
++lexerState->expansionScanDistance;
|
|
||||||
shiftChar();
|
|
||||||
// """ begins a multi-line string
|
// """ begins a multi-line string
|
||||||
if (rawMode) {
|
if (rawMode) {
|
||||||
str += '"';
|
str += '"';
|
||||||
|
|||||||
Reference in New Issue
Block a user