psych 3.0.3.pre3-java → 3.2.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,27 +37,47 @@ yaml_parser_register_anchor(yaml_parser_t *parser,
37
37
  static void
38
38
  yaml_parser_delete_aliases(yaml_parser_t *parser);
39
39
 
40
+ /*
41
+ * Document loading context.
42
+ */
43
+ struct loader_ctx {
44
+ int *start;
45
+ int *end;
46
+ int *top;
47
+ };
48
+
40
49
  /*
41
50
  * Composer functions.
42
51
  */
52
+ static int
53
+ yaml_parser_load_nodes(yaml_parser_t *parser, struct loader_ctx *ctx);
54
+
55
+ static int
56
+ yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *event);
43
57
 
44
58
  static int
45
- yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *first_event);
59
+ yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *event,
60
+ struct loader_ctx *ctx);
46
61
 
47
62
  static int
48
- yaml_parser_load_node(yaml_parser_t *parser, yaml_event_t *first_event);
63
+ yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *event,
64
+ struct loader_ctx *ctx);
49
65
 
50
66
  static int
51
- yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event);
67
+ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *event,
68
+ struct loader_ctx *ctx);
52
69
 
53
70
  static int
54
- yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event);
71
+ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *event,
72
+ struct loader_ctx *ctx);
55
73
 
56
74
  static int
57
- yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event);
75
+ yaml_parser_load_sequence_end(yaml_parser_t *parser, yaml_event_t *event,
76
+ struct loader_ctx *ctx);
58
77
 
59
78
  static int
60
- yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event);
79
+ yaml_parser_load_mapping_end(yaml_parser_t *parser, yaml_event_t *event,
80
+ struct loader_ctx *ctx);
61
81
 
62
82
  /*
63
83
  * Load the next document of the stream.
@@ -72,7 +92,7 @@ yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document)
72
92
  assert(document); /* Non-NULL document object is expected. */
73
93
 
74
94
  memset(document, 0, sizeof(yaml_document_t));
75
- if (!STACK_INIT(parser, document->nodes, INITIAL_STACK_SIZE))
95
+ if (!STACK_INIT(parser, document->nodes, yaml_node_t*))
76
96
  goto error;
77
97
 
78
98
  if (!parser->stream_start_produced) {
@@ -90,7 +110,7 @@ yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document)
90
110
  return 1;
91
111
  }
92
112
 
93
- if (!STACK_INIT(parser, parser->aliases, INITIAL_STACK_SIZE))
113
+ if (!STACK_INIT(parser, parser->aliases, yaml_alias_data_t*))
94
114
  goto error;
95
115
 
96
116
  parser->document = document;
@@ -162,59 +182,78 @@ yaml_parser_delete_aliases(yaml_parser_t *parser)
162
182
  */
163
183
 
164
184
  static int
165
- yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *first_event)
185
+ yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *event)
166
186
  {
167
- yaml_event_t event;
187
+ struct loader_ctx ctx = { NULL, NULL, NULL };
168
188
 
169
- assert(first_event->type == YAML_DOCUMENT_START_EVENT);
189
+ assert(event->type == YAML_DOCUMENT_START_EVENT);
170
190
  /* DOCUMENT-START is expected. */
171
191
 
172
192
  parser->document->version_directive
173
- = first_event->data.document_start.version_directive;
193
+ = event->data.document_start.version_directive;
174
194
  parser->document->tag_directives.start
175
- = first_event->data.document_start.tag_directives.start;
195
+ = event->data.document_start.tag_directives.start;
176
196
  parser->document->tag_directives.end
177
- = first_event->data.document_start.tag_directives.end;
197
+ = event->data.document_start.tag_directives.end;
178
198
  parser->document->start_implicit
179
- = first_event->data.document_start.implicit;
180
- parser->document->start_mark = first_event->start_mark;
181
-
182
- if (!yaml_parser_parse(parser, &event)) return 0;
183
-
184
- if (!yaml_parser_load_node(parser, &event)) return 0;
185
-
186
- if (!yaml_parser_parse(parser, &event)) return 0;
187
- assert(event.type == YAML_DOCUMENT_END_EVENT);
188
- /* DOCUMENT-END is expected. */
199
+ = event->data.document_start.implicit;
200
+ parser->document->start_mark = event->start_mark;
189
201
 
190
- parser->document->end_implicit = event.data.document_end.implicit;
191
- parser->document->end_mark = event.end_mark;
202
+ if (!STACK_INIT(parser, ctx, int*)) return 0;
203
+ if (!yaml_parser_load_nodes(parser, &ctx)) {
204
+ STACK_DEL(parser, ctx);
205
+ return 0;
206
+ }
207
+ STACK_DEL(parser, ctx);
192
208
 
193
209
  return 1;
194
210
  }
