1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-16 05:34:48 +00:00
janet/core/parse.c

431 lines
14 KiB
C
Raw Normal View History

/*
* Copyright (c) 2017 Calvin Rose
2017-07-02 01:51:16 +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:
2017-07-02 01:51:16 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2017-07-02 01:51:16 +00:00
*
* 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-03-22 04:27:18 +00:00
#include <gst/gst.h>
2017-02-09 20:02:59 +00:00
2017-07-03 00:51:52 +00:00
static const char UNEXPECTED_CLOSING_DELIM[] = "unexpected closing delimiter";
2017-02-09 20:02:59 +00:00
/* Handle error in parsing */
#define p_error(p, e) ((p)->error = (e), (p)->status = GST_PARSER_ERROR)
2017-02-09 20:02:59 +00:00
/* Get the top ParseState in the parse stack */
static GstParseState *parser_peek(GstParser *p) {
2017-02-09 20:02:59 +00:00
if (!p->count) {
return NULL;
}
2017-05-10 00:06:53 +00:00
return p->data + (p->count - 1);
2017-02-09 20:02:59 +00:00
}
/* Remove the top state from the ParseStack */
static GstParseState *parser_pop(GstParser * p) {
2017-02-09 20:02:59 +00:00
if (!p->count) {
p_error(p, "parser stack underflow");
2017-02-09 20:02:59 +00:00
return NULL;
}
return p->data + --p->count;
}
2017-07-02 21:35:38 +00:00
/* Quote a value */
static GstValue quote(GstParser *p, GstValue x) {
GstValue *tuple = gst_tuple_begin(p->vm, 2);
tuple[0] = gst_string_cvs(p->vm, "quote");
tuple[1] = x;
return gst_wrap_tuple(gst_tuple_end(p->vm, tuple));
}
2017-02-09 20:02:59 +00:00
/* Add a new, empty ParseState to the ParseStack. */
static void parser_push(GstParser *p, ParseType type, uint8_t character) {
GstParseState *top;
2017-02-09 20:02:59 +00:00
if (p->count >= p->cap) {
2017-05-10 00:06:53 +00:00
uint32_t newCap = 2 * p->count + 2;
GstParseState *data = gst_alloc(p->vm, newCap * sizeof(GstParseState));
gst_memcpy(data, p->data, p->cap * sizeof(GstParseState));
2017-02-09 20:02:59 +00:00
p->data = data;
p->cap = newCap;
}
++p->count;
top = parser_peek(p);
2017-02-09 20:02:59 +00:00
if (!top) return;
top->type = type;
2017-07-02 21:35:38 +00:00
top->quoteCount = p->quoteCount;
p->quoteCount = 0;
2017-02-09 20:02:59 +00:00
switch (type) {
case PTYPE_STRING:
top->buf.string.state = STRING_STATE_BASE;
2017-06-20 03:01:34 +00:00
top->buf.string.buffer = gst_buffer(p->vm, 10);
break;
2017-02-09 20:02:59 +00:00
case PTYPE_TOKEN:
top->buf.string.buffer = gst_buffer(p->vm, 10);
2017-02-09 20:02:59 +00:00
break;
case PTYPE_FORM:
top->buf.form.array = gst_array(p->vm, 10);
if (character == '(') top->buf.form.endDelimiter = ')';
2017-03-07 20:29:40 +00:00
if (character == '[') top->buf.form.endDelimiter = ']';
if (character == '{') top->buf.form.endDelimiter = '}';
2017-05-07 22:20:11 +00:00
break;
2017-02-09 20:02:59 +00:00
}
}
/* Append a value to the top-most state in the Parser's stack. */
static void parser_append(GstParser *p, GstValue x) {
2017-07-02 21:35:38 +00:00
GstParseState *oldtop = parser_pop(p);
GstParseState *top = parser_peek(p);
2017-07-02 21:35:38 +00:00
while (oldtop->quoteCount--)
x = quote(p, x);
2017-05-07 22:20:11 +00:00
if (!top) {
p->value = x;
p->status = GST_PARSER_FULL;
return;
}
2017-02-09 20:02:59 +00:00
switch (top->type) {
case PTYPE_FORM:
gst_array_push(p->vm, top->buf.form.array, x);
2017-02-09 20:02:59 +00:00
break;
default:
2017-03-07 20:29:40 +00:00
p_error(p, "expected container type");
2017-02-09 20:02:59 +00:00
break;
}
}
/* Check if a character is whitespace */
static int is_whitespace(uint8_t c) {
2017-02-09 20:02:59 +00:00
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\0' || c == ',';
}
/* Check if a character is a valid symbol character */
static int is_symbol_char(uint8_t c) {
if (c >= 'a' && c <= 'z') return 1;
if (c >= 'A' && c <= 'Z') return 1;
if (c >= '0' && c <= ':') return 1;
if (c >= '<' && c <= '@') return 1;
if (c >= '*' && c <= '/') return 1;
2017-05-07 20:48:35 +00:00
if (c >= '#' && c <= '&') return 1;
if (c == '_') return 1;
if (c == '^') return 1;
if (c == '!') return 1;
return 0;
2017-02-09 20:02:59 +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) {
2017-02-09 20:02:59 +00:00
while (*ref && start < end) {
if (*ref != *(char *)start) return 0;
++ref;
++start;
}
return !*ref && start == end;
}
/* Build from the token buffer */
static GstValue build_token(GstParser *p, GstBuffer *buf) {
GstValue x;
GstReal real;
GstInteger integer;
uint8_t *data = buf->data;
uint8_t *back = data + buf->count;
2017-07-03 00:51:52 +00:00
if (gst_read_integer(data, back, &integer)) {
x.type = GST_INTEGER;
x.data.integer = integer;
2017-07-03 00:51:52 +00:00
} else if (gst_read_real(data, back, &real, 0)) {
x.type = GST_REAL;
x.data.real = real;
} else if (check_str_const("nil", data, back)) {
x.type = GST_NIL;
2017-02-09 20:02:59 +00:00
x.data.boolean = 0;
} else if (check_str_const("false", data, back)) {
x.type = GST_BOOLEAN;
2017-02-09 20:02:59 +00:00
x.data.boolean = 0;
} else if (check_str_const("true", data, back)) {
x.type = GST_BOOLEAN;
2017-02-09 20:02:59 +00:00
x.data.boolean = 1;
} else {
if (buf->data[0] >= '0' && buf->data[0] <= '9') {
2017-03-07 20:29:40 +00:00
p_error(p, "symbols cannot start with digits");
x.type = GST_NIL;
2017-07-03 15:21:10 +00:00
} else if (buf->data[0] == ':' && buf->count >= 2) {
x.type = GST_STRING;
x.data.string = gst_string_b(p->vm, buf->data + 1, buf->count - 1);
2017-02-09 20:02:59 +00:00
} else {
2017-07-03 15:21:10 +00:00
x.type = GST_SYMBOL;
x.data.string = gst_buffer_to_string(p->vm, buf);
2017-02-09 20:02:59 +00:00
}
}
return x;
}
/* Handle parsing a token */
static int token_state(GstParser *p, uint8_t c) {
GstParseState *top = parser_peek(p);
GstBuffer *buf = top->buf.string.buffer;
if (is_whitespace(c) || c == ')' || c == ']' || c == '}') {
parser_append(p, build_token(p, buf));
2017-02-09 20:02:59 +00:00
return !(c == ')' || c == ']' || c == '}');
} else if (is_symbol_char(c)) {
gst_buffer_push(p->vm, buf, c);
2017-02-09 20:02:59 +00:00
return 1;
} else {
2017-03-07 20:29:40 +00:00
p_error(p, "expected symbol character");
2017-02-09 20:02:59 +00:00
return 1;
}
}
2017-03-07 20:29:40 +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';
} else if (c >= 'A' && c <= 'F') {
return 10 + c - 'A';
} else {
return -1;
}
}
2017-02-09 20:02:59 +00:00
/* Handle parsing a string literal */
static int string_state(GstParser *p, uint8_t c) {
2017-03-07 20:29:40 +00:00
int digit;
GstParseState *top = parser_peek(p);
2017-02-09 20:02:59 +00:00
switch (top->buf.string.state) {
case STRING_STATE_BASE:
if (c == '\\') {
top->buf.string.state = STRING_STATE_ESCAPE;
} else if (c == '"') {
GstValue x;
x.type = GST_STRING;
x.data.string = gst_buffer_to_string(p->vm, top->buf.string.buffer);
2017-07-02 21:17:24 +00:00
parser_append(p, x);
2017-02-09 20:02:59 +00:00
} else {
gst_buffer_push(p->vm, top->buf.string.buffer, c);
2017-02-09 20:02:59 +00:00
}
break;
case STRING_STATE_ESCAPE:
{
uint8_t next;
switch (c) {
case 'n': next = '\n'; break;
case 'r': next = '\r'; break;
case 't': next = '\t'; break;
case 'f': next = '\f'; break;
case '0': next = '\0'; break;
case '"': next = '"'; break;
case '\'': next = '\''; break;
case 'z': next = '\0'; break;
2017-06-25 23:29:38 +00:00
case 'e': next = 27; break;
2017-07-02 01:51:16 +00:00
case 'h':
top->buf.string.state = STRING_STATE_ESCAPE_HEX;
2017-03-07 20:29:40 +00:00
top->buf.string.count = 0;
top->buf.string.accum = 0;
2017-06-25 23:29:38 +00:00
return 1;
2017-02-09 20:02:59 +00:00
default:
2017-03-07 20:29:40 +00:00
p_error(p, "unknown string escape sequence");
return 1;
2017-02-09 20:02:59 +00:00
}
gst_buffer_push(p->vm, top->buf.string.buffer, next);
2017-02-09 20:02:59 +00:00
top->buf.string.state = STRING_STATE_BASE;
}
break;
case STRING_STATE_ESCAPE_HEX:
2017-03-07 20:29:40 +00:00
digit = to_hex(c);
if (digit < 0) {
p_error(p, "invalid hexidecimal digit");
return 1;
} else {
2017-07-02 01:51:16 +00:00
top->buf.string.accum *= 16;
top->buf.string.accum += digit;
2017-03-07 20:29:40 +00:00
}
top->buf.string.accum += digit;
if (++top->buf.string.count == 2) {
gst_buffer_push(p->vm, top->buf.string.buffer, top->buf.string.accum);
top->buf.string.state = STRING_STATE_BASE;
}
2017-02-09 20:02:59 +00:00
break;
case STRING_STATE_ESCAPE_UNICODE:
break;
}
return 1;
}
/* Root state of the parser */
static int root_state(GstParser *p, uint8_t c) {
if (is_whitespace(c)) return 1;
p->status = GST_PARSER_PENDING;
if (c == ']' || c == ')' || c == '}') {
p_error(p, UNEXPECTED_CLOSING_DELIM);
2017-02-09 20:02:59 +00:00
return 1;
}
if (c == '(' || c == '[' || c == '{') {
parser_push(p, PTYPE_FORM, c);
2017-02-09 20:02:59 +00:00
return 1;
}
if (c == '"') {
parser_push(p, PTYPE_STRING, c);
2017-02-09 20:02:59 +00:00
return 1;
}
2017-07-02 21:35:38 +00:00
if (c == '\'') {
p->quoteCount++;
return 1;
}
if (is_symbol_char(c)) {
parser_push(p, PTYPE_TOKEN, c);
return 0;
}
2017-03-07 20:29:40 +00:00
p_error(p, "unexpected character");
return 1;
2017-02-09 20:02:59 +00:00
}
/* Handle parsing a form */
static int form_state(GstParser *p, uint8_t c) {
GstParseState *top = parser_peek(p);
if (c == top->buf.form.endDelimiter) {
GstArray *array = top->buf.form.array;
GstValue x;
2017-03-07 20:29:40 +00:00
if (c == ']') {
x.type = GST_ARRAY;
x.data.array = array;
} else if (c == ')') {
2017-04-15 20:05:59 +00:00
GstValue *tup;
tup = gst_tuple_begin(p->vm, array->count);
gst_memcpy(tup, array->data, array->count * sizeof(GstValue));
x.type = GST_TUPLE;
2017-04-15 20:05:59 +00:00
x.data.tuple = gst_tuple_end(p->vm, tup);
2017-03-07 20:29:40 +00:00
} else { /* c == '{' */
uint32_t i;
if (array->count % 2 != 0) {
p_error(p, "table literal must have even number of elements");
return 1;
}
x.type = GST_TABLE;
x.data.table = gst_table(p->vm, array->count);
for (i = 0; i < array->count; i += 2) {
2017-07-02 01:51:16 +00:00
gst_table_put(p->vm, x.data.table, array->data[i], array->data[i + 1]);
}
2017-03-07 20:29:40 +00:00
}
parser_append(p, x);
2017-02-09 20:02:59 +00:00
return 1;
}
return root_state(p, c);
2017-02-09 20:02:59 +00:00
}
/* Handle a character */
void gst_parse_byte(GstParser *p, uint8_t c) {
2017-02-09 20:02:59 +00:00
int done = 0;
2017-07-01 16:47:57 +00:00
/* Update position in source */
if (c == '\n') {
p->line++;
p->index = 0;
p->comment = GST_PCOMMENT_EXPECTING;
} else {
2017-07-02 01:51:16 +00:00
++p->index;
2017-07-01 16:47:57 +00:00
}
/* Check comments */
switch (p->comment) {
case GST_PCOMMENT_NOT:
break;
case GST_PCOMMENT_EXPECTING:
if (c == '#') {
p->comment = GST_PCOMMENT_INSIDE;
return;
} else if (!is_whitespace(c)) {
p->comment = GST_PCOMMENT_NOT;
}
break;
case GST_PCOMMENT_INSIDE:
return;
}
/* Dispatch character to state */
2017-04-18 02:14:35 +00:00
while (!done) {
GstParseState *top = parser_peek(p);
2017-05-07 22:20:11 +00:00
if (!top) {
done = root_state(p, c);
} else {
switch (top->type) {
case PTYPE_TOKEN:
done = token_state(p, c);
break;
case PTYPE_FORM:
done = form_state(p, c);
break;
case PTYPE_STRING:
done = string_state(p, c);
break;
}
2017-02-09 20:02:59 +00:00
}
}
}
/* Parse a C style string. The first value encountered when parsed is put
* in p->value. The string variable is then updated to the next char that
* was not read. Returns 1 if any values were read, otherwise returns 0.
* Returns the number of bytes read.
*/
int gst_parse_cstring(GstParser *p, const char *string) {
2017-02-09 20:02:59 +00:00
int bytesRead = 0;
2017-06-20 03:01:34 +00:00
if (!string)
return 0;
while ((p->status == GST_PARSER_PENDING || p->status == GST_PARSER_ROOT)
&& (string[bytesRead] != '\0')) {
gst_parse_byte(p, string[bytesRead++]);
2017-02-09 20:02:59 +00:00
}
return bytesRead;
}
2017-03-08 22:34:25 +00:00
/* Parse a gst string */
int gst_parse_string(GstParser *p, const uint8_t *string) {
2017-03-08 22:34:25 +00:00
uint32_t i;
2017-06-20 03:01:34 +00:00
if (!string)
return 0;
for (i = 0; i < gst_string_length(string); ++i) {
if (p->status != GST_PARSER_PENDING && p->status != GST_PARSER_ROOT) break;
gst_parse_byte(p, string[i]);
}
2017-03-08 22:34:25 +00:00
return i;
}
/* Check if a parser has a value that needs to be handled. If
* so, the parser will not parse any more input until that value
* is consumed. */
int gst_parse_hasvalue(GstParser *p) {
return p->status == GST_PARSER_FULL;
}
/* Gets a value from the parser */
GstValue gst_parse_consume(GstParser *p) {
p->status = GST_PARSER_ROOT;
return p->value;
}
2017-02-09 20:02:59 +00:00
/* Parser initialization (memory allocation) */
void gst_parser(GstParser *p, Gst *vm) {
2017-02-09 20:02:59 +00:00
p->vm = vm;
GstParseState *data = gst_alloc(vm, sizeof(GstParseState) * 10);
p->cap = 10;
2017-02-09 20:02:59 +00:00
p->data = data;
p->count = 0;
p->index = 0;
2017-07-01 16:47:57 +00:00
p->line = 1;
p->quoteCount = 0;
2017-02-09 20:02:59 +00:00
p->error = NULL;
p->status = GST_PARSER_ROOT;
p->value.type = GST_NIL;
2017-07-01 16:47:57 +00:00
p->comment = GST_PCOMMENT_EXPECTING;
}