Added line number to most compile-time error messages.

This commit is contained in:
Guido van Rossum 1996-09-17 14:32:04 +00:00
parent ef5cd05c3d
commit 452a9833c9
1 changed files with 90 additions and 88 deletions

View File

@ -256,12 +256,43 @@ struct compiling {
int c_nblocks; /* current block stack level */ int c_nblocks; /* current block stack level */
char *c_filename; /* filename of current node */ char *c_filename; /* filename of current node */
char *c_name; /* name of object (e.g. function) */ char *c_name; /* name of object (e.g. function) */
int c_lineno; /* Current line number */
#ifdef PRIVATE_NAME_MANGLING #ifdef PRIVATE_NAME_MANGLING
char *c_private; /* for private name mangling */ char *c_private; /* for private name mangling */
#endif #endif
}; };
/* Error message including line number */
static void
com_error(c, exc, msg)
struct compiling *c;
object *exc;
char *msg;
{
int n = strlen(msg);
object *v;
char buffer[30];
char *s;
if (c->c_lineno <= 1) {
/* Unknown line number or single interactive command */
err_setstr(exc, msg);
return;
}
sprintf(buffer, " (line %d)", c->c_lineno);
v = newsizedstringobject((char *)NULL, n + strlen(buffer));
if (v == NULL)
return; /* MemoryError, too bad */
s = GETSTRINGVALUE((stringobject *)v);
strcpy(s, msg);
strcat(s, buffer);
err_setval(exc, v);
DECREF(v);
c->c_errors++;
}
/* Interface to the block stack */ /* Interface to the block stack */
static void static void
@ -270,8 +301,7 @@ block_push(c, type)
int type; int type;
{ {
if (c->c_nblocks >= MAXBLOCKS) { if (c->c_nblocks >= MAXBLOCKS) {
err_setstr(SystemError, "too many statically nested blocks"); com_error(c, SystemError, "too many statically nested blocks");
c->c_errors++;
} }
else { else {
c->c_block[c->c_nblocks++] = type; c->c_block[c->c_nblocks++] = type;
@ -286,8 +316,7 @@ block_pop(c, type)
if (c->c_nblocks > 0) if (c->c_nblocks > 0)
c->c_nblocks--; c->c_nblocks--;
if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
err_setstr(SystemError, "bad block pop"); com_error(c, SystemError, "bad block pop");
c->c_errors++;
} }
} }
@ -343,6 +372,7 @@ com_init(c, filename)
c->c_nblocks = 0; c->c_nblocks = 0;
c->c_filename = filename; c->c_filename = filename;
c->c_name = "?"; c->c_name = "?";
c->c_lineno = 0;
return 1; return 1;
fail_000: fail_000:
@ -391,8 +421,7 @@ com_addbyte(c, byte)
fprintf(stderr, "XXX compiling bad byte: %d\n", byte); fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
fatal("com_addbyte: byte out of range"); fatal("com_addbyte: byte out of range");
*/ */
err_setstr(SystemError, "com_addbyte: byte out of range"); com_error(c, SystemError, "com_addbyte: byte out of range");
c->c_errors++;
} }
if (c->c_code == NULL) if (c->c_code == NULL)
return; return;
@ -421,6 +450,8 @@ com_addoparg(c, op, arg)
int op; int op;
int arg; int arg;
{ {
if (op == SET_LINENO)
c->c_lineno = arg;
com_addbyte(c, op); com_addbyte(c, op);
com_addint(c, arg); com_addint(c, arg);
} }
@ -592,8 +623,8 @@ com_addopname(c, op, n)
for (i = 0; i < NCH(n); i += 2) { for (i = 0; i < NCH(n); i += 2) {
char *s = STR(CHILD(n, i)); char *s = STR(CHILD(n, i));
if (p + strlen(s) > buffer + (sizeof buffer) - 2) { if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
err_setstr(MemoryError, com_error(c, MemoryError,
"dotted_name too long"); "dotted_name too long");
name = NULL; name = NULL;
break; break;
} }
@ -611,7 +642,8 @@ com_addopname(c, op, n)
} }
static object * static object *
parsenumber(s) parsenumber(co, s)
struct compiling *co;
char *s; char *s;
{ {
extern long mystrtol PROTO((const char *, char **, int)); extern long mystrtol PROTO((const char *, char **, int));
@ -637,8 +669,8 @@ parsenumber(s)
x = mystrtol(s, &end, 0); x = mystrtol(s, &end, 0);
if (*end == '\0') { if (*end == '\0') {
if (errno != 0) { if (errno != 0) {
err_setstr(OverflowError, com_error(co, OverflowError,
"integer literal too large"); "integer literal too large");
return NULL; return NULL;
} }
return newintobject(x); return newintobject(x);
@ -818,8 +850,7 @@ com_atom(c, n)
com_addbyte(c, UNARY_CONVERT); com_addbyte(c, UNARY_CONVERT);
break; break;
case NUMBER: case NUMBER:
if ((v = parsenumber(STR(ch))) == NULL) { if ((v = parsenumber(c, STR(ch))) == NULL) {
c->c_errors++;
i = 255; i = 255;
} }
else { else {
@ -845,8 +876,7 @@ com_atom(c, n)
break; break;
default: default:
fprintf(stderr, "node type %d\n", TYPE(ch)); fprintf(stderr, "node type %d\n", TYPE(ch));
err_setstr(SystemError, "com_atom: unexpected node type"); com_error(c, SystemError, "com_atom: unexpected node type");
c->c_errors++;
} }
} }
@ -886,9 +916,8 @@ com_argument(c, n, inkeywords)
REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */ REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */
if (NCH(n) == 1) { if (NCH(n) == 1) {
if (inkeywords) { if (inkeywords) {
err_setstr(SyntaxError, com_error(c, SyntaxError,
"non-keyword arg after keyword arg"); "non-keyword arg after keyword arg");
c->c_errors++;
} }
else { else {
com_node(c, CHILD(n, 0)); com_node(c, CHILD(n, 0));
@ -900,8 +929,7 @@ com_argument(c, n, inkeywords)
m = CHILD(m, 0); m = CHILD(m, 0);
} while (NCH(m) == 1); } while (NCH(m) == 1);
if (TYPE(m) != NAME) { if (TYPE(m) != NAME) {
err_setstr(SyntaxError, "keyword can't be an expression"); com_error(c, SyntaxError, "keyword can't be an expression");
c->c_errors++;
} }
else { else {
object *v = newstringobject(STR(m)); object *v = newstringobject(STR(m));
@ -938,8 +966,7 @@ com_call_function(c, n)
nk++; nk++;
} }
if (na > 255 || nk > 255) { if (na > 255 || nk > 255) {
err_setstr(SyntaxError, "more than 255 arguments"); com_error(c, SyntaxError, "more than 255 arguments");
c->c_errors++;
} }
com_addoparg(c, CALL_FUNCTION, na | (nk << 8)); com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
} }
@ -1071,9 +1098,8 @@ com_apply_trailer(c, n)
com_subscriptlist(c, CHILD(n, 1), OP_APPLY); com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
break; break;
default: default:
err_setstr(SystemError, com_error(c, SystemError,
"com_apply_trailer: unknown trailer type"); "com_apply_trailer: unknown trailer type");
c->c_errors++;
} }
} }
@ -1141,9 +1167,8 @@ com_term(c, n)
op = BINARY_MODULO; op = BINARY_MODULO;
break; break;
default: default:
err_setstr(SystemError, com_error(c, SystemError,
"com_term: operator not *, / or %"); "com_term: operator not *, / or %");
c->c_errors++;
op = 255; op = 255;
} }
com_addbyte(c, op); com_addbyte(c, op);
@ -1169,9 +1194,8 @@ com_arith_expr(c, n)
op = BINARY_SUBTRACT; op = BINARY_SUBTRACT;
break; break;
default: default:
err_setstr(SystemError, com_error(c, SystemError,
"com_arith_expr: operator not + or -"); "com_arith_expr: operator not + or -");
c->c_errors++;
op = 255; op = 255;
} }
com_addbyte(c, op); com_addbyte(c, op);
@ -1197,9 +1221,8 @@ com_shift_expr(c, n)
op = BINARY_RSHIFT; op = BINARY_RSHIFT;
break; break;
default: default:
err_setstr(SystemError, com_error(c, SystemError,
"com_shift_expr: operator not << or >>"); "com_shift_expr: operator not << or >>");
c->c_errors++;
op = 255; op = 255;
} }
com_addbyte(c, op); com_addbyte(c, op);
@ -1221,9 +1244,8 @@ com_and_expr(c, n)
op = BINARY_AND; op = BINARY_AND;
} }
else { else {
err_setstr(SystemError, com_error(c, SystemError,
"com_and_expr: operator not &"); "com_and_expr: operator not &");
c->c_errors++;
op = 255; op = 255;
} }
com_addbyte(c, op); com_addbyte(c, op);
@ -1245,9 +1267,8 @@ com_xor_expr(c, n)
op = BINARY_XOR; op = BINARY_XOR;
} }
else { else {
err_setstr(SystemError, com_error(c, SystemError,
"com_xor_expr: operator not ^"); "com_xor_expr: operator not ^");
c->c_errors++;
op = 255; op = 255;
} }
com_addbyte(c, op); com_addbyte(c, op);
@ -1269,9 +1290,8 @@ com_expr(c, n)
op = BINARY_OR; op = BINARY_OR;
} }
else { else {
err_setstr(SystemError, com_error(c, SystemError,
"com_expr: expr operator not |"); "com_expr: expr operator not |");
c->c_errors++;
op = 255; op = 255;
} }
com_addbyte(c, op); com_addbyte(c, op);
@ -1366,9 +1386,8 @@ com_comparison(c, n)
} }
op = cmp_type(CHILD(n, i-1)); op = cmp_type(CHILD(n, i-1));
if (op == BAD) { if (op == BAD) {
err_setstr(SystemError, com_error(c, SystemError,
"com_comparison: unknown comparison op"); "com_comparison: unknown comparison op");
c->c_errors++;
} }
com_addoparg(c, COMPARE_OP, op); com_addoparg(c, COMPARE_OP, op);
if (i+2 < NCH(n)) { if (i+2 < NCH(n)) {
@ -1504,8 +1523,7 @@ com_assign_trailer(c, n, assigning)
REQ(n, trailer); REQ(n, trailer);
switch (TYPE(CHILD(n, 0))) { switch (TYPE(CHILD(n, 0))) {
case LPAR: /* '(' [exprlist] ')' */ case LPAR: /* '(' [exprlist] ')' */
err_setstr(SyntaxError, "can't assign to function call"); com_error(c, SyntaxError, "can't assign to function call");
c->c_errors++;
break; break;
case DOT: /* '.' NAME */ case DOT: /* '.' NAME */
com_assign_attr(c, CHILD(n, 1), assigning); com_assign_attr(c, CHILD(n, 1), assigning);
@ -1514,8 +1532,7 @@ com_assign_trailer(c, n, assigning)
com_subscriptlist(c, CHILD(n, 1), assigning); com_subscriptlist(c, CHILD(n, 1), assigning);
break; break;
default: default:
err_setstr(SystemError, "unknown trailer type"); com_error(c, SystemError, "unknown trailer type");
c->c_errors++;
} }
} }
@ -1588,9 +1605,8 @@ com_assign(c, n, assigning)
case term: case term:
case factor: case factor:
if (NCH(n) > 1) { if (NCH(n) > 1) {
err_setstr(SyntaxError, com_error(c, SyntaxError,
"can't assign to operator"); "can't assign to operator");
c->c_errors++;
return; return;
} }
n = CHILD(n, 0); n = CHILD(n, 0);
@ -1599,9 +1615,8 @@ com_assign(c, n, assigning)
case power: /* atom trailer* ('**' power)* */ case power: /* atom trailer* ('**' power)* */
/* ('+'|'-'|'~') factor | atom trailer* */ /* ('+'|'-'|'~') factor | atom trailer* */
if (TYPE(CHILD(n, 0)) != atom) { if (TYPE(CHILD(n, 0)) != atom) {
err_setstr(SyntaxError, com_error(c, SyntaxError,
"can't assign to operator"); "can't assign to operator");
c->c_errors++;
return; return;
} }
if (NCH(n) > 1) { /* trailer or exponent present */ if (NCH(n) > 1) { /* trailer or exponent present */
@ -1609,9 +1624,8 @@ com_assign(c, n, assigning)
com_node(c, CHILD(n, 0)); com_node(c, CHILD(n, 0));
for (i = 1; i+1 < NCH(n); i++) { for (i = 1; i+1 < NCH(n); i++) {
if (TYPE(CHILD(n, i)) == DOUBLESTAR) { if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
err_setstr(SyntaxError, com_error(c, SyntaxError,
"can't assign to operator"); "can't assign to operator");
c->c_errors++;
return; return;
} }
com_apply_trailer(c, CHILD(n, i)); com_apply_trailer(c, CHILD(n, i));
@ -1629,18 +1643,16 @@ com_assign(c, n, assigning)
n = CHILD(n, 1); n = CHILD(n, 1);
if (TYPE(n) == RPAR) { if (TYPE(n) == RPAR) {
/* XXX Should allow () = () ??? */ /* XXX Should allow () = () ??? */
err_setstr(SyntaxError, com_error(c, SyntaxError,
"can't assign to ()"); "can't assign to ()");
c->c_errors++;
return; return;
} }
break; break;
case LSQB: case LSQB:
n = CHILD(n, 1); n = CHILD(n, 1);
if (TYPE(n) == RSQB) { if (TYPE(n) == RSQB) {
err_setstr(SyntaxError, com_error(c, SyntaxError,
"can't assign to []"); "can't assign to []");
c->c_errors++;
return; return;
} }
com_assign_list(c, n, assigning); com_assign_list(c, n, assigning);
@ -1649,22 +1661,19 @@ com_assign(c, n, assigning)
com_assign_name(c, CHILD(n, 0), assigning); com_assign_name(c, CHILD(n, 0), assigning);
return; return;
default: default:
err_setstr(SyntaxError, com_error(c, SyntaxError,
"can't assign to literal"); "can't assign to literal");
c->c_errors++;
return; return;
} }
break; break;
case lambdef: case lambdef:
err_setstr(SyntaxError, "can't assign to lambda"); com_error(c, SyntaxError, "can't assign to lambda");
c->c_errors++;
return; return;
default: default:
fprintf(stderr, "node type %d\n", TYPE(n)); fprintf(stderr, "node type %d\n", TYPE(n));
err_setstr(SystemError, "com_assign: bad node"); com_error(c, SystemError, "com_assign: bad node");
c->c_errors++;
return; return;
} }
@ -1717,8 +1726,7 @@ com_return_stmt(c, n)
{ {
REQ(n, return_stmt); /* 'return' [testlist] */ REQ(n, return_stmt); /* 'return' [testlist] */
if (!c->c_infunction) { if (!c->c_infunction) {
err_setstr(SyntaxError, "'return' outside function"); com_error(c, SyntaxError, "'return' outside function");
c->c_errors++;
} }
if (NCH(n) < 2) if (NCH(n) < 2)
com_addoparg(c, LOAD_CONST, com_addconst(c, None)); com_addoparg(c, LOAD_CONST, com_addconst(c, None));
@ -1787,8 +1795,7 @@ com_global_stmt(c, n)
s = buffer; s = buffer;
#endif #endif
if (dictlookup(c->c_locals, s) != NULL) { if (dictlookup(c->c_locals, s) != NULL) {
err_setstr(SyntaxError, "name is local and global"); com_error(c, SyntaxError, "name is local and global");
c->c_errors++;
} }
else if (dictinsert(c->c_globals, s, None) != 0) else if (dictinsert(c->c_globals, s, None) != 0)
c->c_errors++; c->c_errors++;
@ -1805,8 +1812,7 @@ com_newlocal_o(c, nameval)
if (getlistsize(c->c_varnames) != c->c_nlocals) { if (getlistsize(c->c_varnames) != c->c_nlocals) {
/* This is usually caused by an error on a previous call */ /* This is usually caused by an error on a previous call */
if (c->c_errors == 0) { if (c->c_errors == 0) {
err_setstr(SystemError, "mixed up var name/index"); com_error(c, SystemError, "mixed up var name/index");
c->c_errors++;
} }
return 0; return 0;
} }
@ -2110,9 +2116,8 @@ com_try_except(c, n)
i += 3) { i += 3) {
/* except_clause: 'except' [expr [',' expr]] */ /* except_clause: 'except' [expr [',' expr]] */
if (except_anchor == 0) { if (except_anchor == 0) {
err_setstr(SyntaxError, com_error(c, SyntaxError,
"default 'except:' must be last"); "default 'except:' must be last");
c->c_errors++;
break; break;
} }
except_anchor = 0; except_anchor = 0;
@ -2272,8 +2277,7 @@ com_continue_stmt(c, n)
com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
} }
else { else {
err_setstr(SyntaxError, "'continue' not properly in loop"); com_error(c, SyntaxError, "'continue' not properly in loop");
c->c_errors++;
} }
/* XXX Could allow it inside a 'finally' clause /* XXX Could allow it inside a 'finally' clause
XXX if we could pop the exception still on the stack */ XXX if we could pop the exception still on the stack */
@ -2459,8 +2463,7 @@ com_node(c, n)
break; break;
case break_stmt: case break_stmt:
if (c->c_loops == 0) { if (c->c_loops == 0) {
err_setstr(SyntaxError, "'break' outside loop"); com_error(c, SyntaxError, "'break' outside loop");
c->c_errors++;
} }
com_addbyte(c, BREAK_LOOP); com_addbyte(c, BREAK_LOOP);
break; break;
@ -2553,8 +2556,7 @@ com_node(c, n)
default: default:
fprintf(stderr, "node type %d\n", TYPE(n)); fprintf(stderr, "node type %d\n", TYPE(n));
err_setstr(SystemError, "com_node: unexpected node type"); com_error(c, SystemError, "com_node: unexpected node type");
c->c_errors++;
} }
} }
@ -2827,8 +2829,8 @@ compile_node(c, n)
default: default:
fprintf(stderr, "node type %d\n", TYPE(n)); fprintf(stderr, "node type %d\n", TYPE(n));
err_setstr(SystemError, "compile_node: unexpected node type"); com_error(c, SystemError,
c->c_errors++; "compile_node: unexpected node type");
} }
} }