2017-09-09 18:39:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* 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>
|
2017-12-08 20:57:02 +00:00
|
|
|
#include "strtod.h"
|
2017-09-09 18:39:51 +00:00
|
|
|
|
|
|
|
/* Checks if a string slice is equal to a string constant */
|
|
|
|
static int check_str_const(const char *ref, const uint8_t *start, const uint8_t *end) {
|
|
|
|
while (*ref && start < end) {
|
|
|
|
if (*ref != *(char *)start) return 0;
|
|
|
|
++ref;
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
return !*ref && start == end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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'
|
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
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Code gen
|
|
|
|
|
|
|
|
printf("static uint32_t symchars[8] = {\n\t");
|
|
|
|
for (int i = 0; i < 256; i += 32) {
|
|
|
|
uint32_t block = 0;
|
|
|
|
for (int j = 0; j < 32; j++) {
|
|
|
|
block |= is_symbol_char_gen(i + j) << j;
|
|
|
|
}
|
|
|
|
printf("0x%08x%s", block, (i == (256 - 32)) ? "" : ", ");
|
|
|
|
}
|
|
|
|
printf("\n};\n");
|
|
|
|
|
|
|
|
static int is_symbol_char_gen(uint8_t c) {
|
|
|
|
if (c >= 'a' && c <= 'z') return 1;
|
|
|
|
if (c >= 'A' && c <= 'Z') return 1;
|
|
|
|
if (c >= '0' && c <= '9') return 1;
|
|
|
|
return (c == '!' ||
|
|
|
|
c == '$' ||
|
2017-12-30 21:46:59 +00:00
|
|
|
c == '%' ||
|
2017-12-21 04:03:34 +00:00
|
|
|
c == '&' ||
|
|
|
|
c == '*' ||
|
|
|
|
c == '+' ||
|
|
|
|
c == '-' ||
|
|
|
|
c == '.' ||
|
|
|
|
c == '/' ||
|
|
|
|
c == ':' ||
|
|
|
|
c == '<' ||
|
|
|
|
c == '=' ||
|
|
|
|
c == '>' ||
|
|
|
|
c == '@' ||
|
|
|
|
c == '\\' ||
|
|
|
|
c == '^' ||
|
|
|
|
c == '_' ||
|
|
|
|
c == '~' ||
|
|
|
|
c == '|');
|
|
|
|
}
|
|
|
|
|
|
|
|
The table contains 256 bits, where each bit is 1
|
|
|
|
if the corresponding ascci code is a symbol char, and 0
|
2017-12-30 21:46:59 +00:00
|
|
|
if not. The upper characters are also considered symbol
|
|
|
|
chars and are then checked for utf-8 compliance. */
|
2017-12-21 04:03:34 +00:00
|
|
|
static uint32_t symchars[256] = {
|
2017-12-30 21:46:59 +00:00
|
|
|
0x00000000, 0x77ffec72, 0xd7ffffff, 0x57fffffe,
|
2017-12-21 04:03:34 +00:00
|
|
|
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
|
|
|
|
};
|
|
|
|
|
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 */
|
|
|
|
if (nexti > len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Ensure trailing bytes are well formed (10XX XXXX) */
|
|
|
|
for (j = i + 1; j < nexti; j++) {
|
|
|
|
if ((str[j] >> 6) != 2)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:52:09 +00:00
|
|
|
/* Make source mapping for atom (non recursive structure) */
|
2018-01-06 16:09:15 +00:00
|
|
|
static Dst atom_map(int32_t start, int32_t end) {
|
|
|
|
Dst *t = dst_tuple_begin(2);
|
2017-12-03 17:52:09 +00:00
|
|
|
t[0] = dst_wrap_integer(start);
|
|
|
|
t[1] = dst_wrap_integer(end);
|
|
|
|
return dst_wrap_tuple(dst_tuple_end(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create mappingd for recursive data structure */
|
2018-01-06 16:09:15 +00:00
|
|
|
static Dst ds_map(int32_t start, int32_t end, Dst submap) {
|
|
|
|
Dst *t = dst_tuple_begin(3);
|
2017-12-03 17:52:09 +00:00
|
|
|
t[0] = dst_wrap_integer(start);
|
|
|
|
t[1] = dst_wrap_integer(end);
|
|
|
|
t[2] = submap;
|
|
|
|
return dst_wrap_tuple(dst_tuple_end(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a sourcemapping for a key value pair */
|
2018-01-06 16:09:15 +00:00
|
|
|
static Dst kv_map(Dst k, Dst v) {
|
|
|
|
Dst *t = dst_tuple_begin(2);
|
2017-12-03 17:52:09 +00:00
|
|
|
t[0] = k;
|
|
|
|
t[1] = v;
|
|
|
|
return dst_wrap_tuple(dst_tuple_end(t));
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
typedef struct {
|
2017-11-21 02:39:44 +00:00
|
|
|
DstArray stack;
|
2017-12-03 17:52:09 +00:00
|
|
|
DstArray mapstack;
|
|
|
|
const uint8_t *srcstart;
|
2017-09-09 18:39:51 +00:00
|
|
|
const uint8_t *end;
|
2017-11-01 21:53:43 +00:00
|
|
|
const char *errmsg;
|
2017-11-21 02:39:44 +00:00
|
|
|
DstParseStatus status;
|
2017-09-09 18:39:51 +00:00
|
|
|
} ParseArgs;
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Entry point of the recursive descent parser */
|
|
|
|
static const uint8_t *parse_recur(
|
2017-09-09 18:39:51 +00:00
|
|
|
ParseArgs *args,
|
|
|
|
const uint8_t *src,
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t recur) {
|
2017-09-09 18:39:51 +00:00
|
|
|
|
|
|
|
const uint8_t *end = args->end;
|
2017-12-03 17:52:09 +00:00
|
|
|
const uint8_t *mapstart;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t qcount = 0;
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst ret;
|
|
|
|
Dst submapping;
|
2017-09-09 18:39:51 +00:00
|
|
|
|
|
|
|
/* Prevent stack overflow */
|
|
|
|
if (recur == 0) goto too_much_recur;
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* try parsing again */
|
|
|
|
begin:
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Trim leading whitespace and count quotes */
|
|
|
|
while (src < end && (is_whitespace(*src) || *src == '\'')) {
|
|
|
|
if (*src == '\'') {
|
|
|
|
++qcount;
|
|
|
|
}
|
|
|
|
++src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for end of source */
|
2017-12-30 21:46:59 +00:00
|
|
|
if (src >= end) {
|
|
|
|
if (qcount || recur != DST_RECURSION_GUARD) {
|
|
|
|
goto unexpected_eos;
|
|
|
|
} else {
|
|
|
|
goto nodata;
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 17:52:09 +00:00
|
|
|
|
|
|
|
/* Open mapping */
|
|
|
|
mapstart = src;
|
|
|
|
submapping = dst_wrap_nil();
|
2017-09-09 18:39:51 +00:00
|
|
|
|
|
|
|
/* Detect token type based on first character */
|
|
|
|
switch (*src) {
|
|
|
|
|
|
|
|
/* Numbers, symbols, simple literals */
|
2017-11-21 02:39:44 +00:00
|
|
|
default:
|
|
|
|
atom:
|
|
|
|
{
|
2018-01-05 21:17:55 +00:00
|
|
|
int ascii = 1;
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst numcheck;
|
2017-09-09 18:39:51 +00:00
|
|
|
const uint8_t *tokenend = src;
|
|
|
|
if (!is_symbol_char(*src)) goto unexpected_character;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (tokenend < end && is_symbol_char(*tokenend)) {
|
|
|
|
if (*tokenend++ > 127) ascii = 0;
|
|
|
|
}
|
2017-12-08 20:57:02 +00:00
|
|
|
numcheck = dst_scan_number(src, tokenend - src);
|
|
|
|
if (!dst_checktype(numcheck, DST_NIL)) {
|
|
|
|
ret = numcheck;
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (check_str_const("nil", src, tokenend)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
ret = dst_wrap_nil();
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (check_str_const("false", src, tokenend)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
ret = dst_wrap_boolean(0);
|
2017-09-09 18:39:51 +00:00
|
|
|
} else if (check_str_const("true", src, tokenend)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
ret = dst_wrap_boolean(1);
|
2017-09-09 18:39:51 +00:00
|
|
|
} else {
|
|
|
|
if (*src >= '0' && *src <= '9') {
|
|
|
|
goto sym_nodigits;
|
|
|
|
} else {
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Don't do full utf8 check unless we have seen non ascii characters. */
|
|
|
|
int valid = ascii || valid_utf8(src, tokenend - src);
|
|
|
|
if (!valid)
|
2017-12-21 04:03:34 +00:00
|
|
|
goto invalid_utf8;
|
2017-12-30 21:46:59 +00:00
|
|
|
if (*src == ':') {
|
|
|
|
ret = dst_stringv(src + 1, tokenend - src - 1);
|
|
|
|
} else {
|
|
|
|
ret = dst_symbolv(src, tokenend - src);
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
src = tokenend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
case '#':
|
|
|
|
{
|
|
|
|
/* Jump to next newline */
|
|
|
|
while (src < end && *src != '\n')
|
|
|
|
++src;
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* String literals */
|
2017-11-21 02:39:44 +00:00
|
|
|
case '"':
|
|
|
|
{
|
2017-09-09 18:39:51 +00:00
|
|
|
const uint8_t *strend = ++src;
|
2017-11-21 02:39:44 +00:00
|
|
|
const uint8_t *strstart = strend;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
int containsEscape = 0;
|
|
|
|
/* Preprocess string to check for escapes and string end */
|
|
|
|
while (strend < end && *strend != '"') {
|
|
|
|
len++;
|
|
|
|
if (*strend++ == '\\') {
|
2017-11-01 21:53:43 +00:00
|
|
|
containsEscape = 1;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (strend >= end) goto unexpected_eos;
|
|
|
|
if (*strend == 'h') {
|
2017-11-21 02:39:44 +00:00
|
|
|
strend += 3;
|
2017-09-09 18:39:51 +00:00
|
|
|
if (strend >= end) goto unexpected_eos;
|
2017-11-01 21:53:43 +00:00
|
|
|
} else {
|
|
|
|
strend++;
|
2017-11-21 02:39:44 +00:00
|
|
|
if (strend >= end) goto unexpected_eos;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (containsEscape) {
|
2017-11-06 03:05:47 +00:00
|
|
|
uint8_t *buf = dst_string_begin(len);
|
2017-09-09 18:39:51 +00:00
|
|
|
uint8_t *write = buf;
|
2017-11-01 21:53:43 +00:00
|
|
|
while (src < strend) {
|
|
|
|
if (*src == '\\') {
|
|
|
|
src++;
|
2017-11-21 02:39:44 +00:00
|
|
|
switch (*src++) {
|
2017-09-09 18:39:51 +00:00
|
|
|
case 'n': *write++ = '\n'; break;
|
|
|
|
case 'r': *write++ = '\r'; break;
|
|
|
|
case 't': *write++ = '\t'; break;
|
|
|
|
case 'f': *write++ = '\f'; break;
|
|
|
|
case '0': *write++ = '\0'; break;
|
|
|
|
case '"': *write++ = '"'; break;
|
|
|
|
case '\'': *write++ = '\''; break;
|
|
|
|
case 'z': *write++ = '\0'; break;
|
|
|
|
case 'e': *write++ = 27; break;
|
|
|
|
case 'h': {
|
2017-11-01 21:53:43 +00:00
|
|
|
int d1 = to_hex(*src++);
|
|
|
|
int d2 = to_hex(*src++);
|
2017-09-09 18:39:51 +00:00
|
|
|
if (d1 < 0 || d2 < 0) goto invalid_hex;
|
2017-11-01 21:53:43 +00:00
|
|
|
*write++ = 16 * d1 + d2;
|
2017-09-09 18:39:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
goto unknown_strescape;
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-01 21:53:43 +00:00
|
|
|
*write++ = *src++;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
ret = dst_wrap_string(dst_string_end(buf));
|
2017-09-09 18:39:51 +00:00
|
|
|
} else {
|
2017-11-06 03:05:47 +00:00
|
|
|
ret = dst_wrap_string(dst_string(strstart, strend - strstart));
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
src = strend + 1;
|
2017-09-09 18:39:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data Structure literals */
|
2017-11-21 02:39:44 +00:00
|
|
|
case '@':
|
|
|
|
if (src[1] != '{')
|
|
|
|
goto atom;
|
2017-11-01 21:53:43 +00:00
|
|
|
case '(':
|
|
|
|
case '[':
|
2017-11-21 02:39:44 +00:00
|
|
|
case '{':
|
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t n = 0, i = 0;
|
|
|
|
int32_t istable = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
uint8_t close;
|
|
|
|
switch (*src++) {
|
|
|
|
case '[': close = ']'; break;
|
|
|
|
case '{': close = '}'; break;
|
2017-11-21 02:39:44 +00:00
|
|
|
case '@': close = '}'; src++; istable = 1; break;
|
2017-09-09 18:39:51 +00:00
|
|
|
default: close = ')'; break;
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Trim trailing whitespace */
|
|
|
|
while (src < end && (is_whitespace(*src)))
|
|
|
|
++src;
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Recursively parse inside literal */
|
|
|
|
while (*src != close) {
|
2017-11-01 21:53:43 +00:00
|
|
|
src = parse_recur(args, src, recur - 1);
|
|
|
|
if (args->errmsg || !src) return src;
|
|
|
|
n++;
|
|
|
|
/* Trim trailing whitespace */
|
|
|
|
while (src < end && (is_whitespace(*src)))
|
|
|
|
++src;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
src++;
|
|
|
|
switch (close) {
|
|
|
|
case ')':
|
2017-11-06 03:05:47 +00:00
|
|
|
{
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst *tup = dst_tuple_begin(n);
|
|
|
|
Dst *subtup = dst_tuple_begin(n);
|
2017-12-03 17:52:09 +00:00
|
|
|
for (i = n; i > 0; i--) {
|
2017-11-21 02:39:44 +00:00
|
|
|
tup[i - 1] = dst_array_pop(&args->stack);
|
2017-12-03 17:52:09 +00:00
|
|
|
subtup[i - 1] = dst_array_pop(&args->mapstack);
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
ret = dst_wrap_tuple(dst_tuple_end(tup));
|
2017-12-03 17:52:09 +00:00
|
|
|
submapping = dst_wrap_tuple(dst_tuple_end(subtup));
|
2017-09-09 18:39:51 +00:00
|
|
|
break;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
case ']':
|
|
|
|
{
|
|
|
|
DstArray *arr = dst_array(n);
|
2017-12-03 17:52:09 +00:00
|
|
|
DstArray *subarr = dst_array(n);
|
|
|
|
for (i = n; i > 0; i--) {
|
2017-11-21 02:39:44 +00:00
|
|
|
arr->data[i - 1] = dst_array_pop(&args->stack);
|
2017-12-03 17:52:09 +00:00
|
|
|
subarr->data[i - 1] = dst_array_pop(&args->mapstack);
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
arr->count = n;
|
2017-12-03 17:52:09 +00:00
|
|
|
subarr->count = n;
|
2017-11-21 02:39:44 +00:00
|
|
|
ret = dst_wrap_array(arr);
|
2017-12-03 17:52:09 +00:00
|
|
|
submapping = dst_wrap_array(subarr);
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
case '}':
|
2017-11-06 03:05:47 +00:00
|
|
|
{
|
2017-12-30 21:46:59 +00:00
|
|
|
if (n & 1) {
|
|
|
|
if (istable)
|
|
|
|
goto table_oddargs;
|
|
|
|
goto struct_oddargs;
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
if (istable) {
|
|
|
|
DstTable *t = dst_table(n);
|
2017-12-03 17:52:09 +00:00
|
|
|
DstTable *subt = dst_table(n);
|
2017-11-21 02:39:44 +00:00
|
|
|
for (i = n; i > 0; i -= 2) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst val = dst_array_pop(&args->stack);
|
|
|
|
Dst key = dst_array_pop(&args->stack);
|
|
|
|
Dst subval = dst_array_pop(&args->mapstack);
|
|
|
|
Dst subkey = dst_array_pop(&args->mapstack);
|
2017-12-03 17:52:09 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_table_put(t, key, val);
|
2017-12-03 17:52:09 +00:00
|
|
|
dst_table_put(subt, key, kv_map(subkey, subval));
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
ret = dst_wrap_table(t);
|
2017-12-03 17:52:09 +00:00
|
|
|
submapping = dst_wrap_table(subt);
|
2017-11-21 02:39:44 +00:00
|
|
|
} else {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *st = dst_struct_begin(n >> 1);
|
|
|
|
DstKV *subst = dst_struct_begin(n >> 1);
|
2017-11-21 02:39:44 +00:00
|
|
|
for (i = n; i > 0; i -= 2) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst val = dst_array_pop(&args->stack);
|
|
|
|
Dst key = dst_array_pop(&args->stack);
|
|
|
|
Dst subval = dst_array_pop(&args->mapstack);
|
|
|
|
Dst subkey = dst_array_pop(&args->mapstack);
|
2017-12-03 17:52:09 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_struct_put(st, key, val);
|
2017-12-03 17:52:09 +00:00
|
|
|
dst_struct_put(subst, key, kv_map(subkey, subval));
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
ret = dst_wrap_struct(dst_struct_end(st));
|
2017-12-03 17:52:09 +00:00
|
|
|
submapping = dst_wrap_struct(dst_struct_end(subst));
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
break;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:52:09 +00:00
|
|
|
/* Push source mapping */
|
|
|
|
if (dst_checktype(submapping, DST_NIL)) {
|
|
|
|
/* We just parsed an atom */
|
|
|
|
dst_array_push(&args->mapstack, atom_map(
|
|
|
|
mapstart - args->srcstart,
|
|
|
|
src - args->srcstart));
|
|
|
|
} else {
|
|
|
|
/* We just parsed a recursive data structure */
|
|
|
|
dst_array_push(&args->mapstack, ds_map(
|
|
|
|
mapstart - args->srcstart,
|
|
|
|
src - args->srcstart,
|
|
|
|
submapping));
|
|
|
|
}
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* Quote the returned value qcount times */
|
|
|
|
while (qcount--) {
|
|
|
|
int32_t start = mapstart - args->srcstart;
|
|
|
|
int32_t end = src - args->srcstart;
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst sourcemap = dst_array_pop(&args->mapstack);
|
|
|
|
Dst* tup = dst_tuple_begin(2);
|
2017-12-30 21:46:59 +00:00
|
|
|
tup[0] = atom_map(start, end);
|
|
|
|
tup[1] = sourcemap;
|
|
|
|
ret = quote(ret);
|
|
|
|
dst_array_push(&args->mapstack, ds_map(
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
dst_wrap_tuple(dst_tuple_end(tup))));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push the result to the stack */
|
|
|
|
dst_array_push(&args->stack, ret);
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
/* Return the new source position for further calls */
|
|
|
|
return src;
|
|
|
|
|
|
|
|
/* Errors below */
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
nodata:
|
|
|
|
args->status = DST_PARSE_NODATA;
|
|
|
|
return NULL;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
unexpected_eos:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "unexpected end of source";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_UNEXPECTED_EOS;
|
2017-09-09 18:39:51 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
unexpected_character:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "unexpected character";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_ERROR;
|
2017-09-09 18:39:51 +00:00
|
|
|
return src;
|
|
|
|
|
|
|
|
sym_nodigits:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "symbols cannot start with digits";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_ERROR;
|
2017-09-09 18:39:51 +00:00
|
|
|
return src;
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
table_oddargs:
|
|
|
|
args->errmsg = "table literal needs an even number of arguments";
|
|
|
|
args->status = DST_PARSE_ERROR;
|
|
|
|
return src;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
struct_oddargs:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "struct literal needs an even number of arguments";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_ERROR;
|
2017-09-09 18:39:51 +00:00
|
|
|
return src;
|
|
|
|
|
|
|
|
unknown_strescape:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "unknown string escape sequence";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_ERROR;
|
2017-09-09 18:39:51 +00:00
|
|
|
return src;
|
|
|
|
|
|
|
|
invalid_hex:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "invalid hex escape in string";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_ERROR;
|
2017-09-09 18:39:51 +00:00
|
|
|
return src;
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
invalid_utf8:
|
|
|
|
args->errmsg = "identifier is not valid utf-8";
|
|
|
|
args->status = DST_PARSE_ERROR;
|
|
|
|
return src;
|
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
too_much_recur:
|
2017-11-01 21:53:43 +00:00
|
|
|
args->errmsg = "recursed too deeply in parsing";
|
2017-11-06 03:05:47 +00:00
|
|
|
args->status = DST_PARSE_ERROR;
|
2017-09-09 18:39:51 +00:00
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Parse an array of bytes. Return value in the fiber return value. */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstParseResult dst_parse(const uint8_t *src, int32_t len) {
|
2017-11-21 02:39:44 +00:00
|
|
|
DstParseResult res;
|
2017-09-09 18:39:51 +00:00
|
|
|
ParseArgs args;
|
2017-11-21 02:39:44 +00:00
|
|
|
const uint8_t *newsrc;
|
2017-09-09 18:39:51 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_array_init(&args.stack, 10);
|
2017-11-06 03:05:47 +00:00
|
|
|
args.status = DST_PARSE_OK;
|
2017-12-03 17:52:09 +00:00
|
|
|
args.srcstart = src;
|
2017-09-09 18:39:51 +00:00
|
|
|
args.end = src + len;
|
|
|
|
args.errmsg = NULL;
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2017-12-03 17:52:09 +00:00
|
|
|
dst_array_init(&args.mapstack, 10);
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
newsrc = parse_recur(&args, src, DST_RECURSION_GUARD);
|
|
|
|
res.status = args.status;
|
2017-11-28 23:27:55 +00:00
|
|
|
res.bytes_read = (int32_t) (newsrc - src);
|
2017-11-21 02:39:44 +00:00
|
|
|
|
2017-09-09 18:39:51 +00:00
|
|
|
if (args.errmsg) {
|
2017-12-17 04:11:51 +00:00
|
|
|
res.error = dst_cstring(args.errmsg);
|
|
|
|
res.value = dst_wrap_nil();
|
2017-12-08 20:57:02 +00:00
|
|
|
res.map = NULL;
|
2017-11-06 03:05:47 +00:00
|
|
|
} else {
|
2017-12-17 04:11:51 +00:00
|
|
|
res.value = dst_array_pop(&args.stack);
|
|
|
|
res.error = NULL;
|
2017-12-08 20:57:02 +00:00
|
|
|
res.map = dst_unwrap_tuple(dst_array_pop(&args.mapstack));
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_array_deinit(&args.stack);
|
2017-12-03 17:52:09 +00:00
|
|
|
dst_array_deinit(&args.mapstack);
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
return res;
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a c string */
|
2017-11-21 02:39:44 +00:00
|
|
|
DstParseResult dst_parsec(const char *src) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 0;
|
2017-09-09 18:39:51 +00:00
|
|
|
while (src[len]) ++len;
|
2017-11-21 02:39:44 +00:00
|
|
|
return dst_parse((const uint8_t *)src, len);
|
2017-09-09 18:39:51 +00:00
|
|
|
}
|