csonpath 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: csonpath
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Provides-Extra: dev
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: csonpath
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Description-Content-Type: text/markdown
5
5
  License-File: LICENSE
6
6
  Provides-Extra: dev
@@ -1,9 +1,28 @@
1
+ #if !defined(CSONPATH_JSON) || !defined(CSONPATH_NULL) || !defined(CSONPATH_GET) || \
2
+ !defined(CSONPATH_AT) || !defined(CSONPATH_IS_OBJ) || !defined(CSONPATH_IS_ARRAY) || \
3
+ !defined(CSONPATH_CALLBACK) || !defined(CSONPATH_CALLBACK_DATA) || \
4
+ !defined(CSONPATH_EQUAL_STR) || !defined(CSONPATH_CALL_CALLBACK) || \
5
+ !defined(CSONPATH_FOREACH_EXT) || !defined(CSONPATH_APPEND_AT) || \
6
+ !defined(CSONPATH_REMOVE_CHILD) || !defined(CSONPATH_NEED_FOREACH_REDO) || \
7
+ !defined(CSONPATH_ARRAY_APPEND_INCREF) || !defined(CSONPATH_REMOVE)
8
+ # error "some defined are missing"
9
+ #endif
10
+
1
11
  #define CSONPATH_UNUSED __attribute__((unused))
2
12
  #define MAY_ALIAS __attribute__((__may_alias__))
3
13
 
4
- #define CSONPATH_FOREACH(obj, el, code) \
5
- CSONPATH_FOREACH_EXT(obj, el, code, key_idx)
14
+ #ifndef CSONPATH_FOREACH
15
+ # define CSONPATH_FOREACH(obj, el, code) \
16
+ CSONPATH_FOREACH_EXT(obj, el, code, key_idx)
17
+ #endif
18
+
19
+ #ifndef CSONPATH_FIND_ALL_RET
20
+ #define CSONPATH_FIND_ALL_RET CSONPATH_JSON
21
+ #endif
6
22
 
23
+ #ifndef CSONPATH_FIND_ALL_RET_INIT
24
+ #define CSONPATH_FIND_ALL_RET_INIT CSONPATH_NEW_ARRAY
25
+ #endif
7
26
 
8
27
  enum csonpath_instuction_raw {
9
28
  CSONPATH_INST_ROOT,
@@ -54,11 +73,12 @@ struct csonpath_instruction {
54
73
  #define CSONPATH_INST_MIN_ALLOC (1 << 7)
55
74
 
56
75
  struct csonpath {
57
- char *path;
58
- struct csonpath_instruction *inst_lst;
59
- int compiled;
60
- int inst_idx;
61
- int inst_size;
76
+ char *path;
77
+ char *compile_error;
78
+ struct csonpath_instruction *inst_lst;
79
+ int compiled;
80
+ int inst_idx;
81
+ int inst_size;
62
82
  };
63
83
 
64
84
  struct csonpath_child_info {
@@ -69,13 +89,23 @@ struct csonpath_child_info {
69
89
  };
70
90
  };
71
91
 
72
- #define CSONPATH_CLASSIC_ERR(args...) do { \
73
- fprintf(stderr, args); \
74
- goto error; \
75
- } while (0)
92
+ /* I'm assuming error message won't be longer than 125 */
93
+ #define CSONPATH_COMPILE_ERR(tmp, idx, args...) do { \
94
+ int ltmp = strlen(tmp), lidx, oidx = idx; \
95
+ if (cjp->compile_error) \
96
+ free(cjp->compile_error); \
97
+ cjp->compile_error = malloc(ltmp * 2 + 1024); \
98
+ lidx = snprintf(cjp->compile_error, 1024, "colum %d:\n", oidx); \
99
+ strcpy(cjp->compile_error + lidx, tmp); \
100
+ ltmp += lidx; \
101
+ cjp->compile_error[ltmp++] = '\n'; \
102
+ for (; oidx; --oidx) {cjp->compile_error[ltmp++] = ' ';} \
103
+ snprintf(cjp->compile_error + ltmp, 1024 - lidx, "\\-- "args); \
104
+ goto error; \
105
+ } while (0)
76
106
 
