1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Source mapping v4 (line and column instead of byte offsets).

This commit is contained in:
Calvin Rose 2018-06-29 11:12:33 -04:00
parent f93c84c21f
commit 28cf753fa2
8 changed files with 65 additions and 41 deletions

View File

@ -709,8 +709,8 @@ static DstAssembleResult dst_asm1(DstAssembler *parent, Dst source, int flags) {
if (!dst_checktype(tup[1], DST_INTEGER)) { if (!dst_checktype(tup[1], DST_INTEGER)) {
dst_asm_error(&a, "expected integer"); dst_asm_error(&a, "expected integer");
} }
mapping.start = dst_unwrap_integer(tup[0]); mapping.line = dst_unwrap_integer(tup[0]);
mapping.end = dst_unwrap_integer(tup[1]); mapping.column = dst_unwrap_integer(tup[1]);
def->sourcemap[i] = mapping; def->sourcemap[i] = mapping;
} }
} }
@ -868,8 +868,8 @@ Dst dst_disasm(DstFuncDef *def) {
for (i = 0; i < def->bytecode_length; i++) { for (i = 0; i < def->bytecode_length; i++) {
Dst *t = dst_tuple_begin(2); Dst *t = dst_tuple_begin(2);
DstSourceMapping mapping = def->sourcemap[i]; DstSourceMapping mapping = def->sourcemap[i];
t[0] = dst_wrap_integer(mapping.start); t[0] = dst_wrap_integer(mapping.line);
t[1] = dst_wrap_integer(mapping.end); t[1] = dst_wrap_integer(mapping.column);
sourcemap->data[i] = dst_wrap_tuple(dst_tuple_end(t)); sourcemap->data[i] = dst_wrap_tuple(dst_tuple_end(t));
} }
sourcemap->count = def->bytecode_length; sourcemap->count = def->bytecode_length;

View File

@ -958,7 +958,9 @@ onvalue."
(while going (while going
(switch (parser.status p) (switch (parser.status p)
:full (yield (parser.produce p)) :full (yield (parser.produce p))
:error (onerr where "parse" (string (parser.error p) " at byte index " byteindex)) :error (do
(def (line col) (parser.where p))
(onerr where "parse" (string (parser.error p) " on line " line ", column " col)))
(switch (fiber.status chars) (switch (fiber.status chars)
:new (parser.byte p (resume chars)) :new (parser.byte p (resume chars))
:pending (parser.byte p (resume chars)) :pending (parser.byte p (resume chars))
@ -975,10 +977,10 @@ onvalue."
(res) (res)
(do (do
(:= good false) (:= good false)
(def {:error err :error-start errs :error-end erre} res) (def {:error err :error-line errl :error-column errc} res)
(onerr where "compile" (onerr where "compile"
(if (<= 0 errs) (if (< 0 errl)
(string err " at (" errs ":" erre ")") (string err " on line " errl ", column " errc)
err))))) err)))))
:a)) :a))
(def res (resume f)) (def res (resume f))
@ -1013,8 +1015,8 @@ onvalue."
:c c :c c
:name name :name name
:source source :source source
:source-start source-start :source-line source-line
:source-end source-end :source-column source-col
} :in st] } :in st]
(file.write stdout " in") (file.write stdout " in")
(when c (file.write stdout " cfunction")) (when c (file.write stdout " cfunction"))
@ -1024,8 +1026,8 @@ onvalue."
(if source (if source
(do (do
(file.write stdout " [" source "]") (file.write stdout " [" source "]")
(if source-start (file.write stdout " at byte range (" (if source-line (file.write stdout " on line "
(string source-start) ":" (string source-end) ")"))) (string source-line) ", column " (string source-col))))
(if pc (file.write stdout " at (pc=" (string pc) ")"))) (if pc (file.write stdout " at (pc=" (string pc) ")")))
(when tail (file.write stdout " (tailcall)")) (when tail (file.write stdout " (tailcall)"))
(file.write stdout "\n")))) (file.write stdout "\n"))))

