From 4bcf6565cdb9c8de0f956609da8a9998d6e2d140 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 31 Jan 2019 14:48:28 -0500 Subject: [PATCH] Add parser/insert and bump to 0.4.0 --- CHANGELOG.md | 1 + src/core/core.janet | 6 ++++-- src/core/parse.c | 40 +++++++++++++++++++++++++++++++++++++++ src/include/janet/janet.h | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d58720a4..3f879249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/core/core.janet b/src/core/core.janet index 1efafff2..b4a6bcd0 100644 --- a/src/core/core.janet +++ b/src/core/core.janet @@ -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 diff --git a/src/core/parse.c b/src/core/parse.c index 1dbfab56..00cad587 100644 --- a/src/core/parse.c +++ b/src/core/parse.c @@ -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} }; diff --git a/src/include/janet/janet.h b/src/include/janet/janet.h index de0d5ac5..f7785fcc 100644 --- a/src/include/janet/janet.h +++ b/src/include/janet/janet.h @@ -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"