195
211
 
196
212
  /*
197
- * Compose a node.
213
+ * Compose a node tree.
198
214
  */
199
215
 
200
216
  static int
201
- yaml_parser_load_node(yaml_parser_t *parser, yaml_event_t *first_event)
217
+ yaml_parser_load_nodes(yaml_parser_t *parser, struct loader_ctx *ctx)
202
218
  {
203
- switch (first_event->type) {
204
- case YAML_ALIAS_EVENT:
205
- return yaml_parser_load_alias(parser, first_event);
206
- case YAML_SCALAR_EVENT:
207
- return yaml_parser_load_scalar(parser, first_event);
208
- case YAML_SEQUENCE_START_EVENT:
209
- return yaml_parser_load_sequence(parser, first_event);
210
- case YAML_MAPPING_START_EVENT:
211
- return yaml_parser_load_mapping(parser, first_event);
212
- default:
213
- assert(0); /* Could not happen. */
214
- return 0;
215
- }
219
+ yaml_event_t event;
216
220
 
217
- return 0;
221
+ do {
222
+ if (!yaml_parser_parse(parser, &event)) return 0;
223
+
224
+ switch (event.type) {
225
+ case YAML_ALIAS_EVENT:
226
+ if (!yaml_parser_load_alias(parser, &event, ctx)) return 0;
227
+ break;
228
+ case YAML_SCALAR_EVENT:
229
+ if (!yaml_parser_load_scalar(parser, &event, ctx)) return 0;
230
+ break;
231
+ case YAML_SEQUENCE_START_EVENT:
232
+ if (!yaml_parser_load_sequence(parser, &event, ctx)) return 0;
233
+ break;
234
+ case YAML_SEQUENCE_END_EVENT:
235
+ if (!yaml_parser_load_sequence_end(parser, &event, ctx))
236
+ return 0;
237
+ break;
238
+ case YAML_MAPPING_START_EVENT:
239
+ if (!yaml_parser_load_mapping(parser, &event, ctx)) return 0;
240
+ break;
241
+ case YAML_MAPPING_END_EVENT:
242
+ if (!yaml_parser_load_mapping_end(parser, &event, ctx))
243
+ return 0;
244
+ break;
245
+ default:
246
+ assert(0); /* Could not happen. */
247
+ return 0;
248
+ case YAML_DOCUMENT_END_EVENT:
249
+ break;
250
+ }
251
+ } while (event.type != YAML_DOCUMENT_END_EVENT);
252
+
253
+ parser->document->end_implicit = event.data.document_end.implicit;
254
+ parser->document->end_mark = event.end_mark;
255
+
256
+ return 1;
218
257
  }
219
258
 