View File

@ -35,11 +35,11 @@ static void dstc_ast_push(DstCompiler *c, const Dst *tup) {
if (c->result.status == DST_COMPILE_ERROR) { if (c->result.status == DST_COMPILE_ERROR) {
return; return;
} }
mapping.start = dst_tuple_sm_start(tup); mapping.line = dst_tuple_sm_line(tup);
mapping.end = dst_tuple_sm_end(tup); mapping.column = dst_tuple_sm_col(tup);
if (mapping.start < 0 || mapping.end < 0) { if (!mapping.line) {
/* Reuse previous mapping */ /* Reuse previous mapping */
mapping = dst_v_last(c->ast_stack); mapping = c->current_mapping;
} }
dst_v_push(c->ast_stack, mapping); dst_v_push(c->ast_stack, mapping);
c->current_mapping = mapping; c->current_mapping = mapping;
@ -53,8 +53,8 @@ static void dstc_ast_pop(DstCompiler *c) {
if (dst_v_count(c->ast_stack)) { if (dst_v_count(c->ast_stack)) {
c->current_mapping = dst_v_last(c->ast_stack); c->current_mapping = dst_v_last(c->ast_stack);
} else { } else {
c->current_mapping.start = -1; c->current_mapping.line = 0;
c->current_mapping.end = -1; c->current_mapping.column = 0;
} }
} }
@ -964,14 +964,14 @@ static void dstc_init(DstCompiler *c, DstTable *env, const uint8_t *where) {
c->env = env; c->env = env;
c->source = where; c->source = where;
c->ast_stack = NULL; c->ast_stack = NULL;
c->current_mapping.start = -1; c->current_mapping.line = 0;
c->current_mapping.end = -1; c->current_mapping.column = 0;
/* Init result */ /* Init result */
c->result.error = NULL; c->result.error = NULL;
c->result.status = DST_COMPILE_OK; c->result.status = DST_COMPILE_OK;
c->result.funcdef = NULL; c->result.funcdef = NULL;
c->result.error_mapping.start = -1; c->result.error_mapping.line = 0;
c->result.error_mapping.end = -1; c->result.error_mapping.column = 0;
} }
/* Deinitialize a compiler struct */ /* Deinitialize a compiler struct */
@ -1033,8 +1033,8 @@ int dst_compile_cfun(DstArgs args) {
} else { } else {
t = dst_table(2); t = dst_table(2);
dst_table_put(t, dst_csymbolv(":error"), dst_wrap_string(res.error)); dst_table_put(t, dst_csymbolv(":error"), dst_wrap_string(res.error));
dst_table_put(t, dst_csymbolv(":error-start"), dst_wrap_integer(res.error_mapping.start)); dst_table_put(t, dst_csymbolv(":error-line"), dst_wrap_integer(res.error_mapping.line));
dst_table_put(t, dst_csymbolv(":error-end"), dst_wrap_integer(res.error_mapping.end)); dst_table_put(t, dst_csymbolv(":error-column"), dst_wrap_integer(res.error_mapping.column));
DST_RETURN_TABLE(args, t); DST_RETURN_TABLE(args, t);
} }
} }

View File