77
107
  static inline struct csonpath_child_info *csonpath_child_info_set(struct csonpath_child_info *child_info,
78
- const CSONPATH_JSON j, const intptr_t key)
108
+ CSONPATH_JSON j, const intptr_t key)
79
109
  {
80
110
  if (CSONPATH_IS_OBJ(j)) {
81
111
  *child_info = (struct csonpath_child_info){.key=(const char *)key, .type=CSONPATH_STR};
@@ -89,7 +119,8 @@ static inline void csonpath_destroy(struct csonpath cjp[static 1])
89
119
  {
90
120
  free(cjp->path);
91
121
  free(cjp->inst_lst);
92
- cjp->path = NULL;
122
+ free(cjp->compile_error);
123
+ *cjp = (struct csonpath){};
93
124
  }
94
125
 
95
126
  static inline int csonpath_init(struct csonpath cjp[static 1],
@@ -107,6 +138,7 @@ static inline int csonpath_set_path(struct csonpath cjp[static 1],
107
138
  const char path[static 1])
108
139
  {
109
140
  free(cjp->path);
141
+ free(cjp->compile_error);
110
142
  free(cjp->inst_lst);
111
143
  return csonpath_init(cjp, path);
112
144
  }
@@ -125,14 +157,18 @@ void csonpath_push_inst(struct csonpath cjp[static 1], int inst)
125
157
  int csonpath_compile(struct csonpath cjp[static 1])
126
158
  {
127
159
  char *walker = cjp->path;
160
+ char *orig = walker;
128
161
  char *next;
129
162
  char to_check;
163
+ char *tmp;
130
164
 
131
165
  if (cjp->compiled)
132
166
  return 0;
133
- root_again:
167
+
168
+ tmp = strdup(cjp->path);
169
+ root_again:
134
170
  if (*walker != '$') {
135
- CSONPATH_CLASSIC_ERR("'$' needed");
171
+ CSONPATH_COMPILE_ERR(tmp, walker - orig, "%s", "'$' needed");
136
172
  }
137
173
  ++walker;
138
174
  csonpath_push_inst(cjp, CSONPATH_INST_ROOT);
@@ -150,7 +186,7 @@ again:
150
186
  if (*walker == '*') {
151
187
  csonpath_push_inst(cjp, CSONPATH_INST_GET_ALL);
152
188
  if (walker[1] != ']') {
153
- CSONPATH_CLASSIC_ERR("unclose bracket\n");
189
+ CSONPATH_COMPILE_ERR(tmp, walker - orig, "%s", "unclose bracket\n");
154
190
  }
155
191
  cjp->inst_lst[cjp->inst_idx - 1].next = 2;
156
192
  walker += 2;
@@ -227,12 +263,14 @@ again:
227
263
  next = walker;
228
264
  do {
229
265
  if (*next < '0' || *next > '9') {
230
- CSONPATH_CLASSIC_ERR("unexpected '%c', sting, filter or number require\n", *next);
266
+ CSONPATH_COMPILE_ERR(tmp, walker - orig,
267
+ "unexpected '%c', sting, filter or number require\n", *next);
231
268
  }
232
269
  next++;
233
270
  } while (*next && *next != ']');
234
271
  if (!*next) {
235
- CSONPATH_CLASSIC_ERR("unclose bracket\n");
272
+ CSONPATH_COMPILE_ERR(tmp, walker - orig,
273
+ "%s", "unclose bracket\n");
236
274
  }
237
275
  *next = 0;
238
276
  num = atoi(walker);
@@ -269,7 +307,8 @@ again:
269
307
  *next = 0;
270
308
  ++next;
271
309
  if (*next != ']')
272
- CSONPATH_CLASSIC_ERR("']' require instead of '%c'\n", *next);
310
+ CSONPATH_COMPILE_ERR(tmp, walker - orig,
311
+ "']' require instead of '%c'\n", *next);
273
312
 
274
313
  csonpath_push_inst(cjp, CSONPATH_INST_GET_OBJ);
275
314
  cjp->inst_lst[cjp->inst_idx - 1].next = next - walker + 1;
@@ -310,10 +349,12 @@ again:
310
349
  if (*walker == 0) {
311
350
  csonpath_push_inst(cjp, CSONPATH_INST_END);
312
351
  cjp->compiled = 1;
352
+ free(tmp);
313
353
  return 0;
314
354
  }
315
355
  error:
316
356
  cjp->inst_lst[0] = (struct csonpath_instruction){.inst=CSONPATH_INST_BROKEN};
357
+ free(tmp);
317
358
  return -1;
318
359
  }
319
360
 
@@ -362,7 +403,7 @@ need_reloop_in = 0;
362
403
  /* Find All */
363
404
 
364
405
  #define CSONPATH_DO_PRE_OPERATION \
365
- CSONPATH_JSON ret_ar = CSONPATH_NEW_ARRAY();
406
+ CSONPATH_FIND_ALL_RET ret_ar = CSONPATH_FIND_ALL_RET_INIT();
366
407
 
367
408
  #define CSONPATH_DO_POST_OPERATION \
368
409
  if (ret == CSONPATH_NULL) CSONPATH_REMOVE(ret_ar)
@@ -371,7 +412,7 @@ need_reloop_in = 0;
371
412
  int nb_res = 0;
372
413
 
373
414
  #define CSONPATH_DO_FUNC_NAME find_all
374
- #define CSONPATH_DO_RET_TYPE CSONPATH_JSON
415
+ #define CSONPATH_DO_RET_TYPE CSONPATH_FIND_ALL_RET
375
416
  #define CSONPATH_DO_RETURN ({CSONPATH_ARRAY_APPEND_INCREF(ret_ar, tmp); return ret_ar;})
376
417
 
377
418
  #define CSONPATH_DO_FIND_ALL \
@@ -385,7 +426,7 @@ need_reloop_in = 0;
385
426
  return ret_ar;
386
427
 
387
428
  #define CSONPATH_DO_EXTRA_ARGS_IN , ret_ar
388
- #define CSONPATH_DO_EXTRA_DECLATION , CSONPATH_JSON ret_ar
429
+ #define CSONPATH_DO_EXTRA_DECLATION , CSONPATH_FIND_ALL_RET ret_ar
389
430
 
390
431
 
391
432
  #include "csonpath_do.h"
@@ -134,7 +134,7 @@ static CSONPATH_DO_RET_TYPE csonpath_do_dotdot(struct csonpath cjp[static 1],
134
134
  CSONPATH_DO_EXTRA_ARGS_NEESTED);
135
135
  CSONPATH_DO_FIND_ALL;
136
136
  }
137
- });
137
+ })
138
138
  CSONPATH_DO_FIND_ALL_OUT;