220
259
  /*
@@ -252,27 +291,80 @@ yaml_parser_register_anchor(yaml_parser_t *parser,
252
291
  return 1;
253
292
  }
254
293
 
294
+ /*
295
+ * Compose node into its parent in the stree.
296
+ */
297
+
298
+ static int
299
+ yaml_parser_load_node_add(yaml_parser_t *parser, struct loader_ctx *ctx,
300
+ int index)
301
+ {
302
+ struct yaml_node_s *parent;
303
+ int parent_index;
304
+
305
+ if (STACK_EMPTY(parser, *ctx)) {
306
+ /* This is the root node, there's no tree to add it to. */
307
+ return 1;
308
+ }
309
+
310
+ parent_index = *((*ctx).top - 1);
311
+ parent = &parser->document->nodes.start[parent_index-1];
312
+
313
+ switch (parent->type) {
314
+ case YAML_SEQUENCE_NODE:
315
+ if (!STACK_LIMIT(parser, parent->data.sequence.items, INT_MAX-1))
316
+ return 0;
317
+ if (!PUSH(parser, parent->data.sequence.items, index))
318
+ return 0;
319
+ break;
320
+ case YAML_MAPPING_NODE: {
321
+ yaml_node_pair_t pair;
322
+ if (!STACK_EMPTY(parser, parent->data.mapping.pairs)) {
323
+ yaml_node_pair_t *p = parent->data.mapping.pairs.top - 1;
324
+ if (p->key != 0 && p->value == 0) {
325
+ p->value = index;
326
+ break;
327
+ }
328
+ }
329
+
330
+ pair.key = index;
331
+ pair.value = 0;
332
+ if (!STACK_LIMIT(parser, parent->data.mapping.pairs, INT_MAX-1))
333
+ return 0;
334
+ if (!PUSH(parser, parent->data.mapping.pairs, pair))
335
+ return 0;
336
+
337
+ break;
338
+ }
339
+ default:
340
+ assert(0); /* Could not happen. */
341
+ return 0;
342
+ }
343
+ return 1;
344
+ }
345
+
255
346
  /*
256
347
  * Compose a node corresponding to an alias.
257
348
  */
258
349
 
259
350
  static int
260
- yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event)
351
+ yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *event,
352
+ struct loader_ctx *ctx)
261
353
  {
262
- yaml_char_t *anchor = first_event->data.alias.anchor;
354
+ yaml_char_t *anchor = event->data.alias.anchor;
263
355
  yaml_alias_data_t *alias_data;
264
356
 
265
357
  for (alias_data = parser->aliases.start;
266
358
  alias_data != parser->aliases.top; alias_data ++) {
267
359
  if (strcmp((char *)alias_data->anchor, (char *)anchor) == 0) {
268
360
  yaml_free(anchor);
269
- return alias_data->index;
361
+ return yaml_parser_load_node_add(parser, ctx, alias_data->index);
270
362
  }
271
363
  }
272
364
 
273
365
  yaml_free(anchor);
274
366
  return yaml_parser_set_composer_error(parser, "found undefined alias",
275
- first_event->start_mark);
367
+ event->start_mark);
276
368
  }
277
369
 
278
370
  /*
@@ -280,11 +372,12 @@ yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event)
280
372
  */
281
373
 
282
374
  static int
283
- yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event)
375
+ yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *event,
376
+ struct loader_ctx *ctx)
284
377
  {
285
378
  yaml_node_t node;
286
379
  int index;
287
- yaml_char_t *tag = first_event->data.scalar.tag;
380
+ yaml_char_t *tag = event->data.scalar.tag;
288
381
 
289
382
  if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
290
383
 
@@ -294,23 +387,23 @@ yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event)
294
387
  if (!tag) goto error;
295
388
  }
296
389
 
297
- SCALAR_NODE_INIT(node, tag, first_event->data.scalar.value,
298
- first_event->data.scalar.length, first_event->data.scalar.style,
299
- first_event->start_mark, first_event->end_mark);
390
+ SCALAR_NODE_INIT(node, tag, event->data.scalar.value,
391
+ event->data.scalar.length, event->data.scalar.style,
392
+ event->start_mark, event->end_mark);
300
393
 
301
394
  if (!PUSH(parser, parser->document->nodes, node)) goto error;
302
395
 
303
- index = parser->document->nodes.top - parser->document->nodes.start;
396
+ index = (int)(parser->document->nodes.top - parser->document->nodes.start);
304
397
 
305
398
  if (!yaml_parser_register_anchor(parser, index,
306
- first_event->data.scalar.anchor)) return 0;
399
+ event->data.scalar.anchor)) return 0;
307
400
 
308
- return index;
401
+ return yaml_parser_load_node_add(parser, ctx, index);
309
402
 
