2017-03-22 04:27:18 +00:00
|
|
|
#include <gst/gst.h>
|
2017-03-16 00:56:37 +00:00
|
|
|
#include <gst/parse.h>
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
static const char UNEXPECTED_CLOSING_DELIM[] = "Unexpected closing delimiter";
|
|
|
|
|
|
|
|
/* The type of a ParseState */
|
|
|
|
typedef enum ParseType {
|
|
|
|
PTYPE_ROOT,
|
|
|
|
PTYPE_FORM,
|
|
|
|
PTYPE_STRING,
|
|
|
|
PTYPE_TOKEN
|
|
|
|
} ParseType;
|
|
|
|
|
|
|
|
/* Contain a parse state that goes on the parse stack */
|
2017-02-16 02:02:00 +00:00
|
|
|
struct GstParseState {
|
2017-02-09 20:02:59 +00:00
|
|
|
ParseType type;
|
2017-03-14 23:13:17 +00:00
|
|
|
uint32_t quoteCount;
|
2017-02-09 20:02:59 +00:00
|
|
|
union {
|
|
|
|
struct {
|
2017-03-10 05:17:34 +00:00
|
|
|
uint8_t endDelimiter;
|
2017-03-07 20:29:40 +00:00
|
|
|
GstArray *array;
|
2017-02-13 23:58:56 +00:00
|
|
|
} form;
|
2017-02-09 20:02:59 +00:00
|
|
|
struct {
|
2017-03-07 20:29:40 +00:00
|
|
|
GstValue key;
|
|
|
|
int keyFound;
|
2017-03-10 05:17:34 +00:00
|
|
|
GstObject *object;
|
2017-03-07 20:29:40 +00:00
|
|
|
} object;
|
|
|
|
struct {
|
|
|
|
GstBuffer *buffer;
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t accum;
|
2017-02-09 20:02:59 +00:00
|
|
|
enum {
|
|
|
|
STRING_STATE_BASE,
|
|
|
|
STRING_STATE_ESCAPE,
|
|
|
|
STRING_STATE_ESCAPE_UNICODE,
|
|
|
|
STRING_STATE_ESCAPE_HEX
|
|
|
|
} state;
|
|
|
|
} string;
|
|
|
|
} buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Handle error in parsing */
|
2017-02-16 02:02:00 +00:00
|
|
|
#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 */
|
2017-02-16 02:02:00 +00:00
|
|
|
static GstParseState *parser_peek(GstParser *p) {
|
2017-02-09 20:02:59 +00:00
|
|
|
if (!p->count) {
|
2017-02-23 22:21:13 +00:00
|
|
|
p_error(p, "parser stack underflow");
|
2017-02-09 20:02:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return p->data + p->count - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the top state from the ParseStack */
|
2017-02-16 02:02:00 +00:00
|
|
|
static GstParseState *parser_pop(GstParser * p) {
|
2017-02-09 20:02:59 +00:00
|
|
|
if (!p->count) {
|
2017-02-23 22:21:13 +00:00
|
|
|
p_error(p, "parser stack underflow");
|
2017-02-09 20:02:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return p->data + --p->count;
|
|
|
|
}
|
|
|
|
|
2017-03-14 23:13:17 +00:00
|
|
|
/* Quote a value */
|
|
|
|
static GstValue quote(GstParser *p, GstValue x) {
|
|
|
|
/* Load a quote form to get the string literal */
|
|
|
|
GstValue tuplev;
|
|
|
|
GstValue *tuple;
|
2017-04-15 20:05:59 +00:00
|
|
|
tuple = gst_tuple_begin(p->vm, 2);
|
|
|
|
tuple[0] = gst_string_cv(p->vm, "quote");
|
2017-03-14 23:13:17 +00:00
|
|
|
tuple[1] = x;
|
2017-04-15 20:05:59 +00:00
|
|
|
tuplev.type = GST_TUPLE;
|
|
|
|
tuplev.data.tuple = gst_tuple_end(p->vm, tuple);
|
2017-03-14 23:13:17 +00:00
|
|
|
return tuplev;
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:02:59 +00:00
|
|
|
/* Add a new, empty ParseState to the ParseStack. */
|
2017-02-16 02:02:00 +00:00
|
|
|
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) {
|
|
|
|
uint32_t newCap = 2 * p->count;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstParseState *data = gst_alloc(p->vm, newCap);
|
2017-02-09 20:02:59 +00:00
|
|
|
p->data = data;
|
|
|
|
p->cap = newCap;
|
|
|
|
}
|
2017-03-14 23:13:17 +00:00
|
|
|
if (p->count) {
|
|
|
|
top = parser_peek(p);
|
|
|
|
top->quoteCount = p->quoteCount;
|
|
|
|
p->quoteCount = 0;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
++p->count;
|
2017-02-16 02:02:00 +00:00
|
|
|
top = parser_peek(p);
|
2017-02-09 20:02:59 +00:00
|
|
|
if (!top) return;
|
|
|
|
top->type = type;
|
|
|
|
switch (type) {
|
|
|
|
case PTYPE_ROOT:
|
|
|
|
break;
|
|
|
|
case PTYPE_STRING:
|
|
|
|
top->buf.string.state = STRING_STATE_BASE;
|
|
|
|
case PTYPE_TOKEN:
|
2017-02-16 02:02:00 +00:00
|
|
|
top->buf.string.buffer = gst_buffer(p->vm, 10);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
case PTYPE_FORM:
|
2017-02-16 02:02:00 +00:00
|
|
|
top->buf.form.array = gst_array(p->vm, 10);
|
2017-02-13 23:58:56 +00:00
|
|
|
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-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append a value to the top-most state in the Parser's stack. */
|
2017-02-16 02:02:00 +00:00
|
|
|
static void parser_append(GstParser *p, GstValue x) {
|
|
|
|
GstParseState *top = parser_peek(p);
|
2017-02-09 20:02:59 +00:00
|
|
|
if (!top) return;
|
2017-03-14 23:13:17 +00:00
|
|
|
while (top->quoteCount--)
|
|
|
|
x = quote(p, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
switch (top->type) {
|
|
|
|
case PTYPE_ROOT:
|
|
|
|
p->value = x;
|
2017-02-16 02:02:00 +00:00
|
|
|
p->status = GST_PARSER_FULL;
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
case PTYPE_FORM:
|
2017-02-16 02:02:00 +00:00
|
|
|
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 */
|
2017-02-16 02:02:00 +00:00
|
|
|
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 */
|
2017-02-16 02:02:00 +00:00
|
|
|
static int is_symbol_char(uint8_t c) {
|
2017-02-12 20:16:55 +00:00
|
|
|
if (c >= 'a' && c <= 'z') return 1;
|
|
|
|
if (c >= 'A' && c <= 'Z') return 1;
|
2017-02-13 02:54:18 +00:00
|
|
|
if (c >= '0' && c <= ':') return 1;
|
2017-02-12 20:16:55 +00:00
|
|
|
if (c >= '<' && c <= '@') return 1;
|
|
|
|
if (c >= '*' && c <= '/') return 1;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an integer power of 10 */
|
|
|
|
static double exp10(int power) {
|
|
|
|
if (power == 0) return 1;
|
|
|
|
if (power > 0) {
|
|
|
|
double result = 10;
|
|
|
|
int currentPower = 1;
|
|
|
|
while (currentPower * 2 <= power) {
|
|
|
|
result = result * result;
|
|
|
|
currentPower *= 2;
|
|
|
|
}
|
|
|
|
return result * exp10(power - currentPower);
|
|
|
|
} else {
|
|
|
|
return 1 / exp10(-power);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
/* Read a number from a string. Returns if successfuly
|
|
|
|
* parsed a number from the enitre input string.
|
|
|
|
* If returned 1, output is int ret.*/
|
|
|
|
static int read_number(const uint8_t *string, const uint8_t *end, double *ret, int forceInt) {
|
2017-02-09 20:02:59 +00:00
|
|
|
int sign = 1, x = 0;
|
|
|
|
double accum = 0, exp = 1, place = 1;
|
2017-02-16 02:02:00 +00:00
|
|
|
/* Check the sign */
|
2017-02-09 20:02:59 +00:00
|
|
|
if (*string == '-') {
|
|
|
|
sign = -1;
|
|
|
|
++string;
|
|
|
|
} else if (*string == '+') {
|
|
|
|
++string;
|
|
|
|
}
|
|
|
|
if (string >= end) return 0;
|
|
|
|
while (string < end) {
|
|
|
|
if (*string == '.' && !forceInt) {
|
|
|
|
place = 0.1;
|
|
|
|
} else if (!forceInt && (*string == 'e' || *string == 'E')) {
|
2017-02-16 02:02:00 +00:00
|
|
|
/* Read the exponent */
|
2017-02-09 20:02:59 +00:00
|
|
|
++string;
|
2017-02-16 02:02:00 +00:00
|
|
|
if (string >= end) return 0;
|
|
|
|
if (!read_number(string, end, &exp, 1))
|
2017-02-09 20:02:59 +00:00
|
|
|
return 0;
|
|
|
|
exp = exp10(exp);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
x = *string;
|
|
|
|
if (x < '0' || x > '9') return 0;
|
|
|
|
x -= '0';
|
|
|
|
if (place < 1) {
|
|
|
|
accum += x * place;
|
|
|
|
place *= 0.1;
|
|
|
|
} else {
|
|
|
|
accum *= 10;
|
|
|
|
accum += x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++string;
|
|
|
|
}
|
|
|
|
*ret = accum * sign * exp;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks if a string slice is equal to a string constant */
|
2017-02-16 02:02:00 +00:00
|
|
|
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 */
|
2017-02-16 02:02:00 +00:00
|
|
|
static GstValue build_token(GstParser *p, GstBuffer *buf) {
|
|
|
|
GstValue x;
|
|
|
|
GstNumber number;
|
2017-03-22 22:35:54 +00:00
|
|
|
uint8_t *data = buf->data;
|
|
|
|
uint8_t *back = data + buf->count;
|
2017-02-16 02:02:00 +00:00
|
|
|
if (read_number(data, back, &number, 0)) {
|
|
|
|
x.type = GST_NUMBER;
|
2017-02-09 20:02:59 +00:00
|
|
|
x.data.number = number;
|
2017-02-16 02:02:00 +00:00
|
|
|
} else if (check_str_const("nil", data, back)) {
|
|
|
|
x.type = GST_NIL;
|
2017-02-09 20:02:59 +00:00
|
|
|
x.data.boolean = 0;
|
2017-02-16 02:02:00 +00:00
|
|
|
} else if (check_str_const("false", data, back)) {
|
|
|
|
x.type = GST_BOOLEAN;
|
2017-02-09 20:02:59 +00:00
|
|
|
x.data.boolean = 0;
|
2017-02-16 02:02:00 +00:00
|
|
|
} 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");
|
2017-02-16 02:02:00 +00:00
|
|
|
x.type = GST_NIL;
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-04-14 17:41:32 +00:00
|
|
|
x.type = GST_STRING;
|
2017-02-16 02:02:00 +00:00
|
|
|
x.data.string = gst_buffer_to_string(p->vm, buf);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle parsing a token */
|
2017-02-16 02:02:00 +00:00
|
|
|
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_pop(p);
|
|
|
|
parser_append(p, build_token(p, buf));
|
2017-02-09 20:02:59 +00:00
|
|
|
return !(c == ')' || c == ']' || c == '}');
|
2017-02-16 02:02:00 +00:00
|
|
|
} 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 */
|
2017-02-16 02:02:00 +00:00
|
|
|
static int string_state(GstParser *p, uint8_t c) {
|
2017-03-07 20:29:40 +00:00
|
|
|
int digit;
|
2017-02-16 02:02:00 +00:00
|
|
|
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 == '"') {
|
2017-03-14 23:13:17 +00:00
|
|
|
GstValue x;
|
2017-02-16 02:02:00 +00:00
|
|
|
x.type = GST_STRING;
|
|
|
|
x.data.string = gst_buffer_to_string(p->vm, top->buf.string.buffer);
|
|
|
|
parser_pop(p);
|
2017-04-14 17:41:32 +00:00
|
|
|
parser_append(p, quote(p, x));
|
2017-02-09 20:02:59 +00:00
|
|
|
} else {
|
2017-02-16 02:02:00 +00:00
|
|
|
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-03-07 20:29:40 +00:00
|
|
|
case 'h':
|
|
|
|
top->buf.string.state = STRING_STATE_ESCAPE_HEX;
|
|
|
|
top->buf.string.count = 0;
|
|
|
|
top->buf.string.accum = 0;
|
|
|
|
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
|
|
|
}
|
2017-02-16 02:02:00 +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 {
|
|
|
|
top->buf.string.accum *= 16;
|
|
|
|
top->buf.string.accum += digit;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-13 23:58:56 +00:00
|
|
|
/* Root state of the parser */
|
2017-02-16 02:02:00 +00:00
|
|
|
static int root_state(GstParser *p, uint8_t c) {
|
2017-04-17 04:15:18 +00:00
|
|
|
if (is_whitespace(c)) return 1;
|
|
|
|
p->status = GST_PARSER_PENDING;
|
2017-02-13 23:58:56 +00:00
|
|
|
if (c == ']' || c == ')' || c == '}') {
|
2017-02-16 02:02:00 +00:00
|
|
|
p_error(p, UNEXPECTED_CLOSING_DELIM);
|
2017-02-09 20:02:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-13 23:58:56 +00:00
|
|
|
if (c == '(' || c == '[' || c == '{') {
|
2017-02-16 02:02:00 +00:00
|
|
|
parser_push(p, PTYPE_FORM, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-13 23:58:56 +00:00
|
|
|
if (c == '"') {
|
2017-02-16 02:02:00 +00:00
|
|
|
parser_push(p, PTYPE_STRING, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-14 23:13:17 +00:00
|
|
|
if (c == '\'') {
|
|
|
|
p->quoteCount++;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
if (is_symbol_char(c)) {
|
|
|
|
parser_push(p, PTYPE_TOKEN, c);
|
2017-02-13 23:58:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-07 20:29:40 +00:00
|
|
|
p_error(p, "unexpected character");
|
2017-02-13 23:58:56 +00:00
|
|
|
return 1;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 23:58:56 +00:00
|
|
|
/* Handle parsing a form */
|
2017-02-16 02:02:00 +00:00
|
|
|
static int form_state(GstParser *p, uint8_t c) {
|
|
|
|
GstParseState *top = parser_peek(p);
|
2017-02-13 23:58:56 +00:00
|
|
|
if (c == top->buf.form.endDelimiter) {
|
2017-02-16 02:02:00 +00:00
|
|
|
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));
|
2017-03-10 05:17:34 +00:00
|
|
|
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;
|
2017-03-10 05:17:34 +00:00
|
|
|
if (array->count % 2 != 0) {
|
|
|
|
p_error(p, "object literal must have even number of elements");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
x.type = GST_OBJECT;
|
|
|
|
x.data.object = gst_object(p->vm, array->count);
|
|
|
|
for (i = 0; i < array->count; i += 2) {
|
|
|
|
gst_object_put(p->vm, x.data.object, array->data[i], array->data[i + 1]);
|
|
|
|
}
|
2017-03-07 20:29:40 +00:00
|
|
|
}
|
2017-03-10 05:17:34 +00:00
|
|
|
parser_pop(p);
|
2017-02-16 02:02:00 +00:00
|
|
|
parser_append(p, x);
|
2017-02-09 20:02:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
return root_state(p, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle a character */
|
2017-03-08 15:34:22 +00:00
|
|
|
static void dispatch_char(GstParser *p, uint8_t c) {
|
2017-02-09 20:02:59 +00:00
|
|
|
int done = 0;
|
2017-03-08 15:34:22 +00:00
|
|
|
++p->index;
|
|
|
|
/* Handle comments */
|
|
|
|
if (p->flags & GST_PARSER_FLAG_INCOMMENT) {
|
|
|
|
if (c == '\n') {
|
2017-03-10 05:17:34 +00:00
|
|
|
p->flags = GST_PARSER_FLAG_EXPECTING_COMMENT;
|
2017-03-08 15:34:22 +00:00
|
|
|
}
|
2017-03-10 05:17:34 +00:00
|
|
|
return;
|
2017-03-08 15:34:22 +00:00
|
|
|
} else if (p->flags & GST_PARSER_FLAG_EXPECTING_COMMENT) {
|
|
|
|
if (c == '#') {
|
2017-03-10 05:17:34 +00:00
|
|
|
p->flags = GST_PARSER_FLAG_INCOMMENT;
|
|
|
|
return;
|
2017-03-08 15:34:22 +00:00
|
|
|
} else if (!is_whitespace(c)) {
|
2017-03-10 05:17:34 +00:00
|
|
|
p->flags = 0;
|
2017-03-08 15:34:22 +00:00
|
|
|
} else {
|
2017-03-10 05:17:34 +00:00
|
|
|
return;
|
2017-03-08 15:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 19:55:50 +00:00
|
|
|
/* Dispatch character to state */
|
2017-04-17 04:15:18 +00:00
|
|
|
while (!done && (p->status == GST_PARSER_PENDING || p->status == GST_PARSER_ROOT)) {
|
2017-02-16 02:02:00 +00:00
|
|
|
GstParseState *top = parser_peek(p);
|
2017-02-09 20:02:59 +00:00
|
|
|
switch (top->type) {
|
|
|
|
case PTYPE_ROOT:
|
2017-02-16 02:02:00 +00:00
|
|
|
done = root_state(p, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
case PTYPE_TOKEN:
|
2017-02-16 02:02:00 +00:00
|
|
|
done = token_state(p, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
case PTYPE_FORM:
|
2017-02-16 02:02:00 +00:00
|
|
|
done = form_state(p, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
case PTYPE_STRING:
|
2017-02-16 02:02:00 +00:00
|
|
|
done = string_state(p, c);
|
2017-02-09 20:02:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2017-02-16 02:02:00 +00:00
|
|
|
int gst_parse_cstring(GstParser *p, const char *string) {
|
2017-02-09 20:02:59 +00:00
|
|
|
int bytesRead = 0;
|
2017-04-17 04:15:18 +00:00
|
|
|
while ((p->status == GST_PARSER_PENDING || p->status == GST_PARSER_ROOT)
|
|
|
|
&& (string[bytesRead] != '\0')) {
|
2017-02-16 02:02:00 +00:00
|
|
|
dispatch_char(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 */
|
2017-03-22 22:35:54 +00:00
|
|
|
int gst_parse_string(GstParser *p, const uint8_t *string) {
|
2017-03-08 22:34:25 +00:00
|
|
|
uint32_t i;
|
2017-03-10 05:17:34 +00:00
|
|
|
for (i = 0; i < gst_string_length(string); ++i) {
|
2017-04-17 04:15:18 +00:00
|
|
|
if (p->status != GST_PARSER_PENDING && p->status != GST_PARSER_ROOT) break;
|
2017-03-08 22:34:25 +00:00
|
|
|
dispatch_char(p, string[i]);
|
2017-03-10 05:17:34 +00:00
|
|
|
}
|
2017-03-08 22:34:25 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* 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) */
|
2017-02-16 02:02:00 +00:00
|
|
|
void gst_parser(GstParser *p, Gst *vm) {
|
2017-02-09 20:02:59 +00:00
|
|
|
p->vm = vm;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstParseState *data = gst_alloc(vm, sizeof(GstParseState) * 10);
|
2017-03-14 23:13:17 +00:00
|
|
|
p->cap = 10;
|
2017-02-09 20:02:59 +00:00
|
|
|
p->data = data;
|
|
|
|
p->count = 0;
|
|
|
|
p->index = 0;
|
2017-03-14 23:13:17 +00:00
|
|
|
p->quoteCount = 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
p->error = NULL;
|
2017-04-17 04:15:18 +00:00
|
|
|
p->status = GST_PARSER_ROOT;
|
2017-02-16 02:02:00 +00:00
|
|
|
p->value.type = GST_NIL;
|
2017-03-08 15:34:22 +00:00
|
|
|
p->flags = GST_PARSER_FLAG_EXPECTING_COMMENT;
|
2017-02-16 02:02:00 +00:00
|
|
|
parser_push(p, PTYPE_ROOT, ' ');
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|