-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonpath.c
176 lines (137 loc) · 4.17 KB
/
jsonpath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ext/spl/spl_exceptions.h"
#include "ext/standard/info.h"
#include "php.h"
#include "php_jsonpath.h"
#include "src/jsonpath/exceptions.h"
#include "src/jsonpath/interpreter.h"
#include "src/jsonpath/lexer.h"
#include "src/jsonpath/parser.h"
#include "zend_exceptions.h"
static bool scan_tokens(char* json_path, struct jpath_token* tok, int* tok_count);
#ifdef JSONPATH_DEBUG
void print_lex_tokens(struct jpath_token lex_tok[], int lex_tok_count, const char* m);
#endif
zend_class_entry* jsonpath_ce;
zend_class_entry* jsonpath_exception_ce;
#include "jsonpath_arginfo.h"
#define LEX_TOK_ARR_LEN 64
PHP_METHOD(JsonPath_JsonPath, find) {
char* j_path;
size_t j_path_len;
zval* search_target;
/* parse php method parameters */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "as", &search_target, &j_path, &j_path_len) == FAILURE) {
return;
}
/* tokenize JSON-path string */
struct jpath_token lex_tok[LEX_TOK_ARR_LEN];
int lex_tok_count = 0;
bool scan_ok = scan_tokens(j_path, lex_tok, &lex_tok_count);
if (!scan_ok) {
return;
}
#ifdef JSONPATH_DEBUG
print_lex_tokens(lex_tok, lex_tok_count, "Lexer - Processed tokens");
#endif
/* assemble an array of query execution instructions from parsed tokens */
int i = 0;
struct node_pool pool = {0};
struct ast_node* head = parse_jsonpath(lex_tok, &i, lex_tok_count, &pool);
if (head == NULL) {
free_php_objects(&pool);
return;
}
#ifdef JSONPATH_DEBUG
print_ast(head->next, "Parser - AST sent to interpreter", 0);
#endif
/* execute the JSON-path query instructions against the search target (PHP object/array) */
array_init(return_value);
eval_ast(search_target, search_target, head, return_value);
free_php_objects(&pool);
/* return false if no results were found by the JSON-path query */
if (zend_hash_num_elements(HASH_OF(return_value)) == 0) {
convert_to_boolean(return_value);
RETURN_FALSE;
}
}
static bool scan_tokens(char* json_path, struct jpath_token* tok, int* tok_count) {
char* p = json_path;
int i = 0;
while (*p != '\0') {
if (i >= LEX_TOK_ARR_LEN) {
throw_jsonpath_exception("The query is too long, token count exceeds maximum amount (%d)", LEX_TOK_ARR_LEN);
return false;
}
if (!scan(&p, &tok[i], json_path)) {
return false;
}
i++;
}
*tok_count = i;
return true;
}
#ifdef JSONPATH_DEBUG
void print_lex_tokens(struct jpath_token lex_tok[], int lex_tok_count, const char* m) {
printf("--------------------------------------\n");
printf("%s\n\n", m);
for (int i = 0; i < lex_tok_count; i++) {
printf("\t• %s", LEX_STR[lex_tok[i].type]);
if (lex_tok[i].len > 0) {
printf(" [val=%.*s]", lex_tok[i].len, lex_tok[i].val);
}
printf("\n");
}
}
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(jsonpath) {
jsonpath_ce = register_class_JsonPath_JsonPath();
jsonpath_exception_ce = register_class_JsonPath_JsonPathException(spl_ce_RuntimeException);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(jsonpath) {
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(jsonpath) {
php_info_print_table_start();
php_info_print_table_row(2, "jsonpath support", "enabled");
php_info_print_table_row(2, "jsonpath version", PHP_JSONPATH_VERSION);
php_info_print_table_end();
}
/* }}} */
/* {{{ jsonpath_functions[]
*
* Every user visible function must have an entry in jsonpath_functions[].
*/
const zend_function_entry jsonpath_functions[] = {
PHP_FE_END /* Must be the last line in jsonpath_functions[] */
};
/* }}} */
/* {{{ jsonpath_module_entry
*/
zend_module_entry jsonpath_module_entry = {
STANDARD_MODULE_HEADER, /**/
"jsonpath", /**/
jsonpath_functions, /**/
PHP_MINIT(jsonpath), /**/
PHP_MSHUTDOWN(jsonpath), /**/
NULL, /* nothing to do at request start */
NULL, /* nothing to do at request end */
PHP_MINFO(jsonpath), /**/
PHP_JSONPATH_VERSION, /**/
STANDARD_MODULE_PROPERTIES /**/
};
/* }}} */
#ifdef COMPILE_DL_JSONPATH
ZEND_GET_MODULE(jsonpath)
#endif