Use nextChar() for shiftChar() and then peek()

This commit is contained in:
Rangi42
2025-07-20 11:15:06 -04:00
parent 169ac61e14
commit 68ffb01cac

View File

@@ -600,6 +600,7 @@ static bool isMacroChar(char c) {
static int peek(); static int peek();
static void shiftChar(); static void shiftChar();
static int bumpChar(); static int bumpChar();
static int nextChar();
static uint32_t readDecimalNumber(int initial); static uint32_t readDecimalNumber(int initial);
static uint32_t readBracketedMacroArgNum() { static uint32_t readBracketedMacroArgNum() {
@@ -619,8 +620,7 @@ static uint32_t readBracketedMacroArgNum() {
bool negative = c == '-'; bool negative = c == '-';
if (negative) { if (negative) {
shiftChar(); c = nextChar();
c = peek();
} }
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
@@ -632,8 +632,7 @@ static uint32_t readBracketedMacroArgNum() {
num = negative ? -n : static_cast<int32_t>(n); num = negative ? -n : static_cast<int32_t>(n);
} else if (startsIdentifier(c) || c == '#') { } else if (startsIdentifier(c) || c == '#') {
if (c == '#') { if (c == '#') {
shiftChar(); c = nextChar();
c = peek();
if (!startsIdentifier(c)) { if (!startsIdentifier(c)) {
error("Empty raw symbol in bracketed macro argument"); error("Empty raw symbol in bracketed macro argument");
return 0; return 0;
@@ -641,9 +640,8 @@ static uint32_t readBracketedMacroArgNum() {
} }
std::string symName; std::string symName;
for (; continuesIdentifier(c); c = peek()) { for (; continuesIdentifier(c); c = nextChar()) {
symName += c; symName += c;
shiftChar();
} }
Symbol const *sym = sym_FindScopedValidSymbol(symName); Symbol const *sym = sym_FindScopedValidSymbol(symName);
@@ -667,8 +665,7 @@ static uint32_t readBracketedMacroArgNum() {
empty = true; empty = true;
} }
c = peek(); c = bumpChar();
shiftChar();
if (c != '>') { if (c != '>') {
error("Invalid character in bracketed macro argument %s", printChar(c)); error("Invalid character in bracketed macro argument %s", printChar(c));
return 0; return 0;
@@ -801,7 +798,6 @@ int LexerState::peekCharAhead() {
} }
// forward declarations for peek // forward declarations for peek
static void shiftChar();
static std::shared_ptr<std::string> readInterpolation(size_t depth); static std::shared_ptr<std::string> readInterpolation(size_t depth);
static int peek() { static int peek() {
@@ -887,11 +883,16 @@ static int bumpChar() {
return c; return c;
} }
static int nextChar() {
shiftChar();
return peek();
}
template<typename P> template<typename P>
static int skipChars(P predicate) { static int skipChars(P predicate) {
int c = peek(); int c = peek();
for (; predicate(c); c = peek()) { while (predicate(c)) {
shiftChar(); c = nextChar();
} }
return c; return c;
} }
@@ -1002,14 +1003,11 @@ static void discardLineContinuation() {
// Functions to read tokenizable values // Functions to read tokenizable values
static std::string readAnonLabelRef(char c) { static std::string readAnonLabelRef(char c) {
uint32_t n = 0;
// We come here having already peeked at one char, so no need to do it again // We come here having already peeked at one char, so no need to do it again
do { uint32_t n = 1;
shiftChar(); while (nextChar() == c) {
++n; ++n;
} while (peek() == c); }
return sym_MakeAnonLabelName(n, c == '-'); return sym_MakeAnonLabelName(n, c == '-');
} }
@@ -1022,9 +1020,7 @@ static uint32_t readFractionalPart(uint32_t integer) {
READFRACTIONALPART_PRECISION_DIGITS, READFRACTIONALPART_PRECISION_DIGITS,
} state = READFRACTIONALPART_DIGITS; } state = READFRACTIONALPART_DIGITS;
for (;; shiftChar()) { for (int c = peek();; c = nextChar()) {
int c = peek();
if (state == READFRACTIONALPART_DIGITS) { if (state == READFRACTIONALPART_DIGITS) {
if (c == '_') { if (c == '_') {
continue; continue;
@@ -1123,8 +1119,7 @@ static uint32_t readBinaryNumber() {
uint32_t value = 0; uint32_t value = 0;
bool empty = true; bool empty = true;
for (;; shiftChar()) { for (int c = peek();; c = nextChar()) {
int c = peek();
int bit; int bit;
if (c == '_' && !empty) { if (c == '_' && !empty) {
@@ -1155,9 +1150,7 @@ static uint32_t readOctalNumber() {
uint32_t value = 0; uint32_t value = 0;
bool empty = true; bool empty = true;
for (;; shiftChar()) { for (int c = peek();; c = nextChar()) {
int c = peek();
if (c == '_' && !empty) { if (c == '_' && !empty) {
continue; continue;
} else if (c >= '0' && c <= '7') { } else if (c >= '0' && c <= '7') {
@@ -1185,9 +1178,7 @@ static uint32_t readDecimalNumber(int initial) {
uint32_t value = initial ? initial - '0' : 0; uint32_t value = initial ? initial - '0' : 0;
bool empty = !initial; bool empty = !initial;
for (;; shiftChar()) { for (int c = peek();; c = nextChar()) {
int c = peek();
if (c == '_' && !empty) { if (c == '_' && !empty) {
continue; continue;
} else if (c >= '0' && c <= '9') { } else if (c >= '0' && c <= '9') {
@@ -1215,9 +1206,7 @@ static uint32_t readHexNumber() {
uint32_t value = 0; uint32_t value = 0;
bool empty = true; bool empty = true;
for (;; shiftChar()) { for (int c = peek();; c = nextChar()) {
int c = peek();
if (c == '_' && !empty) { if (c == '_' && !empty) {
continue; continue;
} else if (c >= 'a' && c <= 'f') { } else if (c >= 'a' && c <= 'f') {
@@ -1249,8 +1238,7 @@ static uint32_t readGfxConstant() {
uint32_t bitPlaneLower = 0, bitPlaneUpper = 0; uint32_t bitPlaneLower = 0, bitPlaneUpper = 0;
uint8_t width = 0; uint8_t width = 0;
for (;; shiftChar()) { for (int c = peek();; c = nextChar()) {
int c = peek();
uint32_t pixel; uint32_t pixel;
if (c == '_' && width > 0) { if (c == '_' && width > 0) {
@@ -1294,9 +1282,7 @@ static Token readIdentifier(char firstChar, bool raw) {
int tokenType = firstChar == '.' ? T_(LOCAL) : T_(SYMBOL); int tokenType = firstChar == '.' ? T_(LOCAL) : T_(SYMBOL);
// Continue reading while the char is in the identifier charset // Continue reading while the char is in the identifier charset
for (int c = peek(); continuesIdentifier(c); c = peek()) { for (int c = peek(); continuesIdentifier(c); c = nextChar()) {
shiftChar();
// Write the char to the identifier's name // Write the char to the identifier's name
identifier += c; identifier += c;
@@ -1548,11 +1534,10 @@ static void readString(std::string &str, bool rawString) {
str += '"'; str += '"';
} }
if (peek() == '"') { if (peek() == '"') {
shiftChar();
if (rawMode) { if (rawMode) {
str += '"'; str += '"';
} }
if (peek() != '"') { if (nextChar() != '"') {
// "" is an empty string, skip the loop // "" is an empty string, skip the loop
return; return;
} }
@@ -1606,8 +1591,7 @@ static void readString(std::string &str, bool rawString) {
str += c; str += c;
continue; continue;
} }
shiftChar(); if (nextChar() != '"') {
if (peek() != '"') {
str += "\"\""; str += "\"\"";
continue; continue;
} }
@@ -2054,8 +2038,7 @@ static Token yylex_RAW() {
if (isWhitespace(c)) { if (isWhitespace(c)) {
shiftChar(); shiftChar();
} else if (c == '\\') { } else if (c == '\\') {
shiftChar(); c = nextChar();
c = peek();
// If not a line continuation, handle as a normal char // If not a line continuation, handle as a normal char
if (!isWhitespace(c) && c != '\n' && c != '\r') { if (!isWhitespace(c) && c != '\n' && c != '\r') {
goto backslash; goto backslash;
@@ -2083,8 +2066,7 @@ static Token yylex_RAW() {
case '#': // Raw string literals inside macro args case '#': // Raw string literals inside macro args
str += c; str += c;
shiftChar(); if (nextChar() == '"') {
if (peek() == '"') {
shiftChar(); shiftChar();
readString(str, true); readString(str, true);
} }
@@ -2100,8 +2082,7 @@ static Token yylex_RAW() {
goto finish; goto finish;
case '/': // Block comments inside macro args case '/': // Block comments inside macro args
shiftChar(); if (nextChar() == '*') {
if (peek() == '*') {
shiftChar(); shiftChar();
discardBlockComment(); discardBlockComment();
continue; continue;
@@ -2128,8 +2109,7 @@ static Token yylex_RAW() {
goto append; goto append;
case '\\': // Character escape case '\\': // Character escape
shiftChar(); c = nextChar();
c = peek();
backslash: backslash:
switch (c) { switch (c) {