psych-shopifork 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +15 -0
  2. data/.autotest +18 -0
  3. data/.gemtest +0 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.rdoc +414 -0
  6. data/Manifest.txt +113 -0
  7. data/README.rdoc +71 -0
  8. data/Rakefile +72 -0
  9. data/ext/psych/depend +3 -0
  10. data/ext/psych/extconf.rb +36 -0
  11. data/ext/psych/psych.c +34 -0
  12. data/ext/psych/psych.h +20 -0
  13. data/ext/psych/psych_emitter.c +538 -0
  14. data/ext/psych/psych_emitter.h +8 -0
  15. data/ext/psych/psych_parser.c +579 -0
  16. data/ext/psych/psych_parser.h +6 -0
  17. data/ext/psych/psych_to_ruby.c +43 -0
  18. data/ext/psych/psych_to_ruby.h +8 -0
  19. data/ext/psych/psych_yaml_tree.c +24 -0
  20. data/ext/psych/psych_yaml_tree.h +8 -0
  21. data/ext/psych/yaml/LICENSE +19 -0
  22. data/ext/psych/yaml/api.c +1392 -0
  23. data/ext/psych/yaml/config.h +11 -0
  24. data/ext/psych/yaml/dumper.c +394 -0
  25. data/ext/psych/yaml/emitter.c +2335 -0
  26. data/ext/psych/yaml/loader.c +432 -0
  27. data/ext/psych/yaml/parser.c +1374 -0
  28. data/ext/psych/yaml/reader.c +465 -0
  29. data/ext/psych/yaml/scanner.c +3570 -0
  30. data/ext/psych/yaml/writer.c +141 -0
  31. data/ext/psych/yaml/yaml.h +1971 -0
  32. data/ext/psych/yaml/yaml_private.h +643 -0
  33. data/lib/psych.rb +497 -0
  34. data/lib/psych/class_loader.rb +101 -0
  35. data/lib/psych/coder.rb +94 -0
  36. data/lib/psych/core_ext.rb +35 -0
  37. data/lib/psych/deprecated.rb +85 -0
  38. data/lib/psych/exception.rb +13 -0
  39. data/lib/psych/handler.rb +249 -0
  40. data/lib/psych/handlers/document_stream.rb +22 -0
  41. data/lib/psych/handlers/recorder.rb +39 -0
  42. data/lib/psych/json/ruby_events.rb +19 -0
  43. data/lib/psych/json/stream.rb +16 -0
  44. data/lib/psych/json/tree_builder.rb +12 -0
  45. data/lib/psych/json/yaml_events.rb +29 -0
  46. data/lib/psych/nodes.rb +77 -0
  47. data/lib/psych/nodes/alias.rb +18 -0
  48. data/lib/psych/nodes/document.rb +60 -0
  49. data/lib/psych/nodes/mapping.rb +56 -0
  50. data/lib/psych/nodes/node.rb +55 -0
  51. data/lib/psych/nodes/scalar.rb +67 -0
  52. data/lib/psych/nodes/sequence.rb +81 -0
  53. data/lib/psych/nodes/stream.rb +37 -0
  54. data/lib/psych/omap.rb +4 -0
  55. data/lib/psych/parser.rb +51 -0
  56. data/lib/psych/scalar_scanner.rb +149 -0
  57. data/lib/psych/set.rb +4 -0
  58. data/lib/psych/stream.rb +37 -0
  59. data/lib/psych/streaming.rb +27 -0
  60. data/lib/psych/syntax_error.rb +21 -0
  61. data/lib/psych/tree_builder.rb +96 -0
  62. data/lib/psych/visitors.rb +6 -0
  63. data/lib/psych/visitors/depth_first.rb +26 -0
  64. data/lib/psych/visitors/emitter.rb +51 -0
  65. data/lib/psych/visitors/json_tree.rb +24 -0
  66. data/lib/psych/visitors/to_ruby.rb +372 -0
  67. data/lib/psych/visitors/visitor.rb +19 -0
  68. data/lib/psych/visitors/yaml_tree.rb +507 -0
  69. data/lib/psych/y.rb +9 -0
  70. data/test/psych/handlers/test_recorder.rb +25 -0
  71. data/test/psych/helper.rb +114 -0
  72. data/test/psych/json/test_stream.rb +109 -0
  73. data/test/psych/nodes/test_enumerable.rb +43 -0
  74. data/test/psych/test_alias_and_anchor.rb +96 -0
  75. data/test/psych/test_array.rb +57 -0
  76. data/test/psych/test_boolean.rb +36 -0
  77. data/test/psych/test_class.rb +36 -0
  78. data/test/psych/test_coder.rb +184 -0
  79. data/test/psych/test_date_time.rb +25 -0
  80. data/test/psych/test_deprecated.rb +214 -0
  81. data/test/psych/test_document.rb +46 -0
  82. data/test/psych/test_emitter.rb +94 -0
  83. data/test/psych/test_encoding.rb +254 -0
  84. data/test/psych/test_engine_manager.rb +47 -0
  85. data/test/psych/test_exception.rb +151 -0
  86. data/test/psych/test_hash.rb +44 -0
  87. data/test/psych/test_json_tree.rb +65 -0
  88. data/test/psych/test_merge_keys.rb +132 -0
  89. data/test/psych/test_nil.rb +18 -0
  90. data/test/psych/test_null.rb +19 -0
  91. data/test/psych/test_numeric.rb +45 -0
  92. data/test/psych/test_object.rb +44 -0
  93. data/test/psych/test_object_references.rb +67 -0
  94. data/test/psych/test_omap.rb +75 -0
  95. data/test/psych/test_parser.rb +339 -0
  96. data/test/psych/test_psych.rb +168 -0
  97. data/test/psych/test_safe_load.rb +97 -0
  98. data/test/psych/test_scalar.rb +11 -0
  99. data/test/psych/test_scalar_scanner.rb +106 -0
  100. data/test/psych/test_serialize_subclasses.rb +38 -0
  101. data/test/psych/test_set.rb +49 -0
  102. data/test/psych/test_stream.rb +93 -0
  103. data/test/psych/test_string.rb +153 -0
  104. data/test/psych/test_struct.rb +49 -0
  105. data/test/psych/test_symbol.rb +17 -0
  106. data/test/psych/test_tainted.rb +130 -0
  107. data/test/psych/test_to_yaml_properties.rb +63 -0
  108. data/test/psych/test_tree_builder.rb +79 -0
  109. data/test/psych/test_yaml.rb +1289 -0
  110. data/test/psych/test_yamldbm.rb +197 -0
  111. data/test/psych/test_yamlstore.rb +87 -0
  112. data/test/psych/visitors/test_depth_first.rb +49 -0
  113. data/test/psych/visitors/test_emitter.rb +144 -0
  114. data/test/psych/visitors/test_to_ruby.rb +326 -0
  115. data/test/psych/visitors/test_yaml_tree.rb +173 -0
  116. metadata +257 -0
