Comment and improve ParseSymbol and AppendMacroArg

This commit is contained in:
ISSOtm
2019-08-30 20:47:04 +02:00
parent 6068b565f5
commit dc2c97fe0c
3 changed files with 32 additions and 25 deletions

View File

@@ -65,7 +65,7 @@ void lex_SetBuffer(char *buffer, uint32_t len);
int yywrap(void); int yywrap(void);
int yylex(void); int yylex(void);
void yyunput(char c); void yyunput(char c);
void yyunputstr(char *s); void yyunputstr(const char *s);
void yyskipbytes(uint32_t count); void yyskipbytes(uint32_t count);
void yyunputbytes(uint32_t count); void yyunputbytes(uint32_t count);

View File

@@ -188,11 +188,11 @@ uint32_t ParseNumber(char *s, uint32_t size)
} }
/* /*
* If the symbol name ends before the end of the macro arg, return true * If the symbol name ends before the end of the macro arg,
* and point "rest" to the rest of the macro arg. * return a pointer to the rest of the macro arg.
* Otherwise, return false. * Otherwise, return NULL.
*/ */
bool AppendMacroArg(char whichArg, char *dest, size_t *destIndex, char **rest) char *AppendMacroArg(char whichArg, char *dest, size_t *destIndex)
{ {
char *marg; char *marg;
@@ -222,14 +222,13 @@ bool AppendMacroArg(char whichArg, char *dest, size_t *destIndex, char **rest)
dest[*destIndex] = ch; dest[*destIndex] = ch;
(*destIndex)++; (*destIndex)++;
} else { } else {
*rest = marg; return marg;
return true;
} }
marg++; marg++;
} }
return false; return NULL;
} }
uint32_t ParseSymbol(char *src, uint32_t size) uint32_t ParseSymbol(char *src, uint32_t size)
@@ -251,7 +250,9 @@ uint32_t ParseSymbol(char *src, uint32_t size)
*/ */
ch = src[srcIndex++]; ch = src[srcIndex++];
if (AppendMacroArg(ch, dest, &destIndex, &rest)) rest = AppendMacroArg(ch, dest, &destIndex);
/* If the symbol's end was in the middle of the token */
if (rest)
break; break;
} else { } else {
if (destIndex >= MAXSYMLEN) if (destIndex >= MAXSYMLEN)
@@ -262,28 +263,33 @@ uint32_t ParseSymbol(char *src, uint32_t size)
dest[destIndex] = 0; dest[destIndex] = 0;
/* Tell the lexer we read all bytes that we did */
yyskipbytes(srcIndex);
/*
* If an escape's expansion left some chars after the symbol's end,
* such as the `::` in a `Backup\1` expanded to `BackupCamX::`,
* put those into the buffer.
* Note that this NEEDS to be done after the `yyskipbytes` above.
*/
if (rest)
yyunputstr(rest);
/* If the symbol is an EQUS, expand it */
if (!oDontExpandStrings && sym_isString(dest)) { if (!oDontExpandStrings && sym_isString(dest)) {
char *s; char *s;
yyskipbytes(srcIndex); /* Feed the symbol's contents into the buffer */
if (rest)
yyunputstr(rest);
yyunputstr(s = sym_GetStringValue(dest)); yyunputstr(s = sym_GetStringValue(dest));
/* Lines inserted this way shall not increase nLineNo */
while (*s) { while (*s) {
if (*s++ == '\n') if (*s++ == '\n')
nLineNo -= 1; nLineNo--;
} }
return 0; return 0;
} }
yyskipbytes(srcIndex);
if (rest)
yyunputstr(rest);
strcpy(yylval.tzSym, dest); strcpy(yylval.tzSym, dest);
return 1; return 1;
} }

View File

@@ -88,17 +88,18 @@ void yyunput(char c)
*(--pLexBuffer) = c; *(--pLexBuffer) = c;
} }
void yyunputstr(char *s) void yyunputstr(const char *s)
{ {
int32_t i, len; int32_t len;
len = strlen(s); len = strlen(s);
if (pLexBuffer - len < pLexBufferRealStart) pLexBuffer -= len;
if (pLexBuffer < pLexBufferRealStart)
fatalerror("Buffer safety margin exceeded"); fatalerror("Buffer safety margin exceeded");
for (i = len - 1; i >= 0; i--) memcpy(pLexBuffer, s, len);
*(--pLexBuffer) = s[i];
} }
void yy_switch_to_buffer(YY_BUFFER_STATE buf) void yy_switch_to_buffer(YY_BUFFER_STATE buf)