/
parsley.c
676 lines (605 loc) · 18.7 KB
/
parsley.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <stdarg.h>
#include <json/json.h>
#include "parsley.h"
#include "y.tab.h"
#include <json/printbuf.h>
#include "functions.h"
#include "util.h"
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdbool.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlwriter.h>
#include <libxml/debugXML.h>
#include <libexslt/exslt.h>
int yywrap(void){
return 1;
}
struct ll {
xmlChar *name;
struct ll *next;
};
static char *
full_key_name(contextPtr c) {
if(c == NULL || c->parent == NULL) return strdup("/");
static struct ll * last = NULL;
while(c->parent != NULL) {
if(c->tag != NULL) {
struct ll * ptr = calloc(sizeof(struct ll), 1);
ptr->name = c->tag;
ptr->next = last;
last = ptr;
}
c = c->parent;
}
struct printbuf *buf = printbuf_new();
while(last != NULL) {
sprintbuf(buf, "/%s", last->name);
last = last->next;
}
char *out = strdup(buf->buf);
printbuf_free(buf);
return out;
}
void parsed_parsley_free(parsedParsleyPtr ptr) {
if(ptr->xml != NULL) xmlFree(ptr->xml);
if(ptr->error != NULL) free(ptr->error);
free(ptr);
}
static parsedParsleyPtr parse_error(char* format, ...) {
parsedParsleyPtr ptr = (parsedParsleyPtr) calloc(sizeof(parsed_parsley), 1);
ptr->xml = NULL;
va_list args;
va_start(args, format);
vasprintf(&ptr->error, format, args);
va_end(args);
return ptr;
}
parsedParsleyPtr parsley_parse_file(parsleyPtr parsley, char* file, bool html) {
if(html) {
htmlParserCtxtPtr htmlCtxt = htmlNewParserCtxt();
htmlDocPtr html = htmlCtxtReadFile(htmlCtxt, file, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |HTML_PARSE_NOWARNING);
htmlFreeParserCtxt(htmlCtxt);
if(html == NULL) return parse_error("Couldn't parse file: %s\n", file);
parsedParsleyPtr out = parsley_parse_doc(parsley, html);
xmlFreeDoc(html);
return out;
} else {
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
xmlDocPtr xml = xmlCtxtReadFile(ctxt, file, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |HTML_PARSE_NOWARNING);
xmlFreeParserCtxt(ctxt);
if(xml == NULL) return parse_error("Couldn't parse file: %s\n", file);
parsedParsleyPtr out = parsley_parse_doc(parsley, xml);
xmlFreeDoc(xml);
return out;
}
}
parsedParsleyPtr parsley_parse_string(parsleyPtr parsley, char* string, size_t size, bool html) {
if(html) {
htmlParserCtxtPtr htmlCtxt = htmlNewParserCtxt();
htmlDocPtr html = htmlCtxtReadMemory(htmlCtxt, string, size, "http://parslets.com/in-memory-string", NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |HTML_PARSE_NOWARNING);
if(html == NULL) return parse_error("Couldn't parse string");
parsedParsleyPtr out = parsley_parse_doc(parsley, html);
xmlFreeDoc(html);
return out;
} else {
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
xmlDocPtr xml = xmlCtxtReadMemory(ctxt, string, size, "http://parslets.com/in-memory-string", NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |HTML_PARSE_NOWARNING);
if(xml == NULL) return parse_error("Couldn't parse string");
parsedParsleyPtr out = parsley_parse_doc(parsley, xml);
xmlFreeDoc(xml);
return out;
}
}
static char *
xpath_of(xmlNodePtr node) {
if(node == NULL || node->name == NULL || node->parent == NULL) return strdup("/");
struct ll * ptr = (struct ll *) calloc(sizeof(struct ll), 1);
while(node->name != NULL && node->parent != NULL) {
if(node->ns == NULL) {
struct ll * tmp = (struct ll *) calloc(sizeof(struct ll), 1);
tmp->name = node->name;
tmp->next = ptr;
ptr = tmp;
}
node = node->parent;
}
struct printbuf *buf = printbuf_new();
sprintbuf(buf, "");
while(ptr->name != NULL) {
sprintbuf(buf, "/%s", ptr->name);
struct ll * last = ptr;
ptr = ptr->next;
free(last);
}
free(ptr);
char *str = strdup(strlen(buf->buf) ? buf->buf : "/");
printbuf_free(buf);
return str;
}
static void
unlink(xmlNodePtr xml) {
if(xml == NULL || xml->parent == NULL) return;
xmlNodePtr sibling = xml->parent->children;
if(sibling == xml) {
xml->parent->children = xml->next;
return;
}
while(sibling != NULL) {
if(sibling == xml) {
if(xml->prev) xml->prev->next = xml->next;
if(xml->next) xml->next->prev = xml->prev;
}
sibling = sibling->next;
}
}
// static bool
// is_root(xmlElementPtr xml) {
// return xml != NULL && xml->name != NULL && xml->prefix != NULL && !strcmp(xml->name, "root") && !strcmp(xml->prefix, "parsley");
// }
static bool
is_root(xmlNodePtr node) {
return node != NULL && node->ns != NULL && !strcmp(node->ns->prefix, "parsley") && !strcmp(node->name, "root");
}
int compare_pos (const void * a, const void * b)
{
char* as = xmlGetProp(*(xmlNodePtr*)a, "position");
char* bs = xmlGetProp(*(xmlNodePtr*)b, "position");
return atoi(as) - atoi(bs);
}
static void
_xmlAddChild(xmlNodePtr parent, xmlNodePtr child) {
xmlNodePtr node = parent->children;
if(node == NULL) {
parent->children = child;
return;
}
while(node->next != NULL){
node = node->next;
}
node->next = child;
}
static int
_xmlChildElementCount(xmlNodePtr n) {
xmlNodePtr child = n->children;
int i = 0;
while(child != NULL) {
i++;
child = child->next;
}
return i;
}
static void
collate(xmlNodePtr xml) {
// return ;
if(xml->type != XML_ELEMENT_NODE) return;
if(xml->ns != NULL && !strcmp(xml->ns->prefix, "parsley") && !strcmp(xml->name, "zipped")){
xmlNodePtr parent = xml->parent;
xmlNodePtr child = xml->children;
int n = _xmlChildElementCount(xml);
xmlNodePtr* name_nodes = malloc(n * sizeof(xmlNodePtr));
xmlNodePtr* lists = malloc(n * sizeof(xmlNodePtr));
bool* empty = malloc(n * sizeof(bool));
bool* multi = malloc(n * sizeof(bool));
bool* optional = malloc(n * sizeof(bool));
int len = 0;
for(int i = 0; i < n; i++) {
name_nodes[i] = child;
lists[i] = child->children;
multi[i] = false;
optional[i] = xmlGetProp(name_nodes[i], "optional") != NULL;
if(lists[i] != NULL && !strcmp(lists[i]->name, "groups")) {
lists[i] = lists[i]->children;
multi[i] = true;
}
lists[i]->parent->extra = i;
len += _xmlChildElementCount(lists[i]->parent);
child->children = NULL;
child = child->next;
}
xml->children = NULL;
xmlNodePtr* sortable = malloc(len * sizeof(xmlNodePtr));
int j = 0;
for(int i = 0; i < n; i++) {
xmlNodePtr node = lists[i];
while(node != NULL){
sortable[j++] = node;
// printf("%d/%d: %d/%d\n", i, n, j, len);
node = node->next;
}
}
for(int i = 0; i < len; i++) {
sortable[i]->next = NULL;
}
qsort(sortable, len, sizeof(xmlNodePtr), compare_pos);
xmlNodePtr groups = xml->parent;
groups->children = NULL;
xmlNodePtr group;
xmlNodePtr* targets = calloc(sizeof(xmlNodePtr), n);
for(j = 0; j < len; j++) {
int i = sortable[j]->parent->extra;
if (j == 0 || (!empty[i] && !multi[i] && !optional[i])) { // first or full
xmlNodePtr group = xmlNewChild(groups, xml->ns, "group", NULL); //new group
xmlSetProp(group, "optional", "true");
for(int k = 0; k < n; k++) {
empty[k] = true;
targets[k] = xmlCopyNode(name_nodes[k], 2);
_xmlAddChild(group, targets[k]);
if(multi[k]) targets[k] = xmlNewChild(targets[k], xml->ns, "groups", NULL);
}
}
if(!multi[i]) sortable[j] = sortable[j]->children;
if(empty[i] || multi[i]) _xmlAddChild(targets[i], sortable[j]);
empty[i] = false;
}
free(targets);
free(name_nodes);
free(lists);
free(optional);
free(empty);
free(multi);
free(sortable);
// xml->children = NULL;
// xmlFreeNode(xml);
collate(groups);
// TODO: done? figure out what the recursion target needs to be and reimplement
} else {
xmlNodePtr child = xml->children;
while(child != NULL){
collate(child);
child = child->next;
}
}
}
static void
prune(parsedParsleyPtr ptr, xmlNodePtr xml, char* err) {
if(xml == NULL || is_root(xml)) return;
bool optional = xmlGetProp(xml, "optional") != NULL;
if(optional) {
unlink(xml);
visit(ptr, xml->parent, err);
// fprintf(stderr, "optional ok\n");
return;
} else {
if(err == NULL) asprintf(&err, "%s was empty", xpath_of(xml));
if(!is_root(xml->parent)) {
// fprintf(stderr, "prune up: %s\n", xml->parent->name);
prune(ptr, xml->parent, err);
} else {
// fprintf(stderr, "error out\n");
ptr->error = err;
}
}
}
static void
visit(parsedParsleyPtr ptr, xmlNodePtr xml, char* err) {
if(xml == NULL) return;
if(xml->type != XML_ELEMENT_NODE) return;
xmlNodePtr child = xml->children;
xmlNodePtr parent = xml->parent;
if(parent == NULL) return;
if(xml_empty(xml)) {
if(err == NULL) asprintf(&err, "%s was empty", xpath_of(xml));
prune(ptr, xml, err);
} else if(err != NULL) {
free(err);
}
while(err == NULL && child != NULL){
child->parent = xml;
visit(ptr, child, err);
child = child->next;
}
}
static bool
xml_empty(xmlNodePtr xml) {
// fprintf(stderr, "%s\n", xml->name);
xmlNodePtr child = xml->children;
while(child != NULL) {
if(child->type != XML_TEXT_NODE) return false;
if(strlen(child->content)) return false;
child = child->next;
}
// printf("hello!\n");
return true;
}
parsedParsleyPtr parsley_parse_doc(parsleyPtr parsley, xmlDocPtr doc) {
parsedParsleyPtr ptr = (parsedParsleyPtr) calloc(sizeof(parsed_parsley), 1);
ptr->error = NULL;
ptr->parsley = parsley;
ptr->xml = xsltApplyStylesheet(parsley->stylesheet, doc, NULL);
if(ptr->xml != NULL && ptr->error == NULL) {
collate(ptr->xml->children);
visit(ptr, ptr->xml->children, NULL);
}
if(ptr->xml == NULL && ptr->error == NULL) { // == NULL
ptr->error = strdup("Internal runtime error");
}
return ptr;
}
static bool
json_invalid_object(parsleyPtr ptr, struct json_object *json) {
json_object_object_foreach(json, key, val) {
switch(json_object_get_type(val)) {
case json_type_string:
break;
case json_type_array:
if(json_object_array_length(val) != 1) {
ptr->error = strdup("Parselet arrays should have length 1.");
return true;
}
struct json_object * inner = json_object_array_get_idx(val, 0);
switch(json_object_get_type(inner)) {
case json_type_string:
break;
case json_type_object:
if(json_invalid_object(ptr, inner)) return true;
break;
default:
ptr->error = strdup("Arrays may contain either a single string or an object.");
return true;
}
break;
case json_type_object:
if(json_invalid_object(ptr, val)) {
return true;
}
break;
default:
ptr->error = strdup("Parslets can only be made up of strings, arrays, and objects.");
}
if(val == NULL || !json_object_is_type(val, json_type_string)) return false;
}
return false;
}
static bool
json_invalid(parsleyPtr ptr, struct json_object *json) {
if(!json_object_is_type(json, json_type_object)) {
ptr->error = strdup("The parslet root must be an object");
return true;
}
return json_invalid_object(ptr, json);
}
parsleyPtr parsley_compile(char* parsley_str, char* incl) {
parsleyPtr parsley = (parsleyPtr) calloc(sizeof(compiled_parsley), 1);
if(last_parsley_error != NULL) {
free(last_parsley_error);
last_parsley_error = NULL;
}
registerEXSLT();
struct json_object *json = json_tokener_parse(parsley_str);
if(is_error(json)) {
parsley->error = strdup("Your parslet is not valid json.");
// json_object_put(json); // frees json
return parsley;
}
if(json_invalid(parsley, json)) {
// fprintf(stderr, "Invalid parselet structure: %s\n", parsley->error);
return parsley;
}
xmlNodePtr node = new_stylesheet_skeleton(incl);
contextPtr context = new_context(json, node);
__parsley_recurse(context);
json_object_put(json); // frees json
parsley->error = last_parsley_error;
if(parsley->error == NULL) {
parsley->stylesheet = xsltParseStylesheetDoc(node->doc);
}
free_context(context);
return parsley;
}
static void free_context(contextPtr c) {
if(c == NULL) return;
if(c->tag != NULL) free(c->tag);
if(c->filter != NULL) pxpath_free(c->filter);
if(c->expr != NULL) pxpath_free(c->expr);
if(c->parent != NULL && c->parent->child != NULL) {
if(c->parent->child == c) {
c->parent->child = NULL;
} else {
contextPtr ptr = c->parent->child;
while(ptr->next != NULL) {
if(ptr->next == c) {
ptr->next = NULL;
} else {
ptr = ptr->next;
}
}
}
}
if(c->next != NULL) free_context(c->next);
if(c->child != NULL) free_context(c->child);
free(c);
}
static contextPtr new_context(struct json_object * json, xmlNodePtr node) {
contextPtr c = calloc(sizeof(parsley_context), 1);
c->node = node;
c->ns = node->ns;
c->tag = strdup("root");
c->expr = pxpath_new_path(1, "/");
c->json = json;
return c;
}
contextPtr deeper_context(contextPtr context, char* key, struct json_object * val) {
contextPtr c = (contextPtr) calloc(sizeof(parsley_context), 1);
c->node = context->node;
c->ns = context->ns;
c->parent = context;
c->tag = parsley_key_tag(key);
c->flags = parsley_key_flags(key);
parsley_parsing_context = c;
c->array = val != NULL && json_object_is_type(val, json_type_array);
c->json = c->array ? json_object_array_get_idx(val, 0) : val;
c->string = val != NULL && json_object_is_type(c->json, json_type_string);
c->filter = parsley_key_filter(key);
c->magic = context->array && context->filter == NULL;
c->expr = c->string ? myparse(json_object_get_string(c->json)) : NULL;
if(context->child == NULL) {
context->child = c;
} else {
contextPtr tmp = context->child;
while(tmp->next != NULL) tmp = tmp->next;
tmp->next = c;
}
fprintf(stderr, "json: %s\ntag: %s\nexpr: %s\nfilter: %s\n\n", json_object_get_string(c->json), c->tag, pxpath_to_string(c->expr), pxpath_to_string(c->filter));
return c;
}
void parsley_free(parsleyPtr ptr) {
if(ptr->error != NULL)
free(ptr->error);
if(ptr->stylesheet != NULL)
xsltFreeStylesheet(ptr->stylesheet);
free(ptr);
}
void yyerror(const char * s) {
struct printbuf *buf = printbuf_new();
if(last_parsley_error !=NULL) sprintbuf(buf, "%s\n", last_parsley_error);
sprintbuf(buf, "%s in key: %s", s, full_key_name(parsley_parsing_context));
last_parsley_error = strdup(buf->buf);
printbuf_free(buf);
}
static bool
all_strings(struct json_object * json) {
json_object_object_foreach(json, key, val) {
if(val == NULL || !json_object_is_type(val, json_type_string)) return false;
}
return true;
}
static char*
arepl(char* orig, char* old, char* new) {
// printf("y\n");
char* ptr = strdup(orig);
int nlen = strlen(new);
int olen = strlen(old);
char* i;
struct printbuf * buf = printbuf_new();
while((i = strstr(ptr, old)) != NULL) {
printbuf_memappend(buf, ptr, i - ptr);
printbuf_memappend(buf, new, nlen);
ptr = i + olen;
}
printbuf_memappend(buf, ptr, strlen(ptr));
ptr = strdup(buf->buf);
printbuf_free(buf);
return ptr;
}
static char *
inner_path(pxpathPtr p) {
if(p == NULL) return NULL;
if(p->type == PXPATH_PATH) return pxpath_to_string(p);
char *tmp = NULL;
pxpathPtr ptr = p->child;
while(ptr != NULL) {
tmp = inner_path(p->child);
if(tmp != NULL) return tmp;
ptr = ptr->next;
}
return NULL;
}
//TODO: tree-based replace, instead of naive string-based
static char *
inner_path_to_dot(pxpathPtr p) {
char *outer = pxpath_to_string(p);
char *inner = inner_path(p);
char *repl = NULL;
if(inner != NULL) {
repl = arepl(outer, inner, ".");
free(inner);
}
free(outer);
return repl;
}
static bool
inner_path_transform(contextPtr c) {
return c->filter == NULL && c->expr != NULL && inner_path(c->expr) != NULL;
}
static char *
resolve_filter(contextPtr c) {
return inner_path_transform(c) ? inner_path(c->expr) : pxpath_to_string(c->filter);
}
static char *
resolve_expr(contextPtr c) {
return inner_path_transform(c) ? inner_path_to_dot(c->expr) : pxpath_to_string(c->expr);
}
static void
render(contextPtr c) {
char *filter = resolve_filter(c);
char *expr = resolve_expr(c);
char *scope = filter == NULL ? expr : filter;
bool magic_children = c->array && filter == NULL;
bool simple_array = c->array && filter != NULL;
bool filtered = filter != NULL;
bool multiple = (c->array || c->magic) && !magic_children;
printf("node %s\n", c->node->name);
xmlNsPtr parsley = c->ns;
xmlNsPtr xsl = xmlDocGetRootElement(c->node->doc)->ns;
if(c->array) c->node = xmlNewChild(c->node, parsley, "groups", NULL);
if(filtered) {
c->node = xmlNewChild(c->node, xsl, "for-each", NULL);
xmlSetProp(c->node, "select", filter);
}
if(filtered && !multiple) {
c->node = xmlNewChild(c->node, xsl, "if", NULL);
xmlSetProp(c->node, "test", "position() = 1");
}
if(multiple) {
c->node = xmlNewChild(c->node, parsley, "group", NULL);
if (c->flags & PARSLEY_BANG) xmlSetProp(c->node, "optional", "true");
}
xmlNodePtr attr = xmlNewChild(c->node, xsl, "attribute", NULL);
xmlSetProp(attr, "name", "position");
xmlNodePtr counter = xmlNewChild(attr, xsl, "value-of", NULL);
xmlSetProp(counter, "select", "count(preceding::*) + count(ancestor::*)");
if(c->string) {
c->node = xmlNewChild(c->node, xsl, "value-of", NULL);
xmlSetProp(counter, "select", expr);
} else {
if(magic_children) c->node = xmlNewChild(c->node, parsley, "zipped", NULL);
__parsley_recurse(c);
}
if(filter !=NULL) free(filter);
if(expr !=NULL) free(expr);
}
void __parsley_recurse(contextPtr context) {
contextPtr c;
if(context->json == NULL) return;
fprintf(stderr, "<%s> %s\n", context->tag, context->node->name);
json_object_object_foreach(context->json, key, val) {
c = deeper_context(context, key, val);
fprintf(stderr, "<%s>\n", c->tag);
c->node = xmlNewChild(c->node, NULL, c->tag, NULL);
if (c->flags & PARSLEY_OPTIONAL) xmlSetProp(c->node, "optional", "true");
render(c);
}
}
// static char* full_expr(contextPtr context, char* expr) {
// if(expr == NULL) return context->full_expr;
// char* merged = arepl(expr, ".", context->full_expr);
// return arepl(merged, "///", "//");
// }
static char* inner_key_of(struct json_object * json) {
switch(json_object_get_type(json)) {
case json_type_string:
return json_object_get_string(json);
case json_type_array:
return NULL;
case json_type_object:
return inner_key_each(json);
}
}
static char* inner_key_each(struct json_object * json) {
json_object_object_foreach(json, key, val) {
char* inner = inner_key_of(val);
if(inner != NULL) return inner;
}
return NULL;
}