jv jv_load_file(const char *, int);
-typedef enum {
- JV_PARSE_EXPLODE_TOPLEVEL_ARRAY = 1
-} jv_parser_flags;
struct jv_parser;
-struct jv_parser* jv_parser_new(jv_parser_flags);
+struct jv_parser* jv_parser_new();
void jv_parser_set_buf(struct jv_parser*, const char*, int, int);
jv jv_parser_next(struct jv_parser*);
void jv_parser_free(struct jv_parser*);
int stackpos;
int stacklen;
jv next;
-
- jv_parser_flags flags;
char* tokenbuf;
int tokenpos;
};
-static void parser_init(struct jv_parser* p, jv_parser_flags flags) {
- p->flags = flags;
+static void parser_init(struct jv_parser* p) {
p->stack = 0;
p->stacklen = p->stackpos = 0;
p->next = jv_invalid();
break;
case ',':
- if (p->stackpos == 1 && (p->flags & JV_PARSE_EXPLODE_TOPLEVEL_ARRAY) &&
- jv_get_kind(p->stack[0]) == JV_KIND_ARRAY)
- return 0;
if (!jv_is_valid(p->next))
return "Expected value before ','";
if (p->stackpos == 0)
if (p->stackpos == 0 || jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_ARRAY)
return "Unmatched ']'";
if (jv_is_valid(p->next)) {
- if (p->stackpos != 1 || !(p->flags & JV_PARSE_EXPLODE_TOPLEVEL_ARRAY)) {
- p->stack[p->stackpos-1] = jv_array_append(p->stack[p->stackpos-1], p->next);
- p->next = jv_invalid();
- }
+ p->stack[p->stackpos-1] = jv_array_append(p->stack[p->stackpos-1], p->next);
+ p->next = jv_invalid();
} else {
if (jv_array_length(jv_copy(p->stack[p->stackpos-1])) != 0) {
// this case hits on input like [1,2,3,]
return "Expected another array element";
}
}
- if (p->stackpos == 1 && (p->flags & JV_PARSE_EXPLODE_TOPLEVEL_ARRAY)) {
- jv_free(p->stack[--p->stackpos]);
- } else {
- jv_free(p->next);
- p->next = p->stack[--p->stackpos];
- }
+ jv_free(p->next);
+ p->next = p->stack[--p->stackpos];
break;
case '}':
static const presult OK = "output produced";
static int check_done(struct jv_parser* p, jv* out) {
- if ((p->stackpos == 0 && jv_is_valid(p->next)) ||
- (p->stackpos == 1 && (p->flags & JV_PARSE_EXPLODE_TOPLEVEL_ARRAY) &&
- jv_get_kind(p->stack[0]) == JV_KIND_ARRAY && jv_is_valid(p->next))) {
+ if (p->stackpos == 0 && jv_is_valid(p->next)) {
*out = p->next;
p->next = jv_invalid();
return 1;
return answer;
}
-struct jv_parser* jv_parser_new(jv_parser_flags flags) {
+struct jv_parser* jv_parser_new() {
struct jv_parser* p = jv_mem_alloc(sizeof(struct jv_parser));
- parser_init(p, flags);
+ parser_init(p);
return p;
}
jv jv_parse_sized(const char* string, int length) {
struct jv_parser parser;
- parser_init(&parser, 0);
+ parser_init(&parser);
jv_parser_set_buf(&parser, string, length, 0);
jv value = jv_parser_next(&parser);
if (jv_is_valid(value)) {
ninput_files = 0;
int further_args_are_files = 0;
int jq_flags = 0;
- jv_parser_flags parser_flags = 0;
jv program_arguments = jv_array();
for (int i=1; i<argc; i++) {
if (further_args_are_files) {
options |= FROM_FILE;
} else if (isoption(argv[i], 'e', "exit-status")) {
options |= EXIT_STATUS;
- } else if (isoption(argv[i], 'I', "online-input")) {
- parser_flags = JV_PARSE_EXPLODE_TOPLEVEL_ARRAY;
} else if (isoption(argv[i], 0, "arg")) {
if (i >= argc - 2) {
fprintf(stderr, "%s: --arg takes two parameters (e.g. -a varname value)\n", progname);
slurped = jv_array();
}
}
- struct jv_parser* parser = jv_parser_new(parser_flags);
+ struct jv_parser* parser = jv_parser_new();
char buf[4096];
while (read_more(buf, sizeof(buf))) {
if (options & RAW_INPUT) {