Save line number where the token starts for improved error messages.

This commit is contained in:
Tor Andersson
2014-01-18 15:05:43 +01:00
parent 442b8d27a1
commit 7bf5257d00
3 changed files with 5 additions and 3 deletions

View File

@@ -423,6 +423,7 @@ static int lex(js_State *J, const char **sp)
J->newline = 0;
while (1) {
J->lexline = J->line; /* save location of beginning of token */
int c = NEXT();
while (iswhite(c))

View File

@@ -31,7 +31,7 @@ js_Ast *jsP_newnode(js_State *J, int type, js_Ast *a, js_Ast *b, js_Ast *c, js_A
js_Ast *node = malloc(sizeof(js_Ast));
node->type = type;
node->line = J->line;
node->line = J->lexline;
node->a = a;
node->b = b;
node->c = c;
@@ -842,7 +842,7 @@ void jsP_warning(js_State *J, const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s:%d: warning: ", J->filename, J->line);
fprintf(stderr, "%s:%d: warning: ", J->filename, J->lexline);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
@@ -853,7 +853,7 @@ int jsP_error(js_State *J, const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s:%d: error: ", J->filename, J->line);
fprintf(stderr, "%s:%d: error: ", J->filename, J->lexline);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);

View File

@@ -19,6 +19,7 @@ struct js_State
/* lexer */
struct { char *text; size_t len, cap; } buf;
int lexline;
int lasttoken;
int newline;