diff --git a/api.c b/api.c deleted file mode 100644 index 0c4732e..0000000 --- a/api.c +++ /dev/null @@ -1,1392 +0,0 @@ - -#include "yaml_private.h" - -/* - * Get the library version. - */ - -YAML_DECLARE(const char *) -yaml_get_version_string(void) -{ - return YAML_VERSION_STRING; -} - -/* - * Get the library version numbers. - */ - -YAML_DECLARE(void) -yaml_get_version(int *major, int *minor, int *patch) -{ - *major = YAML_VERSION_MAJOR; - *minor = YAML_VERSION_MINOR; - *patch = YAML_VERSION_PATCH; -} - -/* - * Allocate a dynamic memory block. - */ - -YAML_DECLARE(void *) -yaml_malloc(size_t size) -{ - return malloc(size ? size : 1); -} - -/* - * Reallocate a dynamic memory block. - */ - -YAML_DECLARE(void *) -yaml_realloc(void *ptr, size_t size) -{ - return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1); -} - -/* - * Free a dynamic memory block. - */ - -YAML_DECLARE(void) -yaml_free(void *ptr) -{ - if (ptr) free(ptr); -} - -/* - * Duplicate a string. - */ - -YAML_DECLARE(yaml_char_t *) -yaml_strdup(const yaml_char_t *str) -{ - if (!str) - return NULL; - - return (yaml_char_t *)strdup((char *)str); -} - -/* - * Extend a string. - */ - -YAML_DECLARE(int) -yaml_string_extend(yaml_char_t **start, - yaml_char_t **pointer, yaml_char_t **end) -{ - yaml_char_t *new_start = yaml_realloc(*start, (*end - *start)*2); - - if (!new_start) return 0; - - memset(new_start + (*end - *start), 0, *end - *start); - - *pointer = new_start + (*pointer - *start); - *end = new_start + (*end - *start)*2; - *start = new_start; - - return 1; -} - -/* - * Append a string B to a string A. - */ - -YAML_DECLARE(int) -yaml_string_join( - yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end, - yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end) -{ - if (*b_start == *b_pointer) - return 1; - - while (*a_end - *a_pointer <= *b_pointer - *b_start) { - if (!yaml_string_extend(a_start, a_pointer, a_end)) - return 0; - } - - memcpy(*a_pointer, *b_start, *b_pointer - *b_start); - *a_pointer += *b_pointer - *b_start; - - return 1; -} - -/* - * Extend a stack. - */ - -YAML_DECLARE(int) -yaml_stack_extend(void **start, void **top, void **end) -{ - void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); - - if (!new_start) return 0; - - *top = (char *)new_start + ((char *)*top - (char *)*start); - *end = (char *)new_start + ((char *)*end - (char *)*start)*2; - *start = new_start; - - return 1; -} - -/* - * Extend or move a queue. - */ - -YAML_DECLARE(int) -yaml_queue_extend(void **start, void **head, void **tail, void **end) -{ - /* Check if we need to resize the queue. */ - - if (*start == *head && *tail == *end) { - void *new_start = yaml_realloc(*start, - ((char *)*end - (char *)*start)*2); - - if (!new_start) return 0; - - *head = (char *)new_start + ((char *)*head - (char *)*start); - *tail = (char *)new_start + ((char *)*tail - (char *)*start); - *end = (char *)new_start + ((char *)*end - (char *)*start)*2; - *start = new_start; - } - - /* Check if we need to move the queue at the beginning of the buffer. */ - - if (*tail == *end) { - if (*head != *tail) { - memmove(*start, *head, (char *)*tail - (char *)*head); - } - *tail = (char *)*tail - (char *)*head + (char *)*start; - *head = *start; - } - - return 1; -} - - -/* - * Create a new parser object. - */ - -YAML_DECLARE(int) -yaml_parser_initialize(yaml_parser_t *parser) -{ - assert(parser); /* Non-NULL parser object expected. */ - - memset(parser, 0, sizeof(yaml_parser_t)); - if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE)) - goto error; - if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE)) - goto error; - if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE)) - goto error; - if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE)) - goto error; - if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE)) - goto error; - if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE)) - goto error; - if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE)) - goto error; - if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE)) - goto error; - - return 1; - -error: - - BUFFER_DEL(parser, parser->raw_buffer); - BUFFER_DEL(parser, parser->buffer); - QUEUE_DEL(parser, parser->tokens); - STACK_DEL(parser, parser->indents); - STACK_DEL(parser, parser->simple_keys); - STACK_DEL(parser, parser->states); - STACK_DEL(parser, parser->marks); - STACK_DEL(parser, parser->tag_directives); - - return 0; -} - -/* - * Destroy a parser object. - */ - -YAML_DECLARE(void) -yaml_parser_delete(yaml_parser_t *parser) -{ - assert(parser); /* Non-NULL parser object expected. */ - - BUFFER_DEL(parser, parser->raw_buffer); - BUFFER_DEL(parser, parser->buffer); - while (!QUEUE_EMPTY(parser, parser->tokens)) { - yaml_token_delete(&DEQUEUE(parser, parser->tokens)); - } - QUEUE_DEL(parser, parser->tokens); - STACK_DEL(parser, parser->indents); - STACK_DEL(parser, parser->simple_keys); - STACK_DEL(parser, parser->states); - STACK_DEL(parser, parser->marks); - while (!STACK_EMPTY(parser, parser->tag_directives)) { - yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); - yaml_free(tag_directive.handle); - yaml_free(tag_directive.prefix); - } - STACK_DEL(parser, parser->tag_directives); - - memset(parser, 0, sizeof(yaml_parser_t)); -} - -/* - * String read handler. - */ - -static int -yaml_string_read_handler(void *data, unsigned char *buffer, size_t size, - size_t *size_read) -{ - yaml_parser_t *parser = data; - - if (parser->input.string.current == parser->input.string.end) { - *size_read = 0; - return 1; - } - - if (size > (size_t)(parser->input.string.end - - parser->input.string.current)) { - size = parser->input.string.end - parser->input.string.current; - } - - memcpy(buffer, parser->input.string.current, size); - parser->input.string.current += size; - *size_read = size; - return 1; -} - -/* - * File read handler. - */ - -static int -yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, - size_t *size_read) -{ - yaml_parser_t *parser = data; - - *size_read = fread(buffer, 1, size, parser->input.file); - return !ferror(parser->input.file); -} - -/* - * Set a string input. - */ - -YAML_DECLARE(void) -yaml_parser_set_input_string(yaml_parser_t *parser, - const unsigned char *input, size_t size) -{ - assert(parser); /* Non-NULL parser object expected. */ - assert(!parser->read_handler); /* You can set the source only once. */ - assert(input); /* Non-NULL input string expected. */ - - parser->read_handler = yaml_string_read_handler; - parser->read_handler_data = parser; - - parser->input.string.start = input; - parser->input.string.current = input; - parser->input.string.end = input+size; -} - -/* - * Set a file input. - */ - -YAML_DECLARE(void) -yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file) -{ - assert(parser); /* Non-NULL parser object expected. */ - assert(!parser->read_handler); /* You can set the source only once. */ - assert(file); /* Non-NULL file object expected. */ - - parser->read_handler = yaml_file_read_handler; - parser->read_handler_data = parser; - - parser->input.file = file; -} - -/* - * Set a generic input. - */ - -YAML_DECLARE(void) -yaml_parser_set_input(yaml_parser_t *parser, - yaml_read_handler_t *handler, void *data) -{ - assert(parser); /* Non-NULL parser object expected. */ - assert(!parser->read_handler); /* You can set the source only once. */ - assert(handler); /* Non-NULL read handler expected. */ - - parser->read_handler = handler; - parser->read_handler_data = data; -} - -/* - * Set the source encoding. - */ - -YAML_DECLARE(void) -yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding) -{ - assert(parser); /* Non-NULL parser object expected. */ - assert(!parser->encoding); /* Encoding is already set or detected. */ - - parser->encoding = encoding; -} - -/* - * Create a new emitter object. - */ - -YAML_DECLARE(int) -yaml_emitter_initialize(yaml_emitter_t *emitter) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - memset(emitter, 0, sizeof(yaml_emitter_t)); - if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE)) - goto error; - if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE)) - goto error; - if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE)) - goto error; - if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE)) - goto error; - if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE)) - goto error; - if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE)) - goto error; - - return 1; - -error: - - BUFFER_DEL(emitter, emitter->buffer); - BUFFER_DEL(emitter, emitter->raw_buffer); - STACK_DEL(emitter, emitter->states); - QUEUE_DEL(emitter, emitter->events); - STACK_DEL(emitter, emitter->indents); - STACK_DEL(emitter, emitter->tag_directives); - - return 0; -} - -/* - * Destroy an emitter object. - */ - -YAML_DECLARE(void) -yaml_emitter_delete(yaml_emitter_t *emitter) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - BUFFER_DEL(emitter, emitter->buffer); - BUFFER_DEL(emitter, emitter->raw_buffer); - STACK_DEL(emitter, emitter->states); - while (!QUEUE_EMPTY(emitter, emitter->events)) { - yaml_event_delete(&DEQUEUE(emitter, emitter->events)); - } - QUEUE_DEL(emitter, emitter->events); - STACK_DEL(emitter, emitter->indents); - while (!STACK_EMPTY(empty, emitter->tag_directives)) { - yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); - yaml_free(tag_directive.handle); - yaml_free(tag_directive.prefix); - } - STACK_DEL(emitter, emitter->tag_directives); - yaml_free(emitter->anchors); - - memset(emitter, 0, sizeof(yaml_emitter_t)); -} - -/* - * String write handler. - */ - -static int -yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) -{ - yaml_emitter_t *emitter = data; - - if (emitter->output.string.size + *emitter->output.string.size_written - < size) { - memcpy(emitter->output.string.buffer - + *emitter->output.string.size_written, - buffer, - emitter->output.string.size - - *emitter->output.string.size_written); - *emitter->output.string.size_written = emitter->output.string.size; - return 0; - } - - memcpy(emitter->output.string.buffer - + *emitter->output.string.size_written, buffer, size); - *emitter->output.string.size_written += size; - return 1; -} - -/* - * File write handler. - */ - -static int -yaml_file_write_handler(void *data, unsigned char *buffer, size_t size) -{ - yaml_emitter_t *emitter = data; - - return (fwrite(buffer, 1, size, emitter->output.file) == size); -} -/* - * Set a string output. - */ - -YAML_DECLARE(void) -yaml_emitter_set_output_string(yaml_emitter_t *emitter, - unsigned char *output, size_t size, size_t *size_written) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - assert(!emitter->write_handler); /* You can set the output only once. */ - assert(output); /* Non-NULL output string expected. */ - - emitter->write_handler = yaml_string_write_handler; - emitter->write_handler_data = emitter; - - emitter->output.string.buffer = output; - emitter->output.string.size = size; - emitter->output.string.size_written = size_written; - *size_written = 0; -} - -/* - * Set a file output. - */ - -YAML_DECLARE(void) -yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - assert(!emitter->write_handler); /* You can set the output only once. */ - assert(file); /* Non-NULL file object expected. */ - - emitter->write_handler = yaml_file_write_handler; - emitter->write_handler_data = emitter; - - emitter->output.file = file; -} - -/* - * Set a generic output handler. - */ - -YAML_DECLARE(void) -yaml_emitter_set_output(yaml_emitter_t *emitter, - yaml_write_handler_t *handler, void *data) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - assert(!emitter->write_handler); /* You can set the output only once. */ - assert(handler); /* Non-NULL handler object expected. */ - - emitter->write_handler = handler; - emitter->write_handler_data = data; -} - -/* - * Set the output encoding. - */ - -YAML_DECLARE(void) -yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - assert(!emitter->encoding); /* You can set encoding only once. */ - - emitter->encoding = encoding; -} - -/* - * Set the canonical output style. - */ - -YAML_DECLARE(void) -yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - emitter->canonical = (canonical != 0); -} - -/* - * Set the indentation increment. - */ - -YAML_DECLARE(void) -yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - emitter->best_indent = (1 < indent && indent < 10) ? indent : 2; -} - -/* - * Set the preferred line width. - */ - -YAML_DECLARE(void) -yaml_emitter_set_width(yaml_emitter_t *emitter, int width) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - emitter->best_width = (width >= 0) ? width : -1; -} - -/* - * Set if unescaped non-ASCII characters are allowed. - */ - -YAML_DECLARE(void) -yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - emitter->unicode = (unicode != 0); -} - -/* - * Set the preferred line break character. - */ - -YAML_DECLARE(void) -yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break) -{ - assert(emitter); /* Non-NULL emitter object expected. */ - - emitter->line_break = line_break; -} - -/* - * Destroy a token object. - */ - -YAML_DECLARE(void) -yaml_token_delete(yaml_token_t *token) -{ - assert(token); /* Non-NULL token object expected. */ - - switch (token->type) - { - case YAML_TAG_DIRECTIVE_TOKEN: - yaml_free(token->data.tag_directive.handle); - yaml_free(token->data.tag_directive.prefix); - break; - - case YAML_ALIAS_TOKEN: - yaml_free(token->data.alias.value); - break; - - case YAML_ANCHOR_TOKEN: - yaml_free(token->data.anchor.value); - break; - - case YAML_TAG_TOKEN: - yaml_free(token->data.tag.handle); - yaml_free(token->data.tag.suffix); - break; - - case YAML_SCALAR_TOKEN: - yaml_free(token->data.scalar.value); - break; - - default: - break; - } - - memset(token, 0, sizeof(yaml_token_t)); -} - -/* - * Check if a string is a valid UTF-8 sequence. - * - * Check 'reader.c' for more details on UTF-8 encoding. - */ - -static int -yaml_check_utf8(yaml_char_t *start, size_t length) -{ - yaml_char_t *end = start+length; - yaml_char_t *pointer = start; - - while (pointer < end) { - unsigned char octet; - unsigned int width; - unsigned int value; - size_t k; - - octet = pointer[0]; - width = (octet & 0x80) == 0x00 ? 1 : - (octet & 0xE0) == 0xC0 ? 2 : - (octet & 0xF0) == 0xE0 ? 3 : - (octet & 0xF8) == 0xF0 ? 4 : 0; - value = (octet & 0x80) == 0x00 ? octet & 0x7F : - (octet & 0xE0) == 0xC0 ? octet & 0x1F : - (octet & 0xF0) == 0xE0 ? octet & 0x0F : - (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; - if (!width) return 0; - if (pointer+width > end) return 0; - for (k = 1; k < width; k ++) { - octet = pointer[k]; - if ((octet & 0xC0) != 0x80) return 0; - value = (value << 6) + (octet & 0x3F); - } - if (!((width == 1) || - (width == 2 && value >= 0x80) || - (width == 3 && value >= 0x800) || - (width == 4 && value >= 0x10000))) return 0; - - pointer += width; - } - - return 1; -} - -/* - * Create STREAM-START. - */ - -YAML_DECLARE(int) -yaml_stream_start_event_initialize(yaml_event_t *event, - yaml_encoding_t encoding) -{ - yaml_mark_t mark = { 0, 0, 0 }; - - assert(event); /* Non-NULL event object is expected. */ - - STREAM_START_EVENT_INIT(*event, encoding, mark, mark); - - return 1; -} - -/* - * Create STREAM-END. - */ - -YAML_DECLARE(int) -yaml_stream_end_event_initialize(yaml_event_t *event) -{ - yaml_mark_t mark = { 0, 0, 0 }; - - assert(event); /* Non-NULL event object is expected. */ - - STREAM_END_EVENT_INIT(*event, mark, mark); - - return 1; -} - -/* - * Create DOCUMENT-START. - */ - -YAML_DECLARE(int) -yaml_document_start_event_initialize(yaml_event_t *event, - yaml_version_directive_t *version_directive, - yaml_tag_directive_t *tag_directives_start, - yaml_tag_directive_t *tag_directives_end, - int implicit) -{ - struct { - yaml_error_type_t error; - } context; - yaml_mark_t mark = { 0, 0, 0 }; - yaml_version_directive_t *version_directive_copy = NULL; - struct { - yaml_tag_directive_t *start; - yaml_tag_directive_t *end; - yaml_tag_directive_t *top; - } tag_directives_copy = { NULL, NULL, NULL }; - yaml_tag_directive_t value = { NULL, NULL }; - - assert(event); /* Non-NULL event object is expected. */ - assert((tag_directives_start && tag_directives_end) || - (tag_directives_start == tag_directives_end)); - /* Valid tag directives are expected. */ - - if (version_directive) { - version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); - if (!version_directive_copy) goto error; - version_directive_copy->major = version_directive->major; - version_directive_copy->minor = version_directive->minor; - } - - if (tag_directives_start != tag_directives_end) { - yaml_tag_directive_t *tag_directive; - if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) - goto error; - for (tag_directive = tag_directives_start; - tag_directive != tag_directives_end; tag_directive ++) { - assert(tag_directive->handle); - assert(tag_directive->prefix); - if (!yaml_check_utf8(tag_directive->handle, - strlen((char *)tag_directive->handle))) - goto error; - if (!yaml_check_utf8(tag_directive->prefix, - strlen((char *)tag_directive->prefix))) - goto error; - value.handle = yaml_strdup(tag_directive->handle); - value.prefix = yaml_strdup(tag_directive->prefix); - if (!value.handle || !value.prefix) goto error; - if (!PUSH(&context, tag_directives_copy, value)) - goto error; - value.handle = NULL; - value.prefix = NULL; - } - } - - DOCUMENT_START_EVENT_INIT(*event, version_directive_copy, - tag_directives_copy.start, tag_directives_copy.top, - implicit, mark, mark); - - return 1; - -error: - yaml_free(version_directive_copy); - while (!STACK_EMPTY(context, tag_directives_copy)) { - yaml_tag_directive_t value = POP(context, tag_directives_copy); - yaml_free(value.handle); - yaml_free(value.prefix); - } - STACK_DEL(context, tag_directives_copy); - yaml_free(value.handle); - yaml_free(value.prefix); - - return 0; -} - -/* - * Create DOCUMENT-END. - */ - -YAML_DECLARE(int) -yaml_document_end_event_initialize(yaml_event_t *event, int implicit) -{ - yaml_mark_t mark = { 0, 0, 0 }; - - assert(event); /* Non-NULL emitter object is expected. */ - - DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark); - - return 1; -} - -/* - * Create ALIAS. - */ - -YAML_DECLARE(int) -yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor) -{ - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *anchor_copy = NULL; - - assert(event); /* Non-NULL event object is expected. */ - assert(anchor); /* Non-NULL anchor is expected. */ - - if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0; - - anchor_copy = yaml_strdup(anchor); - if (!anchor_copy) - return 0; - - ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark); - - return 1; -} - -/* - * Create SCALAR. - */ - -YAML_DECLARE(int) -yaml_scalar_event_initialize(yaml_event_t *event, - yaml_char_t *anchor, yaml_char_t *tag, - yaml_char_t *value, int length, - int plain_implicit, int quoted_implicit, - yaml_scalar_style_t style) -{ - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *anchor_copy = NULL; - yaml_char_t *tag_copy = NULL; - yaml_char_t *value_copy = NULL; - - assert(event); /* Non-NULL event object is expected. */ - assert(value); /* Non-NULL anchor is expected. */ - - if (anchor) { - if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; - anchor_copy = yaml_strdup(anchor); - if (!anchor_copy) goto error; - } - - if (tag) { - if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; - tag_copy = yaml_strdup(tag); - if (!tag_copy) goto error; - } - - if (length < 0) { - length = strlen((char *)value); - } - - if (!yaml_check_utf8(value, length)) goto error; - value_copy = yaml_malloc(length+1); - if (!value_copy) goto error; - memcpy(value_copy, value, length); - value_copy[length] = '\0'; - - SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length, - plain_implicit, quoted_implicit, style, mark, mark); - - return 1; - -error: - yaml_free(anchor_copy); - yaml_free(tag_copy); - yaml_free(value_copy); - - return 0; -} - -/* - * Create SEQUENCE-START. - */ - -YAML_DECLARE(int) -yaml_sequence_start_event_initialize(yaml_event_t *event, - yaml_char_t *anchor, yaml_char_t *tag, int implicit, - yaml_sequence_style_t style) -{ - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *anchor_copy = NULL; - yaml_char_t *tag_copy = NULL; - - assert(event); /* Non-NULL event object is expected. */ - - if (anchor) { - if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; - anchor_copy = yaml_strdup(anchor); - if (!anchor_copy) goto error; - } - - if (tag) { - if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; - tag_copy = yaml_strdup(tag); - if (!tag_copy) goto error; - } - - SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy, - implicit, style, mark, mark); - - return 1; - -error: - yaml_free(anchor_copy); - yaml_free(tag_copy); - - return 0; -} - -/* - * Create SEQUENCE-END. - */ - -YAML_DECLARE(int) -yaml_sequence_end_event_initialize(yaml_event_t *event) -{ - yaml_mark_t mark = { 0, 0, 0 }; - - assert(event); /* Non-NULL event object is expected. */ - - SEQUENCE_END_EVENT_INIT(*event, mark, mark); - - return 1; -} - -/* - * Create MAPPING-START. - */ - -YAML_DECLARE(int) -yaml_mapping_start_event_initialize(yaml_event_t *event, - yaml_char_t *anchor, yaml_char_t *tag, int implicit, - yaml_mapping_style_t style) -{ - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *anchor_copy = NULL; - yaml_char_t *tag_copy = NULL; - - assert(event); /* Non-NULL event object is expected. */ - - if (anchor) { - if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; - anchor_copy = yaml_strdup(anchor); - if (!anchor_copy) goto error; - } - - if (tag) { - if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; - tag_copy = yaml_strdup(tag); - if (!tag_copy) goto error; - } - - MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy, - implicit, style, mark, mark); - - return 1; - -error: - yaml_free(anchor_copy); - yaml_free(tag_copy); - - return 0; -} - -/* - * Create MAPPING-END. - */ - -YAML_DECLARE(int) -yaml_mapping_end_event_initialize(yaml_event_t *event) -{ - yaml_mark_t mark = { 0, 0, 0 }; - - assert(event); /* Non-NULL event object is expected. */ - - MAPPING_END_EVENT_INIT(*event, mark, mark); - - return 1; -} - -/* - * Destroy an event object. - */ - -YAML_DECLARE(void) -yaml_event_delete(yaml_event_t *event) -{ - yaml_tag_directive_t *tag_directive; - - assert(event); /* Non-NULL event object expected. */ - - switch (event->type) - { - case YAML_DOCUMENT_START_EVENT: - yaml_free(event->data.document_start.version_directive); - for (tag_directive = event->data.document_start.tag_directives.start; - tag_directive != event->data.document_start.tag_directives.end; - tag_directive++) { - yaml_free(tag_directive->handle); - yaml_free(tag_directive->prefix); - } - yaml_free(event->data.document_start.tag_directives.start); - break; - - case YAML_ALIAS_EVENT: - yaml_free(event->data.alias.anchor); - break; - - case YAML_SCALAR_EVENT: - yaml_free(event->data.scalar.anchor); - yaml_free(event->data.scalar.tag); - yaml_free(event->data.scalar.value); - break; - - case YAML_SEQUENCE_START_EVENT: - yaml_free(event->data.sequence_start.anchor); - yaml_free(event->data.sequence_start.tag); - break; - - case YAML_MAPPING_START_EVENT: - yaml_free(event->data.mapping_start.anchor); - yaml_free(event->data.mapping_start.tag); - break; - - default: - break; - } - - memset(event, 0, sizeof(yaml_event_t)); -} - -/* - * Create a document object. - */ - -YAML_DECLARE(int) -yaml_document_initialize(yaml_document_t *document, - yaml_version_directive_t *version_directive, - yaml_tag_directive_t *tag_directives_start, - yaml_tag_directive_t *tag_directives_end, - int start_implicit, int end_implicit) -{ - struct { - yaml_error_type_t error; - } context; - struct { - yaml_node_t *start; - yaml_node_t *end; - yaml_node_t *top; - } nodes = { NULL, NULL, NULL }; - yaml_version_directive_t *version_directive_copy = NULL; - struct { - yaml_tag_directive_t *start; - yaml_tag_directive_t *end; - yaml_tag_directive_t *top; - } tag_directives_copy = { NULL, NULL, NULL }; - yaml_tag_directive_t value = { NULL, NULL }; - yaml_mark_t mark = { 0, 0, 0 }; - - assert(document); /* Non-NULL document object is expected. */ - assert((tag_directives_start && tag_directives_end) || - (tag_directives_start == tag_directives_end)); - /* Valid tag directives are expected. */ - - if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error; - - if (version_directive) { - version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)); - if (!version_directive_copy) goto error; - version_directive_copy->major = version_directive->major; - version_directive_copy->minor = version_directive->minor; - } - - if (tag_directives_start != tag_directives_end) { - yaml_tag_directive_t *tag_directive; - if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) - goto error; - for (tag_directive = tag_directives_start; - tag_directive != tag_directives_end; tag_directive ++) { - assert(tag_directive->handle); - assert(tag_directive->prefix); - if (!yaml_check_utf8(tag_directive->handle, - strlen((char *)tag_directive->handle))) - goto error; - if (!yaml_check_utf8(tag_directive->prefix, - strlen((char *)tag_directive->prefix))) - goto error; - value.handle = yaml_strdup(tag_directive->handle); - value.prefix = yaml_strdup(tag_directive->prefix); - if (!value.handle || !value.prefix) goto error; - if (!PUSH(&context, tag_directives_copy, value)) - goto error; - value.handle = NULL; - value.prefix = NULL; - } - } - - DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy, - tag_directives_copy.start, tag_directives_copy.top, - start_implicit, end_implicit, mark, mark); - - return 1; - -error: - STACK_DEL(&context, nodes); - yaml_free(version_directive_copy); - while (!STACK_EMPTY(&context, tag_directives_copy)) { - yaml_tag_directive_t value = POP(&context, tag_directives_copy); - yaml_free(value.handle); - yaml_free(value.prefix); - } - STACK_DEL(&context, tag_directives_copy); - yaml_free(value.handle); - yaml_free(value.prefix); - - return 0; -} - -/* - * Destroy a document object. - */ - -YAML_DECLARE(void) -yaml_document_delete(yaml_document_t *document) -{ - struct { - yaml_error_type_t error; - } context; - yaml_tag_directive_t *tag_directive; - - context.error = YAML_NO_ERROR; /* Eliminate a compliler warning. */ - - assert(document); /* Non-NULL document object is expected. */ - - while (!STACK_EMPTY(&context, document->nodes)) { - yaml_node_t node = POP(&context, document->nodes); - yaml_free(node.tag); - switch (node.type) { - case YAML_SCALAR_NODE: - yaml_free(node.data.scalar.value); - break; - case YAML_SEQUENCE_NODE: - STACK_DEL(&context, node.data.sequence.items); - break; - case YAML_MAPPING_NODE: - STACK_DEL(&context, node.data.mapping.pairs); - break; - default: - assert(0); /* Should not happen. */ - } - } - STACK_DEL(&context, document->nodes); - - yaml_free(document->version_directive); - for (tag_directive = document->tag_directives.start; - tag_directive != document->tag_directives.end; - tag_directive++) { - yaml_free(tag_directive->handle); - yaml_free(tag_directive->prefix); - } - yaml_free(document->tag_directives.start); - - memset(document, 0, sizeof(yaml_document_t)); -} - -/** - * Get a document node. - */ - -YAML_DECLARE(yaml_node_t *) -yaml_document_get_node(yaml_document_t *document, int index) -{ - assert(document); /* Non-NULL document object is expected. */ - - if (index > 0 && document->nodes.start + index <= document->nodes.top) { - return document->nodes.start + index - 1; - } - return NULL; -} - -/** - * Get the root object. - */ - -YAML_DECLARE(yaml_node_t *) -yaml_document_get_root_node(yaml_document_t *document) -{ - assert(document); /* Non-NULL document object is expected. */ - - if (document->nodes.top != document->nodes.start) { - return document->nodes.start; - } - return NULL; -} - -/* - * Add a scalar node to a document. - */ - -YAML_DECLARE(int) -yaml_document_add_scalar(yaml_document_t *document, - yaml_char_t *tag, yaml_char_t *value, int length, - yaml_scalar_style_t style) -{ - struct { - yaml_error_type_t error; - } context; - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *tag_copy = NULL; - yaml_char_t *value_copy = NULL; - yaml_node_t node; - - assert(document); /* Non-NULL document object is expected. */ - assert(value); /* Non-NULL value is expected. */ - - if (!tag) { - tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG; - } - - if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; - tag_copy = yaml_strdup(tag); - if (!tag_copy) goto error; - - if (length < 0) { - length = strlen((char *)value); - } - - if (!yaml_check_utf8(value, length)) goto error; - value_copy = yaml_malloc(length+1); - if (!value_copy) goto error; - memcpy(value_copy, value, length); - value_copy[length] = '\0'; - - SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark); - if (!PUSH(&context, document->nodes, node)) goto error; - - return document->nodes.top - document->nodes.start; - -error: - yaml_free(tag_copy); - yaml_free(value_copy); - - return 0; -} - -/* - * Add a sequence node to a document. - */ - -YAML_DECLARE(int) -yaml_document_add_sequence(yaml_document_t *document, - yaml_char_t *tag, yaml_sequence_style_t style) -{ - struct { - yaml_error_type_t error; - } context; - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *tag_copy = NULL; - struct { - yaml_node_item_t *start; - yaml_node_item_t *end; - yaml_node_item_t *top; - } items = { NULL, NULL, NULL }; - yaml_node_t node; - - assert(document); /* Non-NULL document object is expected. */ - - if (!tag) { - tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG; - } - - if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; - tag_copy = yaml_strdup(tag); - if (!tag_copy) goto error; - - if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error; - - SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, - style, mark, mark); - if (!PUSH(&context, document->nodes, node)) goto error; - - return document->nodes.top - document->nodes.start; - -error: - STACK_DEL(&context, items); - yaml_free(tag_copy); - - return 0; -} - -/* - * Add a mapping node to a document. - */ - -YAML_DECLARE(int) -yaml_document_add_mapping(yaml_document_t *document, - yaml_char_t *tag, yaml_mapping_style_t style) -{ - struct { - yaml_error_type_t error; - } context; - yaml_mark_t mark = { 0, 0, 0 }; - yaml_char_t *tag_copy = NULL; - struct { - yaml_node_pair_t *start; - yaml_node_pair_t *end; - yaml_node_pair_t *top; - } pairs = { NULL, NULL, NULL }; - yaml_node_t node; - - assert(document); /* Non-NULL document object is expected. */ - - if (!tag) { - tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG; - } - - if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; - tag_copy = yaml_strdup(tag); - if (!tag_copy) goto error; - - if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error; - - MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, - style, mark, mark); - if (!PUSH(&context, document->nodes, node)) goto error; - - return document->nodes.top - document->nodes.start; - -error: - STACK_DEL(&context, pairs); - yaml_free(tag_copy); - - return 0; -} - -/* - * Append an item to a sequence node. - */ - -YAML_DECLARE(int) -yaml_document_append_sequence_item(yaml_document_t *document, - int sequence, int item) -{ - struct { - yaml_error_type_t error; - } context; - - assert(document); /* Non-NULL document is required. */ - assert(sequence > 0 - && document->nodes.start + sequence <= document->nodes.top); - /* Valid sequence id is required. */ - assert(document->nodes.start[sequence-1].type == YAML_SEQUENCE_NODE); - /* A sequence node is required. */ - assert(item > 0 && document->nodes.start + item <= document->nodes.top); - /* Valid item id is required. */ - - if (!PUSH(&context, - document->nodes.start[sequence-1].data.sequence.items, item)) - return 0; - - return 1; -} - -/* - * Append a pair of a key and a value to a mapping node. - */ - -YAML_DECLARE(int) -yaml_document_append_mapping_pair(yaml_document_t *document, - int mapping, int key, int value) -{ - struct { - yaml_error_type_t error; - } context; - - yaml_node_pair_t pair; - - assert(document); /* Non-NULL document is required. */ - assert(mapping > 0 - && document->nodes.start + mapping <= document->nodes.top); - /* Valid mapping id is required. */ - assert(document->nodes.start[mapping-1].type == YAML_MAPPING_NODE); - /* A mapping node is required. */ - assert(key > 0 && document->nodes.start + key <= document->nodes.top); - /* Valid key id is required. */ - assert(value > 0 && document->nodes.start + value <= document->nodes.top); - /* Valid value id is required. */ - - pair.key = key; - pair.value = value; - - if (!PUSH(&context, - document->nodes.start[mapping-1].data.mapping.pairs, pair)) - return 0; - - return 1; -} - - diff --git a/api_c.go b/api_c.go index 50b162d..9cdb1aa 100644 --- a/api_c.go +++ b/api_c.go @@ -91,7 +91,7 @@ func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) { func yaml_emitter_initialize(emitter *yaml_emitter_t) bool { // [Go] These should be initialized lazily instead. *emitter = yaml_emitter_t{ - buffer: make([]byte, 0, output_buffer_size), + buffer: make([]byte, output_buffer_size), raw_buffer: make([]byte, 0, output_raw_buffer_size), states: make([]yaml_emitter_state_t, 0, initial_stack_size), events: make([]yaml_event_t, 0, initial_queue_size), diff --git a/config.h b/config.h deleted file mode 100644 index 6a3d605..0000000 --- a/config.h +++ /dev/null @@ -1,4 +0,0 @@ -#define YAML_VERSION_STRING "0.1.3" -#define YAML_VERSION_MAJOR 0 -#define YAML_VERSION_MINOR 1 -#define YAML_VERSION_PATCH 3 diff --git a/emitter.c b/emitter.c deleted file mode 100644 index c4b56a2..0000000 --- a/emitter.c +++ /dev/null @@ -1,2329 +0,0 @@ - -#include "yaml_private.h" - -/* - * Flush the buffer if needed. - */ - -#define FLUSH(emitter) \ - ((emitter->buffer.pointer+5 < emitter->buffer.end) \ - || yaml_emitter_flush(emitter)) - -/* - * Put a character to the output buffer. - */ - -#define PUT(emitter,value) \ - (FLUSH(emitter) \ - && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \ - emitter->column ++, \ - 1)) - -/* - * Put a line break to the output buffer. - */ - -#define PUT_BREAK(emitter) \ - (FLUSH(emitter) \ - && ((emitter->line_break == YAML_CR_BREAK ? \ - (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \ - emitter->line_break == YAML_LN_BREAK ? \ - (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \ - emitter->line_break == YAML_CRLN_BREAK ? \ - (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \ - *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \ - emitter->column = 0, \ - emitter->line ++, \ - 1)) - -/* - * Copy a character from a string into buffer. - */ - -#define WRITE(emitter,string) \ - (FLUSH(emitter) \ - && (COPY(emitter->buffer,string), \ - emitter->column ++, \ - 1)) - -/* - * Copy a line break character from a string into buffer. - */ - -#define WRITE_BREAK(emitter,string) \ - (FLUSH(emitter) \ - && (CHECK(string,'\n') ? \ - (PUT_BREAK(emitter), \ - string.pointer ++, \ - 1) : \ - (COPY(emitter->buffer,string), \ - emitter->column = 0, \ - emitter->line ++, \ - 1))) - -/* - * API functions. - */ - -YAML_DECLARE(int) -yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); - -/* - * Utility functions. - */ - -static int -yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem); - -static int -yaml_emitter_need_more_events(yaml_emitter_t *emitter); - -static int -yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, - yaml_tag_directive_t value, int allow_duplicates); - -static int -yaml_emitter_increase_indent(yaml_emitter_t *emitter, - int flow, int indentless); - -/* - * State functions. - */ - -static int -yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event); - -static int -yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, - yaml_event_t *event); - -static int -yaml_emitter_emit_document_start(yaml_emitter_t *emitter, - yaml_event_t *event, int first); - -static int -yaml_emitter_emit_document_content(yaml_emitter_t *emitter, - yaml_event_t *event); - -static int -yaml_emitter_emit_document_end(yaml_emitter_t *emitter, - yaml_event_t *event); - -static int -yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, - yaml_event_t *event, int first); - -static int -yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, - yaml_event_t *event, int first); - -static int -yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, - yaml_event_t *event, int simple); - -static int -yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, - yaml_event_t *event, int first); - -static int -yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, - yaml_event_t *event, int first); - -static int -yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, - yaml_event_t *event, int simple); - -static int -yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, - int root, int sequence, int mapping, int simple_key); - -static int -yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event); - -static int -yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event); - -static int -yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event); - -static int -yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event); - -/* - * Checkers. - */ - -static int -yaml_emitter_check_empty_document(yaml_emitter_t *emitter); - -static int -yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter); - -static int -yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter); - -static int -yaml_emitter_check_simple_key(yaml_emitter_t *emitter); - -static int -yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event); - -/* - * Processors. - */ - -static int -yaml_emitter_process_anchor(yaml_emitter_t *emitter); - -static int -yaml_emitter_process_tag(yaml_emitter_t *emitter); - -static int -yaml_emitter_process_scalar(yaml_emitter_t *emitter); - -/* - * Analyzers. - */ - -static int -yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, - yaml_version_directive_t version_directive); - -static int -yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, - yaml_tag_directive_t tag_directive); - -static int -yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, - yaml_char_t *anchor, int alias); - -static int -yaml_emitter_analyze_tag(yaml_emitter_t *emitter, - yaml_char_t *tag); - -static int -yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length); - -static int -yaml_emitter_analyze_event(yaml_emitter_t *emitter, - yaml_event_t *event); - -/* - * Writers. - */ - -static int -yaml_emitter_write_bom(yaml_emitter_t *emitter); - -static int -yaml_emitter_write_indent(yaml_emitter_t *emitter); - -static int -yaml_emitter_write_indicator(yaml_emitter_t *emitter, - char *indicator, int need_whitespace, - int is_whitespace, int is_indention); - -static int -yaml_emitter_write_anchor(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length); - -static int -yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length); - -static int -yaml_emitter_write_tag_content(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int need_whitespace); - -static int -yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int allow_breaks); - -static int -yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int allow_breaks); - -static int -yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int allow_breaks); - -static int -yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, - yaml_string_t string); - -static int -yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length); - -static int -yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length); - -/* - * Set an emitter error and return 0. - */ - -static int -yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem) -{ - emitter->error = YAML_EMITTER_ERROR; - emitter->problem = problem; - - return 0; -} - -/* - * Emit an event. - */ - -YAML_DECLARE(int) -yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) -{ - if (!ENQUEUE(emitter, emitter->events, *event)) { - yaml_event_delete(event); - return 0; - } - - while (!yaml_emitter_need_more_events(emitter)) { - if (!yaml_emitter_analyze_event(emitter, emitter->events.head)) - return 0; - if (!yaml_emitter_state_machine(emitter, emitter->events.head)) - return 0; - yaml_event_delete(&DEQUEUE(emitter, emitter->events)); - } - - return 1; -} - -/* - * Check if we need to accumulate more events before emitting. - * - * We accumulate extra - * - 1 event for DOCUMENT-START - * - 2 events for SEQUENCE-START - * - 3 events for MAPPING-START - */ - -static int -yaml_emitter_need_more_events(yaml_emitter_t *emitter) -{ - int level = 0; - int accumulate = 0; - yaml_event_t *event; - - if (QUEUE_EMPTY(emitter, emitter->events)) - return 1; - - switch (emitter->events.head->type) { - case YAML_DOCUMENT_START_EVENT: - accumulate = 1; - break; - case YAML_SEQUENCE_START_EVENT: - accumulate = 2; - break; - case YAML_MAPPING_START_EVENT: - accumulate = 3; - break; - default: - return 0; - } - - if (emitter->events.tail - emitter->events.head > accumulate) - return 0; - - for (event = emitter->events.head; event != emitter->events.tail; event ++) { - switch (event->type) { - case YAML_STREAM_START_EVENT: - case YAML_DOCUMENT_START_EVENT: - case YAML_SEQUENCE_START_EVENT: - case YAML_MAPPING_START_EVENT: - level += 1; - break; - case YAML_STREAM_END_EVENT: - case YAML_DOCUMENT_END_EVENT: - case YAML_SEQUENCE_END_EVENT: - case YAML_MAPPING_END_EVENT: - level -= 1; - break; - default: - break; - } - if (!level) - return 0; - } - - return 1; -} - -/* - * Append a directive to the directives stack. - */ - -static int -yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, - yaml_tag_directive_t value, int allow_duplicates) -{ - yaml_tag_directive_t *tag_directive; - yaml_tag_directive_t copy = { NULL, NULL }; - - for (tag_directive = emitter->tag_directives.start; - tag_directive != emitter->tag_directives.top; tag_directive ++) { - if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { - if (allow_duplicates) - return 1; - return yaml_emitter_set_emitter_error(emitter, - "duplicate %TAG directive"); - } - } - - copy.handle = yaml_strdup(value.handle); - copy.prefix = yaml_strdup(value.prefix); - if (!copy.handle || !copy.prefix) { - emitter->error = YAML_MEMORY_ERROR; - goto error; - } - - if (!PUSH(emitter, emitter->tag_directives, copy)) - goto error; - - return 1; - -error: - yaml_free(copy.handle); - yaml_free(copy.prefix); - return 0; -} - -/* - * Increase the indentation level. - */ - -static int -yaml_emitter_increase_indent(yaml_emitter_t *emitter, - int flow, int indentless) -{ - if (!PUSH(emitter, emitter->indents, emitter->indent)) - return 0; - - if (emitter->indent < 0) { - emitter->indent = flow ? emitter->best_indent : 0; - } - else if (!indentless) { - emitter->indent += emitter->best_indent; - } - - return 1; -} - -/* - * State dispatcher. - */ - -static int -yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event) -{ - switch (emitter->state) - { - case YAML_EMIT_STREAM_START_STATE: - return yaml_emitter_emit_stream_start(emitter, event); - - case YAML_EMIT_FIRST_DOCUMENT_START_STATE: - return yaml_emitter_emit_document_start(emitter, event, 1); - - case YAML_EMIT_DOCUMENT_START_STATE: - return yaml_emitter_emit_document_start(emitter, event, 0); - - case YAML_EMIT_DOCUMENT_CONTENT_STATE: - return yaml_emitter_emit_document_content(emitter, event); - - case YAML_EMIT_DOCUMENT_END_STATE: - return yaml_emitter_emit_document_end(emitter, event); - - case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: - return yaml_emitter_emit_flow_sequence_item(emitter, event, 1); - - case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE: - return yaml_emitter_emit_flow_sequence_item(emitter, event, 0); - - case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: - return yaml_emitter_emit_flow_mapping_key(emitter, event, 1); - - case YAML_EMIT_FLOW_MAPPING_KEY_STATE: - return yaml_emitter_emit_flow_mapping_key(emitter, event, 0); - - case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: - return yaml_emitter_emit_flow_mapping_value(emitter, event, 1); - - case YAML_EMIT_FLOW_MAPPING_VALUE_STATE: - return yaml_emitter_emit_flow_mapping_value(emitter, event, 0); - - case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: - return yaml_emitter_emit_block_sequence_item(emitter, event, 1); - - case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE: - return yaml_emitter_emit_block_sequence_item(emitter, event, 0); - - case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: - return yaml_emitter_emit_block_mapping_key(emitter, event, 1); - - case YAML_EMIT_BLOCK_MAPPING_KEY_STATE: - return yaml_emitter_emit_block_mapping_key(emitter, event, 0); - - case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: - return yaml_emitter_emit_block_mapping_value(emitter, event, 1); - - case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE: - return yaml_emitter_emit_block_mapping_value(emitter, event, 0); - - case YAML_EMIT_END_STATE: - return yaml_emitter_set_emitter_error(emitter, - "expected nothing after STREAM-END"); - - default: - assert(1); /* Invalid state. */ - } - - return 0; -} - -/* - * Expect STREAM-START. - */ - -static int -yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, - yaml_event_t *event) -{ - if (event->type == YAML_STREAM_START_EVENT) - { - if (!emitter->encoding) { - emitter->encoding = event->data.stream_start.encoding; - } - - if (!emitter->encoding) { - emitter->encoding = YAML_UTF8_ENCODING; - } - - if (emitter->best_indent < 2 || emitter->best_indent > 9) { - emitter->best_indent = 2; - } - - if (emitter->best_width >= 0 - && emitter->best_width <= emitter->best_indent*2) { - emitter->best_width = 80; - } - - if (emitter->best_width < 0) { - emitter->best_width = INT_MAX; - } - - if (!emitter->line_break) { - emitter->line_break = YAML_LN_BREAK; - } - - emitter->indent = -1; - - emitter->line = 0; - emitter->column = 0; - emitter->whitespace = 1; - emitter->indention = 1; - - if (emitter->encoding != YAML_UTF8_ENCODING) { - if (!yaml_emitter_write_bom(emitter)) - return 0; - } - - emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE; - - return 1; - } - - return yaml_emitter_set_emitter_error(emitter, - "expected STREAM-START"); -} - -/* - * Expect DOCUMENT-START or STREAM-END. - */ - -static int -yaml_emitter_emit_document_start(yaml_emitter_t *emitter, - yaml_event_t *event, int first) -{ - if (event->type == YAML_DOCUMENT_START_EVENT) - { - yaml_tag_directive_t default_tag_directives[] = { - {(yaml_char_t *)"!", (yaml_char_t *)"!"}, - {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"}, - {NULL, NULL} - }; - yaml_tag_directive_t *tag_directive; - int implicit; - - if (event->data.document_start.version_directive) { - if (!yaml_emitter_analyze_version_directive(emitter, - *event->data.document_start.version_directive)) - return 0; - } - - for (tag_directive = event->data.document_start.tag_directives.start; - tag_directive != event->data.document_start.tag_directives.end; - tag_directive ++) { - if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive)) - return 0; - if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0)) - return 0; - } - - for (tag_directive = default_tag_directives; - tag_directive->handle; tag_directive ++) { - if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1)) - return 0; - } - - implicit = event->data.document_start.implicit; - if (!first || emitter->canonical) { - implicit = 0; - } - - if ((event->data.document_start.version_directive || - (event->data.document_start.tag_directives.start - != event->data.document_start.tag_directives.end)) && - emitter->open_ended) - { - if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - - if (event->data.document_start.version_directive) { - implicit = 0; - if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - - if (event->data.document_start.tag_directives.start - != event->data.document_start.tag_directives.end) { - implicit = 0; - for (tag_directive = event->data.document_start.tag_directives.start; - tag_directive != event->data.document_start.tag_directives.end; - tag_directive ++) { - if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle, - strlen((char *)tag_directive->handle))) - return 0; - if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix, - strlen((char *)tag_directive->prefix), 1)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - } - - if (yaml_emitter_check_empty_document(emitter)) { - implicit = 0; - } - - if (!implicit) { - if (!yaml_emitter_write_indent(emitter)) - return 0; - if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0)) - return 0; - if (emitter->canonical) { - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - } - - emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE; - - return 1; - } - - else if (event->type == YAML_STREAM_END_EVENT) - { - if (emitter->open_ended) - { - if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - - if (!yaml_emitter_flush(emitter)) - return 0; - - emitter->state = YAML_EMIT_END_STATE; - - return 1; - } - - return yaml_emitter_set_emitter_error(emitter, - "expected DOCUMENT-START or STREAM-END"); -} - -/* - * Expect the root node. - */ - -static int -yaml_emitter_emit_document_content(yaml_emitter_t *emitter, - yaml_event_t *event) -{ - if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0); -} - -/* - * Expect DOCUMENT-END. - */ - -static int -yaml_emitter_emit_document_end(yaml_emitter_t *emitter, - yaml_event_t *event) -{ - if (event->type == YAML_DOCUMENT_END_EVENT) - { - if (!yaml_emitter_write_indent(emitter)) - return 0; - if (!event->data.document_end.implicit) { - if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - if (!yaml_emitter_flush(emitter)) - return 0; - - emitter->state = YAML_EMIT_DOCUMENT_START_STATE; - - while (!STACK_EMPTY(emitter, emitter->tag_directives)) { - yaml_tag_directive_t tag_directive = POP(emitter, - emitter->tag_directives); - yaml_free(tag_directive.handle); - yaml_free(tag_directive.prefix); - } - - return 1; - } - - return yaml_emitter_set_emitter_error(emitter, - "expected DOCUMENT-END"); -} - -/* - * - * Expect a flow item node. - */ - -static int -yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, - yaml_event_t *event, int first) -{ - if (first) - { - if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0)) - return 0; - if (!yaml_emitter_increase_indent(emitter, 1, 0)) - return 0; - emitter->flow_level ++; - } - - if (event->type == YAML_SEQUENCE_END_EVENT) - { - emitter->flow_level --; - emitter->indent = POP(emitter, emitter->indents); - if (emitter->canonical && !first) { - if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0)) - return 0; - emitter->state = POP(emitter, emitter->states); - - return 1; - } - - if (!first) { - if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) - return 0; - } - - if (emitter->canonical || emitter->column > emitter->best_width) { - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); -} - -/* - * Expect a flow key node. - */ - -static int -yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, - yaml_event_t *event, int first) -{ - if (first) - { - if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0)) - return 0; - if (!yaml_emitter_increase_indent(emitter, 1, 0)) - return 0; - emitter->flow_level ++; - } - - if (event->type == YAML_MAPPING_END_EVENT) - { - emitter->flow_level --; - emitter->indent = POP(emitter, emitter->indents); - if (emitter->canonical && !first) { - if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) - return 0; - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0)) - return 0; - emitter->state = POP(emitter, emitter->states); - - return 1; - } - - if (!first) { - if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) - return 0; - } - if (emitter->canonical || emitter->column > emitter->best_width) { - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - - if (!emitter->canonical && yaml_emitter_check_simple_key(emitter)) - { - if (!PUSH(emitter, emitter->states, - YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); - } - else - { - if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0)) - return 0; - if (!PUSH(emitter, emitter->states, - YAML_EMIT_FLOW_MAPPING_VALUE_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); - } -} - -/* - * Expect a flow value node. - */ - -static int -yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, - yaml_event_t *event, int simple) -{ - if (simple) { - if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) - return 0; - } - else { - if (emitter->canonical || emitter->column > emitter->best_width) { - if (!yaml_emitter_write_indent(emitter)) - return 0; - } - if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0)) - return 0; - } - if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE)) - return 0; - return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); -} - -/* - * Expect a block item node. - */ - -static int -yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, - yaml_event_t *event, int first) -{ - if (first) - { - if (!yaml_emitter_increase_indent(emitter, 0, - (emitter->mapping_context && !emitter->indention))) - return 0; - } - - if (event->type == YAML_SEQUENCE_END_EVENT) - { - emitter->indent = POP(emitter, emitter->indents); - emitter->state = POP(emitter, emitter->states); - - return 1; - } - - if (!yaml_emitter_write_indent(emitter)) - return 0; - if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1)) - return 0; - if (!PUSH(emitter, emitter->states, - YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); -} - -/* - * Expect a block key node. - */ - -static int -yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, - yaml_event_t *event, int first) -{ - if (first) - { - if (!yaml_emitter_increase_indent(emitter, 0, 0)) - return 0; - } - - if (event->type == YAML_MAPPING_END_EVENT) - { - emitter->indent = POP(emitter, emitter->indents); - emitter->state = POP(emitter, emitter->states); - - return 1; - } - - if (!yaml_emitter_write_indent(emitter)) - return 0; - - if (yaml_emitter_check_simple_key(emitter)) - { - if (!PUSH(emitter, emitter->states, - YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); - } - else - { - if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1)) - return 0; - if (!PUSH(emitter, emitter->states, - YAML_EMIT_BLOCK_MAPPING_VALUE_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); - } -} - -/* - * Expect a block value node. - */ - -static int -yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, - yaml_event_t *event, int simple) -{ - if (simple) { - if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) - return 0; - } - else { - if (!yaml_emitter_write_indent(emitter)) - return 0; - if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1)) - return 0; - } - if (!PUSH(emitter, emitter->states, - YAML_EMIT_BLOCK_MAPPING_KEY_STATE)) - return 0; - - return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); -} - -/* - * Expect a node. - */ - -static int -yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, - int root, int sequence, int mapping, int simple_key) -{ - emitter->root_context = root; - emitter->sequence_context = sequence; - emitter->mapping_context = mapping; - emitter->simple_key_context = simple_key; - - switch (event->type) - { - case YAML_ALIAS_EVENT: - return yaml_emitter_emit_alias(emitter, event); - - case YAML_SCALAR_EVENT: - return yaml_emitter_emit_scalar(emitter, event); - - case YAML_SEQUENCE_START_EVENT: - return yaml_emitter_emit_sequence_start(emitter, event); - - case YAML_MAPPING_START_EVENT: - return yaml_emitter_emit_mapping_start(emitter, event); - - default: - return yaml_emitter_set_emitter_error(emitter, - "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); - } - - return 0; -} - -/* - * Expect ALIAS. - */ - -static int -yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event) -{ - if (!yaml_emitter_process_anchor(emitter)) - return 0; - emitter->state = POP(emitter, emitter->states); - - return 1; -} - -/* - * Expect SCALAR. - */ - -static int -yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event) -{ - if (!yaml_emitter_select_scalar_style(emitter, event)) - return 0; - if (!yaml_emitter_process_anchor(emitter)) - return 0; - if (!yaml_emitter_process_tag(emitter)) - return 0; - if (!yaml_emitter_increase_indent(emitter, 1, 0)) - return 0; - if (!yaml_emitter_process_scalar(emitter)) - return 0; - emitter->indent = POP(emitter, emitter->indents); - emitter->state = POP(emitter, emitter->states); - - return 1; -} - -/* - * Expect SEQUENCE-START. - */ - -static int -yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event) -{ - if (!yaml_emitter_process_anchor(emitter)) - return 0; - if (!yaml_emitter_process_tag(emitter)) - return 0; - - if (emitter->flow_level || emitter->canonical - || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE - || yaml_emitter_check_empty_sequence(emitter)) { - emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE; - } - else { - emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE; - } - - return 1; -} - -/* - * Expect MAPPING-START. - */ - -static int -yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) -{ - if (!yaml_emitter_process_anchor(emitter)) - return 0; - if (!yaml_emitter_process_tag(emitter)) - return 0; - - if (emitter->flow_level || emitter->canonical - || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE - || yaml_emitter_check_empty_mapping(emitter)) { - emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE; - } - else { - emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE; - } - - return 1; -} - -/* - * Check if the document content is an empty scalar. - */ - -static int -yaml_emitter_check_empty_document(yaml_emitter_t *emitter) -{ - return 0; -} - -/* - * Check if the next events represent an empty sequence. - */ - -static int -yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter) -{ - if (emitter->events.tail - emitter->events.head < 2) - return 0; - - return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT - && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT); -} - -/* - * Check if the next events represent an empty mapping. - */ - -static int -yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter) -{ - if (emitter->events.tail - emitter->events.head < 2) - return 0; - - return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT - && emitter->events.head[1].type == YAML_MAPPING_END_EVENT); -} - -/* - * Check if the next node can be expressed as a simple key. - */ - -static int -yaml_emitter_check_simple_key(yaml_emitter_t *emitter) -{ - yaml_event_t *event = emitter->events.head; - size_t length = 0; - - switch (event->type) - { - case YAML_ALIAS_EVENT: - length += emitter->anchor_data.anchor_length; - break; - - case YAML_SCALAR_EVENT: - if (emitter->scalar_data.multiline) - return 0; - length += emitter->anchor_data.anchor_length - + emitter->tag_data.handle_length - + emitter->tag_data.suffix_length - + emitter->scalar_data.length; - break; - - case YAML_SEQUENCE_START_EVENT: - if (!yaml_emitter_check_empty_sequence(emitter)) - return 0; - length += emitter->anchor_data.anchor_length - + emitter->tag_data.handle_length - + emitter->tag_data.suffix_length; - break; - - case YAML_MAPPING_START_EVENT: - if (!yaml_emitter_check_empty_mapping(emitter)) - return 0; - length += emitter->anchor_data.anchor_length - + emitter->tag_data.handle_length - + emitter->tag_data.suffix_length; - break; - - default: - return 0; - } - - if (length > 128) - return 0; - - return 1; -} - -/* - * Determine an acceptable scalar style. - */ - -static int -yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event) -{ - yaml_scalar_style_t style = event->data.scalar.style; - int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix); - - if (no_tag && !event->data.scalar.plain_implicit - && !event->data.scalar.quoted_implicit) { - return yaml_emitter_set_emitter_error(emitter, - "neither tag nor implicit flags are specified"); - } - - if (style == YAML_ANY_SCALAR_STYLE) - style = YAML_PLAIN_SCALAR_STYLE; - - if (emitter->canonical) - style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; - - if (emitter->simple_key_context && emitter->scalar_data.multiline) - style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; - - if (style == YAML_PLAIN_SCALAR_STYLE) - { - if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed) - || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed)) - style = YAML_SINGLE_QUOTED_SCALAR_STYLE; - if (!emitter->scalar_data.length - && (emitter->flow_level || emitter->simple_key_context)) - style = YAML_SINGLE_QUOTED_SCALAR_STYLE; - if (no_tag && !event->data.scalar.plain_implicit) - style = YAML_SINGLE_QUOTED_SCALAR_STYLE; - } - - if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE) - { - if (!emitter->scalar_data.single_quoted_allowed) - style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; - } - - if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE) - { - if (!emitter->scalar_data.block_allowed - || emitter->flow_level || emitter->simple_key_context) - style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; - } - - if (no_tag && !event->data.scalar.quoted_implicit - && style != YAML_PLAIN_SCALAR_STYLE) - { - emitter->tag_data.handle = (yaml_char_t *)"!"; - emitter->tag_data.handle_length = 1; - } - - emitter->scalar_data.style = style; - - return 1; -} - -/* - * Write an achor. - */ - -static int -yaml_emitter_process_anchor(yaml_emitter_t *emitter) -{ - if (!emitter->anchor_data.anchor) - return 1; - - if (!yaml_emitter_write_indicator(emitter, - (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0)) - return 0; - - return yaml_emitter_write_anchor(emitter, - emitter->anchor_data.anchor, emitter->anchor_data.anchor_length); -} - -/* - * Write a tag. - */ - -static int -yaml_emitter_process_tag(yaml_emitter_t *emitter) -{ - if (!emitter->tag_data.handle && !emitter->tag_data.suffix) - return 1; - - if (emitter->tag_data.handle) - { - if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle, - emitter->tag_data.handle_length)) - return 0; - if (emitter->tag_data.suffix) { - if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, - emitter->tag_data.suffix_length, 0)) - return 0; - } - } - else - { - if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, - emitter->tag_data.suffix_length, 0)) - return 0; - if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0)) - return 0; - } - - return 1; -} - -/* - * Write a scalar. - */ - -static int -yaml_emitter_process_scalar(yaml_emitter_t *emitter) -{ - switch (emitter->scalar_data.style) - { - case YAML_PLAIN_SCALAR_STYLE: - return yaml_emitter_write_plain_scalar(emitter, - emitter->scalar_data.value, emitter->scalar_data.length, - !emitter->simple_key_context); - - case YAML_SINGLE_QUOTED_SCALAR_STYLE: - return yaml_emitter_write_single_quoted_scalar(emitter, - emitter->scalar_data.value, emitter->scalar_data.length, - !emitter->simple_key_context); - - case YAML_DOUBLE_QUOTED_SCALAR_STYLE: - return yaml_emitter_write_double_quoted_scalar(emitter, - emitter->scalar_data.value, emitter->scalar_data.length, - !emitter->simple_key_context); - - case YAML_LITERAL_SCALAR_STYLE: - return yaml_emitter_write_literal_scalar(emitter, - emitter->scalar_data.value, emitter->scalar_data.length); - - case YAML_FOLDED_SCALAR_STYLE: - return yaml_emitter_write_folded_scalar(emitter, - emitter->scalar_data.value, emitter->scalar_data.length); - - default: - assert(1); /* Impossible. */ - } - - return 0; -} - -/* - * Check if a %YAML directive is valid. - */ - -static int -yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, - yaml_version_directive_t version_directive) -{ - if (version_directive.major != 1 || version_directive.minor != 1) { - return yaml_emitter_set_emitter_error(emitter, - "incompatible %YAML directive"); - } - - return 1; -} - -/* - * Check if a %TAG directive is valid. - */ - -static int -yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, - yaml_tag_directive_t tag_directive) -{ - yaml_string_t handle; - yaml_string_t prefix; - size_t handle_length; - size_t prefix_length; - - handle_length = strlen((char *)tag_directive.handle); - prefix_length = strlen((char *)tag_directive.prefix); - STRING_ASSIGN(handle, tag_directive.handle, handle_length); - STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length); - - if (handle.start == handle.end) { - return yaml_emitter_set_emitter_error(emitter, - "tag handle must not be empty"); - } - - if (handle.start[0] != '!') { - return yaml_emitter_set_emitter_error(emitter, - "tag handle must start with '!'"); - } - - if (handle.end[-1] != '!') { - return yaml_emitter_set_emitter_error(emitter, - "tag handle must end with '!'"); - } - - handle.pointer ++; - - while (handle.pointer < handle.end-1) { - if (!IS_ALPHA(handle)) { - return yaml_emitter_set_emitter_error(emitter, - "tag handle must contain alphanumerical characters only"); - } - MOVE(handle); - } - - if (prefix.start == prefix.end) { - return yaml_emitter_set_emitter_error(emitter, - "tag prefix must not be empty"); - } - - return 1; -} - -/* - * Check if an anchor is valid. - */ - -static int -yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, - yaml_char_t *anchor, int alias) -{ - size_t anchor_length; - yaml_string_t string; - - anchor_length = strlen((char *)anchor); - STRING_ASSIGN(string, anchor, anchor_length); - - if (string.start == string.end) { - return yaml_emitter_set_emitter_error(emitter, alias ? - "alias value must not be empty" : - "anchor value must not be empty"); - } - - while (string.pointer != string.end) { - if (!IS_ALPHA(string)) { - return yaml_emitter_set_emitter_error(emitter, alias ? - "alias value must contain alphanumerical characters only" : - "anchor value must contain alphanumerical characters only"); - } - MOVE(string); - } - - emitter->anchor_data.anchor = string.start; - emitter->anchor_data.anchor_length = string.end - string.start; - emitter->anchor_data.alias = alias; - - return 1; -} - -/* - * Check if a tag is valid. - */ - -static int -yaml_emitter_analyze_tag(yaml_emitter_t *emitter, - yaml_char_t *tag) -{ - size_t tag_length; - yaml_string_t string; - yaml_tag_directive_t *tag_directive; - - tag_length = strlen((char *)tag); - STRING_ASSIGN(string, tag, tag_length); - - if (string.start == string.end) { - return yaml_emitter_set_emitter_error(emitter, - "tag value must not be empty"); - } - - for (tag_directive = emitter->tag_directives.start; - tag_directive != emitter->tag_directives.top; tag_directive ++) { - size_t prefix_length = strlen((char *)tag_directive->prefix); - if (prefix_length < (size_t)(string.end - string.start) - && strncmp((char *)tag_directive->prefix, (char *)string.start, - prefix_length) == 0) - { - emitter->tag_data.handle = tag_directive->handle; - emitter->tag_data.handle_length = - strlen((char *)tag_directive->handle); - emitter->tag_data.suffix = string.start + prefix_length; - emitter->tag_data.suffix_length = - (string.end - string.start) - prefix_length; - return 1; - } - } - - emitter->tag_data.suffix = string.start; - emitter->tag_data.suffix_length = string.end - string.start; - - return 1; -} - -/* - * Check if a scalar is valid. - */ - -static int -yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length) -{ - yaml_string_t string; - - int block_indicators = 0; - int flow_indicators = 0; - int line_breaks = 0; - int special_characters = 0; - - int leading_space = 0; - int leading_break = 0; - int trailing_space = 0; - int trailing_break = 0; - int break_space = 0; - int space_break = 0; - - int preceeded_by_whitespace = 0; - int followed_by_whitespace = 0; - int previous_space = 0; - int previous_break = 0; - - STRING_ASSIGN(string, value, length); - - emitter->scalar_data.value = value; - emitter->scalar_data.length = length; - - if (string.start == string.end) - { - emitter->scalar_data.multiline = 0; - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 1; - emitter->scalar_data.single_quoted_allowed = 1; - emitter->scalar_data.block_allowed = 0; - - return 1; - } - - if ((CHECK_AT(string, '-', 0) - && CHECK_AT(string, '-', 1) - && CHECK_AT(string, '-', 2)) - || (CHECK_AT(string, '.', 0) - && CHECK_AT(string, '.', 1) - && CHECK_AT(string, '.', 2))) { - block_indicators = 1; - flow_indicators = 1; - } - - preceeded_by_whitespace = 1; - followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); - - while (string.pointer != string.end) - { - if (string.start == string.pointer) - { - if (CHECK(string, '#') || CHECK(string, ',') - || CHECK(string, '[') || CHECK(string, ']') - || CHECK(string, '{') || CHECK(string, '}') - || CHECK(string, '&') || CHECK(string, '*') - || CHECK(string, '!') || CHECK(string, '|') - || CHECK(string, '>') || CHECK(string, '\'') - || CHECK(string, '"') || CHECK(string, '%') - || CHECK(string, '@') || CHECK(string, '`')) { - flow_indicators = 1; - block_indicators = 1; - } - - if (CHECK(string, '?') || CHECK(string, ':')) { - flow_indicators = 1; - if (followed_by_whitespace) { - block_indicators = 1; - } - } - - if (CHECK(string, '-') && followed_by_whitespace) { - flow_indicators = 1; - block_indicators = 1; - } - } - else - { - if (CHECK(string, ',') || CHECK(string, '?') - || CHECK(string, '[') || CHECK(string, ']') - || CHECK(string, '{') || CHECK(string, '}')) { - flow_indicators = 1; - } - - if (CHECK(string, ':')) { - flow_indicators = 1; - if (followed_by_whitespace) { - block_indicators = 1; - } - } - - if (CHECK(string, '#') && preceeded_by_whitespace) { - flow_indicators = 1; - block_indicators = 1; - } - } - - if (!IS_PRINTABLE(string) - || (!IS_ASCII(string) && !emitter->unicode)) { - special_characters = 1; - } - - if (IS_BREAK(string)) { - line_breaks = 1; - } - - if (IS_SPACE(string)) - { - if (string.start == string.pointer) { - leading_space = 1; - } - if (string.pointer+WIDTH(string) == string.end) { - trailing_space = 1; - } - if (previous_break) { - break_space = 1; - } - previous_space = 1; - previous_break = 0; - } - else if (IS_BREAK(string)) - { - if (string.start == string.pointer) { - leading_break = 1; - } - if (string.pointer+WIDTH(string) == string.end) { - trailing_break = 1; - } - if (previous_space) { - space_break = 1; - } - previous_space = 0; - previous_break = 1; - } - else - { - previous_space = 0; - previous_break = 0; - } - - preceeded_by_whitespace = IS_BLANKZ(string); - MOVE(string); - if (string.pointer != string.end) { - followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); - } - } - - emitter->scalar_data.multiline = line_breaks; - - emitter->scalar_data.flow_plain_allowed = 1; - emitter->scalar_data.block_plain_allowed = 1; - emitter->scalar_data.single_quoted_allowed = 1; - emitter->scalar_data.block_allowed = 1; - - if (leading_space || leading_break || trailing_space || trailing_break) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; - } - - if (trailing_space) { - emitter->scalar_data.block_allowed = 0; - } - - if (break_space) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; - emitter->scalar_data.single_quoted_allowed = 0; - } - - if (space_break || special_characters) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; - emitter->scalar_data.single_quoted_allowed = 0; - emitter->scalar_data.block_allowed = 0; - } - - if (line_breaks) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; - } - - if (flow_indicators) { - emitter->scalar_data.flow_plain_allowed = 0; - } - - if (block_indicators) { - emitter->scalar_data.block_plain_allowed = 0; - } - - return 1; -} - -/* - * Check if the event data is valid. - */ - -static int -yaml_emitter_analyze_event(yaml_emitter_t *emitter, - yaml_event_t *event) -{ - emitter->anchor_data.anchor = NULL; - emitter->anchor_data.anchor_length = 0; - emitter->tag_data.handle = NULL; - emitter->tag_data.handle_length = 0; - emitter->tag_data.suffix = NULL; - emitter->tag_data.suffix_length = 0; - emitter->scalar_data.value = NULL; - emitter->scalar_data.length = 0; - - switch (event->type) - { - case YAML_ALIAS_EVENT: - if (!yaml_emitter_analyze_anchor(emitter, - event->data.alias.anchor, 1)) - return 0; - return 1; - - case YAML_SCALAR_EVENT: - if (event->data.scalar.anchor) { - if (!yaml_emitter_analyze_anchor(emitter, - event->data.scalar.anchor, 0)) - return 0; - } - if (event->data.scalar.tag && (emitter->canonical || - (!event->data.scalar.plain_implicit - && !event->data.scalar.quoted_implicit))) { - if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag)) - return 0; - } - if (!yaml_emitter_analyze_scalar(emitter, - event->data.scalar.value, event->data.scalar.length)) - return 0; - return 1; - - case YAML_SEQUENCE_START_EVENT: - if (event->data.sequence_start.anchor) { - if (!yaml_emitter_analyze_anchor(emitter, - event->data.sequence_start.anchor, 0)) - return 0; - } - if (event->data.sequence_start.tag && (emitter->canonical || - !event->data.sequence_start.implicit)) { - if (!yaml_emitter_analyze_tag(emitter, - event->data.sequence_start.tag)) - return 0; - } - return 1; - - case YAML_MAPPING_START_EVENT: - if (event->data.mapping_start.anchor) { - if (!yaml_emitter_analyze_anchor(emitter, - event->data.mapping_start.anchor, 0)) - return 0; - } - if (event->data.mapping_start.tag && (emitter->canonical || - !event->data.mapping_start.implicit)) { - if (!yaml_emitter_analyze_tag(emitter, - event->data.mapping_start.tag)) - return 0; - } - return 1; - - default: - return 1; - } -} - -/* - * Write the BOM character. - */ - -static int -yaml_emitter_write_bom(yaml_emitter_t *emitter) -{ - if (!FLUSH(emitter)) return 0; - - *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF'; - *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB'; - *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF'; - - return 1; -} - -static int -yaml_emitter_write_indent(yaml_emitter_t *emitter) -{ - int indent = (emitter->indent >= 0) ? emitter->indent : 0; - - if (!emitter->indention || emitter->column > indent - || (emitter->column == indent && !emitter->whitespace)) { - if (!PUT_BREAK(emitter)) return 0; - } - - while (emitter->column < indent) { - if (!PUT(emitter, ' ')) return 0; - } - - emitter->whitespace = 1; - emitter->indention = 1; - - return 1; -} - -static int -yaml_emitter_write_indicator(yaml_emitter_t *emitter, - char *indicator, int need_whitespace, - int is_whitespace, int is_indention) -{ - size_t indicator_length; - yaml_string_t string; - - indicator_length = strlen(indicator); - STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length); - - if (need_whitespace && !emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; - } - - while (string.pointer != string.end) { - if (!WRITE(emitter, string)) return 0; - } - - emitter->whitespace = is_whitespace; - emitter->indention = (emitter->indention && is_indention); - emitter->open_ended = 0; - - return 1; -} - -static int -yaml_emitter_write_anchor(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length) -{ - yaml_string_t string; - STRING_ASSIGN(string, value, length); - - while (string.pointer != string.end) { - if (!WRITE(emitter, string)) return 0; - } - - emitter->whitespace = 0; - emitter->indention = 0; - - return 1; -} - -static int -yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length) -{ - yaml_string_t string; - STRING_ASSIGN(string, value, length); - - if (!emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; - } - - while (string.pointer != string.end) { - if (!WRITE(emitter, string)) return 0; - } - - emitter->whitespace = 0; - emitter->indention = 0; - - return 1; -} - -static int -yaml_emitter_write_tag_content(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, - int need_whitespace) -{ - yaml_string_t string; - STRING_ASSIGN(string, value, length); - - if (need_whitespace && !emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; - } - - while (string.pointer != string.end) { - if (IS_ALPHA(string) - || CHECK(string, ';') || CHECK(string, '/') - || CHECK(string, '?') || CHECK(string, ':') - || CHECK(string, '@') || CHECK(string, '&') - || CHECK(string, '=') || CHECK(string, '+') - || CHECK(string, '$') || CHECK(string, ',') - || CHECK(string, '_') || CHECK(string, '.') - || CHECK(string, '~') || CHECK(string, '*') - || CHECK(string, '\'') || CHECK(string, '(') - || CHECK(string, ')') || CHECK(string, '[') - || CHECK(string, ']')) { - if (!WRITE(emitter, string)) return 0; - } - else { - int width = WIDTH(string); - unsigned int value; - while (width --) { - value = *(string.pointer++); - if (!PUT(emitter, '%')) return 0; - if (!PUT(emitter, (value >> 4) - + ((value >> 4) < 10 ? '0' : 'A' - 10))) - return 0; - if (!PUT(emitter, (value & 0x0F) - + ((value & 0x0F) < 10 ? '0' : 'A' - 10))) - return 0; - } - } - } - - emitter->whitespace = 0; - emitter->indention = 0; - - return 1; -} - -static int -yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int allow_breaks) -{ - yaml_string_t string; - int spaces = 0; - int breaks = 0; - - STRING_ASSIGN(string, value, length); - - if (!emitter->whitespace) { - if (!PUT(emitter, ' ')) return 0; - } - - while (string.pointer != string.end) - { - if (IS_SPACE(string)) - { - if (allow_breaks && !spaces - && emitter->column > emitter->best_width - && !IS_SPACE_AT(string, 1)) { - if (!yaml_emitter_write_indent(emitter)) return 0; - MOVE(string); - } - else { - if (!WRITE(emitter, string)) return 0; - } - spaces = 1; - } - else if (IS_BREAK(string)) - { - if (!breaks && CHECK(string, '\n')) { - if (!PUT_BREAK(emitter)) return 0; - } - if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; - breaks = 1; - } - else - { - if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; - } - if (!WRITE(emitter, string)) return 0; - emitter->indention = 0; - spaces = 0; - breaks = 0; - } - } - - emitter->whitespace = 0; - emitter->indention = 0; - if (emitter->root_context) - { - emitter->open_ended = 1; - } - - return 1; -} - -static int -yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int allow_breaks) -{ - yaml_string_t string; - int spaces = 0; - int breaks = 0; - - STRING_ASSIGN(string, value, length); - - if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) - return 0; - - while (string.pointer != string.end) - { - if (IS_SPACE(string)) - { - if (allow_breaks && !spaces - && emitter->column > emitter->best_width - && string.pointer != string.start - && string.pointer != string.end - 1 - && !IS_SPACE_AT(string, 1)) { - if (!yaml_emitter_write_indent(emitter)) return 0; - MOVE(string); - } - else { - if (!WRITE(emitter, string)) return 0; - } - spaces = 1; - } - else if (IS_BREAK(string)) - { - if (!breaks && CHECK(string, '\n')) { - if (!PUT_BREAK(emitter)) return 0; - } - if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; - breaks = 1; - } - else - { - if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; - } - if (CHECK(string, '\'')) { - if (!PUT(emitter, '\'')) return 0; - } - if (!WRITE(emitter, string)) return 0; - emitter->indention = 0; - spaces = 0; - breaks = 0; - } - } - - if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0)) - return 0; - - emitter->whitespace = 0; - emitter->indention = 0; - - return 1; -} - -static int -yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length, int allow_breaks) -{ - yaml_string_t string; - int spaces = 0; - - STRING_ASSIGN(string, value, length); - - if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) - return 0; - - while (string.pointer != string.end) - { - if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string)) - || IS_BOM(string) || IS_BREAK(string) - || CHECK(string, '"') || CHECK(string, '\\')) - { - unsigned char octet; - unsigned int width; - unsigned int value; - int k; - - octet = string.pointer[0]; - width = (octet & 0x80) == 0x00 ? 1 : - (octet & 0xE0) == 0xC0 ? 2 : - (octet & 0xF0) == 0xE0 ? 3 : - (octet & 0xF8) == 0xF0 ? 4 : 0; - value = (octet & 0x80) == 0x00 ? octet & 0x7F : - (octet & 0xE0) == 0xC0 ? octet & 0x1F : - (octet & 0xF0) == 0xE0 ? octet & 0x0F : - (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; - for (k = 1; k < (int)width; k ++) { - octet = string.pointer[k]; - value = (value << 6) + (octet & 0x3F); - } - string.pointer += width; - - if (!PUT(emitter, '\\')) return 0; - - switch (value) - { - case 0x00: - if (!PUT(emitter, '0')) return 0; - break; - - case 0x07: - if (!PUT(emitter, 'a')) return 0; - break; - - case 0x08: - if (!PUT(emitter, 'b')) return 0; - break; - - case 0x09: - if (!PUT(emitter, 't')) return 0; - break; - - case 0x0A: - if (!PUT(emitter, 'n')) return 0; - break; - - case 0x0B: - if (!PUT(emitter, 'v')) return 0; - break; - - case 0x0C: - if (!PUT(emitter, 'f')) return 0; - break; - - case 0x0D: - if (!PUT(emitter, 'r')) return 0; - break; - - case 0x1B: - if (!PUT(emitter, 'e')) return 0; - break; - - case 0x22: - if (!PUT(emitter, '\"')) return 0; - break; - - case 0x5C: - if (!PUT(emitter, '\\')) return 0; - break; - - case 0x85: - if (!PUT(emitter, 'N')) return 0; - break; - - case 0xA0: - if (!PUT(emitter, '_')) return 0; - break; - - case 0x2028: - if (!PUT(emitter, 'L')) return 0; - break; - - case 0x2029: - if (!PUT(emitter, 'P')) return 0; - break; - - default: - if (value <= 0xFF) { - if (!PUT(emitter, 'x')) return 0; - width = 2; - } - else if (value <= 0xFFFF) { - if (!PUT(emitter, 'u')) return 0; - width = 4; - } - else { - if (!PUT(emitter, 'U')) return 0; - width = 8; - } - for (k = (width-1)*4; k >= 0; k -= 4) { - int digit = (value >> k) & 0x0F; - if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10))) - return 0; - } - } - spaces = 0; - } - else if (IS_SPACE(string)) - { - if (allow_breaks && !spaces - && emitter->column > emitter->best_width - && string.pointer != string.start - && string.pointer != string.end - 1) { - if (!yaml_emitter_write_indent(emitter)) return 0; - if (IS_SPACE_AT(string, 1)) { - if (!PUT(emitter, '\\')) return 0; - } - MOVE(string); - } - else { - if (!WRITE(emitter, string)) return 0; - } - spaces = 1; - } - else - { - if (!WRITE(emitter, string)) return 0; - spaces = 0; - } - } - - if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0)) - return 0; - - emitter->whitespace = 0; - emitter->indention = 0; - - return 1; -} - -static int -yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, - yaml_string_t string) -{ - char indent_hint[2]; - char *chomp_hint = NULL; - - if (IS_SPACE(string) || IS_BREAK(string)) - { - indent_hint[0] = '0' + (char)emitter->best_indent; - indent_hint[1] = '\0'; - if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0)) - return 0; - } - - emitter->open_ended = 0; - - string.pointer = string.end; - if (string.start == string.pointer) - { - chomp_hint = "-"; - } - else - { - do { - string.pointer --; - } while ((*string.pointer & 0xC0) == 0x80); - if (!IS_BREAK(string)) - { - chomp_hint = "-"; - } - else if (string.start == string.pointer) - { - chomp_hint = "+"; - emitter->open_ended = 1; - } - else - { - do { - string.pointer --; - } while ((*string.pointer & 0xC0) == 0x80); - if (IS_BREAK(string)) - { - chomp_hint = "+"; - emitter->open_ended = 1; - } - } - } - - if (chomp_hint) - { - if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0)) - return 0; - } - - return 1; -} - -static int -yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length) -{ - yaml_string_t string; - int breaks = 1; - - STRING_ASSIGN(string, value, length); - - if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_block_scalar_hints(emitter, string)) - return 0; - if (!PUT_BREAK(emitter)) return 0; - emitter->indention = 1; - emitter->whitespace = 1; - - while (string.pointer != string.end) - { - if (IS_BREAK(string)) - { - if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; - breaks = 1; - } - else - { - if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; - } - if (!WRITE(emitter, string)) return 0; - emitter->indention = 0; - breaks = 0; - } - } - - return 1; -} - -static int -yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, - yaml_char_t *value, size_t length) -{ - yaml_string_t string; - int breaks = 1; - int leading_spaces = 1; - - STRING_ASSIGN(string, value, length); - - if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0)) - return 0; - if (!yaml_emitter_write_block_scalar_hints(emitter, string)) - return 0; - if (!PUT_BREAK(emitter)) return 0; - emitter->indention = 1; - emitter->whitespace = 1; - - while (string.pointer != string.end) - { - if (IS_BREAK(string)) - { - if (!breaks && !leading_spaces && CHECK(string, '\n')) { - int k = 0; - while (IS_BREAK_AT(string, k)) { - k += WIDTH_AT(string, k); - } - if (!IS_BLANKZ_AT(string, k)) { - if (!PUT_BREAK(emitter)) return 0; - } - } - if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; - breaks = 1; - } - else - { - if (breaks) { - if (!yaml_emitter_write_indent(emitter)) return 0; - leading_spaces = IS_BLANK(string); - } - if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) - && emitter->column > emitter->best_width) { - if (!yaml_emitter_write_indent(emitter)) return 0; - MOVE(string); - } - else { - if (!WRITE(emitter, string)) return 0; - } - emitter->indention = 0; - breaks = 0; - } - } - - return 1; -} - diff --git a/emitter_c.go b/emitter_c.go index a97d406..2f806c8 100644 --- a/emitter_c.go +++ b/emitter_c.go @@ -1,2330 +1,1686 @@ package goyaml -//#include "yaml_private.h" -// -///* -// * Flush the buffer if needed. -// */ -// -//#define FLUSH(emitter) \ -// ((emitter->buffer.pointer+5 < emitter->buffer.end) \ -// || yaml_emitter_flush(emitter)) -// -///* -// * Put a character to the output buffer. -// */ -// -//#define PUT(emitter,value) \ -// (FLUSH(emitter) \ -// && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \ -// emitter->column ++, \ -// 1)) -// -///* -// * Put a line break to the output buffer. -// */ -// -//#define PUT_BREAK(emitter) \ -// (FLUSH(emitter) \ -// && ((emitter->line_break == YAML_CR_BREAK ? \ -// (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \ -// emitter->line_break == YAML_LN_BREAK ? \ -// (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \ -// emitter->line_break == YAML_CRLN_BREAK ? \ -// (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \ -// *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \ -// emitter->column = 0, \ -// emitter->line ++, \ -// 1)) -// -///* -// * Copy a character from a string into buffer. -// */ -// -//#define WRITE(emitter,string) \ -// (FLUSH(emitter) \ -// && (COPY(emitter->buffer,string), \ -// emitter->column ++, \ -// 1)) -// -///* -// * Copy a line break character from a string into buffer. -// */ -// -//#define WRITE_BREAK(emitter,string) \ -// (FLUSH(emitter) \ -// && (CHECK(string,'\n') ? \ -// (PUT_BREAK(emitter), \ -// string.pointer ++, \ -// 1) : \ -// (COPY(emitter->buffer,string), \ -// emitter->column = 0, \ -// emitter->line ++, \ -// 1))) -// -///* -// * API functions. -// */ -// -//YAML_DECLARE(int) -//yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); -// -///* -// * Utility functions. -// */ -// -//static int -//yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem); -// -//static int -//yaml_emitter_need_more_events(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, -// yaml_tag_directive_t value, int allow_duplicates); -// -//static int -//yaml_emitter_increase_indent(yaml_emitter_t *emitter, -// int flow, int indentless); -// -///* -// * State functions. -// */ -// -//static int -//yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event); -// -//static int -//yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, -// yaml_event_t *event); -// -//static int -//yaml_emitter_emit_document_start(yaml_emitter_t *emitter, -// yaml_event_t *event, int first); -// -//static int -//yaml_emitter_emit_document_content(yaml_emitter_t *emitter, -// yaml_event_t *event); -// -//static int -//yaml_emitter_emit_document_end(yaml_emitter_t *emitter, -// yaml_event_t *event); -// -//static int -//yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, -// yaml_event_t *event, int first); -// -//static int -//yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, -// yaml_event_t *event, int first); -// -//static int -//yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, -// yaml_event_t *event, int simple); -// -//static int -//yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, -// yaml_event_t *event, int first); -// -//static int -//yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, -// yaml_event_t *event, int first); -// -//static int -//yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, -// yaml_event_t *event, int simple); -// -//static int -//yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, -// int root, int sequence, int mapping, int simple_key); -// -//static int -//yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event); -// -//static int -//yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event); -// -//static int -//yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event); -// -//static int -//yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event); -// -///* -// * Checkers. -// */ -// -//static int -//yaml_emitter_check_empty_document(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_check_simple_key(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event); -// -///* -// * Processors. -// */ -// -//static int -//yaml_emitter_process_anchor(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_process_tag(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_process_scalar(yaml_emitter_t *emitter); -// -///* -// * Analyzers. -// */ -// -//static int -//yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, -// yaml_version_directive_t version_directive); -// -//static int -//yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, -// yaml_tag_directive_t tag_directive); -// -//static int -//yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, -// yaml_char_t *anchor, int alias); -// -//static int -//yaml_emitter_analyze_tag(yaml_emitter_t *emitter, -// yaml_char_t *tag); -// -//static int -//yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length); -// -//static int -//yaml_emitter_analyze_event(yaml_emitter_t *emitter, -// yaml_event_t *event); -// -///* -// * Writers. -// */ -// -//static int -//yaml_emitter_write_bom(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_write_indent(yaml_emitter_t *emitter); -// -//static int -//yaml_emitter_write_indicator(yaml_emitter_t *emitter, -// char *indicator, int need_whitespace, -// int is_whitespace, int is_indention); -// -//static int -//yaml_emitter_write_anchor(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length); -// -//static int -//yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length); -// -//static int -//yaml_emitter_write_tag_content(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int need_whitespace); -// -//static int -//yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int allow_breaks); -// -//static int -//yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int allow_breaks); -// -//static int -//yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int allow_breaks); -// -//static int -//yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, -// yaml_string_t string); -// -//static int -//yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length); -// -//static int -//yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length); -// -///* -// * Set an emitter error and return 0. -// */ -// -//static int -//yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem) -//{ -// emitter->error = YAML_EMITTER_ERROR; -// emitter->problem = problem; -// -// return 0; -//} -// -///* -// * Emit an event. -// */ -// -//YAML_DECLARE(int) -//yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// if (!ENQUEUE(emitter, emitter->events, *event)) { -// yaml_event_delete(event); -// return 0; -// } -// -// while (!yaml_emitter_need_more_events(emitter)) { -// if (!yaml_emitter_analyze_event(emitter, emitter->events.head)) -// return 0; -// if (!yaml_emitter_state_machine(emitter, emitter->events.head)) -// return 0; -// yaml_event_delete(&DEQUEUE(emitter, emitter->events)); -// } -// -// return 1; -//} -// -///* -// * Check if we need to accumulate more events before emitting. -// * -// * We accumulate extra -// * - 1 event for DOCUMENT-START -// * - 2 events for SEQUENCE-START -// * - 3 events for MAPPING-START -// */ -// -//static int -//yaml_emitter_need_more_events(yaml_emitter_t *emitter) -//{ -// int level = 0; -// int accumulate = 0; -// yaml_event_t *event; -// -// if (QUEUE_EMPTY(emitter, emitter->events)) -// return 1; -// -// switch (emitter->events.head->type) { -// case YAML_DOCUMENT_START_EVENT: -// accumulate = 1; -// break; -// case YAML_SEQUENCE_START_EVENT: -// accumulate = 2; -// break; -// case YAML_MAPPING_START_EVENT: -// accumulate = 3; -// break; -// default: -// return 0; -// } -// -// if (emitter->events.tail - emitter->events.head > accumulate) -// return 0; -// -// for (event = emitter->events.head; event != emitter->events.tail; event ++) { -// switch (event->type) { -// case YAML_STREAM_START_EVENT: -// case YAML_DOCUMENT_START_EVENT: -// case YAML_SEQUENCE_START_EVENT: -// case YAML_MAPPING_START_EVENT: -// level += 1; -// break; -// case YAML_STREAM_END_EVENT: -// case YAML_DOCUMENT_END_EVENT: -// case YAML_SEQUENCE_END_EVENT: -// case YAML_MAPPING_END_EVENT: -// level -= 1; -// break; -// default: -// break; -// } -// if (!level) -// return 0; -// } -// -// return 1; -//} -// -///* -// * Append a directive to the directives stack. -// */ -// -//static int -//yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, -// yaml_tag_directive_t value, int allow_duplicates) -//{ -// yaml_tag_directive_t *tag_directive; -// yaml_tag_directive_t copy = { NULL, NULL }; -// -// for (tag_directive = emitter->tag_directives.start; -// tag_directive != emitter->tag_directives.top; tag_directive ++) { -// if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { -// if (allow_duplicates) -// return 1; -// return yaml_emitter_set_emitter_error(emitter, -// "duplicate %TAG directive"); -// } -// } -// -// copy.handle = yaml_strdup(value.handle); -// copy.prefix = yaml_strdup(value.prefix); -// if (!copy.handle || !copy.prefix) { -// emitter->error = YAML_MEMORY_ERROR; -// goto error; -// } -// -// if (!PUSH(emitter, emitter->tag_directives, copy)) -// goto error; -// -// return 1; -// -//error: -// yaml_free(copy.handle); -// yaml_free(copy.prefix); -// return 0; -//} -// -///* -// * Increase the indentation level. -// */ -// -//static int -//yaml_emitter_increase_indent(yaml_emitter_t *emitter, -// int flow, int indentless) -//{ -// if (!PUSH(emitter, emitter->indents, emitter->indent)) -// return 0; -// -// if (emitter->indent < 0) { -// emitter->indent = flow ? emitter->best_indent : 0; -// } -// else if (!indentless) { -// emitter->indent += emitter->best_indent; -// } -// -// return 1; -//} -// -///* -// * State dispatcher. -// */ -// -//static int -//yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// switch (emitter->state) -// { -// case YAML_EMIT_STREAM_START_STATE: -// return yaml_emitter_emit_stream_start(emitter, event); -// -// case YAML_EMIT_FIRST_DOCUMENT_START_STATE: -// return yaml_emitter_emit_document_start(emitter, event, 1); -// -// case YAML_EMIT_DOCUMENT_START_STATE: -// return yaml_emitter_emit_document_start(emitter, event, 0); -// -// case YAML_EMIT_DOCUMENT_CONTENT_STATE: -// return yaml_emitter_emit_document_content(emitter, event); -// -// case YAML_EMIT_DOCUMENT_END_STATE: -// return yaml_emitter_emit_document_end(emitter, event); -// -// case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: -// return yaml_emitter_emit_flow_sequence_item(emitter, event, 1); -// -// case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE: -// return yaml_emitter_emit_flow_sequence_item(emitter, event, 0); -// -// case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: -// return yaml_emitter_emit_flow_mapping_key(emitter, event, 1); -// -// case YAML_EMIT_FLOW_MAPPING_KEY_STATE: -// return yaml_emitter_emit_flow_mapping_key(emitter, event, 0); -// -// case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: -// return yaml_emitter_emit_flow_mapping_value(emitter, event, 1); -// -// case YAML_EMIT_FLOW_MAPPING_VALUE_STATE: -// return yaml_emitter_emit_flow_mapping_value(emitter, event, 0); -// -// case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: -// return yaml_emitter_emit_block_sequence_item(emitter, event, 1); -// -// case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE: -// return yaml_emitter_emit_block_sequence_item(emitter, event, 0); -// -// case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: -// return yaml_emitter_emit_block_mapping_key(emitter, event, 1); -// -// case YAML_EMIT_BLOCK_MAPPING_KEY_STATE: -// return yaml_emitter_emit_block_mapping_key(emitter, event, 0); -// -// case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: -// return yaml_emitter_emit_block_mapping_value(emitter, event, 1); -// -// case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE: -// return yaml_emitter_emit_block_mapping_value(emitter, event, 0); -// -// case YAML_EMIT_END_STATE: -// return yaml_emitter_set_emitter_error(emitter, -// "expected nothing after STREAM-END"); -// -// default: -// assert(1); /* Invalid state. */ -// } -// -// return 0; -//} -// -///* -// * Expect STREAM-START. -// */ -// -//static int -//yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, -// yaml_event_t *event) -//{ -// if (event->type == YAML_STREAM_START_EVENT) -// { -// if (!emitter->encoding) { -// emitter->encoding = event->data.stream_start.encoding; -// } -// -// if (!emitter->encoding) { -// emitter->encoding = YAML_UTF8_ENCODING; -// } -// -// if (emitter->best_indent < 2 || emitter->best_indent > 9) { -// emitter->best_indent = 2; -// } -// -// if (emitter->best_width >= 0 -// && emitter->best_width <= emitter->best_indent*2) { -// emitter->best_width = 80; -// } -// -// if (emitter->best_width < 0) { -// emitter->best_width = INT_MAX; -// } -// -// if (!emitter->line_break) { -// emitter->line_break = YAML_LN_BREAK; -// } -// -// emitter->indent = -1; -// -// emitter->line = 0; -// emitter->column = 0; -// emitter->whitespace = 1; -// emitter->indention = 1; -// -// if (emitter->encoding != YAML_UTF8_ENCODING) { -// if (!yaml_emitter_write_bom(emitter)) -// return 0; -// } -// -// emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE; -// -// return 1; -// } -// -// return yaml_emitter_set_emitter_error(emitter, -// "expected STREAM-START"); -//} -// -///* -// * Expect DOCUMENT-START or STREAM-END. -// */ -// -//static int -//yaml_emitter_emit_document_start(yaml_emitter_t *emitter, -// yaml_event_t *event, int first) -//{ -// if (event->type == YAML_DOCUMENT_START_EVENT) -// { -// yaml_tag_directive_t default_tag_directives[] = { -// {(yaml_char_t *)"!", (yaml_char_t *)"!"}, -// {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"}, -// {NULL, NULL} -// }; -// yaml_tag_directive_t *tag_directive; -// int implicit; -// -// if (event->data.document_start.version_directive) { -// if (!yaml_emitter_analyze_version_directive(emitter, -// *event->data.document_start.version_directive)) -// return 0; -// } -// -// for (tag_directive = event->data.document_start.tag_directives.start; -// tag_directive != event->data.document_start.tag_directives.end; -// tag_directive ++) { -// if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive)) -// return 0; -// if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0)) -// return 0; -// } -// -// for (tag_directive = default_tag_directives; -// tag_directive->handle; tag_directive ++) { -// if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1)) -// return 0; -// } -// -// implicit = event->data.document_start.implicit; -// if (!first || emitter->canonical) { -// implicit = 0; -// } -// -// if ((event->data.document_start.version_directive || -// (event->data.document_start.tag_directives.start -// != event->data.document_start.tag_directives.end)) && -// emitter->open_ended) -// { -// if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// -// if (event->data.document_start.version_directive) { -// implicit = 0; -// if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// -// if (event->data.document_start.tag_directives.start -// != event->data.document_start.tag_directives.end) { -// implicit = 0; -// for (tag_directive = event->data.document_start.tag_directives.start; -// tag_directive != event->data.document_start.tag_directives.end; -// tag_directive ++) { -// if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle, -// strlen((char *)tag_directive->handle))) -// return 0; -// if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix, -// strlen((char *)tag_directive->prefix), 1)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// } -// -// if (yaml_emitter_check_empty_document(emitter)) { -// implicit = 0; -// } -// -// if (!implicit) { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0)) -// return 0; -// if (emitter->canonical) { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// } -// -// emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE; -// -// return 1; -// } -// -// else if (event->type == YAML_STREAM_END_EVENT) -// { -// if (emitter->open_ended) -// { -// if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// -// if (!yaml_emitter_flush(emitter)) -// return 0; -// -// emitter->state = YAML_EMIT_END_STATE; -// -// return 1; -// } -// -// return yaml_emitter_set_emitter_error(emitter, -// "expected DOCUMENT-START or STREAM-END"); -//} -// -///* -// * Expect the root node. -// */ -// -//static int -//yaml_emitter_emit_document_content(yaml_emitter_t *emitter, -// yaml_event_t *event) -//{ -// if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0); -//} -// -///* -// * Expect DOCUMENT-END. -// */ -// -//static int -//yaml_emitter_emit_document_end(yaml_emitter_t *emitter, -// yaml_event_t *event) -//{ -// if (event->type == YAML_DOCUMENT_END_EVENT) -// { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// if (!event->data.document_end.implicit) { -// if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// if (!yaml_emitter_flush(emitter)) -// return 0; -// -// emitter->state = YAML_EMIT_DOCUMENT_START_STATE; -// -// while (!STACK_EMPTY(emitter, emitter->tag_directives)) { -// yaml_tag_directive_t tag_directive = POP(emitter, -// emitter->tag_directives); -// yaml_free(tag_directive.handle); -// yaml_free(tag_directive.prefix); -// } -// -// return 1; -// } -// -// return yaml_emitter_set_emitter_error(emitter, -// "expected DOCUMENT-END"); -//} -// -///* -// * -// * Expect a flow item node. -// */ -// -//static int -//yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, -// yaml_event_t *event, int first) -//{ -// if (first) -// { -// if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0)) -// return 0; -// if (!yaml_emitter_increase_indent(emitter, 1, 0)) -// return 0; -// emitter->flow_level ++; -// } -// -// if (event->type == YAML_SEQUENCE_END_EVENT) -// { -// emitter->flow_level --; -// emitter->indent = POP(emitter, emitter->indents); -// if (emitter->canonical && !first) { -// if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0)) -// return 0; -// emitter->state = POP(emitter, emitter->states); -// -// return 1; -// } -// -// if (!first) { -// if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) -// return 0; -// } -// -// if (emitter->canonical || emitter->column > emitter->best_width) { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); -//} -// -///* -// * Expect a flow key node. -// */ -// -//static int -//yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, -// yaml_event_t *event, int first) -//{ -// if (first) -// { -// if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0)) -// return 0; -// if (!yaml_emitter_increase_indent(emitter, 1, 0)) -// return 0; -// emitter->flow_level ++; -// } -// -// if (event->type == YAML_MAPPING_END_EVENT) -// { -// emitter->flow_level --; -// emitter->indent = POP(emitter, emitter->indents); -// if (emitter->canonical && !first) { -// if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) -// return 0; -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0)) -// return 0; -// emitter->state = POP(emitter, emitter->states); -// -// return 1; -// } -// -// if (!first) { -// if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) -// return 0; -// } -// if (emitter->canonical || emitter->column > emitter->best_width) { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// -// if (!emitter->canonical && yaml_emitter_check_simple_key(emitter)) -// { -// if (!PUSH(emitter, emitter->states, -// YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); -// } -// else -// { -// if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0)) -// return 0; -// if (!PUSH(emitter, emitter->states, -// YAML_EMIT_FLOW_MAPPING_VALUE_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); -// } -//} -// -///* -// * Expect a flow value node. -// */ -// -//static int -//yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, -// yaml_event_t *event, int simple) -//{ -// if (simple) { -// if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) -// return 0; -// } -// else { -// if (emitter->canonical || emitter->column > emitter->best_width) { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// } -// if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0)) -// return 0; -// } -// if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE)) -// return 0; -// return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); -//} -// -///* -// * Expect a block item node. -// */ -// -//static int -//yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, -// yaml_event_t *event, int first) -//{ -// if (first) -// { -// if (!yaml_emitter_increase_indent(emitter, 0, -// (emitter->mapping_context && !emitter->indention))) -// return 0; -// } -// -// if (event->type == YAML_SEQUENCE_END_EVENT) -// { -// emitter->indent = POP(emitter, emitter->indents); -// emitter->state = POP(emitter, emitter->states); -// -// return 1; -// } -// -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1)) -// return 0; -// if (!PUSH(emitter, emitter->states, -// YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); -//} -// -///* -// * Expect a block key node. -// */ -// -//static int -//yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, -// yaml_event_t *event, int first) -//{ -// if (first) -// { -// if (!yaml_emitter_increase_indent(emitter, 0, 0)) -// return 0; -// } -// -// if (event->type == YAML_MAPPING_END_EVENT) -// { -// emitter->indent = POP(emitter, emitter->indents); -// emitter->state = POP(emitter, emitter->states); -// -// return 1; -// } -// -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// -// if (yaml_emitter_check_simple_key(emitter)) -// { -// if (!PUSH(emitter, emitter->states, -// YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); -// } -// else -// { -// if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1)) -// return 0; -// if (!PUSH(emitter, emitter->states, -// YAML_EMIT_BLOCK_MAPPING_VALUE_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); -// } -//} -// -///* -// * Expect a block value node. -// */ -// -//static int -//yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, -// yaml_event_t *event, int simple) -//{ -// if (simple) { -// if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) -// return 0; -// } -// else { -// if (!yaml_emitter_write_indent(emitter)) -// return 0; -// if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1)) -// return 0; -// } -// if (!PUSH(emitter, emitter->states, -// YAML_EMIT_BLOCK_MAPPING_KEY_STATE)) -// return 0; -// -// return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); -//} -// -///* -// * Expect a node. -// */ -// -//static int -//yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, -// int root, int sequence, int mapping, int simple_key) -//{ -// emitter->root_context = root; -// emitter->sequence_context = sequence; -// emitter->mapping_context = mapping; -// emitter->simple_key_context = simple_key; -// -// switch (event->type) -// { -// case YAML_ALIAS_EVENT: -// return yaml_emitter_emit_alias(emitter, event); -// -// case YAML_SCALAR_EVENT: -// return yaml_emitter_emit_scalar(emitter, event); -// -// case YAML_SEQUENCE_START_EVENT: -// return yaml_emitter_emit_sequence_start(emitter, event); -// -// case YAML_MAPPING_START_EVENT: -// return yaml_emitter_emit_mapping_start(emitter, event); -// -// default: -// return yaml_emitter_set_emitter_error(emitter, -// "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); -// } -// -// return 0; -//} -// -///* -// * Expect ALIAS. -// */ -// -//static int -//yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// if (!yaml_emitter_process_anchor(emitter)) -// return 0; -// emitter->state = POP(emitter, emitter->states); -// -// return 1; -//} -// -///* -// * Expect SCALAR. -// */ -// -//static int -//yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// if (!yaml_emitter_select_scalar_style(emitter, event)) -// return 0; -// if (!yaml_emitter_process_anchor(emitter)) -// return 0; -// if (!yaml_emitter_process_tag(emitter)) -// return 0; -// if (!yaml_emitter_increase_indent(emitter, 1, 0)) -// return 0; -// if (!yaml_emitter_process_scalar(emitter)) -// return 0; -// emitter->indent = POP(emitter, emitter->indents); -// emitter->state = POP(emitter, emitter->states); -// -// return 1; -//} -// -///* -// * Expect SEQUENCE-START. -// */ -// -//static int -//yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// if (!yaml_emitter_process_anchor(emitter)) -// return 0; -// if (!yaml_emitter_process_tag(emitter)) -// return 0; -// -// if (emitter->flow_level || emitter->canonical -// || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE -// || yaml_emitter_check_empty_sequence(emitter)) { -// emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE; -// } -// else { -// emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE; -// } -// -// return 1; -//} -// -///* -// * Expect MAPPING-START. -// */ -// -//static int -//yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// if (!yaml_emitter_process_anchor(emitter)) -// return 0; -// if (!yaml_emitter_process_tag(emitter)) -// return 0; -// -// if (emitter->flow_level || emitter->canonical -// || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE -// || yaml_emitter_check_empty_mapping(emitter)) { -// emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE; -// } -// else { -// emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE; -// } -// -// return 1; -//} -// -///* -// * Check if the document content is an empty scalar. -// */ -// -//static int -//yaml_emitter_check_empty_document(yaml_emitter_t *emitter) -//{ -// return 0; -//} -// -///* -// * Check if the next events represent an empty sequence. -// */ -// -//static int -//yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter) -//{ -// if (emitter->events.tail - emitter->events.head < 2) -// return 0; -// -// return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT -// && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT); -//} -// -///* -// * Check if the next events represent an empty mapping. -// */ -// -//static int -//yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter) -//{ -// if (emitter->events.tail - emitter->events.head < 2) -// return 0; -// -// return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT -// && emitter->events.head[1].type == YAML_MAPPING_END_EVENT); -//} -// -///* -// * Check if the next node can be expressed as a simple key. -// */ -// -//static int -//yaml_emitter_check_simple_key(yaml_emitter_t *emitter) -//{ -// yaml_event_t *event = emitter->events.head; -// size_t length = 0; -// -// switch (event->type) -// { -// case YAML_ALIAS_EVENT: -// length += emitter->anchor_data.anchor_length; -// break; -// -// case YAML_SCALAR_EVENT: -// if (emitter->scalar_data.multiline) -// return 0; -// length += emitter->anchor_data.anchor_length -// + emitter->tag_data.handle_length -// + emitter->tag_data.suffix_length -// + emitter->scalar_data.length; -// break; -// -// case YAML_SEQUENCE_START_EVENT: -// if (!yaml_emitter_check_empty_sequence(emitter)) -// return 0; -// length += emitter->anchor_data.anchor_length -// + emitter->tag_data.handle_length -// + emitter->tag_data.suffix_length; -// break; -// -// case YAML_MAPPING_START_EVENT: -// if (!yaml_emitter_check_empty_mapping(emitter)) -// return 0; -// length += emitter->anchor_data.anchor_length -// + emitter->tag_data.handle_length -// + emitter->tag_data.suffix_length; -// break; -// -// default: -// return 0; -// } -// -// if (length > 128) -// return 0; -// -// return 1; -//} -// -///* -// * Determine an acceptable scalar style. -// */ -// -//static int -//yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event) -//{ -// yaml_scalar_style_t style = event->data.scalar.style; -// int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix); -// -// if (no_tag && !event->data.scalar.plain_implicit -// && !event->data.scalar.quoted_implicit) { -// return yaml_emitter_set_emitter_error(emitter, -// "neither tag nor implicit flags are specified"); -// } -// -// if (style == YAML_ANY_SCALAR_STYLE) -// style = YAML_PLAIN_SCALAR_STYLE; -// -// if (emitter->canonical) -// style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; -// -// if (emitter->simple_key_context && emitter->scalar_data.multiline) -// style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; -// -// if (style == YAML_PLAIN_SCALAR_STYLE) -// { -// if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed) -// || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed)) -// style = YAML_SINGLE_QUOTED_SCALAR_STYLE; -// if (!emitter->scalar_data.length -// && (emitter->flow_level || emitter->simple_key_context)) -// style = YAML_SINGLE_QUOTED_SCALAR_STYLE; -// if (no_tag && !event->data.scalar.plain_implicit) -// style = YAML_SINGLE_QUOTED_SCALAR_STYLE; -// } -// -// if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE) -// { -// if (!emitter->scalar_data.single_quoted_allowed) -// style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; -// } -// -// if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE) -// { -// if (!emitter->scalar_data.block_allowed -// || emitter->flow_level || emitter->simple_key_context) -// style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; -// } -// -// if (no_tag && !event->data.scalar.quoted_implicit -// && style != YAML_PLAIN_SCALAR_STYLE) -// { -// emitter->tag_data.handle = (yaml_char_t *)"!"; -// emitter->tag_data.handle_length = 1; -// } -// -// emitter->scalar_data.style = style; -// -// return 1; -//} -// -///* -// * Write an achor. -// */ -// -//static int -//yaml_emitter_process_anchor(yaml_emitter_t *emitter) -//{ -// if (!emitter->anchor_data.anchor) -// return 1; -// -// if (!yaml_emitter_write_indicator(emitter, -// (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0)) -// return 0; -// -// return yaml_emitter_write_anchor(emitter, -// emitter->anchor_data.anchor, emitter->anchor_data.anchor_length); -//} -// -///* -// * Write a tag. -// */ -// -//static int -//yaml_emitter_process_tag(yaml_emitter_t *emitter) -//{ -// if (!emitter->tag_data.handle && !emitter->tag_data.suffix) -// return 1; -// -// if (emitter->tag_data.handle) -// { -// if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle, -// emitter->tag_data.handle_length)) -// return 0; -// if (emitter->tag_data.suffix) { -// if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, -// emitter->tag_data.suffix_length, 0)) -// return 0; -// } -// } -// else -// { -// if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, -// emitter->tag_data.suffix_length, 0)) -// return 0; -// if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0)) -// return 0; -// } -// -// return 1; -//} -// -///* -// * Write a scalar. -// */ -// -//static int -//yaml_emitter_process_scalar(yaml_emitter_t *emitter) -//{ -// switch (emitter->scalar_data.style) -// { -// case YAML_PLAIN_SCALAR_STYLE: -// return yaml_emitter_write_plain_scalar(emitter, -// emitter->scalar_data.value, emitter->scalar_data.length, -// !emitter->simple_key_context); -// -// case YAML_SINGLE_QUOTED_SCALAR_STYLE: -// return yaml_emitter_write_single_quoted_scalar(emitter, -// emitter->scalar_data.value, emitter->scalar_data.length, -// !emitter->simple_key_context); -// -// case YAML_DOUBLE_QUOTED_SCALAR_STYLE: -// return yaml_emitter_write_double_quoted_scalar(emitter, -// emitter->scalar_data.value, emitter->scalar_data.length, -// !emitter->simple_key_context); -// -// case YAML_LITERAL_SCALAR_STYLE: -// return yaml_emitter_write_literal_scalar(emitter, -// emitter->scalar_data.value, emitter->scalar_data.length); -// -// case YAML_FOLDED_SCALAR_STYLE: -// return yaml_emitter_write_folded_scalar(emitter, -// emitter->scalar_data.value, emitter->scalar_data.length); -// -// default: -// assert(1); /* Impossible. */ -// } -// -// return 0; -//} -// -///* -// * Check if a %YAML directive is valid. -// */ -// -//static int -//yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, -// yaml_version_directive_t version_directive) -//{ -// if (version_directive.major != 1 || version_directive.minor != 1) { -// return yaml_emitter_set_emitter_error(emitter, -// "incompatible %YAML directive"); -// } -// -// return 1; -//} -// -///* -// * Check if a %TAG directive is valid. -// */ -// -//static int -//yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, -// yaml_tag_directive_t tag_directive) -//{ -// yaml_string_t handle; -// yaml_string_t prefix; -// size_t handle_length; -// size_t prefix_length; -// -// handle_length = strlen((char *)tag_directive.handle); -// prefix_length = strlen((char *)tag_directive.prefix); -// STRING_ASSIGN(handle, tag_directive.handle, handle_length); -// STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length); -// -// if (handle.start == handle.end) { -// return yaml_emitter_set_emitter_error(emitter, -// "tag handle must not be empty"); -// } -// -// if (handle.start[0] != '!') { -// return yaml_emitter_set_emitter_error(emitter, -// "tag handle must start with '!'"); -// } -// -// if (handle.end[-1] != '!') { -// return yaml_emitter_set_emitter_error(emitter, -// "tag handle must end with '!'"); -// } -// -// handle.pointer ++; -// -// while (handle.pointer < handle.end-1) { -// if (!IS_ALPHA(handle)) { -// return yaml_emitter_set_emitter_error(emitter, -// "tag handle must contain alphanumerical characters only"); -// } -// MOVE(handle); -// } -// -// if (prefix.start == prefix.end) { -// return yaml_emitter_set_emitter_error(emitter, -// "tag prefix must not be empty"); -// } -// -// return 1; -//} -// -///* -// * Check if an anchor is valid. -// */ -// -//static int -//yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, -// yaml_char_t *anchor, int alias) -//{ -// size_t anchor_length; -// yaml_string_t string; -// -// anchor_length = strlen((char *)anchor); -// STRING_ASSIGN(string, anchor, anchor_length); -// -// if (string.start == string.end) { -// return yaml_emitter_set_emitter_error(emitter, alias ? -// "alias value must not be empty" : -// "anchor value must not be empty"); -// } -// -// while (string.pointer != string.end) { -// if (!IS_ALPHA(string)) { -// return yaml_emitter_set_emitter_error(emitter, alias ? -// "alias value must contain alphanumerical characters only" : -// "anchor value must contain alphanumerical characters only"); -// } -// MOVE(string); -// } -// -// emitter->anchor_data.anchor = string.start; -// emitter->anchor_data.anchor_length = string.end - string.start; -// emitter->anchor_data.alias = alias; -// -// return 1; -//} -// -///* -// * Check if a tag is valid. -// */ -// -//static int -//yaml_emitter_analyze_tag(yaml_emitter_t *emitter, -// yaml_char_t *tag) -//{ -// size_t tag_length; -// yaml_string_t string; -// yaml_tag_directive_t *tag_directive; -// -// tag_length = strlen((char *)tag); -// STRING_ASSIGN(string, tag, tag_length); -// -// if (string.start == string.end) { -// return yaml_emitter_set_emitter_error(emitter, -// "tag value must not be empty"); -// } -// -// for (tag_directive = emitter->tag_directives.start; -// tag_directive != emitter->tag_directives.top; tag_directive ++) { -// size_t prefix_length = strlen((char *)tag_directive->prefix); -// if (prefix_length < (size_t)(string.end - string.start) -// && strncmp((char *)tag_directive->prefix, (char *)string.start, -// prefix_length) == 0) -// { -// emitter->tag_data.handle = tag_directive->handle; -// emitter->tag_data.handle_length = -// strlen((char *)tag_directive->handle); -// emitter->tag_data.suffix = string.start + prefix_length; -// emitter->tag_data.suffix_length = -// (string.end - string.start) - prefix_length; -// return 1; -// } -// } -// -// emitter->tag_data.suffix = string.start; -// emitter->tag_data.suffix_length = string.end - string.start; -// -// return 1; -//} -// -///* -// * Check if a scalar is valid. -// */ -// -//static int -//yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length) -//{ -// yaml_string_t string; -// -// int block_indicators = 0; -// int flow_indicators = 0; -// int line_breaks = 0; -// int special_characters = 0; -// -// int leading_space = 0; -// int leading_break = 0; -// int trailing_space = 0; -// int trailing_break = 0; -// int break_space = 0; -// int space_break = 0; -// -// int preceeded_by_whitespace = 0; -// int followed_by_whitespace = 0; -// int previous_space = 0; -// int previous_break = 0; -// -// STRING_ASSIGN(string, value, length); -// -// emitter->scalar_data.value = value; -// emitter->scalar_data.length = length; -// -// if (string.start == string.end) -// { -// emitter->scalar_data.multiline = 0; -// emitter->scalar_data.flow_plain_allowed = 0; -// emitter->scalar_data.block_plain_allowed = 1; -// emitter->scalar_data.single_quoted_allowed = 1; -// emitter->scalar_data.block_allowed = 0; -// -// return 1; -// } -// -// if ((CHECK_AT(string, '-', 0) -// && CHECK_AT(string, '-', 1) -// && CHECK_AT(string, '-', 2)) -// || (CHECK_AT(string, '.', 0) -// && CHECK_AT(string, '.', 1) -// && CHECK_AT(string, '.', 2))) { -// block_indicators = 1; -// flow_indicators = 1; -// } -// -// preceeded_by_whitespace = 1; -// followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); -// -// while (string.pointer != string.end) -// { -// if (string.start == string.pointer) -// { -// if (CHECK(string, '#') || CHECK(string, ',') -// || CHECK(string, '[') || CHECK(string, ']') -// || CHECK(string, '{') || CHECK(string, '}') -// || CHECK(string, '&') || CHECK(string, '*') -// || CHECK(string, '!') || CHECK(string, '|') -// || CHECK(string, '>') || CHECK(string, '\'') -// || CHECK(string, '"') || CHECK(string, '%') -// || CHECK(string, '@') || CHECK(string, '`')) { -// flow_indicators = 1; -// block_indicators = 1; -// } -// -// if (CHECK(string, '?') || CHECK(string, ':')) { -// flow_indicators = 1; -// if (followed_by_whitespace) { -// block_indicators = 1; -// } -// } -// -// if (CHECK(string, '-') && followed_by_whitespace) { -// flow_indicators = 1; -// block_indicators = 1; -// } -// } -// else -// { -// if (CHECK(string, ',') || CHECK(string, '?') -// || CHECK(string, '[') || CHECK(string, ']') -// || CHECK(string, '{') || CHECK(string, '}')) { -// flow_indicators = 1; -// } -// -// if (CHECK(string, ':')) { -// flow_indicators = 1; -// if (followed_by_whitespace) { -// block_indicators = 1; -// } -// } -// -// if (CHECK(string, '#') && preceeded_by_whitespace) { -// flow_indicators = 1; -// block_indicators = 1; -// } -// } -// -// if (!IS_PRINTABLE(string) -// || (!IS_ASCII(string) && !emitter->unicode)) { -// special_characters = 1; -// } -// -// if (IS_BREAK(string)) { -// line_breaks = 1; -// } -// -// if (IS_SPACE(string)) -// { -// if (string.start == string.pointer) { -// leading_space = 1; -// } -// if (string.pointer+WIDTH(string) == string.end) { -// trailing_space = 1; -// } -// if (previous_break) { -// break_space = 1; -// } -// previous_space = 1; -// previous_break = 0; -// } -// else if (IS_BREAK(string)) -// { -// if (string.start == string.pointer) { -// leading_break = 1; -// } -// if (string.pointer+WIDTH(string) == string.end) { -// trailing_break = 1; -// } -// if (previous_space) { -// space_break = 1; -// } -// previous_space = 0; -// previous_break = 1; -// } -// else -// { -// previous_space = 0; -// previous_break = 0; -// } -// -// preceeded_by_whitespace = IS_BLANKZ(string); -// MOVE(string); -// if (string.pointer != string.end) { -// followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); -// } -// } -// -// emitter->scalar_data.multiline = line_breaks; -// -// emitter->scalar_data.flow_plain_allowed = 1; -// emitter->scalar_data.block_plain_allowed = 1; -// emitter->scalar_data.single_quoted_allowed = 1; -// emitter->scalar_data.block_allowed = 1; -// -// if (leading_space || leading_break || trailing_space || trailing_break) { -// emitter->scalar_data.flow_plain_allowed = 0; -// emitter->scalar_data.block_plain_allowed = 0; -// } -// -// if (trailing_space) { -// emitter->scalar_data.block_allowed = 0; -// } -// -// if (break_space) { -// emitter->scalar_data.flow_plain_allowed = 0; -// emitter->scalar_data.block_plain_allowed = 0; -// emitter->scalar_data.single_quoted_allowed = 0; -// } -// -// if (space_break || special_characters) { -// emitter->scalar_data.flow_plain_allowed = 0; -// emitter->scalar_data.block_plain_allowed = 0; -// emitter->scalar_data.single_quoted_allowed = 0; -// emitter->scalar_data.block_allowed = 0; -// } -// -// if (line_breaks) { -// emitter->scalar_data.flow_plain_allowed = 0; -// emitter->scalar_data.block_plain_allowed = 0; -// } -// -// if (flow_indicators) { -// emitter->scalar_data.flow_plain_allowed = 0; -// } -// -// if (block_indicators) { -// emitter->scalar_data.block_plain_allowed = 0; -// } -// -// return 1; -//} -// -///* -// * Check if the event data is valid. -// */ -// -//static int -//yaml_emitter_analyze_event(yaml_emitter_t *emitter, -// yaml_event_t *event) -//{ -// emitter->anchor_data.anchor = NULL; -// emitter->anchor_data.anchor_length = 0; -// emitter->tag_data.handle = NULL; -// emitter->tag_data.handle_length = 0; -// emitter->tag_data.suffix = NULL; -// emitter->tag_data.suffix_length = 0; -// emitter->scalar_data.value = NULL; -// emitter->scalar_data.length = 0; -// -// switch (event->type) -// { -// case YAML_ALIAS_EVENT: -// if (!yaml_emitter_analyze_anchor(emitter, -// event->data.alias.anchor, 1)) -// return 0; -// return 1; -// -// case YAML_SCALAR_EVENT: -// if (event->data.scalar.anchor) { -// if (!yaml_emitter_analyze_anchor(emitter, -// event->data.scalar.anchor, 0)) -// return 0; -// } -// if (event->data.scalar.tag && (emitter->canonical || -// (!event->data.scalar.plain_implicit -// && !event->data.scalar.quoted_implicit))) { -// if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag)) -// return 0; -// } -// if (!yaml_emitter_analyze_scalar(emitter, -// event->data.scalar.value, event->data.scalar.length)) -// return 0; -// return 1; -// -// case YAML_SEQUENCE_START_EVENT: -// if (event->data.sequence_start.anchor) { -// if (!yaml_emitter_analyze_anchor(emitter, -// event->data.sequence_start.anchor, 0)) -// return 0; -// } -// if (event->data.sequence_start.tag && (emitter->canonical || -// !event->data.sequence_start.implicit)) { -// if (!yaml_emitter_analyze_tag(emitter, -// event->data.sequence_start.tag)) -// return 0; -// } -// return 1; -// -// case YAML_MAPPING_START_EVENT: -// if (event->data.mapping_start.anchor) { -// if (!yaml_emitter_analyze_anchor(emitter, -// event->data.mapping_start.anchor, 0)) -// return 0; -// } -// if (event->data.mapping_start.tag && (emitter->canonical || -// !event->data.mapping_start.implicit)) { -// if (!yaml_emitter_analyze_tag(emitter, -// event->data.mapping_start.tag)) -// return 0; -// } -// return 1; -// -// default: -// return 1; -// } -//} -// -///* -// * Write the BOM character. -// */ -// -//static int -//yaml_emitter_write_bom(yaml_emitter_t *emitter) -//{ -// if (!FLUSH(emitter)) return 0; -// -// *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF'; -// *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB'; -// *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF'; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_indent(yaml_emitter_t *emitter) -//{ -// int indent = (emitter->indent >= 0) ? emitter->indent : 0; -// -// if (!emitter->indention || emitter->column > indent -// || (emitter->column == indent && !emitter->whitespace)) { -// if (!PUT_BREAK(emitter)) return 0; -// } -// -// while (emitter->column < indent) { -// if (!PUT(emitter, ' ')) return 0; -// } -// -// emitter->whitespace = 1; -// emitter->indention = 1; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_indicator(yaml_emitter_t *emitter, -// char *indicator, int need_whitespace, -// int is_whitespace, int is_indention) -//{ -// size_t indicator_length; -// yaml_string_t string; -// -// indicator_length = strlen(indicator); -// STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length); -// -// if (need_whitespace && !emitter->whitespace) { -// if (!PUT(emitter, ' ')) return 0; -// } -// -// while (string.pointer != string.end) { -// if (!WRITE(emitter, string)) return 0; -// } -// -// emitter->whitespace = is_whitespace; -// emitter->indention = (emitter->indention && is_indention); -// emitter->open_ended = 0; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_anchor(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length) -//{ -// yaml_string_t string; -// STRING_ASSIGN(string, value, length); -// -// while (string.pointer != string.end) { -// if (!WRITE(emitter, string)) return 0; -// } -// -// emitter->whitespace = 0; -// emitter->indention = 0; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length) -//{ -// yaml_string_t string; -// STRING_ASSIGN(string, value, length); -// -// if (!emitter->whitespace) { -// if (!PUT(emitter, ' ')) return 0; -// } -// -// while (string.pointer != string.end) { -// if (!WRITE(emitter, string)) return 0; -// } -// -// emitter->whitespace = 0; -// emitter->indention = 0; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_tag_content(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, -// int need_whitespace) -//{ -// yaml_string_t string; -// STRING_ASSIGN(string, value, length); -// -// if (need_whitespace && !emitter->whitespace) { -// if (!PUT(emitter, ' ')) return 0; -// } -// -// while (string.pointer != string.end) { -// if (IS_ALPHA(string) -// || CHECK(string, ';') || CHECK(string, '/') -// || CHECK(string, '?') || CHECK(string, ':') -// || CHECK(string, '@') || CHECK(string, '&') -// || CHECK(string, '=') || CHECK(string, '+') -// || CHECK(string, '$') || CHECK(string, ',') -// || CHECK(string, '_') || CHECK(string, '.') -// || CHECK(string, '~') || CHECK(string, '*') -// || CHECK(string, '\'') || CHECK(string, '(') -// || CHECK(string, ')') || CHECK(string, '[') -// || CHECK(string, ']')) { -// if (!WRITE(emitter, string)) return 0; -// } -// else { -// int width = WIDTH(string); -// unsigned int value; -// while (width --) { -// value = *(string.pointer++); -// if (!PUT(emitter, '%')) return 0; -// if (!PUT(emitter, (value >> 4) -// + ((value >> 4) < 10 ? '0' : 'A' - 10))) -// return 0; -// if (!PUT(emitter, (value & 0x0F) -// + ((value & 0x0F) < 10 ? '0' : 'A' - 10))) -// return 0; -// } -// } -// } -// -// emitter->whitespace = 0; -// emitter->indention = 0; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int allow_breaks) -//{ -// yaml_string_t string; -// int spaces = 0; -// int breaks = 0; -// -// STRING_ASSIGN(string, value, length); -// -// if (!emitter->whitespace) { -// if (!PUT(emitter, ' ')) return 0; -// } -// -// while (string.pointer != string.end) -// { -// if (IS_SPACE(string)) -// { -// if (allow_breaks && !spaces -// && emitter->column > emitter->best_width -// && !IS_SPACE_AT(string, 1)) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// MOVE(string); -// } -// else { -// if (!WRITE(emitter, string)) return 0; -// } -// spaces = 1; -// } -// else if (IS_BREAK(string)) -// { -// if (!breaks && CHECK(string, '\n')) { -// if (!PUT_BREAK(emitter)) return 0; -// } -// if (!WRITE_BREAK(emitter, string)) return 0; -// emitter->indention = 1; -// breaks = 1; -// } -// else -// { -// if (breaks) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// } -// if (!WRITE(emitter, string)) return 0; -// emitter->indention = 0; -// spaces = 0; -// breaks = 0; -// } -// } -// -// emitter->whitespace = 0; -// emitter->indention = 0; -// if (emitter->root_context) -// { -// emitter->open_ended = 1; -// } -// -// return 1; -//} -// -//static int -//yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int allow_breaks) -//{ -// yaml_string_t string; -// int spaces = 0; -// int breaks = 0; -// -// STRING_ASSIGN(string, value, length); -// -// if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) -// return 0; -// -// while (string.pointer != string.end) -// { -// if (IS_SPACE(string)) -// { -// if (allow_breaks && !spaces -// && emitter->column > emitter->best_width -// && string.pointer != string.start -// && string.pointer != string.end - 1 -// && !IS_SPACE_AT(string, 1)) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// MOVE(string); -// } -// else { -// if (!WRITE(emitter, string)) return 0; -// } -// spaces = 1; -// } -// else if (IS_BREAK(string)) -// { -// if (!breaks && CHECK(string, '\n')) { -// if (!PUT_BREAK(emitter)) return 0; -// } -// if (!WRITE_BREAK(emitter, string)) return 0; -// emitter->indention = 1; -// breaks = 1; -// } -// else -// { -// if (breaks) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// } -// if (CHECK(string, '\'')) { -// if (!PUT(emitter, '\'')) return 0; -// } -// if (!WRITE(emitter, string)) return 0; -// emitter->indention = 0; -// spaces = 0; -// breaks = 0; -// } -// } -// -// if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0)) -// return 0; -// -// emitter->whitespace = 0; -// emitter->indention = 0; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length, int allow_breaks) -//{ -// yaml_string_t string; -// int spaces = 0; -// -// STRING_ASSIGN(string, value, length); -// -// if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) -// return 0; -// -// while (string.pointer != string.end) -// { -// if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string)) -// || IS_BOM(string) || IS_BREAK(string) -// || CHECK(string, '"') || CHECK(string, '\\')) -// { -// unsigned char octet; -// unsigned int width; -// unsigned int value; -// int k; -// -// octet = string.pointer[0]; -// width = (octet & 0x80) == 0x00 ? 1 : -// (octet & 0xE0) == 0xC0 ? 2 : -// (octet & 0xF0) == 0xE0 ? 3 : -// (octet & 0xF8) == 0xF0 ? 4 : 0; -// value = (octet & 0x80) == 0x00 ? octet & 0x7F : -// (octet & 0xE0) == 0xC0 ? octet & 0x1F : -// (octet & 0xF0) == 0xE0 ? octet & 0x0F : -// (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; -// for (k = 1; k < (int)width; k ++) { -// octet = string.pointer[k]; -// value = (value << 6) + (octet & 0x3F); -// } -// string.pointer += width; -// -// if (!PUT(emitter, '\\')) return 0; -// -// switch (value) -// { -// case 0x00: -// if (!PUT(emitter, '0')) return 0; -// break; -// -// case 0x07: -// if (!PUT(emitter, 'a')) return 0; -// break; -// -// case 0x08: -// if (!PUT(emitter, 'b')) return 0; -// break; -// -// case 0x09: -// if (!PUT(emitter, 't')) return 0; -// break; -// -// case 0x0A: -// if (!PUT(emitter, 'n')) return 0; -// break; -// -// case 0x0B: -// if (!PUT(emitter, 'v')) return 0; -// break; -// -// case 0x0C: -// if (!PUT(emitter, 'f')) return 0; -// break; -// -// case 0x0D: -// if (!PUT(emitter, 'r')) return 0; -// break; -// -// case 0x1B: -// if (!PUT(emitter, 'e')) return 0; -// break; -// -// case 0x22: -// if (!PUT(emitter, '\"')) return 0; -// break; -// -// case 0x5C: -// if (!PUT(emitter, '\\')) return 0; -// break; -// -// case 0x85: -// if (!PUT(emitter, 'N')) return 0; -// break; -// -// case 0xA0: -// if (!PUT(emitter, '_')) return 0; -// break; -// -// case 0x2028: -// if (!PUT(emitter, 'L')) return 0; -// break; -// -// case 0x2029: -// if (!PUT(emitter, 'P')) return 0; -// break; -// -// default: -// if (value <= 0xFF) { -// if (!PUT(emitter, 'x')) return 0; -// width = 2; -// } -// else if (value <= 0xFFFF) { -// if (!PUT(emitter, 'u')) return 0; -// width = 4; -// } -// else { -// if (!PUT(emitter, 'U')) return 0; -// width = 8; -// } -// for (k = (width-1)*4; k >= 0; k -= 4) { -// int digit = (value >> k) & 0x0F; -// if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10))) -// return 0; -// } -// } -// spaces = 0; -// } -// else if (IS_SPACE(string)) -// { -// if (allow_breaks && !spaces -// && emitter->column > emitter->best_width -// && string.pointer != string.start -// && string.pointer != string.end - 1) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// if (IS_SPACE_AT(string, 1)) { -// if (!PUT(emitter, '\\')) return 0; -// } -// MOVE(string); -// } -// else { -// if (!WRITE(emitter, string)) return 0; -// } -// spaces = 1; -// } -// else -// { -// if (!WRITE(emitter, string)) return 0; -// spaces = 0; -// } -// } -// -// if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0)) -// return 0; -// -// emitter->whitespace = 0; -// emitter->indention = 0; -// -// return 1; -//} -// -//static int -//yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, -// yaml_string_t string) -//{ -// char indent_hint[2]; -// char *chomp_hint = NULL; -// -// if (IS_SPACE(string) || IS_BREAK(string)) -// { -// indent_hint[0] = '0' + (char)emitter->best_indent; -// indent_hint[1] = '\0'; -// if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0)) -// return 0; -// } -// -// emitter->open_ended = 0; -// -// string.pointer = string.end; -// if (string.start == string.pointer) -// { -// chomp_hint = "-"; -// } -// else -// { -// do { -// string.pointer --; -// } while ((*string.pointer & 0xC0) == 0x80); -// if (!IS_BREAK(string)) -// { -// chomp_hint = "-"; -// } -// else if (string.start == string.pointer) -// { -// chomp_hint = "+"; -// emitter->open_ended = 1; -// } -// else -// { -// do { -// string.pointer --; -// } while ((*string.pointer & 0xC0) == 0x80); -// if (IS_BREAK(string)) -// { -// chomp_hint = "+"; -// emitter->open_ended = 1; -// } -// } -// } -// -// if (chomp_hint) -// { -// if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0)) -// return 0; -// } -// -// return 1; -//} -// -//static int -//yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length) -//{ -// yaml_string_t string; -// int breaks = 1; -// -// STRING_ASSIGN(string, value, length); -// -// if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_block_scalar_hints(emitter, string)) -// return 0; -// if (!PUT_BREAK(emitter)) return 0; -// emitter->indention = 1; -// emitter->whitespace = 1; -// -// while (string.pointer != string.end) -// { -// if (IS_BREAK(string)) -// { -// if (!WRITE_BREAK(emitter, string)) return 0; -// emitter->indention = 1; -// breaks = 1; -// } -// else -// { -// if (breaks) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// } -// if (!WRITE(emitter, string)) return 0; -// emitter->indention = 0; -// breaks = 0; -// } -// } -// -// return 1; -//} -// -//static int -//yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, -// yaml_char_t *value, size_t length) -//{ -// yaml_string_t string; -// int breaks = 1; -// int leading_spaces = 1; -// -// STRING_ASSIGN(string, value, length); -// -// if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0)) -// return 0; -// if (!yaml_emitter_write_block_scalar_hints(emitter, string)) -// return 0; -// if (!PUT_BREAK(emitter)) return 0; -// emitter->indention = 1; -// emitter->whitespace = 1; -// -// while (string.pointer != string.end) -// { -// if (IS_BREAK(string)) -// { -// if (!breaks && !leading_spaces && CHECK(string, '\n')) { -// int k = 0; -// while (IS_BREAK_AT(string, k)) { -// k += WIDTH_AT(string, k); -// } -// if (!IS_BLANKZ_AT(string, k)) { -// if (!PUT_BREAK(emitter)) return 0; -// } -// } -// if (!WRITE_BREAK(emitter, string)) return 0; -// emitter->indention = 1; -// breaks = 1; -// } -// else -// { -// if (breaks) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// leading_spaces = IS_BLANK(string); -// } -// if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) -// && emitter->column > emitter->best_width) { -// if (!yaml_emitter_write_indent(emitter)) return 0; -// MOVE(string); -// } -// else { -// if (!WRITE(emitter, string)) return 0; -// } -// emitter->indention = 0; -// breaks = 0; -// } -// } -// -// return 1; -//} -// +import ( + "bytes" +) + +// Flush the buffer if needed. +func flush(emitter *yaml_emitter_t) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) { + return yaml_emitter_flush(emitter) + } + return true +} + +// Put a character to the output buffer. +func put(emitter *yaml_emitter_t, value byte) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) { + return false + } + emitter.buffer[emitter.buffer_pos] = value + emitter.buffer_pos++ + emitter.column++ + return true +} + +// Put a line break to the output buffer. +func put_break(emitter *yaml_emitter_t) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) { + return false + } + switch emitter.line_break { + case yaml_CR_BREAK: + emitter.buffer[emitter.buffer_pos] = '\r' + emitter.buffer_pos += 1 + case yaml_LN_BREAK: + emitter.buffer[emitter.buffer_pos] = '\n' + emitter.buffer_pos += 1 + case yaml_CRLN_BREAK: + emitter.buffer[emitter.buffer_pos+0] = '\r' + emitter.buffer[emitter.buffer_pos+1] = '\n' + emitter.buffer_pos += 2 + default: + panic("unknown line break setting") + } + emitter.column = 0 + emitter.line++ + return true +} + +// Copy a character from a string into buffer. +func write(emitter *yaml_emitter_t, s []byte, i *int) bool { + if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) { + return false + } + p := emitter.buffer_pos + w := width(s[*i]) + switch w { + case 4: + emitter.buffer[p+3] = s[*i+3] + fallthrough + case 3: + emitter.buffer[p+2] = s[*i+2] + fallthrough + case 2: + emitter.buffer[p+1] = s[*i+1] + fallthrough + case 1: + emitter.buffer[p+0] = s[*i+0] + default: + panic("unknown character width") + } + emitter.column++ + emitter.buffer_pos += w + *i += w + return true +} + +// Write a whole string into buffer. +func write_all(emitter *yaml_emitter_t, s []byte) bool { + for i := 0; i < len(s); { + if !write(emitter, s, &i) { + return false + } + } + return true +} + +// Copy a line break character from a string into buffer. +func write_break(emitter *yaml_emitter_t, s []byte, i *int) bool { + if s[*i] == '\n' { + if !put_break(emitter) { + return false + } + *i++ + } else { + if !write(emitter, s, i) { + return false + } + emitter.column = 0 + emitter.line++ + } + return true +} + +// Set an emitter error and return false. +func yaml_emitter_set_emitter_error(emitter *yaml_emitter_t, problem string) bool { + emitter.error = yaml_EMITTER_ERROR + emitter.problem = problem + return false +} + +// Emit an event. +func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool { + emitter.events = append(emitter.events, *event) + for !yaml_emitter_need_more_events(emitter) { + event := &emitter.events[emitter.events_head] + if !yaml_emitter_analyze_event(emitter, event) { + return false + } + if !yaml_emitter_state_machine(emitter, event) { + return false + } + yaml_event_delete(event) + emitter.events_head++ + } + return true +} + +// Check if we need to accumulate more events before emitting. +// +// We accumulate extra +// - 1 event for DOCUMENT-START +// - 2 events for SEQUENCE-START +// - 3 events for MAPPING-START +// +func yaml_emitter_need_more_events(emitter *yaml_emitter_t) bool { + if emitter.events_head == len(emitter.events) { + return true + } + var accumulate int + switch emitter.events[emitter.events_head].typ { + case yaml_DOCUMENT_START_EVENT: + accumulate = 1 + break + case yaml_SEQUENCE_START_EVENT: + accumulate = 2 + break + case yaml_MAPPING_START_EVENT: + accumulate = 3 + break + default: + return false + } + if len(emitter.events)-emitter.events_head > accumulate { + return false + } + var level int + for i := emitter.events_head; i < len(emitter.events); i++ { + switch emitter.events[i].typ { + case yaml_STREAM_START_EVENT, yaml_DOCUMENT_START_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT: + level++ + case yaml_STREAM_END_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT: + level-- + } + if level == 0 { + return false + } + } + return true +} + +// Append a directive to the directives stack. +func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_directive_t, allow_duplicates bool) bool { + for i := 0; i < len(emitter.tag_directives); i++ { + if bytes.Equal(value.handle, emitter.tag_directives[i].handle) { + if allow_duplicates { + return true + } + return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive") + } + } + + // [Go] Do we actually need to copy this given garbage collection + // and the lack of deallocating destructors? + tag_copy := yaml_tag_directive_t{ + handle: make([]byte, len(value.handle)), + prefix: make([]byte, len(value.prefix)), + } + copy(tag_copy.handle, value.handle) + copy(tag_copy.prefix, value.prefix) + emitter.tag_directives = append(emitter.tag_directives, tag_copy) + return true +} + +// Increase the indentation level. +func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool { + emitter.indents = append(emitter.indents, emitter.indent) + if emitter.indent < 0 { + if flow { + emitter.indent = emitter.best_indent + } else { + emitter.indent = 0 + } + } else if !indentless { + emitter.indent += emitter.best_indent + } + return true +} + +// State dispatcher. +func yaml_emitter_state_machine(emitter *yaml_emitter_t, event *yaml_event_t) bool { + switch emitter.state { + default: + case yaml_EMIT_STREAM_START_STATE: + return yaml_emitter_emit_stream_start(emitter, event) + + case yaml_EMIT_FIRST_DOCUMENT_START_STATE: + return yaml_emitter_emit_document_start(emitter, event, true) + + case yaml_EMIT_DOCUMENT_START_STATE: + return yaml_emitter_emit_document_start(emitter, event, false) + + case yaml_EMIT_DOCUMENT_CONTENT_STATE: + return yaml_emitter_emit_document_content(emitter, event) + + case yaml_EMIT_DOCUMENT_END_STATE: + return yaml_emitter_emit_document_end(emitter, event) + + case yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: + return yaml_emitter_emit_flow_sequence_item(emitter, event, true) + + case yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE: + return yaml_emitter_emit_flow_sequence_item(emitter, event, false) + + case yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: + return yaml_emitter_emit_flow_mapping_key(emitter, event, true) + + case yaml_EMIT_FLOW_MAPPING_KEY_STATE: + return yaml_emitter_emit_flow_mapping_key(emitter, event, false) + + case yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: + return yaml_emitter_emit_flow_mapping_value(emitter, event, true) + + case yaml_EMIT_FLOW_MAPPING_VALUE_STATE: + return yaml_emitter_emit_flow_mapping_value(emitter, event, false) + + case yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: + return yaml_emitter_emit_block_sequence_item(emitter, event, true) + + case yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE: + return yaml_emitter_emit_block_sequence_item(emitter, event, false) + + case yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: + return yaml_emitter_emit_block_mapping_key(emitter, event, true) + + case yaml_EMIT_BLOCK_MAPPING_KEY_STATE: + return yaml_emitter_emit_block_mapping_key(emitter, event, false) + + case yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: + return yaml_emitter_emit_block_mapping_value(emitter, event, true) + + case yaml_EMIT_BLOCK_MAPPING_VALUE_STATE: + return yaml_emitter_emit_block_mapping_value(emitter, event, false) + + case yaml_EMIT_END_STATE: + return yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END") + } + panic("invalid emitter state") +} + +// Expect STREAM-START. +func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if event.typ != yaml_STREAM_START_EVENT { + return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START") + } + if emitter.encoding == yaml_ANY_ENCODING { + emitter.encoding = event.stream_start.encoding + if emitter.encoding == yaml_ANY_ENCODING { + emitter.encoding = yaml_UTF8_ENCODING + } + } + if emitter.best_indent < 2 || emitter.best_indent > 9 { + emitter.best_indent = 2 + } + if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 { + emitter.best_width = 80 + } + if emitter.best_width < 0 { + emitter.best_width = 1<<31 - 1 + } + if emitter.line_break == yaml_ANY_BREAK { + emitter.line_break = yaml_LN_BREAK + } + + emitter.indent = -1 + emitter.line = 0 + emitter.column = 0 + emitter.whitespace = true + emitter.indention = true + + if emitter.encoding != yaml_UTF8_ENCODING { + if !yaml_emitter_write_bom(emitter) { + return false + } + } + emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE + return true +} + +// Expect DOCUMENT-START or STREAM-END. +func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + + if event.typ == yaml_DOCUMENT_START_EVENT { + + if event.document_start.version_directive != nil { + if !yaml_emitter_analyze_version_directive(emitter, event.document_start.version_directive) { + return false + } + } + + for i := 0; i < len(event.document_start.tag_directives); i++ { + tag_directive := &event.document_start.tag_directives[i] + if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) { + return false + } + if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) { + return false + } + } + + for i := 0; i < len(default_tag_directives); i++ { + tag_directive := &default_tag_directives[i] + if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) { + return false + } + } + + implicit := event.document_start.implicit + if !first || emitter.canonical { + implicit = false + } + + if emitter.open_ended && (event.document_start.version_directive != nil || len(event.document_start.tag_directives) > 0) { + if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if event.document_start.version_directive != nil { + implicit = false + if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if len(event.document_start.tag_directives) > 0 { + implicit = false + for i := 0; i < len(event.document_start.tag_directives); i++ { + tag_directive := &event.document_start.tag_directives[i] + if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) { + return false + } + if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) { + return false + } + if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + } + + if yaml_emitter_check_empty_document(emitter) { + implicit = false + } + if !implicit { + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) { + return false + } + if emitter.canonical { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + } + + emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE + return true + } + + if event.typ == yaml_STREAM_END_EVENT { + if emitter.open_ended { + if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_flush(emitter) { + return false + } + emitter.state = yaml_EMIT_END_STATE + return true + } + + return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END") +} + +// Expect the root node. +func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool { + emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE) + return yaml_emitter_emit_node(emitter, event, true, false, false, false) +} + +// Expect DOCUMENT-END. +func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if event.typ != yaml_DOCUMENT_END_EVENT { + return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END") + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !event.document_end.implicit { + // [Go] Allocate the slice elsewhere. + if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_flush(emitter) { + return false + } + emitter.state = yaml_EMIT_DOCUMENT_START_STATE + emitter.tag_directives = emitter.tag_directives[:0] + return true +} + +// Expect a flow item node. +func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + if first { + if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) { + return false + } + if !yaml_emitter_increase_indent(emitter, true, false) { + return false + } + emitter.flow_level++ + } + + if event.typ == yaml_SEQUENCE_END_EVENT { + emitter.flow_level-- + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + if emitter.canonical && !first { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) { + return false + } + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + + return true + } + + if !first { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + + if emitter.canonical || emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE) + return yaml_emitter_emit_node(emitter, event, false, true, false, false) +} + +// Expect a flow key node. +func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + if first { + if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) { + return false + } + if !yaml_emitter_increase_indent(emitter, true, false) { + return false + } + emitter.flow_level++ + } + + if event.typ == yaml_MAPPING_END_EVENT { + emitter.flow_level-- + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + if emitter.canonical && !first { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) { + return false + } + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true + } + + if !first { + if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { + return false + } + } + if emitter.canonical || emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + + if !emitter.canonical && yaml_emitter_check_simple_key(emitter) { + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, true) + } else { + if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) { + return false + } + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, false) + } +} + +// Expect a flow value node. +func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool { + if simple { + if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) { + return false + } + } else { + if emitter.canonical || emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) { + return false + } + } + emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, false) +} + +// Expect a block item node. +func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + if first { + if !yaml_emitter_increase_indent(emitter, false, emitter.mapping_context && !emitter.indention) { + return false + } + } + if event.typ == yaml_SEQUENCE_END_EVENT { + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) { + return false + } + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE) + return yaml_emitter_emit_node(emitter, event, false, true, false, false) +} + +// Expect a block key node. +func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { + if first { + if !yaml_emitter_increase_indent(emitter, false, false) { + return false + } + } + if event.typ == yaml_MAPPING_END_EVENT { + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true + } + if !yaml_emitter_write_indent(emitter) { + return false + } + if yaml_emitter_check_simple_key(emitter) { + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, true) + } else { + if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) { + return false + } + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, false) + } +} + +// Expect a block value node. +func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool { + if simple { + if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) { + return false + } + } else { + if !yaml_emitter_write_indent(emitter) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) { + return false + } + } + emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE) + return yaml_emitter_emit_node(emitter, event, false, false, true, false) +} + +// Expect a node. +func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t, + root bool, sequence bool, mapping bool, simple_key bool) bool { + + emitter.root_context = root + emitter.sequence_context = sequence + emitter.mapping_context = mapping + emitter.simple_key_context = simple_key + + switch event.typ { + case yaml_ALIAS_EVENT: + return yaml_emitter_emit_alias(emitter, event) + case yaml_SCALAR_EVENT: + return yaml_emitter_emit_scalar(emitter, event) + case yaml_SEQUENCE_START_EVENT: + return yaml_emitter_emit_sequence_start(emitter, event) + case yaml_MAPPING_START_EVENT: + return yaml_emitter_emit_mapping_start(emitter, event) + default: + return yaml_emitter_set_emitter_error(emitter, + "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS") + } + return false +} + +// Expect ALIAS. +func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_process_anchor(emitter) { + return false + } + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true +} + +// Expect SCALAR. +func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_select_scalar_style(emitter, event) { + return false + } + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } + if !yaml_emitter_increase_indent(emitter, true, false) { + return false + } + if !yaml_emitter_process_scalar(emitter) { + return false + } + emitter.indent = emitter.indents[len(emitter.indents)-1] + emitter.indents = emitter.indents[:len(emitter.indents)-1] + emitter.state = emitter.states[len(emitter.states)-1] + emitter.states = emitter.states[:len(emitter.states)-1] + return true +} + +// Expect SEQUENCE-START. +func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } + if emitter.flow_level > 0 || emitter.canonical || + event.sequence_start.style == yaml_FLOW_SEQUENCE_STYLE || + yaml_emitter_check_empty_sequence(emitter) { + emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE + } else { + emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE + } + return true +} + +// Expect MAPPING-START. +func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { + if !yaml_emitter_process_anchor(emitter) { + return false + } + if !yaml_emitter_process_tag(emitter) { + return false + } + if emitter.flow_level > 0 || emitter.canonical || + event.mapping_start.style == yaml_FLOW_MAPPING_STYLE || + yaml_emitter_check_empty_mapping(emitter) { + emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE + } else { + emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE + } + return true +} + +// Check if the document content is an empty scalar. +func yaml_emitter_check_empty_document(emitter *yaml_emitter_t) bool { + return false // [Go] Huh? +} + +// Check if the next events represent an empty sequence. +func yaml_emitter_check_empty_sequence(emitter *yaml_emitter_t) bool { + if len(emitter.events)-emitter.events_head < 2 { + return false + } + return emitter.events[emitter.events_head].typ == yaml_SEQUENCE_START_EVENT && + emitter.events[emitter.events_head+1].typ == yaml_SEQUENCE_END_EVENT +} + +// Check if the next events represent an empty mapping. +func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool { + if len(emitter.events)-emitter.events_head < 2 { + return false + } + return emitter.events[emitter.events_head].typ == yaml_MAPPING_START_EVENT && + emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT +} + +// Check if the next node can be expressed as a simple key. +func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool { + length := 0 + switch emitter.events[emitter.events_head].typ { + case yaml_ALIAS_EVENT: + length += len(emitter.anchor_data.anchor) + case yaml_SCALAR_EVENT: + if emitter.scalar_data.multiline { + return false + } + length += len(emitter.anchor_data.anchor) + + len(emitter.tag_data.handle) + + len(emitter.tag_data.suffix) + + len(emitter.scalar_data.value) + case yaml_SEQUENCE_START_EVENT: + if !yaml_emitter_check_empty_sequence(emitter) { + return false + } + length += len(emitter.anchor_data.anchor) + + len(emitter.tag_data.handle) + + len(emitter.tag_data.suffix) + case yaml_MAPPING_START_EVENT: + if !yaml_emitter_check_empty_mapping(emitter) { + return false + } + length += len(emitter.anchor_data.anchor) + + len(emitter.tag_data.handle) + + len(emitter.tag_data.suffix) + default: + return false + } + return length <= 128 +} + +// Determine an acceptable scalar style. +func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event_t) bool { + + no_tag := len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 + if no_tag && !event.scalar.plain_implicit && !event.scalar.quoted_implicit { + return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified") + } + + style := event.scalar.style + if style == yaml_ANY_SCALAR_STYLE { + style = yaml_PLAIN_SCALAR_STYLE + } + if emitter.canonical { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + if emitter.simple_key_context && emitter.scalar_data.multiline { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + + if style == yaml_PLAIN_SCALAR_STYLE { + if emitter.flow_level > 0 && !emitter.scalar_data.flow_plain_allowed || + emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed { + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + } + if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) { + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + } + if no_tag && !event.scalar.plain_implicit { + style = yaml_SINGLE_QUOTED_SCALAR_STYLE + } + } + if style == yaml_SINGLE_QUOTED_SCALAR_STYLE { + if !emitter.scalar_data.single_quoted_allowed { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + } + if style == yaml_LITERAL_SCALAR_STYLE || style == yaml_FOLDED_SCALAR_STYLE { + if !emitter.scalar_data.block_allowed || emitter.flow_level > 0 || emitter.simple_key_context { + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + } + } + + if no_tag && !event.scalar.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE { + emitter.tag_data.handle = []byte{'!'} + } + emitter.scalar_data.style = style + return true +} + +// Write an achor. +func yaml_emitter_process_anchor(emitter *yaml_emitter_t) bool { + if emitter.anchor_data.anchor == nil { + return true + } + c := []byte{'&'} + if emitter.anchor_data.alias { + c[0] = '*' + } + if !yaml_emitter_write_indicator(emitter, c, true, false, false) { + return false + } + return yaml_emitter_write_anchor(emitter, emitter.anchor_data.anchor) +} + +// Write a tag. +func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool { + if len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 { + return true + } + if len(emitter.tag_data.handle) > 0 { + if !yaml_emitter_write_tag_handle(emitter, emitter.tag_data.handle) { + return false + } + if len(emitter.tag_data.suffix) > 0 { + if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) { + return false + } + } + } else { + // [Go] Allocate these slices elsewhere. + if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) { + return false + } + if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) { + return false + } + if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) { + return false + } + } + return true +} + +// Write a scalar. +func yaml_emitter_process_scalar(emitter *yaml_emitter_t) bool { + switch emitter.scalar_data.style { + case yaml_PLAIN_SCALAR_STYLE: + return yaml_emitter_write_plain_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context) + + case yaml_SINGLE_QUOTED_SCALAR_STYLE: + return yaml_emitter_write_single_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context) + + case yaml_DOUBLE_QUOTED_SCALAR_STYLE: + return yaml_emitter_write_double_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context) + + case yaml_LITERAL_SCALAR_STYLE: + return yaml_emitter_write_literal_scalar(emitter, emitter.scalar_data.value) + + case yaml_FOLDED_SCALAR_STYLE: + return yaml_emitter_write_folded_scalar(emitter, emitter.scalar_data.value) + } + panic("unknown scalar style") +} + +// Check if a %YAML directive is valid. +func yaml_emitter_analyze_version_directive(emitter *yaml_emitter_t, version_directive *yaml_version_directive_t) bool { + if version_directive.major != 1 || version_directive.minor != 1 { + return yaml_emitter_set_emitter_error(emitter, "incompatible %YAML directive") + } + return true +} + +// Check if a %TAG directive is valid. +func yaml_emitter_analyze_tag_directive(emitter *yaml_emitter_t, tag_directive *yaml_tag_directive_t) bool { + handle := tag_directive.handle + prefix := tag_directive.prefix + if len(handle) == 0 { + return yaml_emitter_set_emitter_error(emitter, "tag handle must not be empty") + } + if handle[0] != '!' { + return yaml_emitter_set_emitter_error(emitter, "tag handle must start with '!'") + } + if handle[len(handle)-1] != '!' { + return yaml_emitter_set_emitter_error(emitter, "tag handle must end with '!'") + } + for i := 1; i < len(handle)-1; i += width(handle[i]) { + if !is_alpha(handle, i) { + return yaml_emitter_set_emitter_error(emitter, "tag handle must contain alphanumerical characters only") + } + } + if len(prefix) == 0 { + return yaml_emitter_set_emitter_error(emitter, "tag prefix must not be empty") + } + return true +} + +// Check if an anchor is valid. +func yaml_emitter_analyze_anchor(emitter *yaml_emitter_t, anchor []byte, alias bool) bool { + if len(anchor) == 0 { + problem := "anchor value must not be empty" + if alias { + problem = "alias value must not be empty" + } + return yaml_emitter_set_emitter_error(emitter, problem) + } + for i := 0; i < len(anchor); i += width(anchor[i]) { + if !is_alpha(anchor, i) { + problem := "anchor value must contain alphanumerical characters only" + if alias { + problem = "alias value must contain alphanumerical characters only" + } + return yaml_emitter_set_emitter_error(emitter, problem) + } + } + emitter.anchor_data.anchor = anchor + emitter.anchor_data.alias = alias + return true +} + +// Check if a tag is valid. +func yaml_emitter_analyze_tag(emitter *yaml_emitter_t, tag []byte) bool { + if len(tag) == 0 { + return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty") + } + for i := 0; i < len(emitter.tag_directives); i++ { + tag_directive := &emitter.tag_directives[i] + if bytes.HasPrefix(tag, tag_directive.prefix) { + emitter.tag_data.handle = tag_directive.handle + emitter.tag_data.suffix = tag[len(tag_directive.prefix):] + } + return true + } + emitter.tag_data.suffix = tag + return true +} + +// Check if a scalar is valid. +func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool { + var ( + block_indicators = false + flow_indicators = false + line_breaks = false + special_characters = false + + leading_space = false + leading_break = false + trailing_space = false + trailing_break = false + break_space = false + space_break = false + + preceeded_by_whitespace = false + followed_by_whitespace = false + previous_space = false + previous_break = false + ) + + emitter.scalar_data.value = value + + if len(value) == 0 { + emitter.scalar_data.multiline = false + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = true + emitter.scalar_data.single_quoted_allowed = true + emitter.scalar_data.block_allowed = false + return true + } + + if (value[0] == '-' && value[1] == '-' && value[2] == '-') || (value[0] == '.' && value[1] == '.' && value[2] == '.') { + block_indicators = true + flow_indicators = true + } + + preceeded_by_whitespace = true + for i, w := 0, 0; i < len(value); i += w { + w = width(value[0]) + followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w) + + if i == 0 { + switch value[i] { + case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`': + flow_indicators = true + block_indicators = true + case '?', ':': + flow_indicators = true + if followed_by_whitespace { + block_indicators = true + } + case '-': + if followed_by_whitespace { + flow_indicators = true + block_indicators = true + } + } + } else { + switch value[i] { + case ',', '?', '[', ']', '{', '}': + flow_indicators = true + case ':': + flow_indicators = true + if followed_by_whitespace { + block_indicators = true + } + case '#': + if preceeded_by_whitespace { + flow_indicators = true + block_indicators = true + } + } + } + + if !is_printable(value, i) || !is_ascii(value, i) && !emitter.unicode { + special_characters = true + } + if is_space(value, i) { + if i == 0 { + leading_space = true + } + if i+width(value[i]) == len(value) { + trailing_space = true + } + if previous_break { + break_space = true + } + previous_space = true + previous_break = false + } else if is_break(value, i) { + line_breaks = true + if i == 0 { + leading_break = true + } + if i+width(value[i]) == len(value) { + trailing_break = true + } + if previous_space { + space_break = true + } + previous_space = false + previous_break = true + } else { + previous_space = false + previous_break = false + } + + // [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition. + preceeded_by_whitespace = is_blankz(value, i) + } + + emitter.scalar_data.multiline = line_breaks + emitter.scalar_data.flow_plain_allowed = true + emitter.scalar_data.block_plain_allowed = true + emitter.scalar_data.single_quoted_allowed = true + emitter.scalar_data.block_allowed = true + + if leading_space || leading_break || trailing_space || trailing_break { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + } + if trailing_space { + emitter.scalar_data.block_allowed = false + } + if break_space { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + emitter.scalar_data.single_quoted_allowed = false + } + if space_break || special_characters { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + emitter.scalar_data.single_quoted_allowed = false + emitter.scalar_data.block_allowed = false + } + if line_breaks { + emitter.scalar_data.flow_plain_allowed = false + emitter.scalar_data.block_plain_allowed = false + } + if flow_indicators { + emitter.scalar_data.flow_plain_allowed = false + } + if block_indicators { + emitter.scalar_data.block_plain_allowed = false + } + return true +} + +// Check if the event data is valid. +func yaml_emitter_analyze_event(emitter *yaml_emitter_t, event *yaml_event_t) bool { + + emitter.anchor_data.anchor = nil + emitter.tag_data.handle = nil + emitter.tag_data.suffix = nil + emitter.scalar_data.value = nil + + switch event.typ { + case yaml_ALIAS_EVENT: + if !yaml_emitter_analyze_anchor(emitter, event.alias.anchor, true) { + return false + } + + case yaml_SCALAR_EVENT: + if len(event.scalar.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.scalar.anchor, false) { + return false + } + } + if len(event.scalar.tag) > 0 && (emitter.canonical || (!event.scalar.plain_implicit && !event.scalar.quoted_implicit)) { + if !yaml_emitter_analyze_tag(emitter, event.scalar.tag) { + return false + } + } + if !yaml_emitter_analyze_scalar(emitter, event.scalar.value) { + return false + } + + case yaml_SEQUENCE_START_EVENT: + if len(event.sequence_start.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.sequence_start.anchor, false) { + return false + } + } + if len(event.sequence_start.tag) > 0 && (emitter.canonical || !event.sequence_start.implicit) { + if !yaml_emitter_analyze_tag(emitter, event.sequence_start.tag) { + return false + } + } + + case yaml_MAPPING_START_EVENT: + if len(event.mapping_start.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.mapping_start.anchor, false) { + return false + } + } + if len(event.mapping_start.tag) > 0 && (emitter.canonical || !event.mapping_start.implicit) { + if !yaml_emitter_analyze_tag(emitter, event.mapping_start.tag) { + return false + } + } + } + return true +} + +// Write the BOM character. +func yaml_emitter_write_bom(emitter *yaml_emitter_t) bool { + if !flush(emitter) { + return false + } + pos := emitter.buffer_pos + emitter.buffer[pos+0] = '\xEF' + emitter.buffer[pos+1] = '\xBB' + emitter.buffer[pos+2] = '\xBF' + emitter.buffer_pos += 3 + return true +} + +func yaml_emitter_write_indent(emitter *yaml_emitter_t) bool { + indent := emitter.indent + if indent < 0 { + indent = 0 + } + if !emitter.indention || emitter.column > indent || (emitter.column == indent && !emitter.whitespace) { + if !put_break(emitter) { + return false + } + } + for emitter.column < indent { + if !put(emitter, ' ') { + return false + } + } + emitter.whitespace = true + emitter.indention = true + return true +} + +func yaml_emitter_write_indicator(emitter *yaml_emitter_t, indicator []byte, need_whitespace, is_whitespace, is_indention bool) bool { + if need_whitespace && !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + if !write_all(emitter, indicator) { + return false + } + emitter.whitespace = is_whitespace + emitter.indention = (emitter.indention && is_indention) + emitter.open_ended = false + return true +} + +func yaml_emitter_write_anchor(emitter *yaml_emitter_t, value []byte) bool { + if !write_all(emitter, value) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool { + if !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + if !write_all(emitter, value) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool { + if need_whitespace && !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + for i := 0; i < len(value); { + var must_write bool + switch value[i] { + case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']': + must_write = true + default: + must_write = is_alpha(value, i) + } + if must_write { + if !write(emitter, value, &i) { + return false + } + } else { + w := width(value[i]) + for k := 0; k < w; k++ { + octet := value[i] + i++ + + c := octet >> 4 + if c < 10 { + c += '0' + } else { + c += 'A' - 10 + } + if !put(emitter, c) { + return false + } + + c = octet & 0x0f + if c < 10 { + c += '0' + } else { + c += 'A' - 10 + } + if !put(emitter, c) { + return false + } + } + } + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_plain_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool { + if !emitter.whitespace { + if !put(emitter, ' ') { + return false + } + } + + spaces := false + breaks := false + for i := 0; i < len(value); { + if is_space(value, i) { + if allow_breaks && !spaces && emitter.column > emitter.best_width && !is_space(value, i+1) { + if !yaml_emitter_write_indent(emitter) { + return false + } + i += width(value[i]) + } else { + if !write(emitter, value, &i) { + return false + } + } + spaces = true + } else if is_break(value, i) { + if !breaks && value[i] == '\n' { + if !put_break(emitter) { + return false + } + } + if !write_break(emitter, value, &i) { + return false + } + emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !write(emitter, value, &i) { + return false + } + emitter.indention = false + spaces = false + breaks = false + } + } + + emitter.whitespace = false + emitter.indention = false + if emitter.root_context { + emitter.open_ended = true + } + + return true +} + +func yaml_emitter_write_single_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool { + + if !yaml_emitter_write_indicator(emitter, []byte{'\''}, true, false, false) { + return false + } + + spaces := false + breaks := false + for i := 0; i < len(value); { + if is_space(value, i) { + if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 && !is_space(value, i+1) { + if !yaml_emitter_write_indent(emitter) { + return false + } + i += width(value[i]) + } else { + if !write(emitter, value, &i) { + return false + } + } + spaces = true + } else if is_break(value, i) { + if !breaks && value[i] == '\n' { + if !put_break(emitter) { + return false + } + } + if !write_break(emitter, value, &i) { + return false + } + emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if value[i] == '\'' { + if !put(emitter, '\'') { + return false + } + } + if !write(emitter, value, &i) { + return false + } + emitter.indention = false + spaces = false + breaks = false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{'\''}, false, false, false) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_double_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool { + spaces := false + if !yaml_emitter_write_indicator(emitter, []byte{'"'}, true, false, false) { + return false + } + + for i := 0; i < len(value); { + if !is_printable(value, i) || (!emitter.unicode && !is_ascii(value, i)) || + is_bom(value, i) || is_break(value, i) || + value[i] == '"' || value[i] == '\\' { + + octet := value[i] + + var w int + var v rune + switch { + case octet&0x80 == 0x00: + w, v = 1, rune(octet&0x7F) + case octet&0xE0 == 0xC0: + w, v = 2, rune(octet&0x1F) + case octet&0xF0 == 0xE0: + w, v = 3, rune(octet&0x0F) + case octet&0xF8 == 0xF0: + w, v = 4, rune(octet&0x07) + } + for k := 1; k < w; k++ { + octet = value[i+k] + v = (v << 6) + (rune(octet) & 0x3F) + } + i += w + + if !put(emitter, '\\') { + return false + } + + var ok bool + switch v { + case 0x00: + ok = put(emitter, '0') + case 0x07: + ok = put(emitter, 'a') + case 0x08: + ok = put(emitter, 'b') + case 0x09: + ok = put(emitter, 't') + case 0x0A: + ok = put(emitter, 'n') + case 0x0b: + ok = put(emitter, 'v') + case 0x0c: + ok = put(emitter, 'f') + case 0x0d: + ok = put(emitter, 'r') + case 0x1b: + ok = put(emitter, 'e') + case 0x22: + ok = put(emitter, '"') + case 0x5c: + ok = put(emitter, '\\') + case 0x85: + ok = put(emitter, 'N') + case 0xA0: + ok = put(emitter, '_') + case 0x2028: + ok = put(emitter, 'L') + case 0x2029: + ok = put(emitter, 'P') + default: + if v <= 0xFF { + ok = put(emitter, 'x') + w= 2 + } else if v <= 0xFFFF { + ok = put(emitter, 'u') + w= 4 + } else { + ok = put(emitter, 'U') + w= 8 + } + for k := (w - 1) * 4; ok && k >= 0; k -= 4 { + digit := byte((v >> uint(k)) & 0x0F) + if digit < 10 { + ok = put(emitter, digit+'0') + } else { + ok = put(emitter, digit+'A'-10) + } + } + } + if !ok { + return false + } + spaces = false + } else if is_space(value, i) { + if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 { + if !yaml_emitter_write_indent(emitter) { + return false + } + if is_space(value, i+1) { + if !put(emitter, '\\') { + return false + } + } + i += width(value[i]) + } else if !write(emitter, value, &i) { + return false + } + spaces = true + } else { + if !write(emitter, value, &i) { + return false + } + spaces = false + } + } + if !yaml_emitter_write_indicator(emitter, []byte{'"'}, false, false, false) { + return false + } + emitter.whitespace = false + emitter.indention = false + return true +} + +func yaml_emitter_write_block_scalar_hints(emitter *yaml_emitter_t, value []byte) bool { + if is_space(value, 0) || is_break(value, 0) { + indent_hint := []byte{'0' + byte(emitter.best_indent)} + if !yaml_emitter_write_indicator(emitter, indent_hint, false, false, false) { + return false + } + } + + emitter.open_ended = false + + var chomp_hint [1]byte + if len(value) == 0 { + chomp_hint[0] = '-' + } else { + i := len(value) - 1 + for value[i]&0xC0 == 0x80 { + i-- + } + if !is_break(value, i) { + chomp_hint[0] = '-' + } else if i == 0 { + chomp_hint[0] = '+' + emitter.open_ended = true + } else { + i-- + for value[i]&0xC0 == 0x80 { + i-- + } + if is_break(value, i) { + chomp_hint[0] = '+' + emitter.open_ended = true + } + } + } + if chomp_hint[0] != 0 { + if !yaml_emitter_write_indicator(emitter, chomp_hint[:], false, false, false) { + return false + } + } + return true +} + +func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bool { + if !yaml_emitter_write_indicator(emitter, []byte{'|'}, true, false, false) { + return false + } + if !yaml_emitter_write_block_scalar_hints(emitter, value) { + return false + } + if !put_break(emitter) { + return false + } + emitter.indention = true + emitter.whitespace = true + breaks := true + for i := 0; i < len(value); { + if is_break(value, i) { + if !write_break(emitter, value, &i) { + return false + } + emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + } + if !write(emitter, value, &i) { + return false + } + emitter.indention = false + breaks = false + } + } + + return true +} + +func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) bool { + if !yaml_emitter_write_indicator(emitter, []byte{'>'}, true, false, false) { + return false + } + if !yaml_emitter_write_block_scalar_hints(emitter, value) { + return false + } + + if !put_break(emitter) { + return false + } + emitter.indention = true + emitter.whitespace = true + + breaks := true + leading_spaces := true + for i := 0; i < len(value); { + if is_break(value, i) { + if !breaks && !leading_spaces && value[i] == '\n' { + k := 0 + for is_break(value, k) { + k += width(value[k]) + } + if !is_blankz(value, k) { + if !put_break(emitter) { + return false + } + } + } + if !write_break(emitter, value, &i) { + return false + } + emitter.indention = true + breaks = true + } else { + if breaks { + if !yaml_emitter_write_indent(emitter) { + return false + } + leading_spaces = is_blank(value, i) + } + if !breaks && is_space(value, i) && !is_space(value, i+1) && emitter.column > emitter.best_width { + if !yaml_emitter_write_indent(emitter) { + return false + } + i += width(value[i]) + } else { + if !write(emitter, value, &i) { + return false + } + } + emitter.indention = false + breaks = false + } + } + return true +} diff --git a/encode.go b/encode.go index e6375e2..ad7853c 100644 --- a/encode.go +++ b/encode.go @@ -1,88 +1,56 @@ package goyaml -// #cgo LDFLAGS: -lm -lpthread -// #cgo windows CFLAGS: -DYAML_DECLARE_STATIC=1 -Dstrdup=_strdup -// #cgo CFLAGS: -I. -DHAVE_CONFIG_H=1 -// -// #include "helpers.h" -import "C" - import ( "reflect" "sort" "strconv" - "unsafe" ) type encoder struct { - emitter C.yaml_emitter_t - event C.yaml_event_t + emitter yaml_emitter_t + event yaml_event_t out []byte - tmp []byte - tmph *reflect.SliceHeader flow bool } -//export outputHandler -func outputHandler(data unsafe.Pointer, buffer *C.uchar, size C.size_t) C.int { - e := (*encoder)(data) - e.tmph.Data = uintptr(unsafe.Pointer(buffer)) - e.tmph.Len = int(size) - e.tmph.Cap = int(size) - e.out = append(e.out, e.tmp...) - return 1 -} - func newEncoder() (e *encoder) { e = &encoder{} - e.tmph = (*reflect.SliceHeader)(unsafe.Pointer(&e.tmp)) - if C.yaml_emitter_initialize(&e.emitter) == 0 { - panic("Failed to initialize YAML emitter") - } - C.set_output_handler(&e.emitter) - C.yaml_stream_start_event_initialize(&e.event, C.YAML_UTF8_ENCODING) + e.must(yaml_emitter_initialize(&e.emitter)) + yaml_emitter_set_output_string(&e.emitter, &e.out) + e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)) e.emit() - C.yaml_document_start_event_initialize(&e.event, nil, nil, nil, 1) + e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true)) e.emit() return e } func (e *encoder) finish() { - C.yaml_document_end_event_initialize(&e.event, 1) + e.must(yaml_document_end_event_initialize(&e.event, true)) e.emit() - e.emitter.open_ended = 0 - C.yaml_stream_end_event_initialize(&e.event) + e.emitter.open_ended = false + e.must(yaml_stream_end_event_initialize(&e.event)) e.emit() } func (e *encoder) destroy() { - C.yaml_emitter_delete(&e.emitter) + yaml_emitter_delete(&e.emitter) } func (e *encoder) emit() { // This will internally delete the e.event value. - if C.yaml_emitter_emit(&e.emitter, &e.event) == 0 && - e.event._type != C.YAML_DOCUMENT_END_EVENT && - e.event._type != C.YAML_STREAM_END_EVENT { - if e.emitter.error == C.YAML_EMITTER_ERROR { - // XXX TESTME - panic("YAML emitter error: " + C.GoString(e.emitter.problem)) - } else { - // XXX TESTME - panic("Unknown YAML emitter error") - } + if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT { + e.must(false) } } -func (e *encoder) fail(msg string) { - if msg == "" { - if e.emitter.problem != nil { - msg = C.GoString(e.emitter.problem) - } else { +func (e *encoder) must(ok bool) { + if !ok { + msg := e.emitter.problem + if msg == "" { msg = "Unknown problem generating YAML content" } + panic(msg) } - panic(msg) } func (e *encoder) marshal(tag string, in reflect.Value) { @@ -159,62 +127,43 @@ func (e *encoder) structv(tag string, in reflect.Value) { } func (e *encoder) mappingv(tag string, f func()) { - var ctag *C.yaml_char_t - var free func() - cimplicit := C.int(1) - if tag != "" { - ctag, free = ystr(tag) - defer free() - cimplicit = 0 - } - cstyle := C.yaml_mapping_style_t(C.YAML_BLOCK_MAPPING_STYLE) + implicit := tag == "" + style := yaml_BLOCK_MAPPING_STYLE if e.flow { e.flow = false - cstyle = C.YAML_FLOW_MAPPING_STYLE + style = yaml_FLOW_MAPPING_STYLE } - C.yaml_mapping_start_event_initialize(&e.event, nil, ctag, cimplicit, - cstyle) + e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) e.emit() f() - C.yaml_mapping_end_event_initialize(&e.event) + e.must(yaml_mapping_end_event_initialize(&e.event)) e.emit() } func (e *encoder) slicev(tag string, in reflect.Value) { - var ctag *C.yaml_char_t - var free func() - var cimplicit C.int - if tag != "" { - ctag, free = ystr(tag) - defer free() - cimplicit = 0 - } else { - cimplicit = 1 - } - - cstyle := C.yaml_sequence_style_t(C.YAML_BLOCK_SEQUENCE_STYLE) + implicit := tag == "" + style := yaml_BLOCK_SEQUENCE_STYLE if e.flow { e.flow = false - cstyle = C.YAML_FLOW_SEQUENCE_STYLE + style = yaml_FLOW_SEQUENCE_STYLE } - C.yaml_sequence_start_event_initialize(&e.event, nil, ctag, cimplicit, - cstyle) + e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)) e.emit() n := in.Len() for i := 0; i < n; i++ { e.marshal("", in.Index(i)) } - C.yaml_sequence_end_event_initialize(&e.event) + e.must(yaml_sequence_end_event_initialize(&e.event)) e.emit() } func (e *encoder) stringv(tag string, in reflect.Value) { - var style C.yaml_scalar_style_t + var style yaml_scalar_style_t s := in.String() if rtag, _ := resolve("", s); rtag != "!!str" { - style = C.YAML_DOUBLE_QUOTED_SCALAR_STYLE + style = yaml_DOUBLE_QUOTED_SCALAR_STYLE } else { - style = C.YAML_PLAIN_SCALAR_STYLE + style = yaml_PLAIN_SCALAR_STYLE } e.emitScalar(s, "", tag, style) } @@ -226,17 +175,17 @@ func (e *encoder) boolv(tag string, in reflect.Value) { } else { s = "false" } - e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) } func (e *encoder) intv(tag string, in reflect.Value) { s := strconv.FormatInt(in.Int(), 10) - e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) } func (e *encoder) uintv(tag string, in reflect.Value) { s := strconv.FormatUint(in.Uint(), 10) - e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) } func (e *encoder) floatv(tag string, in reflect.Value) { @@ -250,42 +199,18 @@ func (e *encoder) floatv(tag string, in reflect.Value) { case "NaN": s = ".nan" } - e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE) + e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE) } func (e *encoder) nilv() { - e.emitScalar("null", "", "", C.YAML_PLAIN_SCALAR_STYLE) + e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE) } -func (e *encoder) emitScalar(value, anchor, tag string, style C.yaml_scalar_style_t) { - var canchor, ctag, cvalue *C.yaml_char_t - var cimplicit C.int - var free func() - if anchor != "" { - canchor, free = ystr(anchor) - defer free() - } - if tag != "" { - ctag, free = ystr(tag) - defer free() - cimplicit = 0 - style = C.YAML_PLAIN_SCALAR_STYLE - } else { - cimplicit = 1 - } - cvalue, free = ystr(value) - defer free() - size := C.int(len(value)) - if C.yaml_scalar_event_initialize(&e.event, canchor, ctag, cvalue, size, - cimplicit, cimplicit, style) == 0 { - e.fail("") +func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) { + implicit := tag == "" + if !implicit { + style = yaml_PLAIN_SCALAR_STYLE } + e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style)) e.emit() } - -func ystr(s string) (ys *C.yaml_char_t, free func()) { - up := unsafe.Pointer(C.CString(s)) - ys = (*C.yaml_char_t)(up) - free = func() { C.free(up) } - return ys, free -} diff --git a/helpers.c b/helpers.c deleted file mode 100644 index 733b618..0000000 --- a/helpers.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "_cgo_export.h" -#include "helpers.h" - -#define DEFINE_YUNION_FUNC(name) \ - __typeof__(((yaml_event_t *)0)->data.name) * \ - event_##name(yaml_event_t *event) { \ - return &event->data.name; \ - } - -DEFINE_YUNION_FUNC(scalar) -DEFINE_YUNION_FUNC(alias) -DEFINE_YUNION_FUNC(mapping_start) -DEFINE_YUNION_FUNC(sequence_start) - -void set_output_handler(yaml_emitter_t *e) -{ - yaml_emitter_set_output(e, (yaml_write_handler_t*)outputHandler, (void *)e); -} diff --git a/helpers.h b/helpers.h deleted file mode 100644 index e4d8639..0000000 --- a/helpers.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef helpers_h -#define helpers_h - -#define CGO_LDFLAGS "-lm -lpthread" -#define CGO_CFLAGS "-I. -DHAVE_CONFIG_H=1" - -#include - -#define DECLARE_YUNION_FUNC(name) \ - __typeof__(((yaml_event_t *)0)->data.name) *\ - event_##name(yaml_event_t *event); - -DECLARE_YUNION_FUNC(scalar) -DECLARE_YUNION_FUNC(alias) -DECLARE_YUNION_FUNC(mapping_start) -DECLARE_YUNION_FUNC(sequence_start) - -void set_output_handler(yaml_emitter_t *e); - -#endif diff --git a/scanner_c.go b/scanner_c.go index 9eeeffe..c0e611a 100644 --- a/scanner_c.go +++ b/scanner_c.go @@ -512,10 +512,8 @@ func skip_line(parser *yaml_parser_t) { // Copy a character to a string buffer and advance pointers. func read(parser *yaml_parser_t, s *[]byte) bool { - buf := parser.buffer - pos := parser.buffer_pos - w := width(buf[0]) - *s = append(*s, buf[pos:pos+w]...) + w := width(parser.buffer[0]) + *s = append(*s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...) parser.buffer_pos += w parser.mark.index++ parser.mark.column++ diff --git a/writer.c b/writer.c deleted file mode 100644 index b90019f..0000000 --- a/writer.c +++ /dev/null @@ -1,141 +0,0 @@ - -#include "yaml_private.h" - -/* - * Declarations. - */ - -static int -yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem); - -YAML_DECLARE(int) -yaml_emitter_flush(yaml_emitter_t *emitter); - -/* - * Set the writer error and return 0. - */ - -static int -yaml_emitter_set_writer_error(yaml_emitter_t *emitter, const char *problem) -{ - emitter->error = YAML_WRITER_ERROR; - emitter->problem = problem; - - return 0; -} - -/* - * Flush the output buffer. - */ - -YAML_DECLARE(int) -yaml_emitter_flush(yaml_emitter_t *emitter) -{ - int low, high; - - assert(emitter); /* Non-NULL emitter object is expected. */ - assert(emitter->write_handler); /* Write handler must be set. */ - assert(emitter->encoding); /* Output encoding must be set. */ - - emitter->buffer.last = emitter->buffer.pointer; - emitter->buffer.pointer = emitter->buffer.start; - - /* Check if the buffer is empty. */ - - if (emitter->buffer.start == emitter->buffer.last) { - return 1; - } - - /* If the output encoding is UTF-8, we don't need to recode the buffer. */ - - if (emitter->encoding == YAML_UTF8_ENCODING) - { - if (emitter->write_handler(emitter->write_handler_data, - emitter->buffer.start, - emitter->buffer.last - emitter->buffer.start)) { - emitter->buffer.last = emitter->buffer.start; - emitter->buffer.pointer = emitter->buffer.start; - return 1; - } - else { - return yaml_emitter_set_writer_error(emitter, "write error"); - } - } - - /* Recode the buffer into the raw buffer. */ - - low = (emitter->encoding == YAML_UTF16LE_ENCODING ? 0 : 1); - high = (emitter->encoding == YAML_UTF16LE_ENCODING ? 1 : 0); - - while (emitter->buffer.pointer != emitter->buffer.last) - { - unsigned char octet; - unsigned int width; - unsigned int value; - size_t k; - - /* - * See the "reader.c" code for more details on UTF-8 encoding. Note - * that we assume that the buffer contains a valid UTF-8 sequence. - */ - - /* Read the next UTF-8 character. */ - - octet = emitter->buffer.pointer[0]; - - width = (octet & 0x80) == 0x00 ? 1 : - (octet & 0xE0) == 0xC0 ? 2 : - (octet & 0xF0) == 0xE0 ? 3 : - (octet & 0xF8) == 0xF0 ? 4 : 0; - - value = (octet & 0x80) == 0x00 ? octet & 0x7F : - (octet & 0xE0) == 0xC0 ? octet & 0x1F : - (octet & 0xF0) == 0xE0 ? octet & 0x0F : - (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; - - for (k = 1; k < width; k ++) { - octet = emitter->buffer.pointer[k]; - value = (value << 6) + (octet & 0x3F); - } - - emitter->buffer.pointer += width; - - /* Write the character. */ - - if (value < 0x10000) - { - emitter->raw_buffer.last[high] = value >> 8; - emitter->raw_buffer.last[low] = value & 0xFF; - - emitter->raw_buffer.last += 2; - } - else - { - /* Write the character using a surrogate pair (check "reader.c"). */ - - value -= 0x10000; - emitter->raw_buffer.last[high] = 0xD8 + (value >> 18); - emitter->raw_buffer.last[low] = (value >> 10) & 0xFF; - emitter->raw_buffer.last[high+2] = 0xDC + ((value >> 8) & 0xFF); - emitter->raw_buffer.last[low+2] = value & 0xFF; - - emitter->raw_buffer.last += 4; - } - } - - /* Write the raw buffer. */ - - if (emitter->write_handler(emitter->write_handler_data, - emitter->raw_buffer.start, - emitter->raw_buffer.last - emitter->raw_buffer.start)) { - emitter->buffer.last = emitter->buffer.start; - emitter->buffer.pointer = emitter->buffer.start; - emitter->raw_buffer.last = emitter->raw_buffer.start; - emitter->raw_buffer.pointer = emitter->raw_buffer.start; - return 1; - } - else { - return yaml_emitter_set_writer_error(emitter, "write error"); - } -} - diff --git a/writer_c.go b/writer_c.go index 5d1afaa..4809bfb 100644 --- a/writer_c.go +++ b/writer_c.go @@ -14,16 +14,16 @@ func yaml_emitter_flush(emitter *yaml_emitter_t) bool { } // Check if the buffer is empty. - if len(emitter.buffer) == 0 { + if emitter.buffer_pos == 0 { return true } // If the output encoding is UTF-8, we don't need to recode the buffer. if emitter.encoding == yaml_UTF8_ENCODING { - if err := emitter.write_handler(emitter, emitter.buffer); err != nil { + if err := emitter.write_handler(emitter, emitter.buffer[:emitter.buffer_pos]); err != nil { return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error()) } - emitter.buffer = emitter.buffer[:0] + emitter.buffer_pos = 0 return true } @@ -36,7 +36,7 @@ func yaml_emitter_flush(emitter *yaml_emitter_t) bool { } pos := 0 - for pos < len(emitter.buffer) { + for pos < emitter.buffer_pos { // See the "reader.c" code for more details on UTF-8 encoding. Note // that we assume that the buffer contains a valid UTF-8 sequence. @@ -83,7 +83,7 @@ func yaml_emitter_flush(emitter *yaml_emitter_t) bool { if err := emitter.write_handler(emitter, emitter.raw_buffer); err != nil { return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error()) } - emitter.buffer = emitter.buffer[:0] + emitter.buffer_pos = 0 emitter.raw_buffer = emitter.raw_buffer[:0] return true } diff --git a/yaml.h b/yaml.h deleted file mode 100644 index 400cae1..0000000 --- a/yaml.h +++ /dev/null @@ -1,1971 +0,0 @@ -/** - * @file yaml.h - * @brief Public interface for libyaml. - * - * Include the header file with the code: - * @code - * #include - * @endcode - */ - -#ifndef YAML_H -#define YAML_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -/** - * @defgroup export Export Definitions - * @{ - */ - -/** The public API declaration. */ - -#ifdef WIN32 -# if defined(YAML_DECLARE_STATIC) -# define YAML_DECLARE(type) type -# elif defined(YAML_DECLARE_EXPORT) -# define YAML_DECLARE(type) __declspec(dllexport) type -# else -# define YAML_DECLARE(type) __declspec(dllimport) type -# endif -#else -# define YAML_DECLARE(type) type -#endif - -/** @} */ - -/** - * @defgroup version Version Information - * @{ - */ - -/** - * Get the library version as a string. - * - * @returns The function returns the pointer to a static string of the form - * @c "X.Y.Z", where @c X is the major version number, @c Y is a minor version - * number, and @c Z is the patch version number. - */ - -YAML_DECLARE(const char *) -yaml_get_version_string(void); - -/** - * Get the library version numbers. - * - * @param[out] major Major version number. - * @param[out] minor Minor version number. - * @param[out] patch Patch version number. - */ - -YAML_DECLARE(void) -yaml_get_version(int *major, int *minor, int *patch); - -/** @} */ - -/** - * @defgroup basic Basic Types - * @{ - */ - -/** The character type (UTF-8 octet). */ -typedef unsigned char yaml_char_t; - -/** The version directive data. */ -typedef struct yaml_version_directive_s { - /** The major version number. */ - int major; - /** The minor version number. */ - int minor; -} yaml_version_directive_t; - -/** The tag directive data. */ -typedef struct yaml_tag_directive_s { - /** The tag handle. */ - yaml_char_t *handle; - /** The tag prefix. */ - yaml_char_t *prefix; -} yaml_tag_directive_t; - -/** The stream encoding. */ -typedef enum yaml_encoding_e { - /** Let the parser choose the encoding. */ - YAML_ANY_ENCODING, - /** The default UTF-8 encoding. */ - YAML_UTF8_ENCODING, - /** The UTF-16-LE encoding with BOM. */ - YAML_UTF16LE_ENCODING, - /** The UTF-16-BE encoding with BOM. */ - YAML_UTF16BE_ENCODING -} yaml_encoding_t; - -/** Line break types. */ - -typedef enum yaml_break_e { - /** Let the parser choose the break type. */ - YAML_ANY_BREAK, - /** Use CR for line breaks (Mac style). */ - YAML_CR_BREAK, - /** Use LN for line breaks (Unix style). */ - YAML_LN_BREAK, - /** Use CR LN for line breaks (DOS style). */ - YAML_CRLN_BREAK -} yaml_break_t; - -/** Many bad things could happen with the parser and emitter. */ -typedef enum yaml_error_type_e { - /** No error is produced. */ - YAML_NO_ERROR, - - /** Cannot allocate or reallocate a block of memory. */ - YAML_MEMORY_ERROR, - - /** Cannot read or decode the input stream. */ - YAML_READER_ERROR, - /** Cannot scan the input stream. */ - YAML_SCANNER_ERROR, - /** Cannot parse the input stream. */ - YAML_PARSER_ERROR, - /** Cannot compose a YAML document. */ - YAML_COMPOSER_ERROR, - - /** Cannot write to the output stream. */ - YAML_WRITER_ERROR, - /** Cannot emit a YAML stream. */ - YAML_EMITTER_ERROR -} yaml_error_type_t; - -/** The pointer position. */ -typedef struct yaml_mark_s { - /** The position index. */ - size_t index; - - /** The position line. */ - size_t line; - - /** The position column. */ - size_t column; -} yaml_mark_t; - -/** @} */ - -/** - * @defgroup styles Node Styles - * @{ - */ - -/** Scalar styles. */ -typedef enum yaml_scalar_style_e { - /** Let the emitter choose the style. */ - YAML_ANY_SCALAR_STYLE, - - /** The plain scalar style. */ - YAML_PLAIN_SCALAR_STYLE, - - /** The single-quoted scalar style. */ - YAML_SINGLE_QUOTED_SCALAR_STYLE, - /** The double-quoted scalar style. */ - YAML_DOUBLE_QUOTED_SCALAR_STYLE, - - /** The literal scalar style. */ - YAML_LITERAL_SCALAR_STYLE, - /** The folded scalar style. */ - YAML_FOLDED_SCALAR_STYLE -} yaml_scalar_style_t; - -/** Sequence styles. */ -typedef enum yaml_sequence_style_e { - /** Let the emitter choose the style. */ - YAML_ANY_SEQUENCE_STYLE, - - /** The block sequence style. */ - YAML_BLOCK_SEQUENCE_STYLE, - /** The flow sequence style. */ - YAML_FLOW_SEQUENCE_STYLE -} yaml_sequence_style_t; - -/** Mapping styles. */ -typedef enum yaml_mapping_style_e { - /** Let the emitter choose the style. */ - YAML_ANY_MAPPING_STYLE, - - /** The block mapping style. */ - YAML_BLOCK_MAPPING_STYLE, - /** The flow mapping style. */ - YAML_FLOW_MAPPING_STYLE -/* YAML_FLOW_SET_MAPPING_STYLE */ -} yaml_mapping_style_t; - -/** @} */ - -/** - * @defgroup tokens Tokens - * @{ - */ - -/** Token types. */ -typedef enum yaml_token_type_e { - /** An empty token. */ - YAML_NO_TOKEN, - - /** A STREAM-START token. */ - YAML_STREAM_START_TOKEN, - /** A STREAM-END token. */ - YAML_STREAM_END_TOKEN, - - /** A VERSION-DIRECTIVE token. */ - YAML_VERSION_DIRECTIVE_TOKEN, - /** A TAG-DIRECTIVE token. */ - YAML_TAG_DIRECTIVE_TOKEN, - /** A DOCUMENT-START token. */ - YAML_DOCUMENT_START_TOKEN, - /** A DOCUMENT-END token. */ - YAML_DOCUMENT_END_TOKEN, - - /** A BLOCK-SEQUENCE-START token. */ - YAML_BLOCK_SEQUENCE_START_TOKEN, - /** A BLOCK-SEQUENCE-END token. */ - YAML_BLOCK_MAPPING_START_TOKEN, - /** A BLOCK-END token. */ - YAML_BLOCK_END_TOKEN, - - /** A FLOW-SEQUENCE-START token. */ - YAML_FLOW_SEQUENCE_START_TOKEN, - /** A FLOW-SEQUENCE-END token. */ - YAML_FLOW_SEQUENCE_END_TOKEN, - /** A FLOW-MAPPING-START token. */ - YAML_FLOW_MAPPING_START_TOKEN, - /** A FLOW-MAPPING-END token. */ - YAML_FLOW_MAPPING_END_TOKEN, - - /** A BLOCK-ENTRY token. */ - YAML_BLOCK_ENTRY_TOKEN, - /** A FLOW-ENTRY token. */ - YAML_FLOW_ENTRY_TOKEN, - /** A KEY token. */ - YAML_KEY_TOKEN, - /** A VALUE token. */ - YAML_VALUE_TOKEN, - - /** An ALIAS token. */ - YAML_ALIAS_TOKEN, - /** An ANCHOR token. */ - YAML_ANCHOR_TOKEN, - /** A TAG token. */ - YAML_TAG_TOKEN, - /** A SCALAR token. */ - YAML_SCALAR_TOKEN -} yaml_token_type_t; - -/** The token structure. */ -typedef struct yaml_token_s { - - /** The token type. */ - yaml_token_type_t type; - - /** The token data. */ - union { - - /** The stream start (for @c YAML_STREAM_START_TOKEN). */ - struct { - /** The stream encoding. */ - yaml_encoding_t encoding; - } stream_start; - - /** The alias (for @c YAML_ALIAS_TOKEN). */ - struct { - /** The alias value. */ - yaml_char_t *value; - } alias; - - /** The anchor (for @c YAML_ANCHOR_TOKEN). */ - struct { - /** The anchor value. */ - yaml_char_t *value; - } anchor; - - /** The tag (for @c YAML_TAG_TOKEN). */ - struct { - /** The tag handle. */ - yaml_char_t *handle; - /** The tag suffix. */ - yaml_char_t *suffix; - } tag; - - /** The scalar value (for @c YAML_SCALAR_TOKEN). */ - struct { - /** The scalar value. */ - yaml_char_t *value; - /** The length of the scalar value. */ - size_t length; - /** The scalar style. */ - yaml_scalar_style_t style; - } scalar; - - /** The version directive (for @c YAML_VERSION_DIRECTIVE_TOKEN). */ - struct { - /** The major version number. */ - int major; - /** The minor version number. */ - int minor; - } version_directive; - - /** The tag directive (for @c YAML_TAG_DIRECTIVE_TOKEN). */ - struct { - /** The tag handle. */ - yaml_char_t *handle; - /** The tag prefix. */ - yaml_char_t *prefix; - } tag_directive; - - } data; - - /** The beginning of the token. */ - yaml_mark_t start_mark; - /** The end of the token. */ - yaml_mark_t end_mark; - -} yaml_token_t; - -/** - * Free any memory allocated for a token object. - * - * @param[in,out] token A token object. - */ - -YAML_DECLARE(void) -yaml_token_delete(yaml_token_t *token); - -/** @} */ - -/** - * @defgroup events Events - * @{ - */ - -/** Event types. */ -typedef enum yaml_event_type_e { - /** An empty event. */ - YAML_NO_EVENT, - - /** A STREAM-START event. */ - YAML_STREAM_START_EVENT, - /** A STREAM-END event. */ - YAML_STREAM_END_EVENT, - - /** A DOCUMENT-START event. */ - YAML_DOCUMENT_START_EVENT, - /** A DOCUMENT-END event. */ - YAML_DOCUMENT_END_EVENT, - - /** An ALIAS event. */ - YAML_ALIAS_EVENT, - /** A SCALAR event. */ - YAML_SCALAR_EVENT, - - /** A SEQUENCE-START event. */ - YAML_SEQUENCE_START_EVENT, - /** A SEQUENCE-END event. */ - YAML_SEQUENCE_END_EVENT, - - /** A MAPPING-START event. */ - YAML_MAPPING_START_EVENT, - /** A MAPPING-END event. */ - YAML_MAPPING_END_EVENT -} yaml_event_type_t; - -/** The event structure. */ -typedef struct yaml_event_s { - - /** The event type. */ - yaml_event_type_t type; - - /** The event data. */ - union { - - /** The stream parameters (for @c YAML_STREAM_START_EVENT). */ - struct { - /** The document encoding. */ - yaml_encoding_t encoding; - } stream_start; - - /** The document parameters (for @c YAML_DOCUMENT_START_EVENT). */ - struct { - /** The version directive. */ - yaml_version_directive_t *version_directive; - - /** The list of tag directives. */ - struct { - /** The beginning of the tag directives list. */ - yaml_tag_directive_t *start; - /** The end of the tag directives list. */ - yaml_tag_directive_t *end; - } tag_directives; - - /** Is the document indicator implicit? */ - int implicit; - } document_start; - - /** The document end parameters (for @c YAML_DOCUMENT_END_EVENT). */ - struct { - /** Is the document end indicator implicit? */ - int implicit; - } document_end; - - /** The alias parameters (for @c YAML_ALIAS_EVENT). */ - struct { - /** The anchor. */ - yaml_char_t *anchor; - } alias; - - /** The scalar parameters (for @c YAML_SCALAR_EVENT). */ - struct { - /** The anchor. */ - yaml_char_t *anchor; - /** The tag. */ - yaml_char_t *tag; - /** The scalar value. */ - yaml_char_t *value; - /** The length of the scalar value. */ - size_t length; - /** Is the tag optional for the plain style? */ - int plain_implicit; - /** Is the tag optional for any non-plain style? */ - int quoted_implicit; - /** The scalar style. */ - yaml_scalar_style_t style; - } scalar; - - /** The sequence parameters (for @c YAML_SEQUENCE_START_EVENT). */ - struct { - /** The anchor. */ - yaml_char_t *anchor; - /** The tag. */ - yaml_char_t *tag; - /** Is the tag optional? */ - int implicit; - /** The sequence style. */ - yaml_sequence_style_t style; - } sequence_start; - - /** The mapping parameters (for @c YAML_MAPPING_START_EVENT). */ - struct { - /** The anchor. */ - yaml_char_t *anchor; - /** The tag. */ - yaml_char_t *tag; - /** Is the tag optional? */ - int implicit; - /** The mapping style. */ - yaml_mapping_style_t style; - } mapping_start; - - } data; - - /** The beginning of the event. */ - yaml_mark_t start_mark; - /** The end of the event. */ - yaml_mark_t end_mark; - -} yaml_event_t; - -/** - * Create the STREAM-START event. - * - * @param[out] event An empty event object. - * @param[in] encoding The stream encoding. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_stream_start_event_initialize(yaml_event_t *event, - yaml_encoding_t encoding); - -/** - * Create the STREAM-END event. - * - * @param[out] event An empty event object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_stream_end_event_initialize(yaml_event_t *event); - -/** - * Create the DOCUMENT-START event. - * - * The @a implicit argument is considered as a stylistic parameter and may be - * ignored by the emitter. - * - * @param[out] event An empty event object. - * @param[in] version_directive The %YAML directive value or - * @c NULL. - * @param[in] tag_directives_start The beginning of the %TAG - * directives list. - * @param[in] tag_directives_end The end of the %TAG directives - * list. - * @param[in] implicit If the document start indicator is - * implicit. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_start_event_initialize(yaml_event_t *event, - yaml_version_directive_t *version_directive, - yaml_tag_directive_t *tag_directives_start, - yaml_tag_directive_t *tag_directives_end, - int implicit); - -/** - * Create the DOCUMENT-END event. - * - * The @a implicit argument is considered as a stylistic parameter and may be - * ignored by the emitter. - * - * @param[out] event An empty event object. - * @param[in] implicit If the document end indicator is implicit. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_end_event_initialize(yaml_event_t *event, int implicit); - -/** - * Create an ALIAS event. - * - * @param[out] event An empty event object. - * @param[in] anchor The anchor value. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor); - -/** - * Create a SCALAR event. - * - * The @a style argument may be ignored by the emitter. - * - * Either the @a tag attribute or one of the @a plain_implicit and - * @a quoted_implicit flags must be set. - * - * @param[out] event An empty event object. - * @param[in] anchor The scalar anchor or @c NULL. - * @param[in] tag The scalar tag or @c NULL. - * @param[in] value The scalar value. - * @param[in] length The length of the scalar value. - * @param[in] plain_implicit If the tag may be omitted for the plain - * style. - * @param[in] quoted_implicit If the tag may be omitted for any - * non-plain style. - * @param[in] style The scalar style. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_scalar_event_initialize(yaml_event_t *event, - yaml_char_t *anchor, yaml_char_t *tag, - yaml_char_t *value, int length, - int plain_implicit, int quoted_implicit, - yaml_scalar_style_t style); - -/** - * Create a SEQUENCE-START event. - * - * The @a style argument may be ignored by the emitter. - * - * Either the @a tag attribute or the @a implicit flag must be set. - * - * @param[out] event An empty event object. - * @param[in] anchor The sequence anchor or @c NULL. - * @param[in] tag The sequence tag or @c NULL. - * @param[in] implicit If the tag may be omitted. - * @param[in] style The sequence style. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_sequence_start_event_initialize(yaml_event_t *event, - yaml_char_t *anchor, yaml_char_t *tag, int implicit, - yaml_sequence_style_t style); - -/** - * Create a SEQUENCE-END event. - * - * @param[out] event An empty event object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_sequence_end_event_initialize(yaml_event_t *event); - -/** - * Create a MAPPING-START event. - * - * The @a style argument may be ignored by the emitter. - * - * Either the @a tag attribute or the @a implicit flag must be set. - * - * @param[out] event An empty event object. - * @param[in] anchor The mapping anchor or @c NULL. - * @param[in] tag The mapping tag or @c NULL. - * @param[in] implicit If the tag may be omitted. - * @param[in] style The mapping style. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_mapping_start_event_initialize(yaml_event_t *event, - yaml_char_t *anchor, yaml_char_t *tag, int implicit, - yaml_mapping_style_t style); - -/** - * Create a MAPPING-END event. - * - * @param[out] event An empty event object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_mapping_end_event_initialize(yaml_event_t *event); - -/** - * Free any memory allocated for an event object. - * - * @param[in,out] event An event object. - */ - -YAML_DECLARE(void) -yaml_event_delete(yaml_event_t *event); - -/** @} */ - -/** - * @defgroup nodes Nodes - * @{ - */ - -/** The tag @c !!null with the only possible value: @c null. */ -#define YAML_NULL_TAG "tag:yaml.org,2002:null" -/** The tag @c !!bool with the values: @c true and @c falce. */ -#define YAML_BOOL_TAG "tag:yaml.org,2002:bool" -/** The tag @c !!str for string values. */ -#define YAML_STR_TAG "tag:yaml.org,2002:str" -/** The tag @c !!int for integer values. */ -#define YAML_INT_TAG "tag:yaml.org,2002:int" -/** The tag @c !!float for float values. */ -#define YAML_FLOAT_TAG "tag:yaml.org,2002:float" -/** The tag @c !!timestamp for date and time values. */ -#define YAML_TIMESTAMP_TAG "tag:yaml.org,2002:timestamp" - -/** The tag @c !!seq is used to denote sequences. */ -#define YAML_SEQ_TAG "tag:yaml.org,2002:seq" -/** The tag @c !!map is used to denote mapping. */ -#define YAML_MAP_TAG "tag:yaml.org,2002:map" - -/** The default scalar tag is @c !!str. */ -#define YAML_DEFAULT_SCALAR_TAG YAML_STR_TAG -/** The default sequence tag is @c !!seq. */ -#define YAML_DEFAULT_SEQUENCE_TAG YAML_SEQ_TAG -/** The default mapping tag is @c !!map. */ -#define YAML_DEFAULT_MAPPING_TAG YAML_MAP_TAG - -/** Node types. */ -typedef enum yaml_node_type_e { - /** An empty node. */ - YAML_NO_NODE, - - /** A scalar node. */ - YAML_SCALAR_NODE, - /** A sequence node. */ - YAML_SEQUENCE_NODE, - /** A mapping node. */ - YAML_MAPPING_NODE -} yaml_node_type_t; - -/** The forward definition of a document node structure. */ -typedef struct yaml_node_s yaml_node_t; - -/** An element of a sequence node. */ -typedef int yaml_node_item_t; - -/** An element of a mapping node. */ -typedef struct yaml_node_pair_s { - /** The key of the element. */ - int key; - /** The value of the element. */ - int value; -} yaml_node_pair_t; - -/** The node structure. */ -struct yaml_node_s { - - /** The node type. */ - yaml_node_type_t type; - - /** The node tag. */ - yaml_char_t *tag; - - /** The node data. */ - union { - - /** The scalar parameters (for @c YAML_SCALAR_NODE). */ - struct { - /** The scalar value. */ - yaml_char_t *value; - /** The length of the scalar value. */ - size_t length; - /** The scalar style. */ - yaml_scalar_style_t style; - } scalar; - - /** The sequence parameters (for @c YAML_SEQUENCE_NODE). */ - struct { - /** The stack of sequence items. */ - struct { - /** The beginning of the stack. */ - yaml_node_item_t *start; - /** The end of the stack. */ - yaml_node_item_t *end; - /** The top of the stack. */ - yaml_node_item_t *top; - } items; - /** The sequence style. */ - yaml_sequence_style_t style; - } sequence; - - /** The mapping parameters (for @c YAML_MAPPING_NODE). */ - struct { - /** The stack of mapping pairs (key, value). */ - struct { - /** The beginning of the stack. */ - yaml_node_pair_t *start; - /** The end of the stack. */ - yaml_node_pair_t *end; - /** The top of the stack. */ - yaml_node_pair_t *top; - } pairs; - /** The mapping style. */ - yaml_mapping_style_t style; - } mapping; - - } data; - - /** The beginning of the node. */ - yaml_mark_t start_mark; - /** The end of the node. */ - yaml_mark_t end_mark; - -}; - -/** The document structure. */ -typedef struct yaml_document_s { - - /** The document nodes. */ - struct { - /** The beginning of the stack. */ - yaml_node_t *start; - /** The end of the stack. */ - yaml_node_t *end; - /** The top of the stack. */ - yaml_node_t *top; - } nodes; - - /** The version directive. */ - yaml_version_directive_t *version_directive; - - /** The list of tag directives. */ - struct { - /** The beginning of the tag directives list. */ - yaml_tag_directive_t *start; - /** The end of the tag directives list. */ - yaml_tag_directive_t *end; - } tag_directives; - - /** Is the document start indicator implicit? */ - int start_implicit; - /** Is the document end indicator implicit? */ - int end_implicit; - - /** The beginning of the document. */ - yaml_mark_t start_mark; - /** The end of the document. */ - yaml_mark_t end_mark; - -} yaml_document_t; - -/** - * Create a YAML document. - * - * @param[out] document An empty document object. - * @param[in] version_directive The %YAML directive value or - * @c NULL. - * @param[in] tag_directives_start The beginning of the %TAG - * directives list. - * @param[in] tag_directives_end The end of the %TAG directives - * list. - * @param[in] start_implicit If the document start indicator is - * implicit. - * @param[in] end_implicit If the document end indicator is - * implicit. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_initialize(yaml_document_t *document, - yaml_version_directive_t *version_directive, - yaml_tag_directive_t *tag_directives_start, - yaml_tag_directive_t *tag_directives_end, - int start_implicit, int end_implicit); - -/** - * Delete a YAML document and all its nodes. - * - * @param[in,out] document A document object. - */ - -YAML_DECLARE(void) -yaml_document_delete(yaml_document_t *document); - -/** - * Get a node of a YAML document. - * - * The pointer returned by this function is valid until any of the functions - * modifying the documents are called. - * - * @param[in] document A document object. - * @param[in] index The node id. - * - * @returns the node objct or @c NULL if @c node_id is out of range. - */ - -YAML_DECLARE(yaml_node_t *) -yaml_document_get_node(yaml_document_t *document, int index); - -/** - * Get the root of a YAML document node. - * - * The root object is the first object added to the document. - * - * The pointer returned by this function is valid until any of the functions - * modifying the documents are called. - * - * An empty document produced by the parser signifies the end of a YAML - * stream. - * - * @param[in] document A document object. - * - * @returns the node object or @c NULL if the document is empty. - */ - -YAML_DECLARE(yaml_node_t *) -yaml_document_get_root_node(yaml_document_t *document); - -/** - * Create a SCALAR node and attach it to the document. - * - * The @a style argument may be ignored by the emitter. - * - * @param[in,out] document A document object. - * @param[in] tag The scalar tag. - * @param[in] value The scalar value. - * @param[in] length The length of the scalar value. - * @param[in] style The scalar style. - * - * @returns the node id or @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_add_scalar(yaml_document_t *document, - yaml_char_t *tag, yaml_char_t *value, int length, - yaml_scalar_style_t style); - -/** - * Create a SEQUENCE node and attach it to the document. - * - * The @a style argument may be ignored by the emitter. - * - * @param[in,out] document A document object. - * @param[in] tag The sequence tag. - * @param[in] style The sequence style. - * - * @returns the node id or @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_add_sequence(yaml_document_t *document, - yaml_char_t *tag, yaml_sequence_style_t style); - -/** - * Create a MAPPING node and attach it to the document. - * - * The @a style argument may be ignored by the emitter. - * - * @param[in,out] document A document object. - * @param[in] tag The sequence tag. - * @param[in] style The sequence style. - * - * @returns the node id or @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_add_mapping(yaml_document_t *document, - yaml_char_t *tag, yaml_mapping_style_t style); - -/** - * Add an item to a SEQUENCE node. - * - * @param[in,out] document A document object. - * @param[in] sequence The sequence node id. - * @param[in] item The item node id. -* - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_append_sequence_item(yaml_document_t *document, - int sequence, int item); - -/** - * Add a pair of a key and a value to a MAPPING node. - * - * @param[in,out] document A document object. - * @param[in] mapping The mapping node id. - * @param[in] key The key node id. - * @param[in] value The value node id. -* - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_document_append_mapping_pair(yaml_document_t *document, - int mapping, int key, int value); - -/** @} */ - -/** - * @defgroup parser Parser Definitions - * @{ - */ - -/** - * The prototype of a read handler. - * - * The read handler is called when the parser needs to read more bytes from the - * source. The handler should write not more than @a size bytes to the @a - * buffer. The number of written bytes should be set to the @a length variable. - * - * @param[in,out] data A pointer to an application data specified by - * yaml_parser_set_input(). - * @param[out] buffer The buffer to write the data from the source. - * @param[in] size The size of the buffer. - * @param[out] size_read The actual number of bytes read from the source. - * - * @returns On success, the handler should return @c 1. If the handler failed, - * the returned value should be @c 0. On EOF, the handler should set the - * @a size_read to @c 0 and return @c 1. - */ - -typedef int yaml_read_handler_t(void *data, unsigned char *buffer, size_t size, - size_t *size_read); - -/** - * This structure holds information about a potential simple key. - */ - -typedef struct yaml_simple_key_s { - /** Is a simple key possible? */ - int possible; - - /** Is a simple key required? */ - int required; - - /** The number of the token. */ - size_t token_number; - - /** The position mark. */ - yaml_mark_t mark; -} yaml_simple_key_t; - -/** - * The states of the parser. - */ -typedef enum yaml_parser_state_e { - /** Expect STREAM-START. */ - YAML_PARSE_STREAM_START_STATE, - /** Expect the beginning of an implicit document. */ - YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE, - /** Expect DOCUMENT-START. */ - YAML_PARSE_DOCUMENT_START_STATE, - /** Expect the content of a document. */ - YAML_PARSE_DOCUMENT_CONTENT_STATE, - /** Expect DOCUMENT-END. */ - YAML_PARSE_DOCUMENT_END_STATE, - /** Expect a block node. */ - YAML_PARSE_BLOCK_NODE_STATE, - /** Expect a block node or indentless sequence. */ - YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE, - /** Expect a flow node. */ - YAML_PARSE_FLOW_NODE_STATE, - /** Expect the first entry of a block sequence. */ - YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE, - /** Expect an entry of a block sequence. */ - YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE, - /** Expect an entry of an indentless sequence. */ - YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE, - /** Expect the first key of a block mapping. */ - YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE, - /** Expect a block mapping key. */ - YAML_PARSE_BLOCK_MAPPING_KEY_STATE, - /** Expect a block mapping value. */ - YAML_PARSE_BLOCK_MAPPING_VALUE_STATE, - /** Expect the first entry of a flow sequence. */ - YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE, - /** Expect an entry of a flow sequence. */ - YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE, - /** Expect a key of an ordered mapping. */ - YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE, - /** Expect a value of an ordered mapping. */ - YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE, - /** Expect the and of an ordered mapping entry. */ - YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE, - /** Expect the first key of a flow mapping. */ - YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE, - /** Expect a key of a flow mapping. */ - YAML_PARSE_FLOW_MAPPING_KEY_STATE, - /** Expect a value of a flow mapping. */ - YAML_PARSE_FLOW_MAPPING_VALUE_STATE, - /** Expect an empty value of a flow mapping. */ - YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE, - /** Expect nothing. */ - YAML_PARSE_END_STATE -} yaml_parser_state_t; - -/** - * This structure holds aliases data. - */ - -typedef struct yaml_alias_data_s { - /** The anchor. */ - yaml_char_t *anchor; - /** The node id. */ - int index; - /** The anchor mark. */ - yaml_mark_t mark; -} yaml_alias_data_t; - -/** - * The parser structure. - * - * All members are internal. Manage the structure using the @c yaml_parser_ - * family of functions. - */ - -typedef struct yaml_parser_s { - - /** - * @name Error handling - * @{ - */ - - /** Error type. */ - yaml_error_type_t error; - /** Error description. */ - const char *problem; - /** The byte about which the problem occured. */ - size_t problem_offset; - /** The problematic value (@c -1 is none). */ - int problem_value; - /** The problem position. */ - yaml_mark_t problem_mark; - /** The error context. */ - const char *context; - /** The context position. */ - yaml_mark_t context_mark; - - /** - * @} - */ - - /** - * @name Reader stuff - * @{ - */ - - /** Read handler. */ - yaml_read_handler_t *read_handler; - - /** A pointer for passing to the read handler. */ - void *read_handler_data; - - /** Standard (string or file) input data. */ - union { - /** String input data. */ - struct { - /** The string start pointer. */ - const unsigned char *start; - /** The string end pointer. */ - const unsigned char *end; - /** The string current position. */ - const unsigned char *current; - } string; - - /** File input data. */ - FILE *file; - } input; - - /** EOF flag */ - int eof; - - /** The working buffer. */ - struct { - /** The beginning of the buffer. */ - yaml_char_t *start; - /** The end of the buffer. */ - yaml_char_t *end; - /** The current position of the buffer. */ - yaml_char_t *pointer; - /** The last filled position of the buffer. */ - yaml_char_t *last; - } buffer; - - /* The number of unread characters in the buffer. */ - size_t unread; - - /** The raw buffer. */ - struct { - /** The beginning of the buffer. */ - unsigned char *start; - /** The end of the buffer. */ - unsigned char *end; - /** The current position of the buffer. */ - unsigned char *pointer; - /** The last filled position of the buffer. */ - unsigned char *last; - } raw_buffer; - - /** The input encoding. */ - yaml_encoding_t encoding; - - /** The offset of the current position (in bytes). */ - size_t offset; - - /** The mark of the current position. */ - yaml_mark_t mark; - - /** - * @} - */ - - /** - * @name Scanner stuff - * @{ - */ - - /** Have we started to scan the input stream? */ - int stream_start_produced; - - /** Have we reached the end of the input stream? */ - int stream_end_produced; - - /** The number of unclosed '[' and '{' indicators. */ - int flow_level; - - /** The tokens queue. */ - struct { - /** The beginning of the tokens queue. */ - yaml_token_t *start; - /** The end of the tokens queue. */ - yaml_token_t *end; - /** The head of the tokens queue. */ - yaml_token_t *head; - /** The tail of the tokens queue. */ - yaml_token_t *tail; - } tokens; - - /** The number of tokens fetched from the queue. */ - size_t tokens_parsed; - - /* Does the tokens queue contain a token ready for dequeueing. */ - int token_available; - - /** The indentation levels stack. */ - struct { - /** The beginning of the stack. */ - int *start; - /** The end of the stack. */ - int *end; - /** The top of the stack. */ - int *top; - } indents; - - /** The current indentation level. */ - int indent; - - /** May a simple key occur at the current position? */ - int simple_key_allowed; - - /** The stack of simple keys. */ - struct { - /** The beginning of the stack. */ - yaml_simple_key_t *start; - /** The end of the stack. */ - yaml_simple_key_t *end; - /** The top of the stack. */ - yaml_simple_key_t *top; - } simple_keys; - - /** - * @} - */ - - /** - * @name Parser stuff - * @{ - */ - - /** The parser states stack. */ - struct { - /** The beginning of the stack. */ - yaml_parser_state_t *start; - /** The end of the stack. */ - yaml_parser_state_t *end; - /** The top of the stack. */ - yaml_parser_state_t *top; - } states; - - /** The current parser state. */ - yaml_parser_state_t state; - - /** The stack of marks. */ - struct { - /** The beginning of the stack. */ - yaml_mark_t *start; - /** The end of the stack. */ - yaml_mark_t *end; - /** The top of the stack. */ - yaml_mark_t *top; - } marks; - - /** The list of TAG directives. */ - struct { - /** The beginning of the list. */ - yaml_tag_directive_t *start; - /** The end of the list. */ - yaml_tag_directive_t *end; - /** The top of the list. */ - yaml_tag_directive_t *top; - } tag_directives; - - /** - * @} - */ - - /** - * @name Dumper stuff - * @{ - */ - - /** The alias data. */ - struct { - /** The beginning of the list. */ - yaml_alias_data_t *start; - /** The end of the list. */ - yaml_alias_data_t *end; - /** The top of the list. */ - yaml_alias_data_t *top; - } aliases; - - /** The currently parsed document. */ - yaml_document_t *document; - - /** - * @} - */ - -} yaml_parser_t; - -/** - * Initialize a parser. - * - * This function creates a new parser object. An application is responsible - * for destroying the object using the yaml_parser_delete() function. - * - * @param[out] parser An empty parser object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_parser_initialize(yaml_parser_t *parser); - -/** - * Destroy a parser. - * - * @param[in,out] parser A parser object. - */ - -YAML_DECLARE(void) -yaml_parser_delete(yaml_parser_t *parser); - -/** - * Set a string input. - * - * Note that the @a input pointer must be valid while the @a parser object - * exists. The application is responsible for destroing @a input after - * destroying the @a parser. - * - * @param[in,out] parser A parser object. - * @param[in] input A source data. - * @param[in] size The length of the source data in bytes. - */ - -YAML_DECLARE(void) -yaml_parser_set_input_string(yaml_parser_t *parser, - const unsigned char *input, size_t size); - -/** - * Set a file input. - * - * @a file should be a file object open for reading. The application is - * responsible for closing the @a file. - * - * @param[in,out] parser A parser object. - * @param[in] file An open file. - */ - -YAML_DECLARE(void) -yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file); - -/** - * Set a generic input handler. - * - * @param[in,out] parser A parser object. - * @param[in] handler A read handler. - * @param[in] data Any application data for passing to the read - * handler. - */ - -YAML_DECLARE(void) -yaml_parser_set_input(yaml_parser_t *parser, - yaml_read_handler_t *handler, void *data); - -/** - * Set the source encoding. - * - * @param[in,out] parser A parser object. - * @param[in] encoding The source encoding. - */ - -YAML_DECLARE(void) -yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding); - -/** - * Scan the input stream and produce the next token. - * - * Call the function subsequently to produce a sequence of tokens corresponding - * to the input stream. The initial token has the type - * @c YAML_STREAM_START_TOKEN while the ending token has the type - * @c YAML_STREAM_END_TOKEN. - * - * An application is responsible for freeing any buffers associated with the - * produced token object using the @c yaml_token_delete function. - * - * An application must not alternate the calls of yaml_parser_scan() with the - * calls of yaml_parser_parse() or yaml_parser_load(). Doing this will break - * the parser. - * - * @param[in,out] parser A parser object. - * @param[out] token An empty token object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token); - -/** - * Parse the input stream and produce the next parsing event. - * - * Call the function subsequently to produce a sequence of events corresponding - * to the input stream. The initial event has the type - * @c YAML_STREAM_START_EVENT while the ending event has the type - * @c YAML_STREAM_END_EVENT. - * - * An application is responsible for freeing any buffers associated with the - * produced event object using the yaml_event_delete() function. - * - * An application must not alternate the calls of yaml_parser_parse() with the - * calls of yaml_parser_scan() or yaml_parser_load(). Doing this will break the - * parser. - * - * @param[in,out] parser A parser object. - * @param[out] event An empty event object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event); - -/** - * Parse the input stream and produce the next YAML document. - * - * Call this function subsequently to produce a sequence of documents - * constituting the input stream. - * - * If the produced document has no root node, it means that the document - * end has been reached. - * - * An application is responsible for freeing any data associated with the - * produced document object using the yaml_document_delete() function. - * - * An application must not alternate the calls of yaml_parser_load() with the - * calls of yaml_parser_scan() or yaml_parser_parse(). Doing this will break - * the parser. - * - * @param[in,out] parser A parser object. - * @param[out] document An empty document object. - * - * @return @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document); - -/** @} */ - -/** - * @defgroup emitter Emitter Definitions - * @{ - */ - -/** - * The prototype of a write handler. - * - * The write handler is called when the emitter needs to flush the accumulated - * characters to the output. The handler should write @a size bytes of the - * @a buffer to the output. - * - * @param[in,out] data A pointer to an application data specified by - * yaml_emitter_set_output(). - * @param[in] buffer The buffer with bytes to be written. - * @param[in] size The size of the buffer. - * - * @returns On success, the handler should return @c 1. If the handler failed, - * the returned value should be @c 0. - */ - -typedef int yaml_write_handler_t(void *data, unsigned char *buffer, size_t size); - -/** The emitter states. */ -typedef enum yaml_emitter_state_e { - /** Expect STREAM-START. */ - YAML_EMIT_STREAM_START_STATE, - /** Expect the first DOCUMENT-START or STREAM-END. */ - YAML_EMIT_FIRST_DOCUMENT_START_STATE, - /** Expect DOCUMENT-START or STREAM-END. */ - YAML_EMIT_DOCUMENT_START_STATE, - /** Expect the content of a document. */ - YAML_EMIT_DOCUMENT_CONTENT_STATE, - /** Expect DOCUMENT-END. */ - YAML_EMIT_DOCUMENT_END_STATE, - /** Expect the first item of a flow sequence. */ - YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE, - /** Expect an item of a flow sequence. */ - YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE, - /** Expect the first key of a flow mapping. */ - YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE, - /** Expect a key of a flow mapping. */ - YAML_EMIT_FLOW_MAPPING_KEY_STATE, - /** Expect a value for a simple key of a flow mapping. */ - YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE, - /** Expect a value of a flow mapping. */ - YAML_EMIT_FLOW_MAPPING_VALUE_STATE, - /** Expect the first item of a block sequence. */ - YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE, - /** Expect an item of a block sequence. */ - YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE, - /** Expect the first key of a block mapping. */ - YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE, - /** Expect the key of a block mapping. */ - YAML_EMIT_BLOCK_MAPPING_KEY_STATE, - /** Expect a value for a simple key of a block mapping. */ - YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE, - /** Expect a value of a block mapping. */ - YAML_EMIT_BLOCK_MAPPING_VALUE_STATE, - /** Expect nothing. */ - YAML_EMIT_END_STATE -} yaml_emitter_state_t; - -/** - * The emitter structure. - * - * All members are internal. Manage the structure using the @c yaml_emitter_ - * family of functions. - */ - -typedef struct yaml_emitter_s { - - /** - * @name Error handling - * @{ - */ - - /** Error type. */ - yaml_error_type_t error; - /** Error description. */ - const char *problem; - - /** - * @} - */ - - /** - * @name Writer stuff - * @{ - */ - - /** Write handler. */ - yaml_write_handler_t *write_handler; - - /** A pointer for passing to the white handler. */ - void *write_handler_data; - - /** Standard (string or file) output data. */ - union { - /** String output data. */ - struct { - /** The buffer pointer. */ - unsigned char *buffer; - /** The buffer size. */ - size_t size; - /** The number of written bytes. */ - size_t *size_written; - } string; - - /** File output data. */ - FILE *file; - } output; - - /** The working buffer. */ - struct { - /** The beginning of the buffer. */ - yaml_char_t *start; - /** The end of the buffer. */ - yaml_char_t *end; - /** The current position of the buffer. */ - yaml_char_t *pointer; - /** The last filled position of the buffer. */ - yaml_char_t *last; - } buffer; - - /** The raw buffer. */ - struct { - /** The beginning of the buffer. */ - unsigned char *start; - /** The end of the buffer. */ - unsigned char *end; - /** The current position of the buffer. */ - unsigned char *pointer; - /** The last filled position of the buffer. */ - unsigned char *last; - } raw_buffer; - - /** The stream encoding. */ - yaml_encoding_t encoding; - - /** - * @} - */ - - /** - * @name Emitter stuff - * @{ - */ - - /** If the output is in the canonical style? */ - int canonical; - /** The number of indentation spaces. */ - int best_indent; - /** The preferred width of the output lines. */ - int best_width; - /** Allow unescaped non-ASCII characters? */ - int unicode; - /** The preferred line break. */ - yaml_break_t line_break; - - /** The stack of states. */ - struct { - /** The beginning of the stack. */ - yaml_emitter_state_t *start; - /** The end of the stack. */ - yaml_emitter_state_t *end; - /** The top of the stack. */ - yaml_emitter_state_t *top; - } states; - - /** The current emitter state. */ - yaml_emitter_state_t state; - - /** The event queue. */ - struct { - /** The beginning of the event queue. */ - yaml_event_t *start; - /** The end of the event queue. */ - yaml_event_t *end; - /** The head of the event queue. */ - yaml_event_t *head; - /** The tail of the event queue. */ - yaml_event_t *tail; - } events; - - /** The stack of indentation levels. */ - struct { - /** The beginning of the stack. */ - int *start; - /** The end of the stack. */ - int *end; - /** The top of the stack. */ - int *top; - } indents; - - /** The list of tag directives. */ - struct { - /** The beginning of the list. */ - yaml_tag_directive_t *start; - /** The end of the list. */ - yaml_tag_directive_t *end; - /** The top of the list. */ - yaml_tag_directive_t *top; - } tag_directives; - - /** The current indentation level. */ - int indent; - - /** The current flow level. */ - int flow_level; - - /** Is it the document root context? */ - int root_context; - /** Is it a sequence context? */ - int sequence_context; - /** Is it a mapping context? */ - int mapping_context; - /** Is it a simple mapping key context? */ - int simple_key_context; - - /** The current line. */ - int line; - /** The current column. */ - int column; - /** If the last character was a whitespace? */ - int whitespace; - /** If the last character was an indentation character (' ', '-', '?', ':')? */ - int indention; - /** If an explicit document end is required? */ - int open_ended; - - /** Anchor analysis. */ - struct { - /** The anchor value. */ - yaml_char_t *anchor; - /** The anchor length. */ - size_t anchor_length; - /** Is it an alias? */ - int alias; - } anchor_data; - - /** Tag analysis. */ - struct { - /** The tag handle. */ - yaml_char_t *handle; - /** The tag handle length. */ - size_t handle_length; - /** The tag suffix. */ - yaml_char_t *suffix; - /** The tag suffix length. */ - size_t suffix_length; - } tag_data; - - /** Scalar analysis. */ - struct { - /** The scalar value. */ - yaml_char_t *value; - /** The scalar length. */ - size_t length; - /** Does the scalar contain line breaks? */ - int multiline; - /** Can the scalar be expessed in the flow plain style? */ - int flow_plain_allowed; - /** Can the scalar be expressed in the block plain style? */ - int block_plain_allowed; - /** Can the scalar be expressed in the single quoted style? */ - int single_quoted_allowed; - /** Can the scalar be expressed in the literal or folded styles? */ - int block_allowed; - /** The output style. */ - yaml_scalar_style_t style; - } scalar_data; - - /** - * @} - */ - - /** - * @name Dumper stuff - * @{ - */ - - /** If the stream was already opened? */ - int opened; - /** If the stream was already closed? */ - int closed; - - /** The information associated with the document nodes. */ - struct { - /** The number of references. */ - int references; - /** The anchor id. */ - int anchor; - /** If the node has been emitted? */ - int serialized; - } *anchors; - - /** The last assigned anchor id. */ - int last_anchor_id; - - /** The currently emitted document. */ - yaml_document_t *document; - - /** - * @} - */ - -} yaml_emitter_t; - -/** - * Initialize an emitter. - * - * This function creates a new emitter object. An application is responsible - * for destroying the object using the yaml_emitter_delete() function. - * - * @param[out] emitter An empty parser object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_emitter_initialize(yaml_emitter_t *emitter); - -/** - * Destroy an emitter. - * - * @param[in,out] emitter An emitter object. - */ - -YAML_DECLARE(void) -yaml_emitter_delete(yaml_emitter_t *emitter); - -/** - * Set a string output. - * - * The emitter will write the output characters to the @a output buffer of the - * size @a size. The emitter will set @a size_written to the number of written - * bytes. If the buffer is smaller than required, the emitter produces the - * YAML_WRITE_ERROR error. - * - * @param[in,out] emitter An emitter object. - * @param[in] output An output buffer. - * @param[in] size The buffer size. - * @param[in] size_written The pointer to save the number of written - * bytes. - */ - -YAML_DECLARE(void) -yaml_emitter_set_output_string(yaml_emitter_t *emitter, - unsigned char *output, size_t size, size_t *size_written); - -/** - * Set a file output. - * - * @a file should be a file object open for writing. The application is - * responsible for closing the @a file. - * - * @param[in,out] emitter An emitter object. - * @param[in] file An open file. - */ - -YAML_DECLARE(void) -yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file); - -/** - * Set a generic output handler. - * - * @param[in,out] emitter An emitter object. - * @param[in] handler A write handler. - * @param[in] data Any application data for passing to the write - * handler. - */ - -YAML_DECLARE(void) -yaml_emitter_set_output(yaml_emitter_t *emitter, - yaml_write_handler_t *handler, void *data); - -/** - * Set the output encoding. - * - * @param[in,out] emitter An emitter object. - * @param[in] encoding The output encoding. - */ - -YAML_DECLARE(void) -yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding); - -/** - * Set if the output should be in the "canonical" format as in the YAML - * specification. - * - * @param[in,out] emitter An emitter object. - * @param[in] canonical If the output is canonical. - */ - -YAML_DECLARE(void) -yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical); - -/** - * Set the intendation increment. - * - * @param[in,out] emitter An emitter object. - * @param[in] indent The indentation increment (1 < . < 10). - */ - -YAML_DECLARE(void) -yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent); - -/** - * Set the preferred line width. @c -1 means unlimited. - * - * @param[in,out] emitter An emitter object. - * @param[in] width The preferred line width. - */ - -YAML_DECLARE(void) -yaml_emitter_set_width(yaml_emitter_t *emitter, int width); - -/** - * Set if unescaped non-ASCII characters are allowed. - * - * @param[in,out] emitter An emitter object. - * @param[in] unicode If unescaped Unicode characters are allowed. - */ - -YAML_DECLARE(void) -yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode); - -/** - * Set the preferred line break. - * - * @param[in,out] emitter An emitter object. - * @param[in] line_break The preferred line break. - */ - -YAML_DECLARE(void) -yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break); - -/** - * Emit an event. - * - * The event object may be generated using the yaml_parser_parse() function. - * The emitter takes the responsibility for the event object and destroys its - * content after it is emitted. The event object is destroyed even if the - * function fails. - * - * @param[in,out] emitter An emitter object. - * @param[in,out] event An event object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); - -/** - * Start a YAML stream. - * - * This function should be used before yaml_emitter_dump() is called. - * - * @param[in,out] emitter An emitter object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_emitter_open(yaml_emitter_t *emitter); - -/** - * Finish a YAML stream. - * - * This function should be used after yaml_emitter_dump() is called. - * - * @param[in,out] emitter An emitter object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_emitter_close(yaml_emitter_t *emitter); - -/** - * Emit a YAML document. - * - * The documen object may be generated using the yaml_parser_load() function - * or the yaml_document_initialize() function. The emitter takes the - * responsibility for the document object and destoys its content after - * it is emitted. The document object is destroyedeven if the function fails. - * - * @param[in,out] emitter An emitter object. - * @param[in,out] document A document object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document); - -/** - * Flush the accumulated characters to the output. - * - * @param[in,out] emitter An emitter object. - * - * @returns @c 1 if the function succeeded, @c 0 on error. - */ - -YAML_DECLARE(int) -yaml_emitter_flush(yaml_emitter_t *emitter); - -/** @} */ - -#ifdef __cplusplus -} -#endif - -#endif /* #ifndef YAML_H */ - diff --git a/yaml_private.h b/yaml_private.h deleted file mode 100644 index ed5ea66..0000000 --- a/yaml_private.h +++ /dev/null @@ -1,640 +0,0 @@ - -#if HAVE_CONFIG_H -#include -#endif - -#include - -#include -#include - -/* - * Memory management. - */ - -YAML_DECLARE(void *) -yaml_malloc(size_t size); - -YAML_DECLARE(void *) -yaml_realloc(void *ptr, size_t size); - -YAML_DECLARE(void) -yaml_free(void *ptr); - -YAML_DECLARE(yaml_char_t *) -yaml_strdup(const yaml_char_t *); - -/* - * Reader: Ensure that the buffer contains at least `length` characters. - */ - -YAML_DECLARE(int) -yaml_parser_update_buffer(yaml_parser_t *parser, size_t length); - -/* - * Scanner: Ensure that the token stack contains at least one token ready. - */ - -YAML_DECLARE(int) -yaml_parser_fetch_more_tokens(yaml_parser_t *parser); - -/* - * The size of the input raw buffer. - */ - -#define INPUT_RAW_BUFFER_SIZE 16384 - -/* - * The size of the input buffer. - * - * It should be possible to decode the whole raw buffer. - */ - -#define INPUT_BUFFER_SIZE (INPUT_RAW_BUFFER_SIZE*3) - -/* - * The size of the output buffer. - */ - -#define OUTPUT_BUFFER_SIZE 16384 - -/* - * The size of the output raw buffer. - * - * It should be possible to encode the whole output buffer. - */ - -#define OUTPUT_RAW_BUFFER_SIZE (OUTPUT_BUFFER_SIZE*2+2) - -/* - * The size of other stacks and queues. - */ - -#define INITIAL_STACK_SIZE 16 -#define INITIAL_QUEUE_SIZE 16 -#define INITIAL_STRING_SIZE 16 - -/* - * Buffer management. - */ - -#define BUFFER_INIT(context,buffer,size) \ - (((buffer).start = yaml_malloc(size)) ? \ - ((buffer).last = (buffer).pointer = (buffer).start, \ - (buffer).end = (buffer).start+(size), \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -#define BUFFER_DEL(context,buffer) \ - (yaml_free((buffer).start), \ - (buffer).start = (buffer).pointer = (buffer).end = 0) - -/* - * String management. - */ - -typedef struct { - yaml_char_t *start; - yaml_char_t *end; - yaml_char_t *pointer; -} yaml_string_t; - -YAML_DECLARE(int) -yaml_string_extend(yaml_char_t **start, - yaml_char_t **pointer, yaml_char_t **end); - -YAML_DECLARE(int) -yaml_string_join( - yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end, - yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end); - -#define NULL_STRING { NULL, NULL, NULL } - -#define STRING(string,length) { (string), (string)+(length), (string) } - -#define STRING_ASSIGN(value,string,length) \ - ((value).start = (string), \ - (value).end = (string)+(length), \ - (value).pointer = (string)) - -#define STRING_INIT(context,string,size) \ - (((string).start = yaml_malloc(size)) ? \ - ((string).pointer = (string).start, \ - (string).end = (string).start+(size), \ - memset((string).start, 0, (size)), \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -#define STRING_DEL(context,string) \ - (yaml_free((string).start), \ - (string).start = (string).pointer = (string).end = 0) - -#define STRING_EXTEND(context,string) \ - (((string).pointer+5 < (string).end) \ - || yaml_string_extend(&(string).start, \ - &(string).pointer, &(string).end)) - -#define CLEAR(context,string) \ - ((string).pointer = (string).start, \ - memset((string).start, 0, (string).end-(string).start)) - -#define JOIN(context,string_a,string_b) \ - ((yaml_string_join(&(string_a).start, &(string_a).pointer, \ - &(string_a).end, &(string_b).start, \ - &(string_b).pointer, &(string_b).end)) ? \ - ((string_b).pointer = (string_b).start, \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -/* - * String check operations. - */ - -/* - * Check the octet at the specified position. - */ - -#define CHECK_AT(string,octet,offset) \ - ((string).pointer[offset] == (yaml_char_t)(octet)) - -/* - * Check the current octet in the buffer. - */ - -#define CHECK(string,octet) CHECK_AT((string),(octet),0) - -/* - * Check if the character at the specified position is an alphabetical - * character, a digit, '_', or '-'. - */ - -#define IS_ALPHA_AT(string,offset) \ - (((string).pointer[offset] >= (yaml_char_t) '0' && \ - (string).pointer[offset] <= (yaml_char_t) '9') || \ - ((string).pointer[offset] >= (yaml_char_t) 'A' && \ - (string).pointer[offset] <= (yaml_char_t) 'Z') || \ - ((string).pointer[offset] >= (yaml_char_t) 'a' && \ - (string).pointer[offset] <= (yaml_char_t) 'z') || \ - (string).pointer[offset] == '_' || \ - (string).pointer[offset] == '-') - -#define IS_ALPHA(string) IS_ALPHA_AT((string),0) - -/* - * Check if the character at the specified position is a digit. - */ - -#define IS_DIGIT_AT(string,offset) \ - (((string).pointer[offset] >= (yaml_char_t) '0' && \ - (string).pointer[offset] <= (yaml_char_t) '9')) - -#define IS_DIGIT(string) IS_DIGIT_AT((string),0) - -/* - * Get the value of a digit. - */ - -#define AS_DIGIT_AT(string,offset) \ - ((string).pointer[offset] - (yaml_char_t) '0') - -#define AS_DIGIT(string) AS_DIGIT_AT((string),0) - -/* - * Check if the character at the specified position is a hex-digit. - */ - -#define IS_HEX_AT(string,offset) \ - (((string).pointer[offset] >= (yaml_char_t) '0' && \ - (string).pointer[offset] <= (yaml_char_t) '9') || \ - ((string).pointer[offset] >= (yaml_char_t) 'A' && \ - (string).pointer[offset] <= (yaml_char_t) 'F') || \ - ((string).pointer[offset] >= (yaml_char_t) 'a' && \ - (string).pointer[offset] <= (yaml_char_t) 'f')) - -#define IS_HEX(string) IS_HEX_AT((string),0) - -/* - * Get the value of a hex-digit. - */ - -#define AS_HEX_AT(string,offset) \ - (((string).pointer[offset] >= (yaml_char_t) 'A' && \ - (string).pointer[offset] <= (yaml_char_t) 'F') ? \ - ((string).pointer[offset] - (yaml_char_t) 'A' + 10) : \ - ((string).pointer[offset] >= (yaml_char_t) 'a' && \ - (string).pointer[offset] <= (yaml_char_t) 'f') ? \ - ((string).pointer[offset] - (yaml_char_t) 'a' + 10) : \ - ((string).pointer[offset] - (yaml_char_t) '0')) - -#define AS_HEX(string) AS_HEX_AT((string),0) - -/* - * Check if the character is ASCII. - */ - -#define IS_ASCII_AT(string,offset) \ - ((string).pointer[offset] <= (yaml_char_t) '\x7F') - -#define IS_ASCII(string) IS_ASCII_AT((string),0) - -/* - * Check if the character can be printed unescaped. - */ - -#define IS_PRINTABLE_AT(string,offset) \ - (((string).pointer[offset] == 0x0A) /* . == #x0A */ \ - || ((string).pointer[offset] >= 0x20 /* #x20 <= . <= #x7E */ \ - && (string).pointer[offset] <= 0x7E) \ - || ((string).pointer[offset] == 0xC2 /* #0xA0 <= . <= #xD7FF */ \ - && (string).pointer[offset+1] >= 0xA0) \ - || ((string).pointer[offset] > 0xC2 \ - && (string).pointer[offset] < 0xED) \ - || ((string).pointer[offset] == 0xED \ - && (string).pointer[offset+1] < 0xA0) \ - || ((string).pointer[offset] == 0xEE) \ - || ((string).pointer[offset] == 0xEF /* #xE000 <= . <= #xFFFD */ \ - && !((string).pointer[offset+1] == 0xBB /* && . != #xFEFF */ \ - && (string).pointer[offset+2] == 0xBF) \ - && !((string).pointer[offset+1] == 0xBF \ - && ((string).pointer[offset+2] == 0xBE \ - || (string).pointer[offset+2] == 0xBF)))) - -#define IS_PRINTABLE(string) IS_PRINTABLE_AT((string),0) - -/* - * Check if the character at the specified position is NUL. - */ - -#define IS_Z_AT(string,offset) CHECK_AT((string),'\0',(offset)) - -#define IS_Z(string) IS_Z_AT((string),0) - -/* - * Check if the character at the specified position is BOM. - */ - -#define IS_BOM_AT(string,offset) \ - (CHECK_AT((string),'\xEF',(offset)) \ - && CHECK_AT((string),'\xBB',(offset)+1) \ - && CHECK_AT((string),'\xBF',(offset)+2)) /* BOM (#xFEFF) */ - -#define IS_BOM(string) IS_BOM_AT(string,0) - -/* - * Check if the character at the specified position is space. - */ - -#define IS_SPACE_AT(string,offset) CHECK_AT((string),' ',(offset)) - -#define IS_SPACE(string) IS_SPACE_AT((string),0) - -/* - * Check if the character at the specified position is tab. - */ - -#define IS_TAB_AT(string,offset) CHECK_AT((string),'\t',(offset)) - -#define IS_TAB(string) IS_TAB_AT((string),0) - -/* - * Check if the character at the specified position is blank (space or tab). - */ - -#define IS_BLANK_AT(string,offset) \ - (IS_SPACE_AT((string),(offset)) || IS_TAB_AT((string),(offset))) - -#define IS_BLANK(string) IS_BLANK_AT((string),0) - -/* - * Check if the character at the specified position is a line break. - */ - -#define IS_BREAK_AT(string,offset) \ - (CHECK_AT((string),'\r',(offset)) /* CR (#xD)*/ \ - || CHECK_AT((string),'\n',(offset)) /* LF (#xA) */ \ - || (CHECK_AT((string),'\xC2',(offset)) \ - && CHECK_AT((string),'\x85',(offset)+1)) /* NEL (#x85) */ \ - || (CHECK_AT((string),'\xE2',(offset)) \ - && CHECK_AT((string),'\x80',(offset)+1) \ - && CHECK_AT((string),'\xA8',(offset)+2)) /* LS (#x2028) */ \ - || (CHECK_AT((string),'\xE2',(offset)) \ - && CHECK_AT((string),'\x80',(offset)+1) \ - && CHECK_AT((string),'\xA9',(offset)+2))) /* PS (#x2029) */ - -#define IS_BREAK(string) IS_BREAK_AT((string),0) - -#define IS_CRLF_AT(string,offset) \ - (CHECK_AT((string),'\r',(offset)) && CHECK_AT((string),'\n',(offset)+1)) - -#define IS_CRLF(string) IS_CRLF_AT((string),0) - -/* - * Check if the character is a line break or NUL. - */ - -#define IS_BREAKZ_AT(string,offset) \ - (IS_BREAK_AT((string),(offset)) || IS_Z_AT((string),(offset))) - -#define IS_BREAKZ(string) IS_BREAKZ_AT((string),0) - -/* - * Check if the character is a line break, space, or NUL. - */ - -#define IS_SPACEZ_AT(string,offset) \ - (IS_SPACE_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset))) - -#define IS_SPACEZ(string) IS_SPACEZ_AT((string),0) - -/* - * Check if the character is a line break, space, tab, or NUL. - */ - -#define IS_BLANKZ_AT(string,offset) \ - (IS_BLANK_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset))) - -#define IS_BLANKZ(string) IS_BLANKZ_AT((string),0) - -/* - * Determine the width of the character. - */ - -#define WIDTH_AT(string,offset) \ - (((string).pointer[offset] & 0x80) == 0x00 ? 1 : \ - ((string).pointer[offset] & 0xE0) == 0xC0 ? 2 : \ - ((string).pointer[offset] & 0xF0) == 0xE0 ? 3 : \ - ((string).pointer[offset] & 0xF8) == 0xF0 ? 4 : 0) - -#define WIDTH(string) WIDTH_AT((string),0) - -/* - * Move the string pointer to the next character. - */ - -#define MOVE(string) ((string).pointer += WIDTH((string))) - -/* - * Copy a character and move the pointers of both strings. - */ - -#define COPY(string_a,string_b) \ - ((*(string_b).pointer & 0x80) == 0x00 ? \ - (*((string_a).pointer++) = *((string_b).pointer++)) : \ - (*(string_b).pointer & 0xE0) == 0xC0 ? \ - (*((string_a).pointer++) = *((string_b).pointer++), \ - *((string_a).pointer++) = *((string_b).pointer++)) : \ - (*(string_b).pointer & 0xF0) == 0xE0 ? \ - (*((string_a).pointer++) = *((string_b).pointer++), \ - *((string_a).pointer++) = *((string_b).pointer++), \ - *((string_a).pointer++) = *((string_b).pointer++)) : \ - (*(string_b).pointer & 0xF8) == 0xF0 ? \ - (*((string_a).pointer++) = *((string_b).pointer++), \ - *((string_a).pointer++) = *((string_b).pointer++), \ - *((string_a).pointer++) = *((string_b).pointer++), \ - *((string_a).pointer++) = *((string_b).pointer++)) : 0) - -/* - * Stack and queue management. - */ - -YAML_DECLARE(int) -yaml_stack_extend(void **start, void **top, void **end); - -YAML_DECLARE(int) -yaml_queue_extend(void **start, void **head, void **tail, void **end); - -#define STACK_INIT(context,stack,size) \ - (((stack).start = yaml_malloc((size)*sizeof(*(stack).start))) ? \ - ((stack).top = (stack).start, \ - (stack).end = (stack).start+(size), \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -#define STACK_DEL(context,stack) \ - (yaml_free((stack).start), \ - (stack).start = (stack).top = (stack).end = 0) - -#define STACK_EMPTY(context,stack) \ - ((stack).start == (stack).top) - -#define PUSH(context,stack,value) \ - (((stack).top != (stack).end \ - || yaml_stack_extend((void **)&(stack).start, \ - (void **)&(stack).top, (void **)&(stack).end)) ? \ - (*((stack).top++) = value, \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -#define POP(context,stack) \ - (*(--(stack).top)) - -#define QUEUE_INIT(context,queue,size) \ - (((queue).start = yaml_malloc((size)*sizeof(*(queue).start))) ? \ - ((queue).head = (queue).tail = (queue).start, \ - (queue).end = (queue).start+(size), \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -#define QUEUE_DEL(context,queue) \ - (yaml_free((queue).start), \ - (queue).start = (queue).head = (queue).tail = (queue).end = 0) - -#define QUEUE_EMPTY(context,queue) \ - ((queue).head == (queue).tail) - -#define ENQUEUE(context,queue,value) \ - (((queue).tail != (queue).end \ - || yaml_queue_extend((void **)&(queue).start, (void **)&(queue).head, \ - (void **)&(queue).tail, (void **)&(queue).end)) ? \ - (*((queue).tail++) = value, \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -#define DEQUEUE(context,queue) \ - (*((queue).head++)) - -#define QUEUE_INSERT(context,queue,index,value) \ - (((queue).tail != (queue).end \ - || yaml_queue_extend((void **)&(queue).start, (void **)&(queue).head, \ - (void **)&(queue).tail, (void **)&(queue).end)) ? \ - (memmove((queue).head+(index)+1,(queue).head+(index), \ - ((queue).tail-(queue).head-(index))*sizeof(*(queue).start)), \ - *((queue).head+(index)) = value, \ - (queue).tail++, \ - 1) : \ - ((context)->error = YAML_MEMORY_ERROR, \ - 0)) - -/* - * Token initializers. - */ - -#define TOKEN_INIT(token,token_type,token_start_mark,token_end_mark) \ - (memset(&(token), 0, sizeof(yaml_token_t)), \ - (token).type = (token_type), \ - (token).start_mark = (token_start_mark), \ - (token).end_mark = (token_end_mark)) - -#define STREAM_START_TOKEN_INIT(token,token_encoding,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_STREAM_START_TOKEN,(start_mark),(end_mark)), \ - (token).data.stream_start.encoding = (token_encoding)) - -#define STREAM_END_TOKEN_INIT(token,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_STREAM_END_TOKEN,(start_mark),(end_mark))) - -#define ALIAS_TOKEN_INIT(token,token_value,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_ALIAS_TOKEN,(start_mark),(end_mark)), \ - (token).data.alias.value = (token_value)) - -#define ANCHOR_TOKEN_INIT(token,token_value,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_ANCHOR_TOKEN,(start_mark),(end_mark)), \ - (token).data.anchor.value = (token_value)) - -#define TAG_TOKEN_INIT(token,token_handle,token_suffix,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_TAG_TOKEN,(start_mark),(end_mark)), \ - (token).data.tag.handle = (token_handle), \ - (token).data.tag.suffix = (token_suffix)) - -#define SCALAR_TOKEN_INIT(token,token_value,token_length,token_style,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_SCALAR_TOKEN,(start_mark),(end_mark)), \ - (token).data.scalar.value = (token_value), \ - (token).data.scalar.length = (token_length), \ - (token).data.scalar.style = (token_style)) - -#define VERSION_DIRECTIVE_TOKEN_INIT(token,token_major,token_minor,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_VERSION_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \ - (token).data.version_directive.major = (token_major), \ - (token).data.version_directive.minor = (token_minor)) - -#define TAG_DIRECTIVE_TOKEN_INIT(token,token_handle,token_prefix,start_mark,end_mark) \ - (TOKEN_INIT((token),YAML_TAG_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \ - (token).data.tag_directive.handle = (token_handle), \ - (token).data.tag_directive.prefix = (token_prefix)) - -/* - * Event initializers. - */ - -#define EVENT_INIT(event,event_type,event_start_mark,event_end_mark) \ - (memset(&(event), 0, sizeof(yaml_event_t)), \ - (event).type = (event_type), \ - (event).start_mark = (event_start_mark), \ - (event).end_mark = (event_end_mark)) - -#define STREAM_START_EVENT_INIT(event,event_encoding,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_STREAM_START_EVENT,(start_mark),(end_mark)), \ - (event).data.stream_start.encoding = (event_encoding)) - -#define STREAM_END_EVENT_INIT(event,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_STREAM_END_EVENT,(start_mark),(end_mark))) - -#define DOCUMENT_START_EVENT_INIT(event,event_version_directive, \ - event_tag_directives_start,event_tag_directives_end,event_implicit,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_DOCUMENT_START_EVENT,(start_mark),(end_mark)), \ - (event).data.document_start.version_directive = (event_version_directive), \ - (event).data.document_start.tag_directives.start = (event_tag_directives_start), \ - (event).data.document_start.tag_directives.end = (event_tag_directives_end), \ - (event).data.document_start.implicit = (event_implicit)) - -#define DOCUMENT_END_EVENT_INIT(event,event_implicit,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_DOCUMENT_END_EVENT,(start_mark),(end_mark)), \ - (event).data.document_end.implicit = (event_implicit)) - -#define ALIAS_EVENT_INIT(event,event_anchor,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_ALIAS_EVENT,(start_mark),(end_mark)), \ - (event).data.alias.anchor = (event_anchor)) - -#define SCALAR_EVENT_INIT(event,event_anchor,event_tag,event_value,event_length, \ - event_plain_implicit, event_quoted_implicit,event_style,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_SCALAR_EVENT,(start_mark),(end_mark)), \ - (event).data.scalar.anchor = (event_anchor), \ - (event).data.scalar.tag = (event_tag), \ - (event).data.scalar.value = (event_value), \ - (event).data.scalar.length = (event_length), \ - (event).data.scalar.plain_implicit = (event_plain_implicit), \ - (event).data.scalar.quoted_implicit = (event_quoted_implicit), \ - (event).data.scalar.style = (event_style)) - -#define SEQUENCE_START_EVENT_INIT(event,event_anchor,event_tag, \ - event_implicit,event_style,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_SEQUENCE_START_EVENT,(start_mark),(end_mark)), \ - (event).data.sequence_start.anchor = (event_anchor), \ - (event).data.sequence_start.tag = (event_tag), \ - (event).data.sequence_start.implicit = (event_implicit), \ - (event).data.sequence_start.style = (event_style)) - -#define SEQUENCE_END_EVENT_INIT(event,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_SEQUENCE_END_EVENT,(start_mark),(end_mark))) - -#define MAPPING_START_EVENT_INIT(event,event_anchor,event_tag, \ - event_implicit,event_style,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_MAPPING_START_EVENT,(start_mark),(end_mark)), \ - (event).data.mapping_start.anchor = (event_anchor), \ - (event).data.mapping_start.tag = (event_tag), \ - (event).data.mapping_start.implicit = (event_implicit), \ - (event).data.mapping_start.style = (event_style)) - -#define MAPPING_END_EVENT_INIT(event,start_mark,end_mark) \ - (EVENT_INIT((event),YAML_MAPPING_END_EVENT,(start_mark),(end_mark))) - -/* - * Document initializer. - */ - -#define DOCUMENT_INIT(document,document_nodes_start,document_nodes_end, \ - document_version_directive,document_tag_directives_start, \ - document_tag_directives_end,document_start_implicit, \ - document_end_implicit,document_start_mark,document_end_mark) \ - (memset(&(document), 0, sizeof(yaml_document_t)), \ - (document).nodes.start = (document_nodes_start), \ - (document).nodes.end = (document_nodes_end), \ - (document).nodes.top = (document_nodes_start), \ - (document).version_directive = (document_version_directive), \ - (document).tag_directives.start = (document_tag_directives_start), \ - (document).tag_directives.end = (document_tag_directives_end), \ - (document).start_implicit = (document_start_implicit), \ - (document).end_implicit = (document_end_implicit), \ - (document).start_mark = (document_start_mark), \ - (document).end_mark = (document_end_mark)) - -/* - * Node initializers. - */ - -#define NODE_INIT(node,node_type,node_tag,node_start_mark,node_end_mark) \ - (memset(&(node), 0, sizeof(yaml_node_t)), \ - (node).type = (node_type), \ - (node).tag = (node_tag), \ - (node).start_mark = (node_start_mark), \ - (node).end_mark = (node_end_mark)) - -#define SCALAR_NODE_INIT(node,node_tag,node_value,node_length, \ - node_style,start_mark,end_mark) \ - (NODE_INIT((node),YAML_SCALAR_NODE,(node_tag),(start_mark),(end_mark)), \ - (node).data.scalar.value = (node_value), \ - (node).data.scalar.length = (node_length), \ - (node).data.scalar.style = (node_style)) - -#define SEQUENCE_NODE_INIT(node,node_tag,node_items_start,node_items_end, \ - node_style,start_mark,end_mark) \ - (NODE_INIT((node),YAML_SEQUENCE_NODE,(node_tag),(start_mark),(end_mark)), \ - (node).data.sequence.items.start = (node_items_start), \ - (node).data.sequence.items.end = (node_items_end), \ - (node).data.sequence.items.top = (node_items_start), \ - (node).data.sequence.style = (node_style)) - -#define MAPPING_NODE_INIT(node,node_tag,node_pairs_start,node_pairs_end, \ - node_style,start_mark,end_mark) \ - (NODE_INIT((node),YAML_MAPPING_NODE,(node_tag),(start_mark),(end_mark)), \ - (node).data.mapping.pairs.start = (node_pairs_start), \ - (node).data.mapping.pairs.end = (node_pairs_end), \ - (node).data.mapping.pairs.top = (node_pairs_start), \ - (node).data.mapping.style = (node_style)) -