Skip to content

Commit

Permalink
Add initial line/column calculation for error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mingodad committed Jun 1, 2022
1 parent 5ec5d0d commit 4530bd4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
28 changes: 22 additions & 6 deletions include/chpeg/chpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1639,12 +1639,28 @@ CHPEG_API void ChpegParser_expected(ChpegParser *self, int parent_def, int def,
}
}

static void getLineColum(const unsigned char *input, int offset, int *line_out, int *col_out) {
int i, line = 1;
for(i=0; i <= offset; ++i) {
if(input[i] == '\n')
++line;
}
for(i=offset; i >= 0; --i) {
if(input[i] == '\n')
break;
}
*line_out = line;
*col_out = offset - i;
}

CHPEG_API void ChpegParser_print_error(ChpegParser *self, const unsigned char *input)
{
#if ERRORS
const char *parent_def_name = ChpegByteCode_def_name(self->bc, self->error_parent_def);
const char *def_name = ChpegByteCode_def_name(self->bc, self->error_def);

int line, col;
getLineColum(input, self->error_offset, &line, &col);
if (self->error_expected >= 0) {
if (self->error_inst >= 0) {
int op = CHPEG_INST_OP(self->error_inst);
Expand All @@ -1671,29 +1687,29 @@ CHPEG_API void ChpegParser_print_error(ChpegParser *self, const unsigned char *i
str = buf;
break;
}
fprintf(stderr, "%s \"%s\" in %s at offset %lu\n",
fprintf(stderr, "%s \"%s\" in %s at:%d:%d offset %lu\n",
self->error_expected ? "Expected" : "Unexpected",
str ? str : (esc ? esc : "<NULL>"),
def_name ? def_name : "<N/A>",
self->error_offset);
line, col, self->error_offset);
if (esc) {
CHPEG_FREE(esc);
esc = NULL;
}
}
else {
if (parent_def_name) {
fprintf(stderr, "%s %s in %s at offset %lu\n",
fprintf(stderr, "%s %s in %s at:%d:%d offset %lu\n",
self->error_expected ? "Expected" : "Unexpected",
def_name ? def_name : "<N/A>",
parent_def_name ? parent_def_name : "<N/A>",
self->error_offset);
line, col, self->error_offset);
}
else {
fprintf(stderr, "%s %s at offset %lu\n",
fprintf(stderr, "%s %s at:%d:%d offset %lu\n",
self->error_expected ? "Expected" : "Unexpected",
def_name ? def_name : "<N/A>",
self->error_offset);
line, col, self->error_offset);
}
}
}
Expand Down
28 changes: 22 additions & 6 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,28 @@ CHPEG_API void ChpegParser_expected(ChpegParser *self, int parent_def, int def,
}
}

static void getLineColum(const unsigned char *input, int offset, int *line_out, int *col_out) {
int i, line = 1;
for(i=0; i <= offset; ++i) {
if(input[i] == '\n')
++line;
}
for(i=offset; i >= 0; --i) {
if(input[i] == '\n')
break;
}
*line_out = line;
*col_out = offset - i;
}

CHPEG_API void ChpegParser_print_error(ChpegParser *self, const unsigned char *input)
{
#if ERRORS
const char *parent_def_name = ChpegByteCode_def_name(self->bc, self->error_parent_def);
const char *def_name = ChpegByteCode_def_name(self->bc, self->error_def);

int line, col;
getLineColum(input, self->error_offset, &line, &col);
if (self->error_expected >= 0) {
if (self->error_inst >= 0) {
int op = CHPEG_INST_OP(self->error_inst);
Expand All @@ -270,29 +286,29 @@ CHPEG_API void ChpegParser_print_error(ChpegParser *self, const unsigned char *i
str = buf;
break;
}
fprintf(stderr, "%s \"%s\" in %s at offset %lu\n",
fprintf(stderr, "%s \"%s\" in %s at:%d:%d offset %lu\n",
self->error_expected ? "Expected" : "Unexpected",
str ? str : (esc ? esc : "<NULL>"),
def_name ? def_name : "<N/A>",
self->error_offset);
line, col, self->error_offset);
if (esc) {
CHPEG_FREE(esc);
esc = NULL;
}
}
else {
if (parent_def_name) {
fprintf(stderr, "%s %s in %s at offset %lu\n",
fprintf(stderr, "%s %s in %s at:%d:%d offset %lu\n",
self->error_expected ? "Expected" : "Unexpected",
def_name ? def_name : "<N/A>",
parent_def_name ? parent_def_name : "<N/A>",
self->error_offset);
line, col, self->error_offset);
}
else {
fprintf(stderr, "%s %s at offset %lu\n",
fprintf(stderr, "%s %s at:%d:%d offset %lu\n",
self->error_expected ? "Expected" : "Unexpected",
def_name ? def_name : "<N/A>",
self->error_offset);
line, col, self->error_offset);
}
}
}
Expand Down

0 comments on commit 4530bd4

Please sign in to comment.