psych 2.0.14-java

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