310
403
  error:
311
404
  yaml_free(tag);
312
- yaml_free(first_event->data.scalar.anchor);
313
- yaml_free(first_event->data.scalar.value);
405
+ yaml_free(event->data.scalar.anchor);
406
+ yaml_free(event->data.scalar.value);
314
407
  return 0;
315
408
  }
316
409
 
@@ -319,17 +412,17 @@ error:
319
412
  */
320
413
 
321
414
  static int
322
- yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event)
415
+ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *event,
416
+ struct loader_ctx *ctx)
323
417
  {
324
- yaml_event_t event;
325
418
  yaml_node_t node;
326
419
  struct {
327
420
  yaml_node_item_t *start;
328
421
  yaml_node_item_t *end;
329
422
  yaml_node_item_t *top;
330
423
  } items = { NULL, NULL, NULL };
331
- int index, item_index;
332
- yaml_char_t *tag = first_event->data.sequence_start.tag;
424
+ int index;
425
+ yaml_char_t *tag = event->data.sequence_start.tag;
333
426
 
334
427
  if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
335
428
 
@@ -339,51 +432,57 @@ yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event)
339
432
  if (!tag) goto error;
340
433
  }
341
434
 
342
- if (!STACK_INIT(parser, items, INITIAL_STACK_SIZE)) goto error;
435
+ if (!STACK_INIT(parser, items, yaml_node_item_t*)) goto error;
343
436
 
344
437
  SEQUENCE_NODE_INIT(node, tag, items.start, items.end,
345
- first_event->data.sequence_start.style,
346
- first_event->start_mark, first_event->end_mark);
438
+ event->data.sequence_start.style,
439
+ event->start_mark, event->end_mark);
347
440
 
348
441
  if (!PUSH(parser, parser->document->nodes, node)) goto error;
349
442
 
350
- index = parser->document->nodes.top - parser->document->nodes.start;
443
+ index = (int)(parser->document->nodes.top - parser->document->nodes.start);
351
444
 
352
445
  if (!yaml_parser_register_anchor(parser, index,
353
- first_event->data.sequence_start.anchor)) return 0;
354
-
355
- if (!yaml_parser_parse(parser, &event)) return 0;
356
-
357
- while (event.type != YAML_SEQUENCE_END_EVENT) {
358
- if (!STACK_LIMIT(parser,
359
- parser->document->nodes.start[index-1].data.sequence.items,
360
- INT_MAX-1)) return 0;
361
- item_index = yaml_parser_load_node(parser, &event);
362
- if (!item_index) return 0;
363
- if (!PUSH(parser,
364
- parser->document->nodes.start[index-1].data.sequence.items,
365
- item_index)) return 0;
366
- if (!yaml_parser_parse(parser, &event)) return 0;
367
- }
446
+ event->data.sequence_start.anchor)) return 0;
447
+
448
+ if (!yaml_parser_load_node_add(parser, ctx, index)) return 0;
368
449
 
369
- parser->document->nodes.start[index-1].end_mark = event.end_mark;
450
+ if (!STACK_LIMIT(parser, *ctx, INT_MAX-1)) return 0;
451
+ if (!PUSH(parser, *ctx, index)) return 0;
370
452
 
371
- return index;
453
+ return 1;
372
454
 
373
455
  error:
374
456
  yaml_free(tag);
375
- yaml_free(first_event->data.sequence_start.anchor);
457
+ yaml_free(event->data.sequence_start.anchor);
376
458
  return 0;
377
459
  }
378
460
 
461
+ static int
462
+ yaml_parser_load_sequence_end(yaml_parser_t *parser, yaml_event_t *event,
463
+ struct loader_ctx *ctx)
464
+ {
465
+ int index;
466
+
467
+ assert(((*ctx).top - (*ctx).start) > 0);
468
+
469
+ index = *((*ctx).top - 1);
470
+ assert(parser->document->nodes.start[index-1].type == YAML_SEQUENCE_NODE);
471
+ parser->document->nodes.start[index-1].end_mark = event->end_mark;
472
+
473
+ (void)POP(parser, *ctx);
474
+
475
+ return 1;
476
+ }
477
+
379
478
  /*
380
479
  * Compose a mapping node.
381
480
  */
