2010-03-28 17:49:37 -04:00
|
|
|
#include <psych.h>
|
|
|
|
|
|
|
|
VALUE cPsychParser;
|
|
|
|
VALUE ePsychSyntaxError;
|
|
|
|
|
|
|
|
static ID id_read;
|
2011-01-21 20:26:40 -05:00
|
|
|
static ID id_path;
|
2010-03-28 17:49:37 -04:00
|
|
|
static ID id_empty;
|
|
|
|
static ID id_start_stream;
|
|
|
|
static ID id_end_stream;
|
|
|
|
static ID id_start_document;
|
|
|
|
static ID id_end_document;
|
|
|
|
static ID id_alias;
|
|
|
|
static ID id_scalar;
|
|
|
|
static ID id_start_sequence;
|
|
|
|
static ID id_end_sequence;
|
|
|
|
static ID id_start_mapping;
|
|
|
|
static ID id_end_mapping;
|
2017-11-26 22:11:18 -05:00
|
|
|
static ID id_event_location;
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 12:22:52 -04:00
|
|
|
#define PSYCH_TRANSCODE(_str, _yaml_enc, _internal_enc) \
|
2010-05-10 20:53:16 -04:00
|
|
|
do { \
|
2011-03-22 07:10:34 -04:00
|
|
|
rb_enc_associate_index((_str), (_yaml_enc)); \
|
2010-05-10 12:22:52 -04:00
|
|
|
if(_internal_enc) \
|
2011-03-22 07:10:34 -04:00
|
|
|
(_str) = rb_str_export_to_enc((_str), (_internal_enc)); \
|
2010-05-10 20:53:16 -04:00
|
|
|
} while (0)
|
2010-05-10 12:22:52 -04:00
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
static int io_reader(void * data, unsigned char *buf, size_t size, size_t *read)
|
|
|
|
{
|
|
|
|
VALUE io = (VALUE)data;
|
|
|
|
VALUE string = rb_funcall(io, id_read, 1, INT2NUM(size));
|
|
|
|
|
|
|
|
*read = 0;
|
|
|
|
|
|
|
|
if(! NIL_P(string)) {
|
|
|
|
void * str = (void *)StringValuePtr(string);
|
|
|
|
*read = (size_t)RSTRING_LEN(string);
|
|
|
|
memcpy(buf, str, *read);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-05-18 23:12:03 -04:00
|
|
|
static void dealloc(void * ptr)
|
|
|
|
{
|
|
|
|
yaml_parser_t * parser;
|
|
|
|
|
|
|
|
parser = (yaml_parser_t *)ptr;
|
|
|
|
yaml_parser_delete(parser);
|
|
|
|
xfree(parser);
|
|
|
|
}
|
|
|
|
|
2014-10-04 19:33:19 -04:00
|
|
|
#if 0
|
|
|
|
static size_t memsize(const void *ptr)
|
|
|
|
{
|
|
|
|
const yaml_parser_t *parser = ptr;
|
|
|
|
/* TODO: calculate parser's size */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const rb_data_type_t psych_parser_type = {
|
|
|
|
"Psych/parser",
|
|
|
|
{0, dealloc, 0,},
|
2014-12-01 01:38:04 -05:00
|
|
|
0, 0,
|
2014-12-05 08:43:13 -05:00
|
|
|
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
|
2014-10-04 19:33:19 -04:00
|
|
|
RUBY_TYPED_FREE_IMMEDIATELY,
|
2014-12-05 08:43:13 -05:00
|
|
|
#endif
|
2014-10-04 19:33:19 -04:00
|
|
|
};
|
|
|
|
|
2010-05-18 23:12:03 -04:00
|
|
|
static VALUE allocate(VALUE klass)
|
|
|
|
{
|
|
|
|
yaml_parser_t * parser;
|
2015-05-29 01:44:01 -04:00
|
|
|
VALUE obj = TypedData_Make_Struct(klass, yaml_parser_t, &psych_parser_type, parser);
|
2010-05-18 23:12:03 -04:00
|
|
|
|
|
|
|
yaml_parser_initialize(parser);
|
|
|
|
|
2015-05-29 01:44:01 -04:00
|
|
|
return obj;
|
2010-05-18 23:12:03 -04:00
|
|
|
}
|
|
|
|
|
2011-10-04 13:53:41 -04:00
|
|
|
static VALUE make_exception(yaml_parser_t * parser, VALUE path)
|
|
|
|
{
|
|
|
|
size_t line, column;
|
|
|
|
|
|
|
|
line = parser->context_mark.line + 1;
|
|
|
|
column = parser->context_mark.column + 1;
|
|
|
|
|
|
|
|
return rb_funcall(ePsychSyntaxError, rb_intern("new"), 6,
|
|
|
|
path,
|
|
|
|
INT2NUM(line),
|
|
|
|
INT2NUM(column),
|
|
|
|
INT2NUM(parser->problem_offset),
|
|
|
|
parser->problem ? rb_usascii_str_new2(parser->problem) : Qnil,
|
|
|
|
parser->context ? rb_usascii_str_new2(parser->context) : Qnil);
|
|
|
|
}
|
|
|
|
|
2012-02-23 18:12:57 -05:00
|
|
|
static VALUE transcode_string(VALUE src, int * parser_encoding)
|
|
|
|
{
|
|
|
|
int utf8 = rb_utf8_encindex();
|
2012-05-17 12:29:39 -04:00
|
|
|
int utf16le = rb_enc_find_index("UTF-16LE");
|
|
|
|
int utf16be = rb_enc_find_index("UTF-16BE");
|
2012-02-23 18:12:57 -05:00
|
|
|
int source_encoding = rb_enc_get_index(src);
|
|
|
|
|
|
|
|
if (source_encoding == utf8) {
|
|
|
|
*parser_encoding = YAML_UTF8_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_encoding == utf16le) {
|
|
|
|
*parser_encoding = YAML_UTF16LE_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_encoding == utf16be) {
|
|
|
|
*parser_encoding = YAML_UTF16BE_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = rb_str_export_to_enc(src, rb_utf8_encoding());
|
|
|
|
RB_GC_GUARD(src);
|
|
|
|
|
|
|
|
*parser_encoding = YAML_UTF8_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE transcode_io(VALUE src, int * parser_encoding)
|
|
|
|
{
|
|
|
|
VALUE io_external_encoding;
|
|
|
|
int io_external_enc_index;
|
|
|
|
|
|
|
|
io_external_encoding = rb_funcall(src, rb_intern("external_encoding"), 0);
|
|
|
|
|
|
|
|
/* if no encoding is returned, assume ascii8bit. */
|
|
|
|
if (NIL_P(io_external_encoding)) {
|
|
|
|
io_external_enc_index = rb_ascii8bit_encindex();
|
|
|
|
} else {
|
|
|
|
io_external_enc_index = rb_to_encoding_index(io_external_encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Treat US-ASCII as utf_8 */
|
|
|
|
if (io_external_enc_index == rb_usascii_encindex()) {
|
|
|
|
*parser_encoding = YAML_UTF8_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io_external_enc_index == rb_utf8_encindex()) {
|
|
|
|
*parser_encoding = YAML_UTF8_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io_external_enc_index == rb_enc_find_index("UTF-16LE")) {
|
|
|
|
*parser_encoding = YAML_UTF16LE_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io_external_enc_index == rb_enc_find_index("UTF-16BE")) {
|
|
|
|
*parser_encoding = YAML_UTF16BE_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just guess on ASCII-8BIT */
|
|
|
|
if (io_external_enc_index == rb_ascii8bit_encindex()) {
|
|
|
|
*parser_encoding = YAML_ANY_ENCODING;
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2012-04-06 13:09:03 -04:00
|
|
|
/* If the external encoding is something we don't know how to handle,
|
|
|
|
* fall back to YAML_ANY_ENCODING. */
|
|
|
|
*parser_encoding = YAML_ANY_ENCODING;
|
2012-02-23 18:12:57 -05:00
|
|
|
|
2012-04-06 13:09:03 -04:00
|
|
|
return src;
|
2012-02-23 18:12:57 -05:00
|
|
|
}
|
|
|
|
|
2012-02-23 23:55:33 -05:00
|
|
|
static VALUE protected_start_stream(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall(args[0], id_start_stream, 1, args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_start_document(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall3(args[0], id_start_document, 3, args + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_end_document(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall(args[0], id_end_document, 1, args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_alias(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall(args[0], id_alias, 1, args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_scalar(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall3(args[0], id_scalar, 6, args + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_start_sequence(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall3(args[0], id_start_sequence, 4, args + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_end_sequence(VALUE handler)
|
|
|
|
{
|
|
|
|
return rb_funcall(handler, id_end_sequence, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_start_mapping(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall3(args[0], id_start_mapping, 4, args + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_end_mapping(VALUE handler)
|
|
|
|
{
|
|
|
|
return rb_funcall(handler, id_end_mapping, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_empty(VALUE handler)
|
|
|
|
{
|
|
|
|
return rb_funcall(handler, id_empty, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE protected_end_stream(VALUE handler)
|
|
|
|
{
|
|
|
|
return rb_funcall(handler, id_end_stream, 0);
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:11:18 -05:00
|
|
|
static VALUE protected_event_location(VALUE pointer)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)pointer;
|
|
|
|
return rb_funcall3(args[0], id_event_location, 4, args + 1);
|
|
|
|
}
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* parser.parse(yaml)
|
|
|
|
*
|
|
|
|
* Parse the YAML document contained in +yaml+. Events will be called on
|
|
|
|
* the handler set on the parser instance.
|
|
|
|
*
|
|
|
|
* See Psych::Parser and Psych::Parser#handler
|
|
|
|
*/
|
2011-11-29 19:10:31 -05:00
|
|
|
static VALUE parse(int argc, VALUE *argv, VALUE self)
|
2010-03-28 17:49:37 -04:00
|
|
|
{
|
2011-11-29 19:10:31 -05:00
|
|
|
VALUE yaml, path;
|
2010-05-18 23:12:03 -04:00
|
|
|
yaml_parser_t * parser;
|
2010-03-28 17:49:37 -04:00
|
|
|
yaml_event_t event;
|
2010-03-29 14:58:22 -04:00
|
|
|
int done = 0;
|
2011-04-21 15:17:46 -04:00
|
|
|
int tainted = 0;
|
2012-02-23 23:55:33 -05:00
|
|
|
int state = 0;
|
2012-02-23 18:12:57 -05:00
|
|
|
int parser_encoding = YAML_ANY_ENCODING;
|
2010-05-18 23:12:03 -04:00
|
|
|
int encoding = rb_utf8_encindex();
|
|
|
|
rb_encoding * internal_enc = rb_default_internal_encoding();
|
2010-03-29 14:58:22 -04:00
|
|
|
VALUE handler = rb_iv_get(self, "@handler");
|
|
|
|
|
2011-11-29 19:10:31 -05:00
|
|
|
if (rb_scan_args(argc, argv, "11", &yaml, &path) == 1) {
|
|
|
|
if(rb_respond_to(yaml, id_path))
|
|
|
|
path = rb_funcall(yaml, id_path, 0);
|
|
|
|
else
|
|
|
|
path = rb_str_new2("<unknown>");
|
|
|
|
}
|
|
|
|
|
2014-10-04 19:33:19 -04:00
|
|
|
TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2012-02-09 13:44:27 -05:00
|
|
|
yaml_parser_delete(parser);
|
|
|
|
yaml_parser_initialize(parser);
|
|
|
|
|
2011-04-21 15:17:46 -04:00
|
|
|
if (OBJ_TAINTED(yaml)) tainted = 1;
|
|
|
|
|
2012-02-23 18:12:57 -05:00
|
|
|
if (rb_respond_to(yaml, id_read)) {
|
|
|
|
yaml = transcode_io(yaml, &parser_encoding);
|
|
|
|
yaml_parser_set_encoding(parser, parser_encoding);
|
2010-05-18 23:12:03 -04:00
|
|
|
yaml_parser_set_input(parser, io_reader, (void *)yaml);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (RTEST(rb_obj_is_kind_of(yaml, rb_cIO))) tainted = 1;
|
2010-03-28 17:49:37 -04:00
|
|
|
} else {
|
2010-04-10 19:50:50 -04:00
|
|
|
StringValue(yaml);
|
2012-02-23 18:12:57 -05:00
|
|
|
yaml = transcode_string(yaml, &parser_encoding);
|
|
|
|
yaml_parser_set_encoding(parser, parser_encoding);
|
2010-03-28 17:49:37 -04:00
|
|
|
yaml_parser_set_input_string(
|
2010-05-18 23:12:03 -04:00
|
|
|
parser,
|
2010-04-10 19:50:50 -04:00
|
|
|
(const unsigned char *)RSTRING_PTR(yaml),
|
2010-03-28 17:49:37 -04:00
|
|
|
(size_t)RSTRING_LEN(yaml)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!done) {
|
2017-11-27 01:59:07 -05:00
|
|
|
VALUE event_args[5];
|
|
|
|
VALUE start_line, start_column, end_line, end_column;
|
|
|
|
|
2010-05-18 23:12:03 -04:00
|
|
|
if(!yaml_parser_parse(parser, &event)) {
|
2011-11-29 19:10:31 -05:00
|
|
|
VALUE exception;
|
2011-01-21 20:26:40 -05:00
|
|
|
|
2011-10-04 13:53:41 -04:00
|
|
|
exception = make_exception(parser, path);
|
2011-01-21 20:13:52 -05:00
|
|
|
yaml_parser_delete(parser);
|
|
|
|
yaml_parser_initialize(parser);
|
|
|
|
|
2011-10-04 13:53:41 -04:00
|
|
|
rb_exc_raise(exception);
|
2010-03-28 17:49:37 -04:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:11:18 -05:00
|
|
|
start_line = INT2NUM((long)event.start_mark.line);
|
|
|
|
start_column = INT2NUM((long)event.start_mark.column);
|
|
|
|
end_line = INT2NUM((long)event.end_mark.line);
|
|
|
|
end_column = INT2NUM((long)event.end_mark.column);
|
|
|
|
|
|
|
|
event_args[0] = handler;
|
|
|
|
event_args[1] = start_line;
|
|
|
|
event_args[2] = start_column;
|
|
|
|
event_args[3] = end_line;
|
|
|
|
event_args[4] = end_column;
|
|
|
|
rb_protect(protected_event_location, (VALUE)event_args, &state);
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
switch(event.type) {
|
2012-02-23 23:55:33 -05:00
|
|
|
case YAML_STREAM_START_EVENT:
|
|
|
|
{
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = handler;
|
|
|
|
args[1] = INT2NUM((long)event.data.stream_start.encoding);
|
|
|
|
rb_protect(protected_start_stream, (VALUE)args, &state);
|
|
|
|
}
|
|
|
|
break;
|
2010-05-10 20:53:11 -04:00
|
|
|
case YAML_DOCUMENT_START_EVENT:
|
|
|
|
{
|
2012-02-23 23:55:33 -05:00
|
|
|
VALUE args[4];
|
2010-05-10 20:53:11 -04:00
|
|
|
/* Get a list of tag directives (if any) */
|
|
|
|
VALUE tag_directives = rb_ary_new();
|
|
|
|
/* Grab the document version */
|
|
|
|
VALUE version = event.data.document_start.version_directive ?
|
|
|
|
rb_ary_new3(
|
|
|
|
(long)2,
|
|
|
|
INT2NUM((long)event.data.document_start.version_directive->major),
|
|
|
|
INT2NUM((long)event.data.document_start.version_directive->minor)
|
|
|
|
) : rb_ary_new();
|
|
|
|
|
|
|
|
if(event.data.document_start.tag_directives.start) {
|
|
|
|
yaml_tag_directive_t *start =
|
|
|
|
event.data.document_start.tag_directives.start;
|
|
|
|
yaml_tag_directive_t *end =
|
|
|
|
event.data.document_start.tag_directives.end;
|
|
|
|
for(; start != end; start++) {
|
|
|
|
VALUE handle = Qnil;
|
|
|
|
VALUE prefix = Qnil;
|
|
|
|
if(start->handle) {
|
|
|
|
handle = rb_str_new2((const char *)start->handle);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(handle);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(handle, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
if(start->prefix) {
|
|
|
|
prefix = rb_str_new2((const char *)start->prefix);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(prefix);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(prefix, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
rb_ary_push(tag_directives, rb_ary_new3((long)2, handle, prefix));
|
|
|
|
}
|
|
|
|
}
|
2012-02-23 23:55:33 -05:00
|
|
|
args[0] = handler;
|
|
|
|
args[1] = version;
|
|
|
|
args[2] = tag_directives;
|
|
|
|
args[3] = event.data.document_start.implicit == 1 ? Qtrue : Qfalse;
|
|
|
|
rb_protect(protected_start_document, (VALUE)args, &state);
|
2010-03-28 17:49:37 -04:00
|
|
|
}
|
2010-05-10 20:53:11 -04:00
|
|
|
break;
|
|
|
|
case YAML_DOCUMENT_END_EVENT:
|
2012-02-23 23:55:33 -05:00
|
|
|
{
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = handler;
|
|
|
|
args[1] = event.data.document_end.implicit == 1 ? Qtrue : Qfalse;
|
|
|
|
rb_protect(protected_end_document, (VALUE)args, &state);
|
|
|
|
}
|
2010-05-10 20:53:11 -04:00
|
|
|
break;
|
|
|
|
case YAML_ALIAS_EVENT:
|
|
|
|
{
|
2012-02-23 23:55:33 -05:00
|
|
|
VALUE args[2];
|
2010-05-10 20:53:11 -04:00
|
|
|
VALUE alias = Qnil;
|
|
|
|
if(event.data.alias.anchor) {
|
|
|
|
alias = rb_str_new2((const char *)event.data.alias.anchor);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(alias);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(alias, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2012-02-23 23:55:33 -05:00
|
|
|
args[0] = handler;
|
|
|
|
args[1] = alias;
|
|
|
|
rb_protect(protected_alias, (VALUE)args, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YAML_SCALAR_EVENT:
|
|
|
|
{
|
2012-02-23 23:55:33 -05:00
|
|
|
VALUE args[7];
|
2010-05-10 20:53:11 -04:00
|
|
|
VALUE anchor = Qnil;
|
|
|
|
VALUE tag = Qnil;
|
|
|
|
VALUE plain_implicit, quoted_implicit, style;
|
|
|
|
VALUE val = rb_str_new(
|
|
|
|
(const char *)event.data.scalar.value,
|
|
|
|
(long)event.data.scalar.length
|
|
|
|
);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(val);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(val, encoding, internal_enc);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
if(event.data.scalar.anchor) {
|
|
|
|
anchor = rb_str_new2((const char *)event.data.scalar.anchor);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(anchor);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(anchor, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
if(event.data.scalar.tag) {
|
|
|
|
tag = rb_str_new2((const char *)event.data.scalar.tag);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(tag);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(tag, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
plain_implicit =
|
|
|
|
event.data.scalar.plain_implicit == 0 ? Qfalse : Qtrue;
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
quoted_implicit =
|
|
|
|
event.data.scalar.quoted_implicit == 0 ? Qfalse : Qtrue;
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
style = INT2NUM((long)event.data.scalar.style);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2012-02-23 23:55:33 -05:00
|
|
|
args[0] = handler;
|
|
|
|
args[1] = val;
|
|
|
|
args[2] = anchor;
|
|
|
|
args[3] = tag;
|
|
|
|
args[4] = plain_implicit;
|
|
|
|
args[5] = quoted_implicit;
|
|
|
|
args[6] = style;
|
|
|
|
rb_protect(protected_scalar, (VALUE)args, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YAML_SEQUENCE_START_EVENT:
|
|
|
|
{
|
2012-02-23 23:55:33 -05:00
|
|
|
VALUE args[5];
|
2010-05-10 20:53:11 -04:00
|
|
|
VALUE anchor = Qnil;
|
|
|
|
VALUE tag = Qnil;
|
|
|
|
VALUE implicit, style;
|
|
|
|
if(event.data.sequence_start.anchor) {
|
|
|
|
anchor = rb_str_new2((const char *)event.data.sequence_start.anchor);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(anchor);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(anchor, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
tag = Qnil;
|
|
|
|
if(event.data.sequence_start.tag) {
|
|
|
|
tag = rb_str_new2((const char *)event.data.sequence_start.tag);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(tag);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(tag, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
implicit =
|
|
|
|
event.data.sequence_start.implicit == 0 ? Qfalse : Qtrue;
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
style = INT2NUM((long)event.data.sequence_start.style);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2012-02-23 23:55:33 -05:00
|
|
|
args[0] = handler;
|
|
|
|
args[1] = anchor;
|
|
|
|
args[2] = tag;
|
|
|
|
args[3] = implicit;
|
|
|
|
args[4] = style;
|
|
|
|
|
|
|
|
rb_protect(protected_start_sequence, (VALUE)args, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YAML_SEQUENCE_END_EVENT:
|
2012-02-23 23:55:33 -05:00
|
|
|
rb_protect(protected_end_sequence, handler, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
break;
|
|
|
|
case YAML_MAPPING_START_EVENT:
|
|
|
|
{
|
2012-02-23 23:55:33 -05:00
|
|
|
VALUE args[5];
|
2010-05-10 20:53:11 -04:00
|
|
|
VALUE anchor = Qnil;
|
|
|
|
VALUE tag = Qnil;
|
|
|
|
VALUE implicit, style;
|
|
|
|
if(event.data.mapping_start.anchor) {
|
|
|
|
anchor = rb_str_new2((const char *)event.data.mapping_start.anchor);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(anchor);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(anchor, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
if(event.data.mapping_start.tag) {
|
|
|
|
tag = rb_str_new2((const char *)event.data.mapping_start.tag);
|
2011-04-21 15:17:46 -04:00
|
|
|
if (tainted) OBJ_TAINT(tag);
|
2010-05-10 20:53:16 -04:00
|
|
|
PSYCH_TRANSCODE(tag, encoding, internal_enc);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
implicit =
|
|
|
|
event.data.mapping_start.implicit == 0 ? Qfalse : Qtrue;
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-05-10 20:53:11 -04:00
|
|
|
style = INT2NUM((long)event.data.mapping_start.style);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2012-02-23 23:55:33 -05:00
|
|
|
args[0] = handler;
|
|
|
|
args[1] = anchor;
|
|
|
|
args[2] = tag;
|
|
|
|
args[3] = implicit;
|
|
|
|
args[4] = style;
|
|
|
|
|
|
|
|
rb_protect(protected_start_mapping, (VALUE)args, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YAML_MAPPING_END_EVENT:
|
2012-02-23 23:55:33 -05:00
|
|
|
rb_protect(protected_end_mapping, handler, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
break;
|
|
|
|
case YAML_NO_EVENT:
|
2012-02-23 23:55:33 -05:00
|
|
|
rb_protect(protected_empty, handler, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
break;
|
|
|
|
case YAML_STREAM_END_EVENT:
|
2012-02-23 23:55:33 -05:00
|
|
|
rb_protect(protected_end_stream, handler, &state);
|
2010-05-10 20:53:11 -04:00
|
|
|
done = 1;
|
|
|
|
break;
|
2010-03-28 17:49:37 -04:00
|
|
|
}
|
2011-06-05 20:41:24 -04:00
|
|
|
yaml_event_delete(&event);
|
2012-02-23 23:55:33 -05:00
|
|
|
if (state) rb_jump_tag(state);
|
2010-03-28 17:49:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:44:31 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* parser.mark # => #<Psych::Parser::Mark>
|
|
|
|
*
|
|
|
|
* Returns a Psych::Parser::Mark object that contains line, column, and index
|
|
|
|
* information.
|
|
|
|
*/
|
|
|
|
static VALUE mark(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE mark_klass;
|
|
|
|
VALUE args[3];
|
|
|
|
yaml_parser_t * parser;
|
|
|
|
|
2014-10-04 19:33:19 -04:00
|
|
|
TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
|
2011-01-17 14:44:31 -05:00
|
|
|
mark_klass = rb_const_get_at(cPsychParser, rb_intern("Mark"));
|
|
|
|
args[0] = INT2NUM(parser->mark.index);
|
|
|
|
args[1] = INT2NUM(parser->mark.line);
|
|
|
|
args[2] = INT2NUM(parser->mark.column);
|
|
|
|
|
|
|
|
return rb_class_new_instance(3, args, mark_klass);
|
|
|
|
}
|
|
|
|
|
2014-10-04 19:09:32 -04:00
|
|
|
void Init_psych_parser(void)
|
2010-03-28 17:49:37 -04:00
|
|
|
{
|
2018-02-16 03:39:48 -05:00
|
|
|
#undef rb_intern
|
2010-03-28 17:49:37 -04:00
|
|
|
#if 0
|
|
|
|
mPsych = rb_define_module("Psych");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cPsychParser = rb_define_class_under(mPsych, "Parser", rb_cObject);
|
2010-05-18 23:12:03 -04:00
|
|
|
rb_define_alloc_func(cPsychParser, allocate);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
/* Any encoding: Let the parser choose the encoding */
|
|
|
|
rb_define_const(cPsychParser, "ANY", INT2NUM(YAML_ANY_ENCODING));
|
|
|
|
|
|
|
|
/* UTF-8 Encoding */
|
|
|
|
rb_define_const(cPsychParser, "UTF8", INT2NUM(YAML_UTF8_ENCODING));
|
|
|
|
|
|
|
|
/* UTF-16-LE Encoding with BOM */
|
|
|
|
rb_define_const(cPsychParser, "UTF16LE", INT2NUM(YAML_UTF16LE_ENCODING));
|
|
|
|
|
|
|
|
/* UTF-16-BE Encoding with BOM */
|
|
|
|
rb_define_const(cPsychParser, "UTF16BE", INT2NUM(YAML_UTF16BE_ENCODING));
|
|
|
|
|
2011-10-04 13:53:41 -04:00
|
|
|
rb_require("psych/syntax_error");
|
2012-10-22 17:25:02 -04:00
|
|
|
ePsychSyntaxError = rb_const_get(mPsych, rb_intern("SyntaxError"));
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2011-11-29 19:10:31 -05:00
|
|
|
rb_define_method(cPsychParser, "parse", parse, -1);
|
2011-01-17 14:44:31 -05:00
|
|
|
rb_define_method(cPsychParser, "mark", mark, 0);
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2017-11-26 22:11:18 -05:00
|
|
|
id_read = rb_intern("read");
|
|
|
|
id_path = rb_intern("path");
|
|
|
|
id_empty = rb_intern("empty");
|
|
|
|
id_start_stream = rb_intern("start_stream");
|
|
|
|
id_end_stream = rb_intern("end_stream");
|
|
|
|
id_start_document = rb_intern("start_document");
|
|
|
|
id_end_document = rb_intern("end_document");
|
|
|
|
id_alias = rb_intern("alias");
|
|
|
|
id_scalar = rb_intern("scalar");
|
|
|
|
id_start_sequence = rb_intern("start_sequence");
|
|
|
|
id_end_sequence = rb_intern("end_sequence");
|
|
|
|
id_start_mapping = rb_intern("start_mapping");
|
|
|
|
id_end_mapping = rb_intern("end_mapping");
|
|
|
|
id_event_location = rb_intern("event_location");
|
2010-03-28 17:49:37 -04:00
|
|
|
}
|
|
|
|
/* vim: set noet sws=4 sw=4: */
|