Fix handling of error tokens from within f-strings

This commit is contained in:
K. Lange 2021-02-01 21:28:58 +09:00
parent 0b6b6468a6
commit 7129a3ac7d
3 changed files with 9 additions and 6 deletions

View File

@ -197,7 +197,7 @@ static KrkToken decorator(size_t level, FunctionType type);
static void call(int canAssign);
static void finishError(KrkToken * token) {
size_t i = (token->col - 1);
size_t i = 0;
while (token->linePtr[i] && token->linePtr[i] != '\n') i++;
krk_attachNamedObject(&AS_INSTANCE(vm.currentException)->fields, "line", (KrkObj*)krk_copyString(token->linePtr, i));
@ -214,7 +214,7 @@ static void finishError(KrkToken * token) {
krk_attachNamedValue(&AS_INSTANCE(vm.currentException)->fields, "func", name);
}
parser.panicMode = 1;
parser.panicMode = 1;
parser.hadError = 1;
}

View File

@ -39,14 +39,16 @@ static KrkToken makeToken(KrkTokenType type) {
}
static KrkToken errorToken(const char * errorStr) {
ssize_t column = (scanner.linePtr < scanner.start) ? scanner.start - scanner.linePtr : 0;
ssize_t width = (scanner.start < scanner.cur) ? scanner.cur - scanner.start : 0;
return (KrkToken){
.type = TOKEN_ERROR,
.start = errorStr,
.length = strlen(errorStr),
.line = scanner.line,
.linePtr = scanner.linePtr,
.literalWidth = (size_t)(scanner.cur - scanner.start),
.col = (scanner.start - scanner.linePtr) + 1,
.literalWidth = (size_t)(width),
.col = column + 1,
};
}

View File

@ -1540,15 +1540,16 @@ static KrkValue _exception_repr(int argc, KrkValue argv[]) {
static KrkValue _syntaxerror_repr(int argc, KrkValue argv[]) {
KrkInstance * self = AS_INSTANCE(argv[0]);
/* .arg */
KrkValue file, line, lineno, colno, width, arg, func;
KrkValue file, line, lineno, colno, arg, func;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("file")), &file) || !IS_STRING(file)) goto _badSyntaxError;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("line")), &line) || !IS_STRING(line)) goto _badSyntaxError;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("lineno")), &lineno) || !IS_INTEGER(lineno)) goto _badSyntaxError;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("colno")), &colno) || !IS_INTEGER(colno)) goto _badSyntaxError;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("width")), &width) || !IS_INTEGER(width)) goto _badSyntaxError;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("arg")), &arg) || !IS_STRING(arg)) goto _badSyntaxError;
if (!krk_tableGet(&self->fields, OBJECT_VAL(S("func")), &func)) goto _badSyntaxError;
if (AS_INTEGER(colno) <= 0) colno = INTEGER_VAL(1);
krk_push(OBJECT_VAL(S(" File \"{}\", line {}{}\n {}\n {}^\n{}: {}")));
char * tmp = malloc(AS_INTEGER(colno));
memset(tmp,' ',AS_INTEGER(colno));