Commit dee7ec4a authored by Chris Müller's avatar Chris Müller
Browse files

Add line information for error messages

parent 52ffcd23
...@@ -31,6 +31,7 @@ struct org_cherry_context { ...@@ -31,6 +31,7 @@ struct org_cherry_context {
const char* filename; const char* filename;
const cy_byte_t* begin; const cy_byte_t* begin;
const cy_byte_t* src; const cy_byte_t* src;
uint32_t line;
struct org_cherry_array* buffer; struct org_cherry_array* buffer;
cy_flags_t flags; cy_flags_t flags;
}; };
...@@ -38,6 +39,7 @@ struct org_cherry_context { ...@@ -38,6 +39,7 @@ struct org_cherry_context {
struct org_cherry_context* org_cherry_context(const cy_byte_t* source, const char* filename, cy_flags_t flags); struct org_cherry_context* org_cherry_context(const cy_byte_t* source, const char* filename, cy_flags_t flags);
struct org_cherry_context* org_cherry_context_repl(const cy_byte_t* source); struct org_cherry_context* org_cherry_context_repl(const cy_byte_t* source);
void org_cherry_context_repl_set_source(struct org_cherry_context* c, const cy_byte_t* source);
void org_cherry_error(struct org_cherry_context* context, const char* format, ...); void org_cherry_error(struct org_cherry_context* context, const char* format, ...);
......
...@@ -86,10 +86,10 @@ org_cherry_start_repl(void) ...@@ -86,10 +86,10 @@ org_cherry_start_repl(void)
org_cherry_env_push_exception_point(env); org_cherry_env_push_exception_point(env);
cy_byte_t* line = (cy_byte_t*) readline("> ");
struct org_cherry_context* context = org_cherry_context_repl(line);
while(1) { while(1) {
cy_byte_t* line = (cy_byte_t*) readline("> ");
struct org_cherry_context* context = org_cherry_context_repl(line);
struct org_cherry_value* exp = org_cherry_read(context); struct org_cherry_value* exp = org_cherry_read(context);
if(!setjmp(EXCEPTION_JUMP(env))) { if(!setjmp(EXCEPTION_JUMP(env))) {
...@@ -102,6 +102,9 @@ org_cherry_start_repl(void) ...@@ -102,6 +102,9 @@ org_cherry_start_repl(void)
} }
free(line); free(line);
line = (cy_byte_t*) readline("> ");
org_cherry_context_repl_set_source(context, line);
} }
org_cherry_env_pop_exception_point(env); org_cherry_env_pop_exception_point(env);
......
...@@ -33,6 +33,7 @@ org_cherry_context(const cy_byte_t* source, const char* filename, cy_flags_t fla ...@@ -33,6 +33,7 @@ org_cherry_context(const cy_byte_t* source, const char* filename, cy_flags_t fla
context->filename = filename; context->filename = filename;
context->begin = source; context->begin = source;
context->src = (cy_byte_t*) source; context->src = (cy_byte_t*) source;
context->line = 1;
context->buffer = org_cherry_array_new(sizeof(cy_byte_t)); context->buffer = org_cherry_array_new(sizeof(cy_byte_t));
context->flags = flags; context->flags = flags;
...@@ -47,6 +48,16 @@ org_cherry_context_repl(const cy_byte_t* source) ...@@ -47,6 +48,16 @@ org_cherry_context_repl(const cy_byte_t* source)
} }
void
org_cherry_context_repl_set_source(struct org_cherry_context* c, const cy_byte_t* source)
{
assert(c != 0);
c->begin = source;
c->src = source;
c->line++;
}
void void
org_cherry_error(struct org_cherry_context* context, const char* format, ...) org_cherry_error(struct org_cherry_context* context, const char* format, ...)
{ {
...@@ -54,11 +65,11 @@ org_cherry_error(struct org_cherry_context* context, const char* format, ...) ...@@ -54,11 +65,11 @@ org_cherry_error(struct org_cherry_context* context, const char* format, ...)
va_start(args, format); va_start(args, format);
if(context->filename != 0) if(context->filename != 0)
fprintf(stderr, "ERROR %s", context->filename); fprintf(stderr, "ERROR (%s:%d)", context->filename, context->line);
else else
fprintf(stderr, "ERROR (console)"); fprintf(stderr, "ERROR (console:%d)", context->line);
fprintf(stderr, ": "); fprintf(stderr, " --- ");
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
...@@ -600,7 +611,7 @@ org_cherry_lex(struct org_cherry_context* context) ...@@ -600,7 +611,7 @@ org_cherry_lex(struct org_cherry_context* context)
cy_unicode_t ch = org_cherry_utf8_get(p); cy_unicode_t ch = org_cherry_utf8_get(p);
if(org_cherry_unicode_isspace(ch)) { if(org_cherry_unicode_isspace(ch) && (ch != '\n' || ch != '\r')) {
p = org_cherry_utf8_next(p); p = org_cherry_utf8_next(p);
context->src = p; context->src = p;
continue; continue;
...@@ -609,6 +620,16 @@ org_cherry_lex(struct org_cherry_context* context) ...@@ -609,6 +620,16 @@ org_cherry_lex(struct org_cherry_context* context)
switch(ch) { switch(ch) {
case '\0': case '\0':
return TOK_EOF; return TOK_EOF;
case '\r':
if(*org_cherry_utf8_next(p) == '\n')
p++;
break;
case '\n':
context->line++;
break;
case '(': case '(':
LEX_RETURN(TOK_ROUNDLEFTBRACE); LEX_RETURN(TOK_ROUNDLEFTBRACE);
case ')': case ')':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment