Fix the jv_parser interface.
This commit is contained in:
1 parent
8630fb02bb
commit
8c2e228c74
5 files changed
+55
-50
No files matched your search
@@ -130,10 +130,10 @@ void jv_nomem_handler(jv_nomem_handler_f, void *);
|
|||||||
jv jv_load_file(const char *, int);
|
jv jv_load_file(const char *, int);
|
||||||
|
|
||||||
struct jv_parser;
|
struct jv_parser;
|
||||||
void jv_parser_init(struct jv_parser*);
|
struct jv_parser* jv_parser_new();
|
||||||
void jv_parser_free(struct jv_parser*);
|
|
||||||
void jv_parser_set_buf(struct jv_parser*, const char*, int, int);
|
void jv_parser_set_buf(struct jv_parser*, const char*, int, int);
|
||||||
jv jv_parser_next(struct jv_parser*);
|
jv jv_parser_next(struct jv_parser*);
|
||||||
|
void jv_parser_free(struct jv_parser*);
|
||||||
|
|
||||||
jv jv_get(jv, jv);
|
jv jv_get(jv, jv);
|
||||||
jv jv_set(jv, jv, jv);
|
jv jv_set(jv, jv, jv);
|
||||||
|
|||||||
@@ -4,11 +4,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "jv_parse.h"
|
|
||||||
|
|
||||||
jv jv_load_file(const char* filename, int raw) {
|
jv jv_load_file(const char* filename, int raw) {
|
||||||
FILE* file = fopen(filename, "r");
|
FILE* file = fopen(filename, "r");
|
||||||
struct jv_parser parser;
|
struct jv_parser* parser;
|
||||||
jv data;
|
jv data;
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
||||||
@@ -19,7 +18,7 @@ jv jv_load_file(const char* filename, int raw) {
|
|||||||
data = jv_string("");
|
data = jv_string("");
|
||||||
} else {
|
} else {
|
||||||
data = jv_array();
|
data = jv_array();
|
||||||
jv_parser_init(&parser);
|
parser = jv_parser_new();
|
||||||
}
|
}
|
||||||
while (!feof(file) && !ferror(file)) {
|
while (!feof(file) && !ferror(file)) {
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
@@ -27,15 +26,15 @@ jv jv_load_file(const char* filename, int raw) {
|
|||||||
if (raw) {
|
if (raw) {
|
||||||
data = jv_string_concat(data, jv_string_sized(buf, (int)n));
|
data = jv_string_concat(data, jv_string_sized(buf, (int)n));
|
||||||
} else {
|
} else {
|
||||||
jv_parser_set_buf(&parser, buf, n, !feof(file));
|
jv_parser_set_buf(parser, buf, n, !feof(file));
|
||||||
jv value;
|
jv value;
|
||||||
while (jv_is_valid((value = jv_parser_next(&parser))))
|
while (jv_is_valid((value = jv_parser_next(parser))))
|
||||||
data = jv_array_append(data, value);
|
data = jv_array_append(data, value);
|
||||||
jv_free(value);
|
jv_free(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!raw)
|
if (!raw)
|
||||||
jv_parser_free(&parser);
|
jv_parser_free(parser);
|
||||||
int badread = ferror(file);
|
int badread = ferror(file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
if (badread) {
|
if (badread) {
|
||||||
|
|||||||
+44
-5
@@ -3,9 +3,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "jv_dtoa.h"
|
#include "jv_dtoa.h"
|
||||||
#include "jv_parse.h"
|
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
#include "jv_alloc.h"
|
#include "jv_alloc.h"
|
||||||
|
#include "jv_dtoa.h"
|
||||||
|
|
||||||
typedef const char* presult;
|
typedef const char* presult;
|
||||||
|
|
||||||
@@ -16,7 +16,35 @@ typedef const char* presult;
|
|||||||
#define pfunc presult
|
#define pfunc presult
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void jv_parser_init(struct jv_parser* p) {
|
struct jv_parser {
|
||||||
|
const char* curr_buf;
|
||||||
|
int curr_buf_length;
|
||||||
|
int curr_buf_pos;
|
||||||
|
int curr_buf_is_partial;
|
||||||
|
unsigned bom_strip_position;
|
||||||
|
|
||||||
|
jv* stack;
|
||||||
|
int stackpos;
|
||||||
|
int stacklen;
|
||||||
|
jv next;
|
||||||
|
|
||||||
|
char* tokenbuf;
|
||||||
|
int tokenpos;
|
||||||
|
int tokenlen;
|
||||||
|
|
||||||
|
int line, column;
|
||||||
|
|
||||||
|
struct dtoa_context dtoa;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
JV_PARSER_NORMAL,
|
||||||
|
JV_PARSER_STRING,
|
||||||
|
JV_PARSER_STRING_ESCAPE
|
||||||
|
} st;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void parser_init(struct jv_parser* p) {
|
||||||
p->stack = 0;
|
p->stack = 0;
|
||||||
p->stacklen = p->stackpos = 0;
|
p->stacklen = p->stackpos = 0;
|
||||||
p->next = jv_invalid();
|
p->next = jv_invalid();
|
||||||
@@ -31,7 +59,7 @@ void jv_parser_init(struct jv_parser* p) {
|
|||||||
jvp_dtoa_context_init(&p->dtoa);
|
jvp_dtoa_context_init(&p->dtoa);
|
||||||
}
|
}
|
||||||
|
|
||||||
void jv_parser_free(struct jv_parser* p) {
|
static void parser_free(struct jv_parser* p) {
|
||||||
jv_free(p->next);
|
jv_free(p->next);
|
||||||
for (int i=0; i<p->stackpos; i++)
|
for (int i=0; i<p->stackpos; i++)
|
||||||
jv_free(p->stack[i]);
|
jv_free(p->stack[i]);
|
||||||
@@ -341,6 +369,17 @@ static pfunc scan(struct jv_parser* p, char ch, jv* out) {
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct jv_parser* jv_parser_new() {
|
||||||
|
struct jv_parser* p = jv_mem_alloc(sizeof(struct jv_parser));
|
||||||
|
parser_init(p);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void jv_parser_free(struct jv_parser* p) {
|
||||||
|
parser_free(p);
|
||||||
|
jv_mem_free(p);
|
||||||
|
}
|
||||||
|
|
||||||
static const unsigned char UTF8_BOM[] = {0xEF,0xBB,0xBF};
|
static const unsigned char UTF8_BOM[] = {0xEF,0xBB,0xBF};
|
||||||
|
|
||||||
void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial) {
|
void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial) {
|
||||||
@@ -404,7 +443,7 @@ jv jv_parser_next(struct jv_parser* p) {
|
|||||||
|
|
||||||
jv jv_parse_sized(const char* string, int length) {
|
jv jv_parse_sized(const char* string, int length) {
|
||||||
struct jv_parser parser;
|
struct jv_parser parser;
|
||||||
jv_parser_init(&parser);
|
parser_init(&parser);
|
||||||
jv_parser_set_buf(&parser, string, length, 0);
|
jv_parser_set_buf(&parser, string, length, 0);
|
||||||
jv value = jv_parser_next(&parser);
|
jv value = jv_parser_next(&parser);
|
||||||
if (jv_is_valid(value)) {
|
if (jv_is_valid(value)) {
|
||||||
@@ -429,7 +468,7 @@ jv jv_parse_sized(const char* string, int length) {
|
|||||||
jv_free(value);
|
jv_free(value);
|
||||||
value = jv_invalid_with_msg(jv_string("Expected JSON value"));
|
value = jv_invalid_with_msg(jv_string("Expected JSON value"));
|
||||||
}
|
}
|
||||||
jv_parser_free(&parser);
|
parser_free(&parser);
|
||||||
|
|
||||||
if (!jv_is_valid(value) && jv_invalid_has_msg(jv_copy(value))) {
|
if (!jv_is_valid(value) && jv_invalid_has_msg(jv_copy(value))) {
|
||||||
jv msg = jv_invalid_get_msg(value);
|
jv msg = jv_invalid_get_msg(value);
|
||||||
|
|||||||
-31
@@ -1,31 +0,0 @@
|
|||||||
#ifndef JV_PARSE_H
|
|
||||||
#define JV_PARSE_H
|
|
||||||
#include "jv_dtoa.h"
|
|
||||||
struct jv_parser {
|
|
||||||
const char* curr_buf;
|
|
||||||
int curr_buf_length;
|
|
||||||
int curr_buf_pos;
|
|
||||||
int curr_buf_is_partial;
|
|
||||||
unsigned bom_strip_position;
|
|
||||||
|
|
||||||
jv* stack;
|
|
||||||
int stackpos;
|
|
||||||
int stacklen;
|
|
||||||
jv next;
|
|
||||||
|
|
||||||
char* tokenbuf;
|
|
||||||
int tokenpos;
|
|
||||||
int tokenlen;
|
|
||||||
|
|
||||||
int line, column;
|
|
||||||
|
|
||||||
struct dtoa_context dtoa;
|
|
||||||
|
|
||||||
enum {
|
|
||||||
JV_PARSER_NORMAL,
|
|
||||||
JV_PARSER_STRING,
|
|
||||||
JV_PARSER_STRING_ESCAPE
|
|
||||||
} st;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "jq.h"
|
#include "jq.h"
|
||||||
#include "jv_parse.h"
|
|
||||||
#include "jv_alloc.h"
|
#include "jv_alloc.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
@@ -258,8 +257,7 @@ int main(int argc, char* argv[]) {
|
|||||||
slurped = jv_array();
|
slurped = jv_array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct jv_parser parser;
|
struct jv_parser* parser = jv_parser_new();
|
||||||
jv_parser_init(&parser);
|
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
while (read_more(buf, sizeof(buf))) {
|
while (read_more(buf, sizeof(buf))) {
|
||||||
if (options & RAW_INPUT) {
|
if (options & RAW_INPUT) {
|
||||||
@@ -273,9 +271,9 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
jv_parser_set_buf(&parser, buf, strlen(buf), !feof(stdin));
|
jv_parser_set_buf(parser, buf, strlen(buf), !feof(stdin));
|
||||||
jv value;
|
jv value;
|
||||||
while (jv_is_valid((value = jv_parser_next(&parser)))) {
|
while (jv_is_valid((value = jv_parser_next(parser)))) {
|
||||||
if (options & SLURP) {
|
if (options & SLURP) {
|
||||||
slurped = jv_array_append(slurped, value);
|
slurped = jv_array_append(slurped, value);
|
||||||
} else {
|
} else {
|
||||||
@@ -293,7 +291,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jv_parser_free(&parser);
|
jv_parser_free(parser);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
goto out;
|
goto out;
|
||||||
if (options & SLURP) {
|
if (options & SLURP) {
|
||||||
|
|||||||
Reference in new issue
Block a user