2017-09-09 18:39:51 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-09-09 18:39:51 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2018-06-29 03:36:31 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Quote a value */
|
2018-01-06 16:09:15 +00:00
|
|
|
static Dst quote(Dst x) {
|
|
|
|
Dst *t = dst_tuple_begin(2);
|
2017-11-27 19:03:34 +00:00
|
|
|
t[0] = dst_csymbolv("quote");
|
2017-11-06 03:05:47 +00:00
|
|
|
t[1] = x;
|
|
|
|
return dst_wrap_tuple(dst_tuple_end(t));
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if a character is whitespace */
|
|
|
|
static int is_whitespace(uint8_t c) {
|
2017-11-01 21:53:43 +00:00
|
|
|
return c == ' '
|
|
|
|
|| c == '\t'
|
|
|
|
|| c == '\n'
|
|
|
|
|| c == '\r'
|
|
|
|
|| c == '\0'
|
2018-03-18 18:38:40 +00:00
|
|
|
|| c == '\f'
|
2017-12-30 21:46:59 +00:00
|
|
|
|| c == ';'
|
2017-11-01 21:53:43 +00:00
|
|
|
|| c == ',';
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:28:09 +00:00
|
|
|
/* Code generated by tools/symcharsgen.c.
|
|
|
|
* The table contains 256 bits, where each bit is 1
|
|
|
|
* if the corresponding ascci code is a symbol char, and 0
|
|
|
|
* if not. The upper characters are also considered symbol
|
|
|
|
* chars and are then checked for utf-8 compliance. */
|
2018-05-01 15:06:31 +00:00
|
|
|
static const uint32_t symchars[8] = {
|
2018-05-06 17:28:09 +00:00
|
|
|
0x00000000, 0xf7ffec72, 0xc7ffffff, 0x57fffffe,
|
|
|
|
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
|
2017-12-21 04:03:34 +00:00
|
|
|
};
|
|
|
|
|
2018-01-06 16:09:15 +00:00
|
|
|
/* Check if a character is a valid symbol character
|
2017-12-21 04:03:34 +00:00
|
|
|
* symbol chars are A-Z, a-z, 0-9, or one of !$&*+-./:<=>@\^_~| */
|
2017-09-09 18:39:51 +00:00
|
|
|
static int is_symbol_char(uint8_t c) {
|
2017-12-21 04:03:34 +00:00
|
|
|
return symchars[c >> 5] & (1 << (c & 0x1F));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate some utf8. Useful for identifiers. Only validates
|
|
|
|
* the encoding, does not check for valid codepoints (they
|
|
|
|
* are less well defined than the encoding). */
|
|
|
|
static int valid_utf8(const uint8_t *str, int32_t len) {
|
|
|
|
int32_t i = 0;
|
|
|
|
int32_t j;
|
|
|
|
while (i < len) {
|
|
|
|
int32_t nexti;
|
|
|
|
uint8_t c = str[i];
|
|
|
|
|
|
|
|
/* Check the number of bytes in code point */
|
|
|
|
if (c < 0x80) nexti = i + 1;
|
|
|
|
else if ((c >> 5) == 0x06) nexti = i + 2;
|
|
|
|
else if ((c >> 4) == 0x0E) nexti = i + 3;
|
|
|
|
else if ((c >> 3) == 0x1E) nexti = i + 4;
|
|
|
|
/* Don't allow 5 or 6 byte code points */
|
|
|
|
else return 0;
|
|
|
|
|
|
|
|
/* No overflow */
|
2018-01-20 22:19:47 +00:00
|
|
|
if (nexti > len) return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
|
|
|
|
/* Ensure trailing bytes are well formed (10XX XXXX) */
|
|
|
|
for (j = i + 1; j < nexti; j++) {
|
2018-01-20 22:19:47 +00:00
|
|
|
if ((str[j] >> 6) != 2) return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for overlong encodings */
|
|
|
|
if ((nexti == i + 2) && str[i] < 0xC2) return 0;
|
|
|
|
if ((str[i] == 0xE0) && str[i + 1] < 0xA0) return 0;
|
|
|
|
if ((str[i] == 0xF0) && str[i + 1] < 0x90) return 0;
|
|
|
|
|
|
|
|
i = nexti;
|
|
|
|
}
|
|
|
|
return 1;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get hex digit from a letter */
|
|
|
|
static int to_hex(uint8_t c) {
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
return c - '0';
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
return 10 + c - 'A';
|
2017-11-21 02:39:44 +00:00
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
return 10 + c - 'a';
|
2017-09-09 18:39:51 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
typedef int (*Consumer)(DstParser *p, DstParseState *state, uint8_t c);
|
|
|
|
struct DstParseState {
|
|
|
|
int32_t qcount;
|
|
|
|
int32_t argn;
|
2018-01-17 16:36:10 +00:00
|
|
|
int flags;
|
2018-06-29 15:12:33 +00:00
|
|
|
size_t start_line;
|
|
|
|
size_t start_col;
|
2018-01-18 22:25:45 +00:00
|
|
|
Consumer consumer;
|
|
|
|
};
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-05-16 02:03:45 +00:00
|
|
|
/* Define a stack on the main parser struct */
|
|
|
|
#define DEF_PARSER_STACK(NAME, T, STACK, STACKCOUNT, STACKCAP) \
|
|
|
|
static void NAME(DstParser *p, T x) { \
|
|
|
|
size_t oldcount = p->STACKCOUNT; \
|
|
|
|
size_t newcount = oldcount + 1; \
|
|
|
|
if (newcount > p->STACKCAP) { \
|
|
|
|
T *next; \
|
|
|
|
size_t newcap = 2 * newcount; \
|
|
|
|
next = realloc(p->STACK, sizeof(T) * newcap); \
|
|
|
|
if (NULL == next) { \
|
|
|
|
DST_OUT_OF_MEMORY; \
|
|
|
|
} \
|
|
|
|
p->STACK = next; \
|
|
|
|
p->STACKCAP = newcap; \
|
|
|
|
} \
|
|
|
|
p->STACK[oldcount] = x; \
|
|
|
|
p->STACKCOUNT = newcount; \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_PARSER_STACK(push_buf, uint8_t, buf, bufcount, bufcap)
|
|
|
|
DEF_PARSER_STACK(push_arg, Dst, args, argcount, argcap)
|
|
|
|
DEF_PARSER_STACK(_pushstate, DstParseState, states, statecount, statecap)
|
|
|
|
|
|
|
|
#undef DEF_PARSER_STACK
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
#define PFLAG_CONTAINER 1
|
2018-01-27 20:15:09 +00:00
|
|
|
#define PFLAG_BUFFER 2
|
2018-05-07 03:25:59 +00:00
|
|
|
#define PFLAG_PARENS 4
|
|
|
|
#define PFLAG_SQRBRACKETS 8
|
|
|
|
#define PFLAG_CURLYBRACKETS 16
|
|
|
|
#define PFLAG_STRING 32
|
2018-05-07 05:04:24 +00:00
|
|
|
#define PFLAG_LONGSTRING 64
|
2018-01-17 04:18:45 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static void pushstate(DstParser *p, Consumer consumer, int flags) {
|
|
|
|
DstParseState s;
|
|
|
|
s.qcount = 0;
|
|
|
|
s.argn = 0;
|
|
|
|
s.flags = flags;
|
|
|
|
s.consumer = consumer;
|
2018-06-29 15:12:33 +00:00
|
|
|
s.start_line = p->line;
|
|
|
|
s.start_col = p->col;
|
2018-05-16 02:03:45 +00:00
|
|
|
_pushstate(p, s);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static void popstate(DstParser *p, Dst val) {
|
2018-05-16 02:03:45 +00:00
|
|
|
DstParseState top = p->states[--p->statecount];
|
|
|
|
DstParseState *newtop = p->states + p->statecount - 1;
|
2018-01-18 22:25:45 +00:00
|
|
|
if (newtop->flags & PFLAG_CONTAINER) {
|
|
|
|
int32_t i, len;
|
|
|
|
len = newtop->qcount;
|
|
|
|
/* Quote the returned value qcount times */
|
|
|
|
for (i = 0; i < len; i++) {
|
2018-06-29 14:37:50 +00:00
|
|
|
if (dst_checktype(val, DST_TUPLE)) {
|
2018-06-29 15:12:33 +00:00
|
|
|
dst_tuple_sm_line(dst_unwrap_tuple(val)) = (int32_t) top.start_line;
|
|
|
|
dst_tuple_sm_col(dst_unwrap_tuple(val)) = (int32_t) top.start_col;
|
2018-06-29 14:37:50 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
val = quote(val);
|
|
|
|
}
|
|
|
|
newtop->qcount = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Ast wrap */
|
2018-06-29 14:37:50 +00:00
|
|
|
if (dst_checktype(val, DST_TUPLE)) {
|
2018-06-29 15:12:33 +00:00
|
|
|
dst_tuple_sm_line(dst_unwrap_tuple(val)) = (int32_t) top.start_line;
|
|
|
|
dst_tuple_sm_col(dst_unwrap_tuple(val)) = (int32_t) top.start_col;
|
2018-06-29 14:37:50 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
newtop->argn++;
|
2018-05-16 02:03:45 +00:00
|
|
|
push_arg(p, val);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
|
2018-06-12 18:24:45 +00:00
|
|
|
static int checkescape(uint8_t c) {
|
2018-01-18 22:25:45 +00:00
|
|
|
switch (c) {
|
2018-06-12 18:24:45 +00:00
|
|
|
default: return -1;
|
2018-01-18 22:25:45 +00:00
|
|
|
case 'h': return 1;
|
|
|
|
case 'n': return '\n';
|
|
|
|
case 't': return '\t';
|
|
|
|
case 'r': return '\r';
|
|
|
|
case '0': return '\0';
|
|
|
|
case 'z': return '\0';
|
|
|
|
case 'f': return '\f';
|
|
|
|
case 'e': return 27;
|
|
|
|
case '"': return '"';
|
|
|
|
case '\\': return '\\';
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Forward declare */
|
|
|
|
static int stringchar(DstParser *p, DstParseState *state, uint8_t c);
|
|
|
|
|
|
|
|
static int escapeh(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
int digit = to_hex(c);
|
|
|
|
if (digit < 0) {
|
|
|
|
p->error = "invalid hex digit in hex escape";
|
|
|
|
return 1;
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
state->argn = (state->argn << 4) + digit;;
|
|
|
|
state->qcount--;
|
|
|
|
if (!state->qcount) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, (state->argn & 0xFF));
|
2018-01-18 22:25:45 +00:00
|
|
|
state->argn = 0;
|
|
|
|
state->consumer = stringchar;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int escape1(DstParser *p, DstParseState *state, uint8_t c) {
|
2018-06-12 18:24:45 +00:00
|
|
|
int e = checkescape(c);
|
|
|
|
if (e < 0) {
|
2018-01-18 22:25:45 +00:00
|
|
|
p->error = "invalid string escape sequence";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (c == 'h') {
|
|
|
|
state->qcount = 2;
|
|
|
|
state->argn = 0;
|
|
|
|
state->consumer = escapeh;
|
|
|
|
} else {
|
2018-06-12 18:24:45 +00:00
|
|
|
push_buf(p, (uint8_t) e);
|
2018-01-18 22:25:45 +00:00
|
|
|
state->consumer = stringchar;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:28:09 +00:00
|
|
|
static int stringend(DstParser *p, DstParseState *state) {
|
|
|
|
Dst ret;
|
|
|
|
if (state->flags & PFLAG_BUFFER) {
|
2018-08-06 01:39:48 +00:00
|
|
|
DstBuffer *b = dst_buffer((int32_t)p->bufcount);
|
|
|
|
dst_buffer_push_bytes(b, p->buf, (int32_t)p->bufcount);
|
2018-05-06 17:28:09 +00:00
|
|
|
ret = dst_wrap_buffer(b);
|
|
|
|
} else {
|
2018-08-06 01:39:48 +00:00
|
|
|
ret = dst_wrap_string(dst_string(p->buf, (int32_t)p->bufcount));
|
2018-05-06 17:28:09 +00:00
|
|
|
}
|
2018-05-16 02:03:45 +00:00
|
|
|
p->bufcount = 0;
|
2018-05-06 17:28:09 +00:00
|
|
|
popstate(p, ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int stringchar(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
/* Enter escape */
|
|
|
|
if (c == '\\') {
|
|
|
|
state->consumer = escape1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* String end */
|
|
|
|
if (c == '"') {
|
2018-05-06 17:28:09 +00:00
|
|
|
return stringend(p, state);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
/* normal char */
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, c);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for string equality in the buffer */
|
|
|
|
static int check_str_const(const char *cstr, const uint8_t *str, int32_t len) {
|
|
|
|
int32_t index;
|
|
|
|
for (index = 0; index < len; index++) {
|
|
|
|
uint8_t c = str[index];
|
|
|
|
uint8_t k = ((const uint8_t *)cstr)[index];
|
|
|
|
if (c < k) return -1;
|
|
|
|
if (c > k) return 1;
|
|
|
|
if (k == '\0') break;
|
|
|
|
}
|
|
|
|
return (cstr[index] == '\0') ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tokenchar(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
Dst numcheck, ret;
|
|
|
|
int32_t blen;
|
|
|
|
if (is_symbol_char(c)) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, (uint8_t) c);
|
2018-01-18 22:25:45 +00:00
|
|
|
if (c > 127) state->argn = 1; /* Use to indicate non ascii */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* Token finished */
|
2018-08-06 01:39:48 +00:00
|
|
|
blen = (int32_t) p->bufcount;
|
2018-01-18 22:25:45 +00:00
|
|
|
numcheck = dst_scan_number(p->buf, blen);
|
|
|
|
if (!dst_checktype(numcheck, DST_NIL)) {
|
|
|
|
ret = numcheck;
|
|
|
|
} else if (!check_str_const("nil", p->buf, blen)) {
|
|
|
|
ret = dst_wrap_nil();
|
|
|
|
} else if (!check_str_const("false", p->buf, blen)) {
|
|
|
|
ret = dst_wrap_false();
|
|
|
|
} else if (!check_str_const("true", p->buf, blen)) {
|
|
|
|
ret = dst_wrap_true();
|
2018-01-30 04:38:49 +00:00
|
|
|
} else if (p->buf) {
|
2018-01-18 22:25:45 +00:00
|
|
|
if (p->buf[0] >= '0' && p->buf[0] <= '9') {
|
|
|
|
p->error = "symbol literal cannot start with a digit";
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* Don't do full utf8 check unless we have seen non ascii characters. */
|
|
|
|
int valid = (!state->argn) || valid_utf8(p->buf, blen);
|
|
|
|
if (!valid) {
|
|
|
|
p->error = "invalid utf-8 in symbol";
|
|
|
|
return 0;
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
2018-02-03 18:55:55 +00:00
|
|
|
ret = dst_symbolv(p->buf, blen);
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2018-01-30 04:38:49 +00:00
|
|
|
} else {
|
|
|
|
p->error = "empty symbol invalid";
|
|
|
|
return 0;
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2018-05-16 02:03:45 +00:00
|
|
|
p->bufcount = 0;
|
2018-01-18 22:25:45 +00:00
|
|
|
popstate(p, ret);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int comment(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
(void) state;
|
2018-05-16 02:03:45 +00:00
|
|
|
if (c == '\n') p->statecount--;
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forward declaration */
|
|
|
|
static int root(DstParser *p, DstParseState *state, uint8_t c);
|
|
|
|
|
|
|
|
static int dotuple(DstParser *p, DstParseState *state, uint8_t c) {
|
2018-03-24 16:48:42 +00:00
|
|
|
if (state->flags & PFLAG_SQRBRACKETS
|
|
|
|
? c == ']'
|
|
|
|
: c == ')') {
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t i;
|
|
|
|
Dst *ret = dst_tuple_begin(state->argn);
|
|
|
|
for (i = state->argn - 1; i >= 0; i--) {
|
2018-05-16 02:03:45 +00:00
|
|
|
ret[i] = p->args[--p->argcount];
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
popstate(p, dst_wrap_tuple(dst_tuple_end(ret)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return root(p, state, c);
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int doarray(DstParser *p, DstParseState *state, uint8_t c) {
|
2018-03-24 16:48:42 +00:00
|
|
|
if (state->flags & PFLAG_SQRBRACKETS
|
|
|
|
? c == ']'
|
|
|
|
: c == ')') {
|
2018-01-18 22:25:45 +00:00
|
|
|
int32_t i;
|
|
|
|
DstArray *array = dst_array(state->argn);
|
|
|
|
for (i = state->argn - 1; i >= 0; i--) {
|
2018-05-16 02:03:45 +00:00
|
|
|
array->data[i] = p->args[--p->argcount];
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
array->count = state->argn;
|
|
|
|
popstate(p, dst_wrap_array(array));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return root(p, state, c);
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int dostruct(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
if (c == '}') {
|
|
|
|
int32_t i;
|
|
|
|
DstKV *st;
|
|
|
|
if (state->argn & 1) {
|
|
|
|
p->error = "struct literal expects even number of arguments";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
st = dst_struct_begin(state->argn >> 1);
|
|
|
|
for (i = state->argn; i > 0; i -= 2) {
|
2018-05-16 02:03:45 +00:00
|
|
|
Dst value = p->args[--p->argcount];
|
|
|
|
Dst key = p->args[--p->argcount];
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_struct_put(st, key, value);
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
popstate(p, dst_wrap_struct(dst_struct_end(st)));
|
|
|
|
return 1;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
return root(p, state, c);
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int dotable(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
if (c == '}') {
|
|
|
|
int32_t i;
|
|
|
|
DstTable *table;
|
|
|
|
if (state->argn & 1) {
|
|
|
|
p->error = "table literal expects even number of arguments";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
table = dst_table(state->argn >> 1);
|
|
|
|
for (i = state->argn; i > 0; i -= 2) {
|
2018-05-16 02:03:45 +00:00
|
|
|
Dst value = p->args[--p->argcount];
|
|
|
|
Dst key = p->args[--p->argcount];
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_table_put(table, key, value);
|
|
|
|
}
|
|
|
|
popstate(p, dst_wrap_table(table));
|
|
|
|
return 1;
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
return root(p, state, c);
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-05-07 05:04:24 +00:00
|
|
|
#define PFLAG_INSTRING 128
|
|
|
|
#define PFLAG_END_CANDIDATE 256
|
2018-05-06 17:28:09 +00:00
|
|
|
static int longstring(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
if (state->flags & PFLAG_INSTRING) {
|
|
|
|
/* We are inside the long string */
|
2018-05-09 21:01:58 +00:00
|
|
|
if (c == '`') {
|
2018-05-06 17:28:09 +00:00
|
|
|
state->flags |= PFLAG_END_CANDIDATE;
|
|
|
|
state->flags &= ~PFLAG_INSTRING;
|
2018-05-09 21:01:58 +00:00
|
|
|
state->qcount = 1; /* Use qcount to keep track of number of '=' seen */
|
2018-05-06 17:28:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, c);
|
2018-05-06 17:28:09 +00:00
|
|
|
return 1;
|
|
|
|
} else if (state->flags & PFLAG_END_CANDIDATE) {
|
|
|
|
int i;
|
|
|
|
/* We are checking a potential end of the string */
|
2018-07-18 19:56:21 +00:00
|
|
|
if (state->qcount == state->argn) {
|
2018-05-09 21:01:58 +00:00
|
|
|
stringend(p, state);
|
|
|
|
return 0;
|
2018-05-06 17:28:09 +00:00
|
|
|
}
|
2018-05-09 21:01:58 +00:00
|
|
|
if (c == '`' && state->qcount < state->argn) {
|
2018-05-06 17:28:09 +00:00
|
|
|
state->qcount++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* Failed end candidate */
|
|
|
|
for (i = 0; i < state->qcount; i++) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '`');
|
2018-05-06 17:28:09 +00:00
|
|
|
}
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, c);
|
2018-05-06 17:28:09 +00:00
|
|
|
state->qcount = 0;
|
|
|
|
state->flags &= ~PFLAG_END_CANDIDATE;
|
|
|
|
state->flags |= PFLAG_INSTRING;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* We are at beginning of string */
|
2018-05-09 21:01:58 +00:00
|
|
|
state->argn++;
|
|
|
|
if (c != '`') {
|
|
|
|
state->flags |= PFLAG_INSTRING;
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, c);
|
2018-05-06 17:28:09 +00:00
|
|
|
}
|
2018-05-09 21:01:58 +00:00
|
|
|
return 1;
|
2018-05-06 17:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int ampersand(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
(void) state;
|
2018-05-16 02:03:45 +00:00
|
|
|
p->statecount--;
|
2018-03-24 16:48:42 +00:00
|
|
|
switch (c) {
|
|
|
|
case '{':
|
2018-05-07 03:25:59 +00:00
|
|
|
pushstate(p, dotable, PFLAG_CONTAINER | PFLAG_CURLYBRACKETS);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
2018-03-24 16:48:42 +00:00
|
|
|
case '"':
|
2018-05-07 03:25:59 +00:00
|
|
|
pushstate(p, stringchar, PFLAG_BUFFER | PFLAG_STRING);
|
2018-01-27 20:15:09 +00:00
|
|
|
return 1;
|
2018-05-09 21:01:58 +00:00
|
|
|
case '`':
|
2018-05-07 05:04:24 +00:00
|
|
|
pushstate(p, longstring, PFLAG_BUFFER | PFLAG_LONGSTRING);
|
2018-05-06 17:28:09 +00:00
|
|
|
return 1;
|
2018-03-24 16:48:42 +00:00
|
|
|
case '[':
|
|
|
|
pushstate(p, doarray, PFLAG_CONTAINER | PFLAG_SQRBRACKETS);
|
|
|
|
return 1;
|
|
|
|
case '(':
|
2018-05-07 03:25:59 +00:00
|
|
|
pushstate(p, doarray, PFLAG_CONTAINER | PFLAG_PARENS);
|
2018-03-24 16:48:42 +00:00
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
pushstate(p, tokenchar, 0);
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '@'); /* Push the leading ampersand that was dropped */
|
2018-01-18 22:25:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-17 04:18:45 +00:00
|
|
|
|
2018-05-13 00:31:28 +00:00
|
|
|
/* The root state of the parser */
|
2018-01-18 22:25:45 +00:00
|
|
|
static int root(DstParser *p, DstParseState *state, uint8_t c) {
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
if (is_whitespace(c)) return 1;
|
2018-02-07 18:19:34 +00:00
|
|
|
if (!is_symbol_char(c)) {
|
|
|
|
p->error = "unexpected character";
|
|
|
|
return 1;
|
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
pushstate(p, tokenchar, 0);
|
|
|
|
return 0;
|
|
|
|
case '\'':
|
|
|
|
state->qcount++;
|
|
|
|
return 1;
|
|
|
|
case '"':
|
2018-05-07 05:04:24 +00:00
|
|
|
pushstate(p, stringchar, PFLAG_STRING);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
|
|
|
case '#':
|
|
|
|
pushstate(p, comment, 0);
|
|
|
|
return 1;
|
|
|
|
case '@':
|
|
|
|
pushstate(p, ampersand, 0);
|
|
|
|
return 1;
|
2018-05-09 21:01:58 +00:00
|
|
|
case '`':
|
2018-05-07 05:04:24 +00:00
|
|
|
pushstate(p, longstring, PFLAG_LONGSTRING);
|
2018-05-06 17:28:09 +00:00
|
|
|
return 1;
|
2018-01-18 22:25:45 +00:00
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
case '}':
|
|
|
|
p->error = "mismatched delimiter";
|
|
|
|
return 1;
|
|
|
|
case '(':
|
2018-05-07 03:25:59 +00:00
|
|
|
pushstate(p, dotuple, PFLAG_CONTAINER | PFLAG_PARENS);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
|
|
|
case '[':
|
2018-03-24 16:48:42 +00:00
|
|
|
pushstate(p, dotuple, PFLAG_CONTAINER | PFLAG_SQRBRACKETS);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
|
|
|
case '{':
|
2018-05-07 03:25:59 +00:00
|
|
|
pushstate(p, dostruct, PFLAG_CONTAINER | PFLAG_CURLYBRACKETS);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
int dst_parser_consume(DstParser *parser, uint8_t c) {
|
|
|
|
int consumed = 0;
|
|
|
|
if (parser->error) return 0;
|
2018-06-29 15:12:33 +00:00
|
|
|
if (c == '\n') {
|
|
|
|
parser->line++;
|
|
|
|
parser->col = 0;
|
|
|
|
} else if (c != '\r') {
|
|
|
|
parser->col++;
|
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
while (!consumed && !parser->error) {
|
2018-05-16 02:03:45 +00:00
|
|
|
DstParseState *state = parser->states + parser->statecount - 1;
|
2018-01-18 22:25:45 +00:00
|
|
|
consumed = state->consumer(parser, state, c);
|
|
|
|
}
|
|
|
|
parser->lookback = c;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-01-29 20:46:26 +00:00
|
|
|
enum DstParserStatus dst_parser_status(DstParser *parser) {
|
2018-01-18 22:25:45 +00:00
|
|
|
if (parser->error) return DST_PARSE_ERROR;
|
2018-05-16 02:03:45 +00:00
|
|
|
if (parser->statecount > 1) return DST_PARSE_PENDING;
|
|
|
|
if (parser->argcount) return DST_PARSE_FULL;
|
2018-01-18 22:25:45 +00:00
|
|
|
return DST_PARSE_ROOT;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-05-07 03:25:59 +00:00
|
|
|
void dst_parser_flush(DstParser *parser) {
|
2018-05-16 02:03:45 +00:00
|
|
|
parser->argcount = 0;
|
|
|
|
parser->statecount = 1;
|
|
|
|
parser->bufcount = 0;
|
2018-05-07 03:25:59 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
const char *dst_parser_error(DstParser *parser) {
|
2018-01-29 20:46:26 +00:00
|
|
|
enum DstParserStatus status = dst_parser_status(parser);
|
2018-01-18 22:25:45 +00:00
|
|
|
if (status == DST_PARSE_ERROR) {
|
|
|
|
const char *e = parser->error;
|
|
|
|
parser->error = NULL;
|
2018-05-07 03:25:59 +00:00
|
|
|
dst_parser_flush(parser);
|
2018-01-18 22:25:45 +00:00
|
|
|
return e;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
return NULL;
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
Dst dst_parser_produce(DstParser *parser) {
|
|
|
|
Dst ret;
|
2018-05-16 02:03:45 +00:00
|
|
|
size_t i;
|
2018-01-29 20:46:26 +00:00
|
|
|
enum DstParserStatus status = dst_parser_status(parser);
|
2018-01-18 22:25:45 +00:00
|
|
|
if (status != DST_PARSE_FULL) return dst_wrap_nil();
|
2018-05-16 02:03:45 +00:00
|
|
|
ret = parser->args[0];
|
|
|
|
for (i = 1; i < parser->argcount; i++) {
|
|
|
|
parser->args[i - 1] = parser->args[i];
|
2018-01-24 22:59:00 +00:00
|
|
|
}
|
2018-05-16 02:03:45 +00:00
|
|
|
parser->argcount--;
|
2018-01-18 22:25:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2018-06-29 14:37:50 +00:00
|
|
|
void dst_parser_init(DstParser *parser) {
|
2018-05-16 02:03:45 +00:00
|
|
|
parser->args = NULL;
|
2018-01-18 22:25:45 +00:00
|
|
|
parser->states = NULL;
|
|
|
|
parser->buf = NULL;
|
2018-05-16 02:03:45 +00:00
|
|
|
parser->argcount = 0;
|
|
|
|
parser->argcap = 0;
|
|
|
|
parser->bufcount = 0;
|
|
|
|
parser->bufcap = 0;
|
|
|
|
parser->statecount = 0;
|
|
|
|
parser->statecap = 0;
|
2018-01-18 22:25:45 +00:00
|
|
|
parser->error = NULL;
|
2018-06-29 15:12:33 +00:00
|
|
|
parser->line = 1;
|
|
|
|
parser->col = 0;
|
2018-01-18 22:25:45 +00:00
|
|
|
parser->lookback = -1;
|
2018-06-29 03:36:31 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
pushstate(parser, root, PFLAG_CONTAINER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dst_parser_deinit(DstParser *parser) {
|
2018-05-16 02:03:45 +00:00
|
|
|
free(parser->args);
|
|
|
|
free(parser->buf);
|
|
|
|
free(parser->states);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* C functions */
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int parsermark(void *p, size_t size) {
|
2018-05-16 02:03:45 +00:00
|
|
|
size_t i;
|
2018-01-18 22:25:45 +00:00
|
|
|
DstParser *parser = (DstParser *)p;
|
|
|
|
(void) size;
|
2018-05-16 02:03:45 +00:00
|
|
|
for (i = 0; i < parser->argcount; i++) {
|
|
|
|
dst_mark(parser->args[i]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int parsergc(void *p, size_t size) {
|
|
|
|
DstParser *parser = (DstParser *)p;
|
|
|
|
(void) size;
|
|
|
|
dst_parser_deinit(parser);
|
|
|
|
return 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2018-01-16 04:31:39 +00:00
|
|
|
|
2018-06-29 03:36:31 +00:00
|
|
|
static DstAbstractType dst_parse_parsertype = {
|
|
|
|
":core.parser",
|
2018-01-18 22:25:45 +00:00
|
|
|
parsergc,
|
|
|
|
parsermark
|
|
|
|
};
|
|
|
|
|
2018-06-29 03:36:31 +00:00
|
|
|
DstParser *dst_check_parser(Dst x) {
|
|
|
|
if (!dst_checktype(x, DST_ABSTRACT))
|
|
|
|
return NULL;
|
|
|
|
void *abstract = dst_unwrap_abstract(x);
|
|
|
|
if (dst_abstract_type(abstract) != &dst_parse_parsertype)
|
|
|
|
return NULL;
|
|
|
|
return (DstParser *)abstract;
|
|
|
|
}
|
|
|
|
|
2018-01-16 04:31:39 +00:00
|
|
|
/* C Function parser */
|
2018-01-18 22:25:45 +00:00
|
|
|
static int cfun_parser(DstArgs args) {
|
2018-06-29 14:37:50 +00:00
|
|
|
DST_FIXARITY(args, 0);
|
2018-01-18 22:25:45 +00:00
|
|
|
DstParser *p = dst_abstract(&dst_parse_parsertype, sizeof(DstParser));
|
2018-06-29 14:37:50 +00:00
|
|
|
dst_parser_init(p);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_ABSTRACT(args, p);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_consume(DstArgs args) {
|
|
|
|
const uint8_t *bytes;
|
|
|
|
int32_t len;
|
|
|
|
DstParser *p;
|
|
|
|
int32_t i;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
|
|
|
DST_ARG_BYTES(bytes, len, args, 1);
|
2018-01-27 20:15:09 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
dst_parser_consume(p, bytes[i]);
|
|
|
|
switch (dst_parser_status(p)) {
|
|
|
|
case DST_PARSE_ROOT:
|
|
|
|
case DST_PARSE_PENDING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
DstBuffer *b = dst_buffer(len - i);
|
|
|
|
dst_buffer_push_bytes(b, bytes + i + 1, len - i - 1);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_BUFFER(args, b);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_wrap_nil());
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
static int cfun_byte(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
int32_t i;
|
2018-02-07 05:44:51 +00:00
|
|
|
DstParser *p;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
|
|
|
DST_ARG_INTEGER(i, args, 1);
|
|
|
|
dst_parser_consume(p, 0xFF & i);
|
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 20:15:09 +00:00
|
|
|
static int cfun_status(DstArgs args) {
|
|
|
|
const char *stat = NULL;
|
2018-05-13 00:31:28 +00:00
|
|
|
DstParser *p;
|
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
2018-01-27 20:15:09 +00:00
|
|
|
switch (dst_parser_status(p)) {
|
|
|
|
case DST_PARSE_FULL:
|
2018-02-07 05:44:51 +00:00
|
|
|
stat = ":full";
|
2018-01-27 20:15:09 +00:00
|
|
|
break;
|
|
|
|
case DST_PARSE_PENDING:
|
2018-02-07 05:44:51 +00:00
|
|
|
stat = ":pending";
|
2018-01-27 20:15:09 +00:00
|
|
|
break;
|
|
|
|
case DST_PARSE_ERROR:
|
2018-02-07 05:44:51 +00:00
|
|
|
stat = ":error";
|
2018-01-27 20:15:09 +00:00
|
|
|
break;
|
|
|
|
case DST_PARSE_ROOT:
|
2018-02-07 05:44:51 +00:00
|
|
|
stat = ":root";
|
2018-01-27 20:15:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_CSYMBOL(args, stat);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_error(DstArgs args) {
|
|
|
|
const char *err;
|
2018-05-13 00:31:28 +00:00
|
|
|
DstParser *p;
|
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
2018-01-27 20:15:09 +00:00
|
|
|
err = dst_parser_error(p);
|
|
|
|
if (err) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_CSYMBOL(args, err);
|
2018-01-27 20:15:09 +00:00
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_NIL(args);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_produce(DstArgs args) {
|
|
|
|
Dst val;
|
2018-05-13 00:31:28 +00:00
|
|
|
DstParser *p;
|
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
2018-01-27 20:15:09 +00:00
|
|
|
val = dst_parser_produce(p);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, val);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:25:59 +00:00
|
|
|
static int cfun_flush(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DstParser *p;
|
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
2018-05-07 03:25:59 +00:00
|
|
|
dst_parser_flush(p);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-05-07 03:25:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 15:12:33 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-05-07 03:25:59 +00:00
|
|
|
static int cfun_state(DstArgs args) {
|
2018-05-16 02:03:45 +00:00
|
|
|
size_t i;
|
2018-05-07 03:25:59 +00:00
|
|
|
const uint8_t *str;
|
2018-05-16 02:03:45 +00:00
|
|
|
size_t oldcount;
|
2018-05-13 00:31:28 +00:00
|
|
|
DstParser *p;
|
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_CHECKABSTRACT(args, 0, &dst_parse_parsertype);
|
|
|
|
p = (DstParser *) dst_unwrap_abstract(args.v[0]);
|
2018-05-16 02:03:45 +00:00
|
|
|
oldcount = p->bufcount;
|
|
|
|
for (i = 0; i < p->statecount; i++) {
|
2018-05-07 03:25:59 +00:00
|
|
|
DstParseState *s = p->states + i;
|
|
|
|
if (s->flags & PFLAG_PARENS) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '(');
|
2018-05-07 03:25:59 +00:00
|
|
|
} else if (s->flags & PFLAG_SQRBRACKETS) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '[');
|
2018-05-07 03:25:59 +00:00
|
|
|
} else if (s->flags & PFLAG_CURLYBRACKETS) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '{');
|
2018-05-07 03:25:59 +00:00
|
|
|
} else if (s->flags & PFLAG_STRING) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '"');
|
2018-05-07 05:04:24 +00:00
|
|
|
} else if (s->flags & PFLAG_LONGSTRING) {
|
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < s->argn; i++) {
|
2018-05-16 02:03:45 +00:00
|
|
|
push_buf(p, '`');
|
2018-05-07 05:04:24 +00:00
|
|
|
}
|
2018-05-07 03:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 01:39:48 +00:00
|
|
|
str = dst_string(p->buf + oldcount, (int32_t)(p->bufcount - oldcount));
|
2018-05-16 02:03:45 +00:00
|
|
|
p->bufcount = oldcount;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_STRING(args, str);
|
2018-05-07 03:25:59 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 20:15:09 +00:00
|
|
|
static const DstReg cfuns[] = {
|
2018-05-09 21:01:58 +00:00
|
|
|
{"parser.new", cfun_parser},
|
2018-05-08 23:40:28 +00:00
|
|
|
{"parser.produce", cfun_produce},
|
|
|
|
{"parser.consume", cfun_consume},
|
|
|
|
{"parser.byte", cfun_byte},
|
|
|
|
{"parser.error", cfun_error},
|
|
|
|
{"parser.status", cfun_status},
|
|
|
|
{"parser.flush", cfun_flush},
|
|
|
|
{"parser.state", cfun_state},
|
2018-06-29 15:12:33 +00:00
|
|
|
{"parser.where", cfun_where},
|
2018-01-27 20:15:09 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Load the library */
|
|
|
|
int dst_lib_parse(DstArgs args) {
|
|
|
|
DstTable *env = dst_env_arg(args);
|
2018-01-27 20:15:09 +00:00
|
|
|
dst_env_cfuns(env, cfuns);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 0;
|
2018-01-16 04:31:39 +00:00
|
|
|
}
|