mirror of
https://github.com/janet-lang/janet
synced 2024-11-17 22:24:49 +00:00
Make buffers print their contents.
This commit is contained in:
parent
5d5d88c5ad
commit
8e6ed707e7
@ -73,7 +73,7 @@ static void runfile(const uint8_t *src, int32_t len) {
|
|||||||
const uint8_t *s = src;
|
const uint8_t *s = src;
|
||||||
const uint8_t *end = src + len;
|
const uint8_t *end = src + len;
|
||||||
while (s < end) {
|
while (s < end) {
|
||||||
res = dst_parse(s, end - s);
|
res = dst_parse(s, end - s, DST_PARSEFLAG_SOURCEMAP);
|
||||||
switch (res.status) {
|
switch (res.status) {
|
||||||
case DST_PARSE_NODATA:
|
case DST_PARSE_NODATA:
|
||||||
return;
|
return;
|
||||||
|
@ -50,7 +50,7 @@ static void replonvalue(DstContext *c, Dst value) {
|
|||||||
(void) c;
|
(void) c;
|
||||||
dst_puts(dst_formatc("%v\n", value));
|
dst_puts(dst_formatc("%v\n", value));
|
||||||
if (dst_checktype(c->env, DST_TABLE))
|
if (dst_checktype(c->env, DST_TABLE))
|
||||||
dst_module_def(dst_unwrap_table(c->env), "_", dst_wrap_nil());
|
dst_module_def(dst_unwrap_table(c->env), "_", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handle errors on repl */
|
/* Handle errors on repl */
|
||||||
@ -138,7 +138,7 @@ int dst_context_run(DstContext *c) {
|
|||||||
while (!done) {
|
while (!done) {
|
||||||
DstCompileResult cres;
|
DstCompileResult cres;
|
||||||
DstCompileOptions opts;
|
DstCompileOptions opts;
|
||||||
DstParseResult res = dst_parse(c->buffer.data, c->buffer.count);
|
DstParseResult res = dst_parse(c->buffer.data, c->buffer.count, DST_PARSEFLAG_SOURCEMAP);
|
||||||
switch (res.status) {
|
switch (res.status) {
|
||||||
case DST_PARSE_NODATA:
|
case DST_PARSE_NODATA:
|
||||||
flushcontext(c);
|
flushcontext(c);
|
||||||
|
16
core/parse.c
16
core/parse.c
@ -161,6 +161,7 @@ typedef struct {
|
|||||||
const uint8_t *end;
|
const uint8_t *end;
|
||||||
const char *errmsg;
|
const char *errmsg;
|
||||||
DstParseStatus status;
|
DstParseStatus status;
|
||||||
|
int flags;
|
||||||
} ParseArgs;
|
} ParseArgs;
|
||||||
|
|
||||||
|
|
||||||
@ -397,12 +398,14 @@ static const uint8_t *parse_recur(
|
|||||||
|
|
||||||
/* Quote the returned value qcount times */
|
/* Quote the returned value qcount times */
|
||||||
while (qcount--) {
|
while (qcount--) {
|
||||||
ret = dst_ast_wrap(ret, mapstart - args->srcstart, src - args->srcstart);
|
if (args->flags & DST_PARSEFLAG_SOURCEMAP)
|
||||||
|
ret = dst_ast_wrap(ret, mapstart - args->srcstart, src - args->srcstart);
|
||||||
ret = quote(ret);
|
ret = quote(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ast wrap */
|
/* Ast wrap */
|
||||||
ret = dst_ast_wrap(ret, mapstart - args->srcstart, src - args->srcstart);
|
if (args->flags & DST_PARSEFLAG_SOURCEMAP)
|
||||||
|
ret = dst_ast_wrap(ret, mapstart - args->srcstart, src - args->srcstart);
|
||||||
|
|
||||||
/* Push the result to the stack */
|
/* Push the result to the stack */
|
||||||
dst_array_push(&args->stack, ret);
|
dst_array_push(&args->stack, ret);
|
||||||
@ -463,7 +466,7 @@ static const uint8_t *parse_recur(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Parse an array of bytes. Return value in the fiber return value. */
|
/* Parse an array of bytes. Return value in the fiber return value. */
|
||||||
DstParseResult dst_parse(const uint8_t *src, int32_t len) {
|
DstParseResult dst_parse(const uint8_t *src, int32_t len, int flags) {
|
||||||
DstParseResult res;
|
DstParseResult res;
|
||||||
ParseArgs args;
|
ParseArgs args;
|
||||||
const uint8_t *newsrc;
|
const uint8_t *newsrc;
|
||||||
@ -473,6 +476,7 @@ DstParseResult dst_parse(const uint8_t *src, int32_t len) {
|
|||||||
args.srcstart = src;
|
args.srcstart = src;
|
||||||
args.end = src + len;
|
args.end = src + len;
|
||||||
args.errmsg = NULL;
|
args.errmsg = NULL;
|
||||||
|
args.flags = flags;
|
||||||
|
|
||||||
newsrc = parse_recur(&args, src, DST_RECURSION_GUARD);
|
newsrc = parse_recur(&args, src, DST_RECURSION_GUARD);
|
||||||
res.status = args.status;
|
res.status = args.status;
|
||||||
@ -492,10 +496,10 @@ DstParseResult dst_parse(const uint8_t *src, int32_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Parse a c string */
|
/* Parse a c string */
|
||||||
DstParseResult dst_parsec(const char *src) {
|
DstParseResult dst_parsec(const char *src, int flags) {
|
||||||
int32_t len = 0;
|
int32_t len = 0;
|
||||||
while (src[len]) ++len;
|
while (src[len]) ++len;
|
||||||
return dst_parse((const uint8_t *)src, len);
|
return dst_parse((const uint8_t *)src, len, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* C Function parser */
|
/* C Function parser */
|
||||||
@ -507,7 +511,7 @@ int dst_parse_cfun(DstArgs args) {
|
|||||||
DstTable *t;
|
DstTable *t;
|
||||||
if (args.n < 1) return dst_throw(args, "expected at least on argument");
|
if (args.n < 1) return dst_throw(args, "expected at least on argument");
|
||||||
if (!dst_chararray_view(args.v[0], &src, &len)) return dst_throw(args, "expected string/buffer");
|
if (!dst_chararray_view(args.v[0], &src, &len)) return dst_throw(args, "expected string/buffer");
|
||||||
res = dst_parse(src, len);
|
res = dst_parse(src, len, 0);
|
||||||
t = dst_table(4);
|
t = dst_table(4);
|
||||||
switch (res.status) {
|
switch (res.status) {
|
||||||
case DST_PARSE_OK:
|
case DST_PARSE_OK:
|
||||||
|
@ -204,11 +204,14 @@ static const uint8_t *string_description(const char *title, void *pointer) {
|
|||||||
#undef HEX
|
#undef HEX
|
||||||
#undef DST_BUFSIZE
|
#undef DST_BUFSIZE
|
||||||
|
|
||||||
/* TODO - add more characters to escapes */
|
/* TODO - add more characters to escape.
|
||||||
static int32_t dst_escape_string_length(const uint8_t *str) {
|
*
|
||||||
|
* When more escapes are added, they must correspond
|
||||||
|
* to dst_escape_string_impl exactly or a buffer overrun could occur. */
|
||||||
|
static int32_t dst_escape_string_length(const uint8_t *str, int32_t slen) {
|
||||||
int32_t len = 2;
|
int32_t len = 2;
|
||||||
int32_t i;
|
int32_t i;
|
||||||
for (i = 0; i < dst_string_length(str); ++i) {
|
for (i = 0; i < slen; ++i) {
|
||||||
switch (str[i]) {
|
switch (str[i]) {
|
||||||
case '"':
|
case '"':
|
||||||
case '\n':
|
case '\n':
|
||||||
@ -224,10 +227,10 @@ static int32_t dst_escape_string_length(const uint8_t *str) {
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dst_escape_string_impl(uint8_t *buf, const uint8_t *str) {
|
static void dst_escape_string_impl(uint8_t *buf, const uint8_t *str, int32_t len) {
|
||||||
int32_t i, j;
|
int32_t i, j;
|
||||||
buf[0] = '"';
|
buf[0] = '"';
|
||||||
for (i = 0, j = 1; i < dst_string_length(str); ++i) {
|
for (i = 0, j = 1; i < len; ++i) {
|
||||||
uint8_t c = str[i];
|
uint8_t c = str[i];
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
@ -255,19 +258,32 @@ static void dst_escape_string_impl(uint8_t *buf, const uint8_t *str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void dst_escape_string_b(DstBuffer *buffer, const uint8_t *str) {
|
void dst_escape_string_b(DstBuffer *buffer, const uint8_t *str) {
|
||||||
int32_t len = dst_escape_string_length(str);
|
int32_t len = dst_string_length(str);
|
||||||
dst_buffer_extra(buffer, len);
|
int32_t elen = dst_escape_string_length(str, len);
|
||||||
dst_escape_string_impl(buffer->data + buffer->count, str);
|
dst_buffer_extra(buffer, elen);
|
||||||
buffer->count += len;
|
dst_escape_string_impl(buffer->data + buffer->count, str, len);
|
||||||
|
buffer->count += elen;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t *dst_escape_string(const uint8_t *str) {
|
const uint8_t *dst_escape_string(const uint8_t *str) {
|
||||||
int32_t len = dst_escape_string_length(str);
|
int32_t len = dst_string_length(str);
|
||||||
uint8_t *buf = dst_string_begin(len);
|
int32_t elen = dst_escape_string_length(str, len);
|
||||||
dst_escape_string_impl(buf, str);
|
uint8_t *buf = dst_string_begin(elen);
|
||||||
|
dst_escape_string_impl(buf, str, len);
|
||||||
return dst_string_end(buf);
|
return dst_string_end(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void dst_escape_buffer_b(DstBuffer *buffer, DstBuffer *bx) {
|
||||||
|
int32_t elen = dst_escape_string_length(bx->data, bx->count);
|
||||||
|
dst_buffer_push_u8(buffer, '@');
|
||||||
|
dst_buffer_extra(buffer, elen);
|
||||||
|
dst_escape_string_impl(
|
||||||
|
buffer->data + buffer->count,
|
||||||
|
bx->data,
|
||||||
|
bx->count);
|
||||||
|
buffer->count += elen;
|
||||||
|
}
|
||||||
|
|
||||||
/* Returns a string pointer with the description of the string */
|
/* Returns a string pointer with the description of the string */
|
||||||
const uint8_t *dst_short_description(Dst x) {
|
const uint8_t *dst_short_description(Dst x) {
|
||||||
switch (dst_type(x)) {
|
switch (dst_type(x)) {
|
||||||
@ -319,6 +335,9 @@ void dst_short_description_b(DstBuffer *buffer, Dst x) {
|
|||||||
case DST_STRING:
|
case DST_STRING:
|
||||||
dst_escape_string_b(buffer, dst_unwrap_string(x));
|
dst_escape_string_b(buffer, dst_unwrap_string(x));
|
||||||
return;
|
return;
|
||||||
|
case DST_BUFFER:
|
||||||
|
dst_escape_buffer_b(buffer, dst_unwrap_buffer(x));
|
||||||
|
return;
|
||||||
case DST_ABSTRACT:
|
case DST_ABSTRACT:
|
||||||
string_description_b(buffer,
|
string_description_b(buffer,
|
||||||
dst_abstract_type(dst_unwrap_abstract(x))->name,
|
dst_abstract_type(dst_unwrap_abstract(x))->name,
|
||||||
@ -593,6 +612,8 @@ const uint8_t *dst_to_string(Dst x) {
|
|||||||
switch (dst_type(x)) {
|
switch (dst_type(x)) {
|
||||||
default:
|
default:
|
||||||
return dst_short_description(x);
|
return dst_short_description(x);
|
||||||
|
case DST_BUFFER:
|
||||||
|
return dst_string(dst_unwrap_buffer(x)->data, dst_unwrap_buffer(x)->count);
|
||||||
case DST_STRING:
|
case DST_STRING:
|
||||||
case DST_SYMBOL:
|
case DST_SYMBOL:
|
||||||
return dst_unwrap_string(x);
|
return dst_unwrap_string(x);
|
||||||
|
@ -179,8 +179,8 @@ Dst dst_ast_unwrap1(Dst x);
|
|||||||
Dst dst_ast_unwrap(Dst x);
|
Dst dst_ast_unwrap(Dst x);
|
||||||
|
|
||||||
/* Parsing */
|
/* Parsing */
|
||||||
DstParseResult dst_parse(const uint8_t *src, int32_t len);
|
DstParseResult dst_parse(const uint8_t *src, int32_t len, int flags);
|
||||||
DstParseResult dst_parsec(const char *src);
|
DstParseResult dst_parsec(const char *src, int flags);
|
||||||
int dst_parse_cfun(DstArgs args);
|
int dst_parse_cfun(DstArgs args);
|
||||||
|
|
||||||
/* Native */
|
/* Native */
|
||||||
|
@ -470,6 +470,8 @@ struct DstAst {
|
|||||||
int flags;
|
int flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define DST_PARSEFLAG_SOURCEMAP 1
|
||||||
|
|
||||||
/* Parse structs */
|
/* Parse structs */
|
||||||
enum DstParseStatus {
|
enum DstParseStatus {
|
||||||
DST_PARSE_OK,
|
DST_PARSE_OK,
|
||||||
|
Loading…
Reference in New Issue
Block a user