139
139
  return tret;
140
140
  }
@@ -173,8 +173,8 @@ static CSONPATH_DO_RET_TYPE csonpath_do_internal(struct csonpath cjp[static 1],
173
173
  CSONPATH_JSON el2;
174
174
 
175
175
  CSONPATH_DO_FILTER_LOOP_PRE_SET
176
- if (cjp->inst_lst[idx].inst != CSONPATH_INST_FILTER_OPERAND_STR)
177
- CSONPATH_GETTER_ERR("filter support only comparaiso with STR");
176
+ if (cjp->inst_lst[idx].inst != CSONPATH_INST_FILTER_OPERAND_STR)
177
+ CSONPATH_GETTER_ERR("filter support only comparaison with STR");
178
178
 
179
179
  CSONPATH_FOREACH_EXT(el, el2, {
180
180
  if (!strcmp((char *)neested_key, filter_key)) {
@@ -281,7 +281,7 @@ static CSONPATH_DO_RET_TYPE csonpath_do_(struct csonpath cjp[static 1],
281
281
  (void)ctx;
282
282
  csonpath_compile(cjp);
283
283
  if (cjp->inst_lst[0].inst == CSONPATH_INST_BROKEN) {
284
- CSONPATH_GETTER_ERR("fail to compile\n");
284
+ CSONPATH_GETTER_ERR("fail to compile: %s\n", cjp->compile_error);
285
285
  return CSONPATH_NONE_FOUND_RET;
286
286
  }
287
287
 
@@ -148,7 +148,9 @@ static PyObject *PyCsonPath_new(PyTypeObject *subtype, PyObject* args,
148
148
  return PyErr_NoMemory();
149
149
 
150
150
  if (csonpath_compile(ret) < 0) {
151
- PyErr_Format(PyExc_ValueError, "Fail to compile %s", s);
151
+ char *error = ret->compile_error;
152
+ PyErr_Format(PyExc_ValueError, "compilation fail %s", error);
153
+ csonpath_destroy(ret);
152
154
  return NULL;
153
155
  }
154
156
  self->cp = ret;
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "csonpath"
7
- version = "0.0.2"
7
+ version = "0.0.3"
8
8
  readme = "README.md"
9
9
 
10
10
  [tool.setuptools]
File without changes
File without changes
File without changes
File without changes