commit
dcf1ac0d1f
4 files changed
+72
-62
No files matched your search
+2
-2
@@ -5,7 +5,7 @@ JQ_INCS = jq_parser.h builtin.h bytecode.h compile.h execute.h \
|
|||||||
jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h parser.y \
|
jv_parse.h jv_unicode.h locfile.h opcode.h opcode_list.h parser.y \
|
||||||
jv_utf8_tables.h lexer.l
|
jv_utf8_tables.h lexer.l
|
||||||
|
|
||||||
JQ_SRC = opcode.c bytecode.c compile.c execute.c builtin.c jv.c \
|
JQ_SRC = locfile.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c \
|
||||||
jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c \
|
jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c \
|
||||||
jq_test.c ${JQ_INCS}
|
jq_test.c ${JQ_INCS}
|
||||||
|
|
||||||
@@ -123,4 +123,4 @@ if ENABLE_DOCS
|
|||||||
# 'make clean' doesn't delete the manpage if it can't be rebuilt
|
# 'make clean' doesn't delete the manpage if it can't be rebuilt
|
||||||
clean-local:
|
clean-local:
|
||||||
rm -f jq.1
|
rm -f jq.1
|
||||||
endif
|
endif
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include "jv_alloc.h"
|
||||||
|
#include "locfile.h"
|
||||||
|
|
||||||
|
|
||||||
|
void locfile_init(struct locfile* l, const char* data, int length) {
|
||||||
|
l->data = data;
|
||||||
|
l->length = length;
|
||||||
|
l->nlines = 1;
|
||||||
|
for (int i=0; i<length; i++) {
|
||||||
|
if (data[i] == '\n') l->nlines++;
|
||||||
|
}
|
||||||
|
l->linemap = jv_mem_alloc(sizeof(int) * (l->nlines + 1));
|
||||||
|
l->linemap[0] = 0;
|
||||||
|
int line = 1;
|
||||||
|
for (int i=0; i<length; i++) {
|
||||||
|
if (data[i] == '\n') {
|
||||||
|
l->linemap[line] = i+1; // at start of line, not of \n
|
||||||
|
line++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l->linemap[l->nlines] = length+1; // virtual last \n
|
||||||
|
}
|
||||||
|
|
||||||
|
void locfile_free(struct locfile* l) {
|
||||||
|
jv_mem_free(l->linemap);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int locfile_get_line(struct locfile* l, int pos) {
|
||||||
|
assert(pos < l->length);
|
||||||
|
int line = 1;
|
||||||
|
while (l->linemap[line] <= pos) line++; // == if pos at start (before, never ==, because pos never on \n)
|
||||||
|
assert(line-1 < l->nlines);
|
||||||
|
return line-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int locfile_line_length(struct locfile* l, int line) {
|
||||||
|
assert(line < l->nlines);
|
||||||
|
return l->linemap[line+1] - l->linemap[line] -1; // -1 to omit \n
|
||||||
|
}
|
||||||
|
|
||||||
|
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
||||||
|
va_list fmtargs;
|
||||||
|
va_start(fmtargs, fmt);
|
||||||
|
vfprintf(stderr, fmt, fmtargs);
|
||||||
|
va_end(fmtargs);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
if (loc.start == -1) {
|
||||||
|
fprintf(stderr, "<unknown location>\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int startline = locfile_get_line(l, loc.start);
|
||||||
|
int offset = l->linemap[startline];
|
||||||
|
fprintf(stderr, "%.*s\n", locfile_line_length(l, startline), l->data + offset);
|
||||||
|
fprintf(stderr, "%*s", loc.start - offset, "");
|
||||||
|
for (int i = loc.start;
|
||||||
|
i < loc.end && i < offset + locfile_line_length(l, startline);
|
||||||
|
i++){
|
||||||
|
fprintf(stderr, "^");
|
||||||
|
}
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
@@ -1,10 +1,6 @@
|
|||||||
#ifndef _LOCFILE_H
|
#ifndef _LOCFILE_H
|
||||||
#define _LOCFILE_H
|
#define _LOCFILE_H
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include "jv_alloc.h"
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int start, end;
|
int start, end;
|
||||||
} location;
|
} location;
|
||||||
@@ -18,62 +14,10 @@ struct locfile {
|
|||||||
int nlines;
|
int nlines;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void locfile_init(struct locfile* l, const char* data, int length) {
|
void locfile_init(struct locfile* l, const char* data, int length);
|
||||||
l->data = data;
|
|
||||||
l->length = length;
|
|
||||||
l->nlines = 1;
|
|
||||||
for (int i=0; i<length; i++) {
|
|
||||||
if (data[i] == '\n') l->nlines++;
|
|
||||||
}
|
|
||||||
l->linemap = jv_mem_alloc(sizeof(int) * (l->nlines + 1));
|
|
||||||
l->linemap[0] = 0;
|
|
||||||
int line = 1;
|
|
||||||
for (int i=0; i<length; i++) {
|
|
||||||
if (data[i] == '\n') {
|
|
||||||
l->linemap[line] = i;
|
|
||||||
line++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
l->linemap[l->nlines] = length;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void locfile_free(struct locfile* l) {
|
void locfile_free(struct locfile* l);
|
||||||
jv_mem_free(l->linemap);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int locfile_get_line(struct locfile* l, int pos) {
|
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...);
|
||||||
assert(pos < l->length);
|
|
||||||
int line = 0;
|
|
||||||
while (l->linemap[line+1] < pos) line++;
|
|
||||||
assert(line < l->nlines);
|
|
||||||
return line;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int locfile_line_length(struct locfile* l, int line) {
|
|
||||||
assert(line < l->nlines);
|
|
||||||
return l->linemap[line+1] - l->linemap[line];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
|
||||||
va_list fmtargs;
|
|
||||||
va_start(fmtargs, fmt);
|
|
||||||
vfprintf(stderr, fmt, fmtargs);
|
|
||||||
va_end(fmtargs);
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
if (loc.start == -1) {
|
|
||||||
fprintf(stderr, "<unknown location>\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int startline = locfile_get_line(l, loc.start);
|
|
||||||
int offset = l->linemap[startline];
|
|
||||||
fprintf(stderr, "%.*s\n", locfile_line_length(l, startline), l->data + offset);
|
|
||||||
fprintf(stderr, "%*s", loc.start - offset, "");
|
|
||||||
for (int i = loc.start;
|
|
||||||
i < loc.end && i < offset + locfile_line_length(l, startline);
|
|
||||||
i++){
|
|
||||||
fprintf(stderr, "^");
|
|
||||||
}
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user