Created
April 11, 2020 15:54
-
-
Save ISSOtm/0d17234111d785043695e27c846a75a1 to your computer and use it in GitHub Desktop.
"@ISSOtm what's the hardest part about the lexer [of RGBDS]?"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define keepOnGetc(var, handle) \ | |
do { \ | |
(var) = fgetc(handle); \ | |
} while ((var) == EOF && ferror(handle) && errno == EINTR) | |
/* Pointer to chars to inject in lexer input */ | |
static char const *insertPtr = NULL; | |
/* TODO: this doesn't handle interactive buffers; should it try to? */ | |
/* | |
* This is responsible for expanding macro args (\1 through \9) and \@. | |
* This could in theory be done anywhere we read them, *however* `SHIFT` is a | |
* thing, so we only expand them when there is no other choice. | |
* TODO: This is probably a large performance penalty and should be removed... | |
*/ | |
#define YY_INPUT(buf, result, max_size) \ | |
do { \ | |
assert(max_size > 0); \ | |
int byte; \ | |
if (!insertPtr) { \ | |
keepOnGetc(byte, yyin); \ | |
/* Maybe process macro arg */ \ | |
if (byte == '\\') { \ | |
/* Check which char this is escaping */ \ | |
keepOnGetc(byte, yyin); \ | |
if (byte == '@') { \ | |
insertPtr = macro_GetUniqueIDStr(); \ | |
if (!insertPtr) \ | |
yyerror("Unique label string not defined"); \ | |
} else if (byte >= '1' && byte <= '9') { \ | |
insertPtr = macro_GetArg(byte - '0'); \ | |
if (!insertPtr) \ | |
yyerror("Macro argument '\\%c' not defined", \ | |
byte); \ | |
/* If the escape isn't our job, push it back */ \ | |
} else if (byte != EOF) { \ | |
/* We ever only do one pushback, guaranteeing it */ \ | |
ungetc(byte, yyin); \ | |
byte = '\\'; \ | |
} /* If there is an error or EOF, handle it below */ \ | |
} \ | |
} \ | |
if (insertPtr) { /* Inject chars from the input buf if any */ \ | |
result = musl__strlcpynul(buf, insertPtr, max_size); \ | |
if (result > max_size) { \ | |
result = max_size; \ | |
insertPtr += result; \ | |
} else { /* If the copy finished... */ \ | |
insertPtr = NULL; \ | |
} \ | |
if (result == max_size) \ | |
break; /* Exit now if buffer is full */ \ | |
keepOnGetc(byte, yyin); /* Get a char for below */ \ | |
} else { \ | |
result = YY_NULL; \ | |
} \ | |
if (!insertPtr) { /* If chars remain to be injected, they have priority */ \ | |
if (byte != EOF) { /* Write chars into the buffer */ \ | |
/* YY_NULL is generally 0, but let's be safe */ \ | |
if (YY_NULL != 0 && result == YY_NULL) \ | |
result = 0; \ | |
buf[result++] = byte; \ | |
while (result < max_size) { \ | |
keepOnGetc(byte, yyin); \ | |
if (byte == EOF) \ | |
break; \ | |
if (byte == '\\') { \ | |
ungetc(byte, yyin); \ | |
break; \ | |
} \ | |
if (byte == '\r') \ | |
byte = ' '; \ | |
buf[result++] = byte; \ | |
} \ | |
} \ | |
if (byte == EOF && ferror(yyin)) \ | |
yyerror("Error reading file: %s", \ | |
strerror(errno)); \ | |
} \ | |
} while (0) | |
// Okay I lied, actually the hardest part is not implemented yet. | |
// It's to read macro etc. blocks without expanding anything, with the problem of buffer flushes and everything. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment