shiftChars(1) => shiftChar()

Only two sites were for distances greater than 1:
a `shiftChars(2)`, trivial to just do two `shiftChar()`s;
and `shiftChars(size)` in `reportGarbageChar`, which
can be a `for` loop, and should be fixed anyway to
"avoid having to peek further than 0".
This commit is contained in:
Rangi
2021-03-26 17:00:45 -04:00
committed by Eldred Habert
parent 260d372acd
commit 9558ccea1b

View File

@@ -865,7 +865,7 @@ static int peekInternal(uint8_t distance)
} }
/* forward declarations for peek */ /* forward declarations for peek */
static void shiftChars(uint8_t distance); static void shiftChar(void);
static char const *readInterpolation(void); static char const *readInterpolation(void);
static int peek(uint8_t distance) static int peek(uint8_t distance)
@@ -889,7 +889,8 @@ restart:
* expanded, so skip it and keep peeking. * expanded, so skip it and keep peeking.
*/ */
if (!str[0]) { if (!str[0]) {
shiftChars(2); shiftChar();
shiftChar();
goto restart; goto restart;
} }
@@ -909,7 +910,7 @@ restart:
} }
} else if (c == '{' && !lexerState->disableInterpolation) { } else if (c == '{' && !lexerState->disableInterpolation) {
/* If character is an open brace, do symbol interpolation */ /* If character is an open brace, do symbol interpolation */
shiftChars(1); shiftChar();
char const *ptr = readInterpolation(); char const *ptr = readInterpolation();
if (ptr) { if (ptr) {
@@ -922,24 +923,24 @@ restart:
return c; return c;
} }
static void shiftChars(uint8_t distance) static void shiftChar(void)
{ {
if (lexerState->capturing) { if (lexerState->capturing) {
if (lexerState->captureBuf) { if (lexerState->captureBuf) {
if (lexerState->captureSize + distance >= lexerState->captureCapacity) if (lexerState->captureSize + 1 >= lexerState->captureCapacity)
reallocCaptureBuf(); reallocCaptureBuf();
/* TODO: improve this? */ /* TODO: improve this? */
for (uint8_t i = 0; i < distance; i++) lexerState->captureBuf[lexerState->captureSize] = peek(0);
lexerState->captureBuf[lexerState->captureSize++] = peek(i);
} else {
lexerState->captureSize += distance;
} }
lexerState->captureSize++;
} }
lexerState->macroArgScanDistance -= distance; lexerState->macroArgScanDistance--;
/* FIXME: this may not be too great, as only the top level is considered... */ /* FIXME: this may not be too great, as only the top level is considered... */
uint8_t distance = 1;
/* /*
* The logic is as follows: * The logic is as follows:
* - Any characters up to the expansion need to be consumed in the file * - Any characters up to the expansion need to be consumed in the file
@@ -997,14 +998,14 @@ static int nextChar(void)
/* If not at EOF, advance read position */ /* If not at EOF, advance read position */
if (c != EOF) if (c != EOF)
shiftChars(1); shiftChar();
return c; return c;
} }
static void handleCRLF(int c) static void handleCRLF(int c)
{ {
if (c == '\r' && peek(0) == '\n') if (c == '\r' && peek(0) == '\n')
shiftChars(1); shiftChar();
} }
/* "Services" provided by the lexer to the rest of the program */ /* "Services" provided by the lexer to the rest of the program */
@@ -1060,7 +1061,7 @@ static void discardBlockComment(void)
error("Unterminated block comment\n"); error("Unterminated block comment\n");
goto finish; goto finish;
case '\r': case '\r':
/* Handle CRLF before nextLine() since shiftChars updates colNo */ /* Handle CRLF before nextLine() since shiftChar updates colNo */
handleCRLF(c); handleCRLF(c);
/* fallthrough */ /* fallthrough */
case '\n': case '\n':
@@ -1075,7 +1076,7 @@ static void discardBlockComment(void)
continue; continue;
case '*': case '*':
if (peek(0) == '/') { if (peek(0) == '/') {
shiftChars(1); shiftChar();
goto finish; goto finish;
} }
/* fallthrough */ /* fallthrough */
@@ -1100,7 +1101,7 @@ static void discardComment(void)
if (c == EOF || c == '\r' || c == '\n') if (c == EOF || c == '\r' || c == '\n')
break; break;
shiftChars(1); shiftChar();
} }
lexerState->disableMacroArgs = false; lexerState->disableMacroArgs = false;
lexerState->disableInterpolation = false; lexerState->disableInterpolation = false;
@@ -1115,10 +1116,10 @@ static void readLineContinuation(void)
int c = peek(0); int c = peek(0);
if (isWhitespace(c)) { if (isWhitespace(c)) {
shiftChars(1); shiftChar();
} else if (c == '\r' || c == '\n') { } else if (c == '\r' || c == '\n') {
shiftChars(1); shiftChar();
/* Handle CRLF before nextLine() since shiftChars updates colNo */ /* Handle CRLF before nextLine() since shiftChar updates colNo */
handleCRLF(c); handleCRLF(c);
if (!lexerState->expansions || lexerState->expansions->distance) if (!lexerState->expansions || lexerState->expansions->distance)
nextLine(); nextLine();
@@ -1141,7 +1142,7 @@ static void readAnonLabelRef(char c)
// 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 { do {
shiftChars(1); shiftChar();
n++; n++;
} while (peek(0) == c); } while (peek(0) == c);
@@ -1154,7 +1155,7 @@ static void readNumber(int radix, int32_t baseValue)
{ {
uint32_t value = baseValue; uint32_t value = baseValue;
for (;; shiftChars(1)) { for (;; shiftChar()) {
int c = peek(0); int c = peek(0);
if (c == '_') if (c == '_')
@@ -1174,7 +1175,7 @@ static void readFractionalPart(void)
uint32_t value = 0, divisor = 1; uint32_t value = 0, divisor = 1;
dbgPrint("Reading fractional part\n"); dbgPrint("Reading fractional part\n");
for (;; shiftChars(1)) { for (;; shiftChar()) {
int c = peek(0); int c = peek(0);
if (c == '_') if (c == '_')
@@ -1185,9 +1186,9 @@ static void readFractionalPart(void)
warning(WARNING_LARGE_CONSTANT, warning(WARNING_LARGE_CONSTANT,
"Precision of fixed-point constant is too large\n"); "Precision of fixed-point constant is too large\n");
/* Discard any additional digits */ /* Discard any additional digits */
shiftChars(1); shiftChar();
while (c = peek(0), (c >= '0' && c <= '9') || c == '_') while (c = peek(0), (c >= '0' && c <= '9') || c == '_')
shiftChars(1); shiftChar();
break; break;
} }
value = value * 10 + (c - '0'); value = value * 10 + (c - '0');
@@ -1212,7 +1213,7 @@ static void readBinaryNumber(void)
uint32_t value = 0; uint32_t value = 0;
dbgPrint("Reading binary number with digits [%c,%c]\n", binDigits[0], binDigits[1]); dbgPrint("Reading binary number with digits [%c,%c]\n", binDigits[0], binDigits[1]);
for (;; shiftChars(1)) { for (;; shiftChar()) {
int c = peek(0); int c = peek(0);
int bit; int bit;
@@ -1238,7 +1239,7 @@ static void readHexNumber(void)
bool empty = true; bool empty = true;
dbgPrint("Reading hex number\n"); dbgPrint("Reading hex number\n");
for (;; shiftChars(1)) { for (;; shiftChar()) {
int c = peek(0); int c = peek(0);
if (c >= 'a' && c <= 'f') /* Convert letters to right after digits */ if (c >= 'a' && c <= 'f') /* Convert letters to right after digits */
@@ -1295,7 +1296,7 @@ static void readGfxConstant(void)
} }
if (width < 9) if (width < 9)
width++; width++;
shiftChars(1); shiftChar();
} }
if (width == 0) if (width == 0)
@@ -1332,7 +1333,7 @@ static int readIdentifier(char firstChar)
&& (c > 'z' || c < 'a') && (c > 'z' || c < 'a')
&& c != '#' && c != '.' && c != '@' && c != '_') && c != '#' && c != '.' && c != '@' && c != '_')
break; break;
shiftChars(1); shiftChar();
/* Write the char to the identifier's name */ /* Write the char to the identifier's name */
if (i < sizeof(yylval.tzSym) - 1) if (i < sizeof(yylval.tzSym) - 1)
@@ -1372,7 +1373,7 @@ static char const *readInterpolation(void)
int c = peek(0); int c = peek(0);
if (c == '{') { /* Nested interpolation */ if (c == '{') { /* Nested interpolation */
shiftChars(1); shiftChar();
char const *ptr = readInterpolation(); char const *ptr = readInterpolation();
if (ptr) { if (ptr) {
@@ -1383,10 +1384,10 @@ static char const *readInterpolation(void)
error("Missing }\n"); error("Missing }\n");
break; break;
} else if (c == '}') { } else if (c == '}') {
shiftChars(1); shiftChar();
break; break;
} else if (c == ':' && !fmt_IsFinished(&fmt)) { /* Format spec, only once */ } else if (c == ':' && !fmt_IsFinished(&fmt)) { /* Format spec, only once */
shiftChars(1); shiftChar();
for (size_t j = 0; j < i; j++) for (size_t j = 0; j < i; j++)
fmt_UseCharacter(&fmt, symName[j]); fmt_UseCharacter(&fmt, symName[j]);
fmt_FinishCharacters(&fmt); fmt_FinishCharacters(&fmt);
@@ -1395,7 +1396,7 @@ static char const *readInterpolation(void)
error("Invalid format spec '%s'\n", symName); error("Invalid format spec '%s'\n", symName);
i = 0; /* Now that format has been set, restart at beginning of string */ i = 0; /* Now that format has been set, restart at beginning of string */
} else { } else {
shiftChars(1); shiftChar();
if (i < sizeof(symName)) /* Allow writing an extra char to flag overflow */ if (i < sizeof(symName)) /* Allow writing an extra char to flag overflow */
symName[i++] = c; symName[i++] = c;
} }
@@ -1483,10 +1484,10 @@ static void readString(void)
// We reach this function after reading a single quote, but we also support triple quotes // We reach this function after reading a single quote, but we also support triple quotes
if (peek(0) == '"') { if (peek(0) == '"') {
shiftChars(1); shiftChar();
if (peek(0) == '"') { if (peek(0) == '"') {
// """ begins a multi-line string // """ begins a multi-line string
shiftChars(1); shiftChar();
multiline = true; multiline = true;
} else { } else {
// "" is an empty string, skip the loop // "" is an empty string, skip the loop
@@ -1504,11 +1505,11 @@ static void readString(void)
} }
// We'll be staying in the string, so we can safely consume the char // We'll be staying in the string, so we can safely consume the char
shiftChars(1); shiftChar();
// Handle '\r' or '\n' (in multiline strings only, already handled above otherwise) // Handle '\r' or '\n' (in multiline strings only, already handled above otherwise)
if (c == '\r' || c == '\n') { if (c == '\r' || c == '\n') {
/* Handle CRLF before nextLine() since shiftChars updates colNo */ /* Handle CRLF before nextLine() since shiftChar updates colNo */
handleCRLF(c); handleCRLF(c);
nextLine(); nextLine();
c = '\n'; c = '\n';
@@ -1520,12 +1521,12 @@ static void readString(void)
// Only """ ends a multi-line string // Only """ ends a multi-line string
if (peek(0) != '"') if (peek(0) != '"')
break; break;
shiftChars(1); shiftChar();
if (peek(0) != '"') { if (peek(0) != '"') {
append_yylval_tzString('"'); append_yylval_tzString('"');
break; break;
} }
shiftChars(1); shiftChar();
} }
goto finish; goto finish;
@@ -1537,19 +1538,19 @@ static void readString(void)
case '{': case '{':
case '}': case '}':
// Return that character unchanged // Return that character unchanged
shiftChars(1); shiftChar();
break; break;
case 'n': case 'n':
c = '\n'; c = '\n';
shiftChars(1); shiftChar();
break; break;
case 'r': case 'r':
c = '\r'; c = '\r';
shiftChars(1); shiftChar();
break; break;
case 't': case 't':
c = '\t'; c = '\t';
shiftChars(1); shiftChar();
break; break;
// Line continuation // Line continuation
@@ -1572,7 +1573,7 @@ static void readString(void)
case '7': case '7':
case '8': case '8':
case '9': case '9':
shiftChars(1); shiftChar();
char const *str = readMacroArg(c); char const *str = readMacroArg(c);
while (*str) while (*str)
@@ -1586,7 +1587,7 @@ static void readString(void)
default: default:
error("Illegal character escape %s\n", printChar(c)); error("Illegal character escape %s\n", printChar(c));
shiftChars(1); shiftChar();
break; break;
} }
break; break;
@@ -1633,11 +1634,11 @@ static size_t appendStringLiteral(size_t i)
append_yylval_tzString('"'); append_yylval_tzString('"');
if (peek(0) == '"') { if (peek(0) == '"') {
append_yylval_tzString('"'); append_yylval_tzString('"');
shiftChars(1); shiftChar();
if (peek(0) == '"') { if (peek(0) == '"') {
// """ begins a multi-line string // """ begins a multi-line string
append_yylval_tzString('"'); append_yylval_tzString('"');
shiftChars(1); shiftChar();
multiline = true; multiline = true;
} else { } else {
// "" is an empty string, skip the loop // "" is an empty string, skip the loop
@@ -1655,11 +1656,11 @@ static size_t appendStringLiteral(size_t i)
} }
// We'll be staying in the string, so we can safely consume the char // We'll be staying in the string, so we can safely consume the char
shiftChars(1); shiftChar();
// Handle '\r' or '\n' (in multiline strings only, already handled above otherwise) // Handle '\r' or '\n' (in multiline strings only, already handled above otherwise)
if (c == '\r' || c == '\n') { if (c == '\r' || c == '\n') {
/* Handle CRLF before nextLine() since shiftChars updates colNo */ /* Handle CRLF before nextLine() since shiftChar updates colNo */
handleCRLF(c); handleCRLF(c);
nextLine(); nextLine();
c = '\n'; c = '\n';
@@ -1672,11 +1673,11 @@ static size_t appendStringLiteral(size_t i)
if (peek(0) != '"') if (peek(0) != '"')
break; break;
append_yylval_tzString('"'); append_yylval_tzString('"');
shiftChars(1); shiftChar();
if (peek(0) != '"') if (peek(0) != '"')
break; break;
append_yylval_tzString('"'); append_yylval_tzString('"');
shiftChars(1); shiftChar();
} }
append_yylval_tzString('"'); append_yylval_tzString('"');
goto finish; goto finish;
@@ -1694,7 +1695,7 @@ static size_t appendStringLiteral(size_t i)
case 't': case 't':
// Return that character unchanged // Return that character unchanged
append_yylval_tzString('\\'); append_yylval_tzString('\\');
shiftChars(1); shiftChar();
break; break;
// Line continuation // Line continuation
@@ -1717,7 +1718,7 @@ static size_t appendStringLiteral(size_t i)
case '7': case '7':
case '8': case '8':
case '9': case '9':
shiftChars(1); shiftChar();
char const *str = readMacroArg(c); char const *str = readMacroArg(c);
i = appendEscapedSubstring(str, i); i = appendEscapedSubstring(str, i);
@@ -1731,12 +1732,12 @@ static size_t appendStringLiteral(size_t i)
case ',': /* `\,` inside a macro arg string literal */ case ',': /* `\,` inside a macro arg string literal */
warning(WARNING_OBSOLETE, warning(WARNING_OBSOLETE,
"`\\,` is deprecated inside strings\n"); "`\\,` is deprecated inside strings\n");
shiftChars(1); shiftChar();
break; break;
default: default:
error("Illegal character escape %s\n", printChar(c)); error("Illegal character escape %s\n", printChar(c));
shiftChars(1); shiftChar();
break; break;
} }
break; break;
@@ -1833,14 +1834,14 @@ static int yylex_NORMAL(void)
case '*': /* Either MUL or EXP */ case '*': /* Either MUL or EXP */
if (peek(0) == '*') { if (peek(0) == '*') {
shiftChars(1); shiftChar();
return T_OP_EXP; return T_OP_EXP;
} }
return T_OP_MUL; return T_OP_MUL;
case '/': /* Either division or a block comment */ case '/': /* Either division or a block comment */
if (peek(0) == '*') { if (peek(0) == '*') {
shiftChars(1); shiftChar();
discardBlockComment(); discardBlockComment();
break; break;
} }
@@ -1848,14 +1849,14 @@ static int yylex_NORMAL(void)
case '|': /* Either binary or logical OR */ case '|': /* Either binary or logical OR */
if (peek(0) == '|') { if (peek(0) == '|') {
shiftChars(1); shiftChar();
return T_OP_LOGICOR; return T_OP_LOGICOR;
} }
return T_OP_OR; return T_OP_OR;
case '=': /* Either SET alias, or EQ */ case '=': /* Either SET alias, or EQ */
if (peek(0) == '=') { if (peek(0) == '=') {
shiftChars(1); shiftChar();
return T_OP_LOGICEQU; return T_OP_LOGICEQU;
} }
return T_POP_EQUAL; return T_POP_EQUAL;
@@ -1863,10 +1864,10 @@ static int yylex_NORMAL(void)
case '<': /* Either a LT, LTE, or left shift */ case '<': /* Either a LT, LTE, or left shift */
switch (peek(0)) { switch (peek(0)) {
case '=': case '=':
shiftChars(1); shiftChar();
return T_OP_LOGICLE; return T_OP_LOGICLE;
case '<': case '<':
shiftChars(1); shiftChar();
return T_OP_SHL; return T_OP_SHL;
default: default:
return T_OP_LOGICLT; return T_OP_LOGICLT;
@@ -1875,10 +1876,10 @@ static int yylex_NORMAL(void)
case '>': /* Either a GT, GTE, or right shift */ case '>': /* Either a GT, GTE, or right shift */
switch (peek(0)) { switch (peek(0)) {
case '=': case '=':
shiftChars(1); shiftChar();
return T_OP_LOGICGE; return T_OP_LOGICGE;
case '>': case '>':
shiftChars(1); shiftChar();
return T_OP_SHR; return T_OP_SHR;
default: default:
return T_OP_LOGICGT; return T_OP_LOGICGT;
@@ -1886,7 +1887,7 @@ static int yylex_NORMAL(void)
case '!': /* Either a NEQ, or negation */ case '!': /* Either a NEQ, or negation */
if (peek(0) == '=') { if (peek(0) == '=') {
shiftChars(1); shiftChar();
return T_OP_LOGICNE; return T_OP_LOGICNE;
} }
return T_OP_LOGICNOT; return T_OP_LOGICNOT;
@@ -1910,13 +1911,13 @@ static int yylex_NORMAL(void)
if (yylval.nConstValue == 0xff00) { if (yylval.nConstValue == 0xff00) {
/* Whitespace is ignored anyways */ /* Whitespace is ignored anyways */
while (isWhitespace(c = peek(0))) while (isWhitespace(c = peek(0)))
shiftChars(1); shiftChar();
if (c == '+') { if (c == '+') {
shiftChars(1); shiftChar();
while (isWhitespace(c = peek(0))) while (isWhitespace(c = peek(0)))
shiftChars(1); shiftChar();
if (c == 'c' || c == 'C') { if (c == 'c' || c == 'C') {
shiftChars(1); shiftChar();
return T_MODE_HW_C; return T_MODE_HW_C;
} }
/* Retroactively lex the plus after the $ff00 */ /* Retroactively lex the plus after the $ff00 */
@@ -1937,7 +1938,7 @@ static int yylex_NORMAL(void)
case '9': case '9':
readNumber(10, c - '0'); readNumber(10, c - '0');
if (peek(0) == '.') { if (peek(0) == '.') {
shiftChars(1); shiftChar();
readFractionalPart(); readFractionalPart();
} }
return T_NUMBER; return T_NUMBER;
@@ -1945,7 +1946,7 @@ static int yylex_NORMAL(void)
case '&': case '&':
secondChar = peek(0); secondChar = peek(0);
if (secondChar == '&') { if (secondChar == '&') {
shiftChars(1); shiftChar();
return T_OP_LOGICAND; return T_OP_LOGICAND;
} else if (secondChar >= '0' && secondChar <= '7') { } else if (secondChar >= '0' && secondChar <= '7') {
readNumber(8, 0); readNumber(8, 0);
@@ -2047,14 +2048,14 @@ static int yylex_RAW(void)
/* Trim left whitespace (stops at a block comment or line continuation) */ /* Trim left whitespace (stops at a block comment or line continuation) */
while (isWhitespace(peek(0))) while (isWhitespace(peek(0)))
shiftChars(1); shiftChar();
for (;;) { for (;;) {
c = peek(0); c = peek(0);
switch (c) { switch (c) {
case '"': /* String literals inside macro args */ case '"': /* String literals inside macro args */
shiftChars(1); shiftChar();
i = appendStringLiteral(i); i = appendStringLiteral(i);
break; break;
@@ -2069,9 +2070,9 @@ static int yylex_RAW(void)
goto finish; goto finish;
case '/': /* Block comments inside macro args */ case '/': /* Block comments inside macro args */
shiftChars(1); /* Shift the slash */ shiftChar();
if (peek(0) == '*') { if (peek(0) == '*') {
shiftChars(1); shiftChar();
discardBlockComment(); discardBlockComment();
continue; continue;
} }
@@ -2079,7 +2080,7 @@ static int yylex_RAW(void)
break; break;
case '\\': /* Character escape */ case '\\': /* Character escape */
shiftChars(1); /* Shift the backslash */ shiftChar();
c = peek(0); c = peek(0);
switch (c) { switch (c) {
@@ -2124,7 +2125,7 @@ static int yylex_RAW(void)
default: /* Regular characters will just get copied */ default: /* Regular characters will just get copied */
append_yylval_tzString(c); append_yylval_tzString(c);
shiftChars(1); shiftChar();
break; break;
} }
} }
@@ -2146,7 +2147,7 @@ finish:
// then T_COMMA) without advancing the read. To avoid this, commas in raw // then T_COMMA) without advancing the read. To avoid this, commas in raw
// mode end the current macro argument but are not tokenized themselves. // mode end the current macro argument but are not tokenized themselves.
if (c == ',') { if (c == ',') {
shiftChars(1); shiftChar();
return T_STRING; return T_STRING;
} }
@@ -2161,7 +2162,7 @@ finish:
lexer_SetMode(LEXER_NORMAL); lexer_SetMode(LEXER_NORMAL);
if (c == '\r' || c == '\n') { if (c == '\r' || c == '\n') {
shiftChars(1); shiftChar();
handleCRLF(c); handleCRLF(c);
return T_NEWLINE; return T_NEWLINE;
} }
@@ -2198,11 +2199,11 @@ static int skipIfBlock(bool toEndc)
c = peek(0); c = peek(0);
if (!isWhitespace(c)) if (!isWhitespace(c))
break; break;
shiftChars(1); shiftChar();
} }
if (startsIdentifier(c)) { if (startsIdentifier(c)) {
shiftChars(1); shiftChar();
token = readIdentifier(c); token = readIdentifier(c);
switch (token) { switch (token) {
case T_POP_IF: case T_POP_IF:
@@ -2248,7 +2249,7 @@ static int skipIfBlock(bool toEndc)
} }
if (c == '\r' || c == '\n') { if (c == '\r' || c == '\n') {
/* Handle CRLF before nextLine() since shiftChars updates colNo */ /* Handle CRLF before nextLine() since shiftChar updates colNo */
handleCRLF(c); handleCRLF(c);
/* Do this both on line continuations and plain EOLs */ /* Do this both on line continuations and plain EOLs */
nextLine(); nextLine();
@@ -2293,11 +2294,11 @@ static int yylex_SKIP_TO_ENDR(void)
c = peek(0); c = peek(0);
if (!isWhitespace(c)) if (!isWhitespace(c))
break; break;
shiftChars(1); shiftChar();
} }
if (startsIdentifier(c)) { if (startsIdentifier(c)) {
shiftChars(1); shiftChar();
switch (readIdentifier(c)) { switch (readIdentifier(c)) {
case T_POP_FOR: case T_POP_FOR:
case T_POP_REPT: case T_POP_REPT:
@@ -2335,7 +2336,7 @@ static int yylex_SKIP_TO_ENDR(void)
} }
if (c == '\r' || c == '\n') { if (c == '\r' || c == '\n') {
/* Handle CRLF before nextLine() since shiftChars updates colNo */ /* Handle CRLF before nextLine() since shiftChar updates colNo */
handleCRLF(c); handleCRLF(c);
/* Do this both on line continuations and plain EOLs */ /* Do this both on line continuations and plain EOLs */
nextLine(); nextLine();