@ -365,8 +365,8 @@ static Dst doframe(DstStackFrame *frame) {
dst_table_put(t, dst_csymbolv(":pc"), dst_wrap_integer(off)); dst_table_put(t, dst_csymbolv(":pc"), dst_wrap_integer(off));
if (def->sourcemap) { if (def->sourcemap) {
DstSourceMapping mapping = def->sourcemap[off]; DstSourceMapping mapping = def->sourcemap[off];
dst_table_put(t, dst_csymbolv(":source-start"), dst_wrap_integer(mapping.start)); dst_table_put(t, dst_csymbolv(":source-line"), dst_wrap_integer(mapping.line));
dst_table_put(t, dst_csymbolv(":source-end"), dst_wrap_integer(mapping.end)); dst_table_put(t, dst_csymbolv(":source-column"), dst_wrap_integer(mapping.column));
} }
if (def->source) { if (def->source) {
dst_table_put(t, dst_csymbolv(":source"), dst_wrap_string(def->source)); dst_table_put(t, dst_csymbolv(":source"), dst_wrap_string(def->source));

View File

@ -112,7 +112,8 @@ struct DstParseState {
int32_t qcount; int32_t qcount;
int32_t argn; int32_t argn;
int flags; int flags;
size_t start; size_t start_line;
size_t start_col;
Consumer consumer; Consumer consumer;
}; };
@ -155,7 +156,8 @@ static void pushstate(DstParser *p, Consumer consumer, int flags) {
s.argn = 0; s.argn = 0;
s.flags = flags; s.flags = flags;
s.consumer = consumer; s.consumer = consumer;
s.start = p->index; s.start_line = p->line;
s.start_col = p->col;
_pushstate(p, s); _pushstate(p, s);
} }
@ -168,8 +170,8 @@ static void popstate(DstParser *p, Dst val) {
/* Quote the returned value qcount times */ /* Quote the returned value qcount times */
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (dst_checktype(val, DST_TUPLE)) { if (dst_checktype(val, DST_TUPLE)) {
dst_tuple_sm_start(dst_unwrap_tuple(val)) = (int32_t) top.start; dst_tuple_sm_line(dst_unwrap_tuple(val)) = (int32_t) top.start_line;
dst_tuple_sm_end(dst_unwrap_tuple(val)) = (int32_t) p->index; dst_tuple_sm_col(dst_unwrap_tuple(val)) = (int32_t) top.start_col;
} }
val = quote(val); val = quote(val);
} }
@ -177,8 +179,8 @@ static void popstate(DstParser *p, Dst val) {
/* Ast wrap */ /* Ast wrap */
if (dst_checktype(val, DST_TUPLE)) { if (dst_checktype(val, DST_TUPLE)) {
dst_tuple_sm_start(dst_unwrap_tuple(val)) = (int32_t) top.start; dst_tuple_sm_line(dst_unwrap_tuple(val)) = (int32_t) top.start_line;
dst_tuple_sm_end(dst_unwrap_tuple(val)) = (int32_t) p->index; dst_tuple_sm_col(dst_unwrap_tuple(val)) = (int32_t) top.start_col;
} }
newtop->argn++; newtop->argn++;
@ -518,7 +520,12 @@ static int root(DstParser *p, DstParseState *state, uint8_t c) {
int dst_parser_consume(DstParser *parser, uint8_t c) { int dst_parser_consume(DstParser *parser, uint8_t c) {
int consumed = 0; int consumed = 0;
if (parser->error) return 0; if (parser->error) return 0;
parser->index++; if (c == '\n') {
parser->line++;
parser->col = 0;
} else if (c != '\r') {
parser->col++;
}
while (!consumed && !parser->error) { while (!consumed && !parser->error) {
DstParseState *state = parser->states + parser->statecount - 1; DstParseState *state = parser->states + parser->statecount - 1;
consumed = state->consumer(parser, state, c); consumed = state->consumer(parser, state, c);
@ -575,7 +582,8 @@ void dst_parser_init(DstParser *parser) {
parser->statecount = 0; parser->statecount = 0;
parser->statecap = 0; parser->statecap = 0;
parser->error = NULL; parser->error = NULL;
parser->index = 0; parser->line = 1;
parser->col = 0;
parser->lookback = -1; parser->lookback = -1;
pushstate(parser, root, PFLAG_CONTAINER); pushstate(parser, root, PFLAG_CONTAINER);
@ -722,6 +730,17 @@ static int cfun_flush(DstArgs args) {
DST_RETURN(args, args.v[0]); DST_RETURN(args, args.v[0]);
} }
static int cfun_where(DstArgs args) {
DstParser *p;
DST_FIXARITY(args, 1);
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
Dst *tup = dst_tuple_begin(2);
tup[0] = dst_wrap_integer((int32_t)p->line);
tup[1] = dst_wrap_integer((int32_t)p->col);
DST_RETURN_TUPLE(args, dst_tuple_end(tup));
}
static int cfun_state(DstArgs args) { static int cfun_state(DstArgs args) {
size_t i; size_t i;
const uint8_t *str; const uint8_t *str;
@ -762,6 +781,7 @@ static const DstReg cfuns[] = {
{"parser.status", cfun_status}, {"parser.status", cfun_status},
{"parser.flush", cfun_flush}, {"parser.flush", cfun_flush},
{"parser.state", cfun_state}, {"parser.state", cfun_state},
{"parser.where", cfun_where},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -33,8 +33,8 @@ Dst *dst_tuple_begin(int32_t length) {
char *data = dst_gcalloc(DST_MEMORY_TUPLE, 4 * sizeof(int32_t) + length * sizeof(Dst)); char *data = dst_gcalloc(DST_MEMORY_TUPLE, 4 * sizeof(int32_t) + length * sizeof(Dst));
Dst *tuple = (Dst *)(data + (4 * sizeof(int32_t))); Dst *tuple = (Dst *)(data + (4 * sizeof(int32_t)));
dst_tuple_length(tuple) = length; dst_tuple_length(tuple) = length;
dst_tuple_sm_start(tuple) = -1; dst_tuple_sm_line(tuple) = 0;
dst_tuple_sm_end(tuple) = -1; dst_tuple_sm_col(tuple) = 0;
return tuple; return tuple;
} }

View File

@ -80,8 +80,8 @@ int dst_buffer_push_u64(DstBuffer *buffer, uint64_t x);
#define dst_tuple_raw(t) ((int32_t *)(t) - 4) #define dst_tuple_raw(t) ((int32_t *)(t) - 4)
#define dst_tuple_length(t) (dst_tuple_raw(t)[0]) #define dst_tuple_length(t) (dst_tuple_raw(t)[0])
#define dst_tuple_hash(t) ((dst_tuple_raw(t)[1])) #define dst_tuple_hash(t) ((dst_tuple_raw(t)[1]))
#define dst_tuple_sm_start(t) ((dst_tuple_raw(t)[2])) #define dst_tuple_sm_line(t) ((dst_tuple_raw(t)[2]))
#define dst_tuple_sm_end(t) ((dst_tuple_raw(t)[3])) #define dst_tuple_sm_col(t) ((dst_tuple_raw(t)[3]))
Dst *dst_tuple_begin(int32_t length); Dst *dst_tuple_begin(int32_t length);
const Dst *dst_tuple_end(Dst *tuple); const Dst *dst_tuple_end(Dst *tuple);
const Dst *dst_tuple_n(Dst *values, int32_t n); const Dst *dst_tuple_n(Dst *values, int32_t n);

View File

@ -462,9 +462,10 @@ struct DstKV {
#define DST_FUNCDEF_FLAG_VARARG 1 #define DST_FUNCDEF_FLAG_VARARG 1
#define DST_FUNCDEF_FLAG_NEEDSENV 4 #define DST_FUNCDEF_FLAG_NEEDSENV 4
/* Source mapping structure for a bytecode instruction */
struct DstSourceMapping { struct DstSourceMapping {
int32_t start; int32_t line;
int32_t end; int32_t column;
}; };
/* A function definition. Contains information needed to instantiate closures. */ /* A function definition. Contains information needed to instantiate closures. */
@ -529,7 +530,8 @@ struct DstParser {
size_t statecap; size_t statecap;
size_t bufcount; size_t bufcount;
size_t bufcap; size_t bufcap;
size_t index; size_t line;
size_t col;
int lookback; int lookback;
}; };