@@ -0,0 +1,8 @@
1
+ #ifndef PSYCH_EMITTER_H
2
+ #define PSYCH_EMITTER_H
3
+
4
+ #include <psych.h>
5
+
6
+ void Init_psych_emitter();
7
+
8
+ #endif
@@ -0,0 +1,579 @@
1
+ #include <psych.h>
2
+
3
+ VALUE cPsychParser;
4
+ VALUE ePsychSyntaxError;
5
+
6
+ static ID id_read;
7
+ static ID id_path;
8
+ static ID id_empty;
9
+ static ID id_start_stream;
10
+ static ID id_end_stream;
11
+ static ID id_start_document;
12
+ static ID id_end_document;
13
+ static ID id_alias;
14
+ static ID id_scalar;
15
+ static ID id_start_sequence;
16
+ static ID id_end_sequence;
17
+ static ID id_start_mapping;
18
+ static ID id_end_mapping;
19
+
20
+ #define PSYCH_TRANSCODE(_str, _yaml_enc, _internal_enc) \
21
+ do { \
22
+ rb_enc_associate_index((_str), (_yaml_enc)); \
23
+ if(_internal_enc) \
24
+ (_str) = rb_str_export_to_enc((_str), (_internal_enc)); \
25
+ } while (0)
26
+
27
+ static int io_reader(void * data, unsigned char *buf, size_t size, size_t *read)
28
+ {
29
+ VALUE io = (VALUE)data;
30
+ VALUE string = rb_funcall(io, id_read, 1, INT2NUM(size));
31
+
32
+ *read = 0;
33
+
34
+ if(! NIL_P(string)) {
35
+ void * str = (void *)StringValuePtr(string);
36
+ *read = (size_t)RSTRING_LEN(string);
37
+ memcpy(buf, str, *read);
38
+ }
39
+
40
+ return 1;
41
+ }
42
+
43
+ static void dealloc(void * ptr)
44
+ {
45
+ yaml_parser_t * parser;
46
+
47
+ parser = (yaml_parser_t *)ptr;
48
+ yaml_parser_delete(parser);
49
+ xfree(parser);
50
+ }
51
+
52
+ static VALUE allocate(VALUE klass)
53
+ {
54
+ yaml_parser_t * parser;
55
+
56
+ parser = xmalloc(sizeof(yaml_parser_t));
57
+ yaml_parser_initialize(parser);
58
+
59
+ return Data_Wrap_Struct(klass, 0, dealloc, parser);
60
+ }
61
+
62
+ static VALUE make_exception(yaml_parser_t * parser, VALUE path)
63
+ {
64
+ size_t line, column;
65
+
66
+ line = parser->context_mark.line + 1;
67
+ column = parser->context_mark.column + 1;
68
+
69
+ return rb_funcall(ePsychSyntaxError, rb_intern("new"), 6,
70
+ path,
71
+ INT2NUM(line),
72
+ INT2NUM(column),
73
+ INT2NUM(parser->problem_offset),
74
+ parser->problem ? rb_usascii_str_new2(parser->problem) : Qnil,
75
+ parser->context ? rb_usascii_str_new2(parser->context) : Qnil);
76
+ }
77
+
78
+ #ifdef HAVE_RUBY_ENCODING_H
79
+ static VALUE transcode_string(VALUE src, int * parser_encoding)
80
+ {
81
+ int utf8 = rb_utf8_encindex();
82
+ int utf16le = rb_enc_find_index("UTF-16LE");
83
+ int utf16be = rb_enc_find_index("UTF-16BE");
84
+ int source_encoding = rb_enc_get_index(src);
85
+
86
+ if (source_encoding == utf8) {
87
+ *parser_encoding = YAML_UTF8_ENCODING;
88
+ return src;
89
+ }
90
+
91
+ if (source_encoding == utf16le) {
92
+ *parser_encoding = YAML_UTF16LE_ENCODING;
93
+ return src;
94
+ }
95
+
96
+ if (source_encoding == utf16be) {
97
+ *parser_encoding = YAML_UTF16BE_ENCODING;
98
+ return src;
99
+ }
100
+
101
+ src = rb_str_export_to_enc(src, rb_utf8_encoding());
102
+ RB_GC_GUARD(src);
103
+
104
+ *parser_encoding = YAML_UTF8_ENCODING;
105
+ return src;
106
+ }
107
+
108
+ static VALUE transcode_io(VALUE src, int * parser_encoding)
109
+ {
110
+ VALUE io_external_encoding;
111
+ int io_external_enc_index;
112
+
113
+ io_external_encoding = rb_funcall(src, rb_intern("external_encoding"), 0);
114
+
115
+ /* if no encoding is returned, assume ascii8bit. */
116
+ if (NIL_P(io_external_encoding)) {
117
+ io_external_enc_index = rb_ascii8bit_encindex();
118
+ } else {
119
+ io_external_enc_index = rb_to_encoding_index(io_external_encoding);
120
+ }
121
+
122
+ /* Treat US-ASCII as utf_8 */
123
+ if (io_external_enc_index == rb_usascii_encindex()) {
124
+ *parser_encoding = YAML_UTF8_ENCODING;
125
+ return src;
126
+ }
127
+
128
+ if (io_external_enc_index == rb_utf8_encindex()) {
129
+ *parser_encoding = YAML_UTF8_ENCODING;
130
+ return src;
131
+ }
132
+
133
+ if (io_external_enc_index == rb_enc_find_index("UTF-16LE")) {
134
+ *parser_encoding = YAML_UTF16LE_ENCODING;
135
+ return src;
136
+ }
137
+
138
+ if (io_external_enc_index == rb_enc_find_index("UTF-16BE")) {
139
+ *parser_encoding = YAML_UTF16BE_ENCODING;
140
+ return src;
141
+ }
142
+
143
+ /* Just guess on ASCII-8BIT */
144
+ if (io_external_enc_index == rb_ascii8bit_encindex()) {
145
+ *parser_encoding = YAML_ANY_ENCODING;
146
+ return src;
147
+ }
148
+
149
+ /* If the external encoding is something we don't know how to handle,
150
+ * fall back to YAML_ANY_ENCODING. */
151
+ *parser_encoding = YAML_ANY_ENCODING;
152
+
153
+ return src;
154
+ }
155
+
156
+ #endif
157
+
158
+ static VALUE protected_start_stream(VALUE pointer)
159
+ {
160
+ VALUE *args = (VALUE *)pointer;
161
+ return rb_funcall(args[0], id_start_stream, 1, args[1]);
162
+ }
163
+
164
+ static VALUE protected_start_document(VALUE pointer)
165
+ {
166
+ VALUE *args = (VALUE *)pointer;
167
+ return rb_funcall3(args[0], id_start_document, 3, args + 1);
168
+ }
169
+
170
+ static VALUE protected_end_document(VALUE pointer)
171
+ {
172
+ VALUE *args = (VALUE *)pointer;
173
+ return rb_funcall(args[0], id_end_document, 1, args[1]);
174
+ }
175
+
176
+ static VALUE protected_alias(VALUE pointer)
177
+ {
178
+ VALUE *args = (VALUE *)pointer;
179
+ return rb_funcall(args[0], id_alias, 1, args[1]);
180
+ }
181
+
182
+ static VALUE protected_scalar(VALUE pointer)
183
+ {
184
+ VALUE *args = (VALUE *)pointer;
185
+ return rb_funcall3(args[0], id_scalar, 6, args + 1);
186
+ }
187
+
188
+ static VALUE protected_start_sequence(VALUE pointer)
189
+ {
190
+ VALUE *args = (VALUE *)pointer;
191
+ return rb_funcall3(args[0], id_start_sequence, 4, args + 1);
192
+ }
193
+
194
+ static VALUE protected_end_sequence(VALUE handler)
195
+ {
196
+ return rb_funcall(handler, id_end_sequence, 0);
197
+ }
198
+
199
+ static VALUE protected_start_mapping(VALUE pointer)
200
+ {
201
+ VALUE *args = (VALUE *)pointer;
202
+ return rb_funcall3(args[0], id_start_mapping, 4, args + 1);
203
+ }
204
+
205
+ static VALUE protected_end_mapping(VALUE handler)
206
+ {
207
+ return rb_funcall(handler, id_end_mapping, 0);
208
+ }
209
+
210
+ static VALUE protected_empty(VALUE handler)
211
+ {
212
+ return rb_funcall(handler, id_empty, 0);
213
+ }
214
+
215
+ static VALUE protected_end_stream(VALUE handler)
216
+ {
217
+ return rb_funcall(handler, id_end_stream, 0);
218
+ }
219
+
220
+ /*
221
+ * call-seq:
222
+ * parser.parse(yaml)
223
+ *
224
+ * Parse the YAML document contained in +yaml+. Events will be called on
225
+ * the handler set on the parser instance.
226
+ *
227
+ * See Psych::Parser and Psych::Parser#handler
228
+ */
229
+ static VALUE parse(int argc, VALUE *argv, VALUE self)
230
+ {
231
+ VALUE yaml, path;
232
+ yaml_parser_t * parser;
233
+ yaml_event_t event;
234
+ int done = 0;
235
+ int tainted = 0;
236
+ int state = 0;
237
+ int parser_encoding = YAML_ANY_ENCODING;
238
+ #ifdef HAVE_RUBY_ENCODING_H
239
+ int encoding = rb_utf8_encindex();
240
+ rb_encoding * internal_enc = rb_default_internal_encoding();
241
+ #endif
242
+ VALUE handler = rb_iv_get(self, "@handler");
243
+
244
+ if (rb_scan_args(argc, argv, "11", &yaml, &path) == 1) {
245
+ if(rb_respond_to(yaml, id_path))
246
+ path = rb_funcall(yaml, id_path, 0);
247
+ else
248
+ path = rb_str_new2("<unknown>");
249
+ }
250
+
251
+ Data_Get_Struct(self, yaml_parser_t, parser);
252
+
253
+ yaml_parser_delete(parser);
254
+ yaml_parser_initialize(parser);
255
+
256
+ if (OBJ_TAINTED(yaml)) tainted = 1;
257
+
258
+ if (rb_respond_to(yaml, id_read)) {
259
+ #ifdef HAVE_RUBY_ENCODING_H
260
+ yaml = transcode_io(yaml, &parser_encoding);
261
+ yaml_parser_set_encoding(parser, parser_encoding);
262
+ #endif
263
+ yaml_parser_set_input(parser, io_reader, (void *)yaml);
264
+ if (RTEST(rb_obj_is_kind_of(yaml, rb_cIO))) tainted = 1;
265
+ } else {
266
+ StringValue(yaml);
267
+ #ifdef HAVE_RUBY_ENCODING_H
268
+ yaml = transcode_string(yaml, &parser_encoding);
269
+ yaml_parser_set_encoding(parser, parser_encoding);
270
+ #endif
271
+ yaml_parser_set_input_string(
272
+ parser,
273
+ (const unsigned char *)RSTRING_PTR(yaml),
274
+ (size_t)RSTRING_LEN(yaml)
275
+ );
276
+ }
277
+
278
+ while(!done) {
279
+ if(!yaml_parser_parse(parser, &event)) {
280
+ VALUE exception;
281
+
282
+ exception = make_exception(parser, path);
283
+ yaml_parser_delete(parser);
284
+ yaml_parser_initialize(parser);
285
+
286
+ rb_exc_raise(exception);
287
+ }
288
+
289
+ switch(event.type) {
290
+ case YAML_STREAM_START_EVENT:
291
+ {
292
+ VALUE args[2];
293
+
294
+ args[0] = handler;
295
+ args[1] = INT2NUM((long)event.data.stream_start.encoding);
296
+ rb_protect(protected_start_stream, (VALUE)args, &state);
297
+ }
298
+ break;
299
+ case YAML_DOCUMENT_START_EVENT:
300
+ {
301
+ VALUE args[4];
302
+ /* Get a list of tag directives (if any) */
303
+ VALUE tag_directives = rb_ary_new();
304
+ /* Grab the document version */
305
+ VALUE version = event.data.document_start.version_directive ?
306
+ rb_ary_new3(
307
+ (long)2,
308
+ INT2NUM((long)event.data.document_start.version_directive->major),
309
+ INT2NUM((long)event.data.document_start.version_directive->minor)
310
+ ) : rb_ary_new();
311
+
312
+ if(event.data.document_start.tag_directives.start) {
313
+ yaml_tag_directive_t *start =
314
+ event.data.document_start.tag_directives.start;
315
+ yaml_tag_directive_t *end =
316
+ event.data.document_start.tag_directives.end;
317
+ for(; start != end; start++) {
318
+ VALUE handle = Qnil;
319
+ VALUE prefix = Qnil;
320
+ if(start->handle) {
321
+ handle = rb_str_new2((const char *)start->handle);
322
+ if (tainted) OBJ_TAINT(handle);
323
+ #ifdef HAVE_RUBY_ENCODING_H
324
+ PSYCH_TRANSCODE(handle, encoding, internal_enc);
325
+ #endif
326
+ }
327
+
328
+ if(start->prefix) {
329
+ prefix = rb_str_new2((const char *)start->prefix);
330
+ if (tainted) OBJ_TAINT(prefix);
331
+ #ifdef HAVE_RUBY_ENCODING_H
332
+ PSYCH_TRANSCODE(prefix, encoding, internal_enc);
333
+ #endif
334
+ }
335
+
336
+ rb_ary_push(tag_directives, rb_ary_new3((long)2, handle, prefix));
337
+ }
338
+ }
339
+ args[0] = handler;
340
+ args[1] = version;
341
+ args[2] = tag_directives;
342
+ args[3] = event.data.document_start.implicit == 1 ? Qtrue : Qfalse;
343
+ rb_protect(protected_start_document, (VALUE)args, &state);
344
+ }
345
+ break;
346
+ case YAML_DOCUMENT_END_EVENT:
347
+ {
348
+ VALUE args[2];
349
+
350
+ args[0] = handler;
351
+ args[1] = event.data.document_end.implicit == 1 ? Qtrue : Qfalse;
352
+ rb_protect(protected_end_document, (VALUE)args, &state);
353
+ }
354
+ break;
355
+ case YAML_ALIAS_EVENT:
356
+ {
357
+ VALUE args[2];
358
+ VALUE alias = Qnil;
359
+ if(event.data.alias.anchor) {
360
+ alias = rb_str_new2((const char *)event.data.alias.anchor);
361
+ if (tainted) OBJ_TAINT(alias);
362
+ #ifdef HAVE_RUBY_ENCODING_H
363
+ PSYCH_TRANSCODE(alias, encoding, internal_enc);
364
+ #endif
365
+ }
366
+
367
+ args[0] = handler;
368
+ args[1] = alias;
369
+ rb_protect(protected_alias, (VALUE)args, &state);
370
+ }
371
+ break;
372
+ case YAML_SCALAR_EVENT:
373
+ {
374
+ VALUE args[7];
375
+ VALUE anchor = Qnil;
376
+ VALUE tag = Qnil;
377
+ VALUE plain_implicit, quoted_implicit, style;
378
+ VALUE val = rb_str_new(
379
+ (const char *)event.data.scalar.value,
380
+ (long)event.data.scalar.length
381
+ );
382
+ if (tainted) OBJ_TAINT(val);
383
+
384
+ #ifdef HAVE_RUBY_ENCODING_H
385
+ PSYCH_TRANSCODE(val, encoding, internal_enc);
386
+ #endif
387
+
388
+ if(event.data.scalar.anchor) {
389
+ anchor = rb_str_new2((const char *)event.data.scalar.anchor);
390
+ if (tainted) OBJ_TAINT(anchor);
391
+ #ifdef HAVE_RUBY_ENCODING_H
392
+ PSYCH_TRANSCODE(anchor, encoding, internal_enc);
393
+ #endif
394
+ }
395
+
396
+ if(event.data.scalar.tag) {
397
+ tag = rb_str_new2((const char *)event.data.scalar.tag);
398
+ if (tainted) OBJ_TAINT(tag);
399
+ #ifdef HAVE_RUBY_ENCODING_H
400
+ PSYCH_TRANSCODE(tag, encoding, internal_enc);
401
+ #endif
402
+ }
403
+
404
+ plain_implicit =
405
+ event.data.scalar.plain_implicit == 0 ? Qfalse : Qtrue;
406
+
407
+ quoted_implicit =
408
+ event.data.scalar.quoted_implicit == 0 ? Qfalse : Qtrue;
409
+
410
+ style = INT2NUM((long)event.data.scalar.style);
411
+
412
+ args[0] = handler;
413
+ args[1] = val;
414
+ args[2] = anchor;
415
+ args[3] = tag;
416
+ args[4] = plain_implicit;
417
+ args[5] = quoted_implicit;
418
+ args[6] = style;
419
+ rb_protect(protected_scalar, (VALUE)args, &state);
420
+ }
421
+ break;
422
+ case YAML_SEQUENCE_START_EVENT:
423
+ {
424
+ VALUE args[5];
425
+ VALUE anchor = Qnil;
426
+ VALUE tag = Qnil;
427
+ VALUE implicit, style;
428
+ if(event.data.sequence_start.anchor) {
429
+ anchor = rb_str_new2((const char *)event.data.sequence_start.anchor);
430
+ if (tainted) OBJ_TAINT(anchor);
431
+ #ifdef HAVE_RUBY_ENCODING_H
432
+ PSYCH_TRANSCODE(anchor, encoding, internal_enc);
433
+ #endif
434
+ }
435
+
436
+ tag = Qnil;
437
+ if(event.data.sequence_start.tag) {
438
+ tag = rb_str_new2((const char *)event.data.sequence_start.tag);
439
+ if (tainted) OBJ_TAINT(tag);
440
+ #ifdef HAVE_RUBY_ENCODING_H
441
+ PSYCH_TRANSCODE(tag, encoding, internal_enc);
442
+ #endif
443
+ }
444
+
445
+ implicit =
446
+ event.data.sequence_start.implicit == 0 ? Qfalse : Qtrue;
447
+
448
+ style = INT2NUM((long)event.data.sequence_start.style);
449
+
450
+ args[0] = handler;
451
+ args[1] = anchor;
452
+ args[2] = tag;
453
+ args[3] = implicit;
454
+ args[4] = style;
455
+
456
+ rb_protect(protected_start_sequence, (VALUE)args, &state);
457
+ }
458
+ break;
459
+ case YAML_SEQUENCE_END_EVENT:
460
+ rb_protect(protected_end_sequence, handler, &state);
461
+ break;
462
+ case YAML_MAPPING_START_EVENT:
463
+ {
464
+ VALUE args[5];
465
+ VALUE anchor = Qnil;
466
+ VALUE tag = Qnil;
467
+ VALUE implicit, style;
468
+ if(event.data.mapping_start.anchor) {
469
+ anchor = rb_str_new2((const char *)event.data.mapping_start.anchor);
470
+ if (tainted) OBJ_TAINT(anchor);
471
+ #ifdef HAVE_RUBY_ENCODING_H
472
+ PSYCH_TRANSCODE(anchor, encoding, internal_enc);
473
+ #endif
474
+ }
475
+
476
+ if(event.data.mapping_start.tag) {
477
+ tag = rb_str_new2((const char *)event.data.mapping_start.tag);
478
+ if (tainted) OBJ_TAINT(tag);
479
+ #ifdef HAVE_RUBY_ENCODING_H
480
+ PSYCH_TRANSCODE(tag, encoding, internal_enc);
481
+ #endif
482
+ }
483
+
484
+ implicit =
485
+ event.data.mapping_start.implicit == 0 ? Qfalse : Qtrue;
486
+
487
+ style = INT2NUM((long)event.data.mapping_start.style);
488
+
489
+ args[0] = handler;
490
+ args[1] = anchor;
491
+ args[2] = tag;
492
+ args[3] = implicit;
493
+ args[4] = style;
494
+
495
+ rb_protect(protected_start_mapping, (VALUE)args, &state);
496
+ }
497
+ break;
498
+ case YAML_MAPPING_END_EVENT:
499
+ rb_protect(protected_end_mapping, handler, &state);
500
+ break;
501
+ case YAML_NO_EVENT:
502
+ rb_protect(protected_empty, handler, &state);
503
+ break;
504
+ case YAML_STREAM_END_EVENT:
505
+ rb_protect(protected_end_stream, handler, &state);
506
+ done = 1;
507
+ break;
508
+ }
509
+ yaml_event_delete(&event);
510
+ if (state) rb_jump_tag(state);
511
+ }
512
+
513
+ return self;
514
+ }
515
+
516
+ /*
517
+ * call-seq:
518
+ * parser.mark # => #<Psych::Parser::Mark>
519
+ *
520
+ * Returns a Psych::Parser::Mark object that contains line, column, and index
521
+ * information.
522
+ */
523
+ static VALUE mark(VALUE self)
524
+ {
525
+ VALUE mark_klass;
526
+ VALUE args[3];
527
+ yaml_parser_t * parser;
528
+
529
+ Data_Get_Struct(self, yaml_parser_t, parser);
530
+ mark_klass = rb_const_get_at(cPsychParser, rb_intern("Mark"));
531
+ args[0] = INT2NUM(parser->mark.index);
532
+ args[1] = INT2NUM(parser->mark.line);
533
+ args[2] = INT2NUM(parser->mark.column);
534
+
535
+ return rb_class_new_instance(3, args, mark_klass);
536
+ }
537
+
538
+ void Init_psych_parser()
539
+ {
540
+ #if 0
541
+ mPsych = rb_define_module("Psych");
542
+ #endif
543
+
544
+ cPsychParser = rb_define_class_under(mPsych, "Parser", rb_cObject);
545
+ rb_define_alloc_func(cPsychParser, allocate);
546
+
547
+ /* Any encoding: Let the parser choose the encoding */
548
+ rb_define_const(cPsychParser, "ANY", INT2NUM(YAML_ANY_ENCODING));
549
+
550
+ /* UTF-8 Encoding */
551
+ rb_define_const(cPsychParser, "UTF8", INT2NUM(YAML_UTF8_ENCODING));
552
+
553
+ /* UTF-16-LE Encoding with BOM */
554
+ rb_define_const(cPsychParser, "UTF16LE", INT2NUM(YAML_UTF16LE_ENCODING));
555
+
556
+ /* UTF-16-BE Encoding with BOM */
557
+ rb_define_const(cPsychParser, "UTF16BE", INT2NUM(YAML_UTF16BE_ENCODING));
558
+
559
+ rb_require("psych/syntax_error");
560
+ ePsychSyntaxError = rb_const_get(mPsych, rb_intern("SyntaxError"));
561
+
562
+ rb_define_method(cPsychParser, "parse", parse, -1);
563
+ rb_define_method(cPsychParser, "mark", mark, 0);
564
+
565
+ id_read = rb_intern("read");
566
+ id_path = rb_intern("path");
567
+ id_empty = rb_intern("empty");
568
+ id_start_stream = rb_intern("start_stream");
569
+ id_end_stream = rb_intern("end_stream");
570
+ id_start_document = rb_intern("start_document");
571
+ id_end_document = rb_intern("end_document");
572
+ id_alias = rb_intern("alias");
573
+ id_scalar = rb_intern("scalar");
574
+ id_start_sequence = rb_intern("start_sequence");
575
+ id_end_sequence = rb_intern("end_sequence");
576
+ id_start_mapping = rb_intern("start_mapping");
577
+ id_end_mapping = rb_intern("end_mapping");
578
+ }
579
+ /* vim: set noet sws=4 sw=4: */