1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 16:00:27 +00:00

Add parser/insert and bump to 0.4.0

This commit is contained in:
Calvin Rose 2019-01-31 14:48:28 -05:00
parent 0c950d0846
commit 4bcf6565cd
4 changed files with 46 additions and 3 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.
## 0.4.0 - ??
- Add parser/insert to modify parser state programmatically
- Add debug/stacktrace for easy, pretty stacktraces
- Remove the status-pp function
- Update API to run-context to be much more sane

View File

@ -1393,12 +1393,14 @@ value, one key will be ignored."
where
" around byte "
(string (parser/where p))
(or (parser/error p) "unmatched delimiter")))
": "
(or (parser/error p) "unmatched delimiter")
"\n"))
(defn bad-compile
"Default handler for a compile error."
[msg macrof where]
(file/write stderr msg " while compiling " where "\n")
(file/write stderr "compile error: " msg " while compiling " where "\n")
(when macrof (debug/stacktrace macrof)))
(defn getline

View File

@ -649,6 +649,39 @@ static Janet cfun_parse_consume(int32_t argc, Janet *argv) {
return janet_wrap_integer(i);
}
static Janet cfun_parse_insert(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype);
JanetParseState *s = p->states + p->statecount - 1;
if (s->consumer == tokenchar) {
janet_parser_consume(p, ' ');
p->offset--;
s = p->states + p->statecount - 1;
}
if (s->flags & PFLAG_CONTAINER) {
s->argn++;
if (p->statecount == 1) p->pending++;
push_arg(p, argv[1]);
} else if (s->flags & (PFLAG_STRING | PFLAG_LONGSTRING)) {
const uint8_t *str = janet_to_string(argv[1]);
int32_t slen = janet_string_length(str);
size_t newcount = p->bufcount + slen;
if (p->bufcap > p->bufcount + slen) {
size_t newcap = 2 * newcount;
p->buf = realloc(p->buf, newcap);
if (p->buf == NULL) {
JANET_OUT_OF_MEMORY;
}
p->bufcap = newcap;
}
memcpy(p->buf + p->bufcount, str, slen);
p->bufcount = newcount;
} else {
janet_panic("cannot insert value into parser");
}
return argv[0];
}
static Janet cfun_parse_has_more(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JanetParser *p = janet_getabstract(argv, 0, &janet_parse_parsertype);
@ -807,6 +840,13 @@ static const JanetReg parse_cfuns[] = {
"in the byte stream as a tuple (line, column). Lines and columns are counted from "
"1, (the first byte is line 1, column 1) and a newline is considered ASCII 0x0A.")
},
{
"parser/insert", cfun_parse_insert,
JDOC("(parser/insert parser value)\n\n"
"Insert a value into the parser. This means that the parser state can be manipulated "
"in between chunks of bytes. This would allow a user to add extra elements to arrays "
"and tuples, for example. Returns the parser.")
},
{NULL, NULL, NULL}
};

View File

@ -29,7 +29,7 @@ extern "C" {
/***** START SECTION CONFIG *****/
#define JANET_VERSION "0.3.0"
#define JANET_VERSION "0.4.0"
#ifndef JANET_BUILD
#define JANET_BUILD "local"