git.haldean.org expel / e98a6c9
show failing line in syntax errors Haldean Brown 6 years ago
3 changed file(s) with 83 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
8181 FILE *
8282 xl_stream_fp(struct xl_stream *sp);
8383
84 /* Resets a stream to the start.
85 *
86 * In some cases this may be equivalent to closing and reopening the stream. */
87 void
88 xl_stream_reset(struct xl_stream *sp);
89
8490 /* Closes a stream. */
8591 void
8692 xl_stream_close(struct xl_stream *sp);
3030 xl_parse_context_free(struct xl_parse_context *ctx)
3131 {
3232 xl_vector_free(&ctx->allocs);
33 }
34
35 #define lis_buf_size 1024
36 void
37 _print_line_in_stream(struct xl_stream *stream, size_t line)
38 {
39 char buf[lis_buf_size];
40 size_t i;
41 size_t n_lines_seen;
42 size_t read;
43
44 xl_stream_reset(stream);
45 n_lines_seen = 0;
46
47 for (;;)
48 {
49 read = xl_stream_read(buf, stream, lis_buf_size - 1);
50 if (read == 0)
51 return;
52 buf[read] = '\0';
53
54 for (i = 0; i < lis_buf_size - 1; i++)
55 {
56 if (buf[i] == '\n')
57 {
58 n_lines_seen++;
59 if (n_lines_seen == line)
60 goto line_found;
61 }
62 }
63 }
64
65 line_found:
66 for (;;)
67 {
68 for (i++; i < lis_buf_size - 1; i++)
69 {
70 putchar(buf[i]);
71 if (buf[i] == '\n')
72 return;
73 }
74 read = xl_stream_read(buf, stream, lis_buf_size - 1);
75 if (read == 0)
76 {
77 printf("\n");
78 return;
79 }
80 i = 0;
81 }
82 }
83
84 void
85 _show_char_in_line(size_t column)
86 {
87 size_t i;
88 for (i = 0; i < column - 1; i++)
89 putchar(' ');
90 printf("^\n");
3391 }
3492
3593 no_ignore xl_error
71129 fprintf(stderr, "%s:%lu:%lu: error: %s\n",
72130 source_name, ctx.err_loc->line_start,
73131 ctx.err_loc->col_start, ctx.err_msg);
132 _print_line_in_stream(stream, ctx.err_loc->line_start - 1);
133 _show_char_in_line(ctx.err_loc->col_start);
74134 free(ctx.err_loc);
75135 free(ctx.err_msg);
76136 }
202202 }
203203 return NULL;
204204 }
205
206 void
207 xl_stream_reset(struct xl_stream *sp)
208 {
209 switch (sp->stream_type)
210 {
211 case STREAM_TYPE_FILE_R:
212 case STREAM_TYPE_FILE_W:
213 rewind(sp->file);
214 return;
215
216 case STREAM_TYPE_BUFFER:
217 sp->buffer->read = sp->buffer->start;
218 sp->buffer->write = sp->buffer->start;
219 return;
220 }
221 }