mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Get rid of skip in struct Expansion
This was only used to skip the two macro arg characters, but shiftChar() can skip them before the expansion.
This commit is contained in:
@@ -312,7 +312,6 @@ struct Expansion {
|
|||||||
size_t len;
|
size_t len;
|
||||||
size_t totalLen;
|
size_t totalLen;
|
||||||
size_t distance; /* Distance between the beginning of this expansion and of its parent */
|
size_t distance; /* Distance between the beginning of this expansion and of its parent */
|
||||||
uint8_t skip; /* How many extra characters to skip after the expansion is over */
|
|
||||||
bool owned; /* Whether or not to free contents when this expansion is freed */
|
bool owned; /* Whether or not to free contents when this expansion is freed */
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -684,7 +683,7 @@ static void reallocCaptureBuf(void)
|
|||||||
for (struct Expansion *exp = lexerState->expansions; exp; exp = exp->firstChild) { \
|
for (struct Expansion *exp = lexerState->expansions; exp; exp = exp->firstChild) { \
|
||||||
/* Find the closest expansion whose end is after the target */ \
|
/* Find the closest expansion whose end is after the target */ \
|
||||||
while (exp && exp->totalLen + exp->distance <= (dist)) { \
|
while (exp && exp->totalLen + exp->distance <= (dist)) { \
|
||||||
(dist) -= exp->totalLen + exp->skip; \
|
(dist) -= exp->totalLen; \
|
||||||
exp = exp->next; \
|
exp = exp->next; \
|
||||||
} \
|
} \
|
||||||
/* If there is none, or it begins after the target, stop at the previous level */ \
|
/* If there is none, or it begins after the target, stop at the previous level */ \
|
||||||
@@ -708,7 +707,7 @@ static struct Expansion *getExpansionAtDistance(size_t *distance)
|
|||||||
return expansion;
|
return expansion;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void beginExpansion(uint8_t skip, char const *str, bool owned, char const *name)
|
static void beginExpansion(char const *str, bool owned, char const *name)
|
||||||
{
|
{
|
||||||
size_t size = strlen(str);
|
size_t size = strlen(str);
|
||||||
|
|
||||||
@@ -724,8 +723,8 @@ static void beginExpansion(uint8_t skip, char const *str, bool owned, char const
|
|||||||
unsigned int depth = 0;
|
unsigned int depth = 0;
|
||||||
|
|
||||||
lookupExpansion(distance, exp, {
|
lookupExpansion(distance, exp, {
|
||||||
assert(exp->totalLen <= SIZE_MAX - (size - skip));
|
assert(exp->totalLen <= SIZE_MAX - size);
|
||||||
exp->totalLen += size - skip;
|
exp->totalLen += size;
|
||||||
parent = exp;
|
parent = exp;
|
||||||
|
|
||||||
if (name && depth++ >= nMaxRecursionDepth)
|
if (name && depth++ >= nMaxRecursionDepth)
|
||||||
@@ -748,7 +747,6 @@ static void beginExpansion(uint8_t skip, char const *str, bool owned, char const
|
|||||||
(*insertPoint)->len = size;
|
(*insertPoint)->len = size;
|
||||||
(*insertPoint)->totalLen = size;
|
(*insertPoint)->totalLen = size;
|
||||||
(*insertPoint)->distance = distance;
|
(*insertPoint)->distance = distance;
|
||||||
(*insertPoint)->skip = skip;
|
|
||||||
(*insertPoint)->owned = owned;
|
(*insertPoint)->owned = owned;
|
||||||
|
|
||||||
/* If expansion is the new closest one, update offset */
|
/* If expansion is the new closest one, update offset */
|
||||||
@@ -885,27 +883,24 @@ restart:
|
|||||||
lexerState->macroArgScanDistance++;
|
lexerState->macroArgScanDistance++;
|
||||||
c = peekInternal(1);
|
c = peekInternal(1);
|
||||||
if (isMacroChar(c)) {
|
if (isMacroChar(c)) {
|
||||||
|
shiftChar();
|
||||||
|
shiftChar();
|
||||||
char const *str = readMacroArg(c);
|
char const *str = readMacroArg(c);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the macro arg is an empty string, it cannot be
|
* If the macro arg is an empty string, it cannot be
|
||||||
* expanded, so skip it and keep peeking.
|
* expanded, so skip it and keep peeking.
|
||||||
*/
|
*/
|
||||||
if (!str[0]) {
|
if (!str[0])
|
||||||
shiftChar();
|
|
||||||
shiftChar();
|
|
||||||
goto restart;
|
goto restart;
|
||||||
}
|
|
||||||
|
|
||||||
beginExpansion(2, str, c == '#', NULL);
|
beginExpansion(str, c == '#', NULL);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Assuming macro args can't be recursive (I'll be damned if a way
|
* Assuming macro args can't be recursive (I'll be damned if a way
|
||||||
* is found...), then we mark the entire macro arg as scanned;
|
* is found...), then we mark the entire macro arg as scanned.
|
||||||
* however, the two macro arg characters (\1) will be ignored,
|
|
||||||
* so they shouldn't be counted in the scan distance!
|
|
||||||
*/
|
*/
|
||||||
lexerState->macroArgScanDistance += strlen(str) - 2;
|
lexerState->macroArgScanDistance += strlen(str);
|
||||||
|
|
||||||
c = str[0];
|
c = str[0];
|
||||||
} else {
|
} else {
|
||||||
@@ -917,7 +912,7 @@ restart:
|
|||||||
char const *ptr = readInterpolation();
|
char const *ptr = readInterpolation();
|
||||||
|
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
beginExpansion(0, ptr, false, ptr);
|
beginExpansion(ptr, false, ptr);
|
||||||
goto restart;
|
goto restart;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -962,8 +957,6 @@ nextExpansion:
|
|||||||
/* Add the leftovers to the distance */
|
/* Add the leftovers to the distance */
|
||||||
distance += lexerState->expansionOfs;
|
distance += lexerState->expansionOfs;
|
||||||
distance -= lexerState->expansions->totalLen;
|
distance -= lexerState->expansions->totalLen;
|
||||||
/* Also add in the post-expansion skip */
|
|
||||||
distance += lexerState->expansions->skip;
|
|
||||||
/* Move on to the next expansion */
|
/* Move on to the next expansion */
|
||||||
struct Expansion *next = lexerState->expansions->next;
|
struct Expansion *next = lexerState->expansions->next;
|
||||||
|
|
||||||
@@ -1379,7 +1372,7 @@ static char const *readInterpolation(void)
|
|||||||
char const *ptr = readInterpolation();
|
char const *ptr = readInterpolation();
|
||||||
|
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
beginExpansion(0, ptr, false, ptr);
|
beginExpansion(ptr, false, ptr);
|
||||||
continue; /* Restart, reading from the new buffer */
|
continue; /* Restart, reading from the new buffer */
|
||||||
}
|
}
|
||||||
} else if (c == EOF || c == '\r' || c == '\n' || c == '"') {
|
} else if (c == EOF || c == '\r' || c == '\n' || c == '"') {
|
||||||
@@ -2018,7 +2011,7 @@ static int yylex_NORMAL(void)
|
|||||||
if (sym && sym->type == SYM_EQUS) {
|
if (sym && sym->type == SYM_EQUS) {
|
||||||
char const *s = sym_GetStringValue(sym);
|
char const *s = sym_GetStringValue(sym);
|
||||||
|
|
||||||
beginExpansion(0, s, false, sym->name);
|
beginExpansion(s, false, sym->name);
|
||||||
continue; /* Restart, reading from the new buffer */
|
continue; /* Restart, reading from the new buffer */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user