382
481
 
383
482
  static int
384
- yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event)
483
+ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *event,
484
+ struct loader_ctx *ctx)
385
485
  {
386
- yaml_event_t event;
387
486
  yaml_node_t node;
388
487
  struct {
389
488
  yaml_node_pair_t *start;
@@ -391,8 +490,7 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event)
391
490
  yaml_node_pair_t *top;
392
491
  } pairs = { NULL, NULL, NULL };
393
492
  int index;
394
- yaml_node_pair_t pair;
395
- yaml_char_t *tag = first_event->data.mapping_start.tag;
493
+ yaml_char_t *tag = event->data.mapping_start.tag;
396
494
 
397
495
  if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
398
496
 
@@ -402,43 +500,45 @@ yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event)
402
500
  if (!tag) goto error;
403
501
  }
404
502
 
405
- if (!STACK_INIT(parser, pairs, INITIAL_STACK_SIZE)) goto error;
503
+ if (!STACK_INIT(parser, pairs, yaml_node_pair_t*)) goto error;
406
504
 
407
505
  MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end,
408
- first_event->data.mapping_start.style,
409
- first_event->start_mark, first_event->end_mark);
506
+ event->data.mapping_start.style,
507
+ event->start_mark, event->end_mark);
410
508
 
411
509
  if (!PUSH(parser, parser->document->nodes, node)) goto error;
412
510
 
413
- index = parser->document->nodes.top - parser->document->nodes.start;
511
+ index = (int)(parser->document->nodes.top - parser->document->nodes.start);
414
512
 
415
513
  if (!yaml_parser_register_anchor(parser, index,
416
- first_event->data.mapping_start.anchor)) return 0;
514
+ event->data.mapping_start.anchor)) return 0;
417
515
 
418
- if (!yaml_parser_parse(parser, &event)) return 0;
419
-
420
- while (event.type != YAML_MAPPING_END_EVENT) {
421
- if (!STACK_LIMIT(parser,
422
- parser->document->nodes.start[index-1].data.mapping.pairs,
423
- INT_MAX-1)) return 0;
424
- pair.key = yaml_parser_load_node(parser, &event);
425
- if (!pair.key) return 0;
426
- if (!yaml_parser_parse(parser, &event)) return 0;
427
- pair.value = yaml_parser_load_node(parser, &event);
428
- if (!pair.value) return 0;
429
- if (!PUSH(parser,
430
- parser->document->nodes.start[index-1].data.mapping.pairs,
431
- pair)) return 0;
432
- if (!yaml_parser_parse(parser, &event)) return 0;
433
- }
516
+ if (!yaml_parser_load_node_add(parser, ctx, index)) return 0;
434
517
 
435
- parser->document->nodes.start[index-1].end_mark = event.end_mark;
518
+ if (!STACK_LIMIT(parser, *ctx, INT_MAX-1)) return 0;
519
+ if (!PUSH(parser, *ctx, index)) return 0;
436
520
 
437
- return index;
521
+ return 1;
438
522
 
439
523
  error:
440
524
  yaml_free(tag);
441
- yaml_free(first_event->data.mapping_start.anchor);
525
+ yaml_free(event->data.mapping_start.anchor);
442
526
  return 0;
443
527
  }
444
528
 
529
+ static int
530
+ yaml_parser_load_mapping_end(yaml_parser_t *parser, yaml_event_t *event,
531
+ struct loader_ctx *ctx)
532
+ {
533
+ int index;
534
+
535
+ assert(((*ctx).top - (*ctx).start) > 0);
536
+
537
+ index = *((*ctx).top - 1);
538
+ assert(parser->document->nodes.start[index-1].type == YAML_MAPPING_NODE);
539
+ parser->document->nodes.start[index-1].end_mark = event->end_mark;
540
+
541
+ (void)POP(parser, *ctx);
542
+
543
+ return 1;
544
+ }