From bcd7b8ce20839fae222aac48d0b1328d12e40e67 Mon Sep 17 00:00:00 2001 From: xi Date: Thu, 27 Dec 2007 12:05:17 +0000 Subject: Completed the first phase of API refactoring. git-svn-id: http://svn.pyyaml.org/libyaml/trunk@263 18f92427-320e-0410-9341-c67f048884a3 --- src/api.c | 16 +- src/dumper.c | 4 + src/emitter.c | 456 ++++++++++++++-------------- src/loader.c | 4 + src/parser.c | 163 ++++------ src/reader.c | 93 +++--- src/scanner.c | 836 +++++++++++++++++++++++++++------------------------- src/writer.c | 82 +++--- src/yaml_private.h | 40 +-- tests/run-scanner.c | 12 +- 10 files changed, 836 insertions(+), 870 deletions(-) diff --git a/src/api.c b/src/api.c index 1fefb72..c32fc5e 100644 --- a/src/api.c +++ b/src/api.c @@ -172,9 +172,9 @@ yaml_parser_new(void) return NULL; memset(parser, 0, sizeof(yaml_parser_t)); - if (!BUFFER_INIT(parser, parser->raw_input, RAW_INPUT_BUFFER_CAPACITY)) + if (!STRING_INIT(parser, parser->raw_input, RAW_INPUT_BUFFER_CAPACITY)) goto error; - if (!BUFFER_INIT(parser, parser->input, INPUT_BUFFER_CAPACITY)) + if (!STRING_INIT(parser, parser->input, INPUT_BUFFER_CAPACITY)) goto error; if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_CAPACITY)) goto error; @@ -206,8 +206,8 @@ yaml_parser_delete(yaml_parser_t *parser) { assert(parser); /* Non-NULL parser object expected. */ - BUFFER_DEL(parser, parser->raw_input); - BUFFER_DEL(parser, parser->input); + STRING_DEL(parser, parser->raw_input); + STRING_DEL(parser, parser->input); while (!QUEUE_EMPTY(parser, parser->tokens)) { yaml_token_destroy(&DEQUEUE(parser, parser->tokens)); } @@ -357,9 +357,9 @@ yaml_emitter_new(void) return NULL; memset(emitter, 0, sizeof(yaml_emitter_t)); - if (!BUFFER_INIT(emitter, emitter->output, OUTPUT_BUFFER_CAPACITY)) + if (!STRING_INIT(emitter, emitter->output, OUTPUT_BUFFER_CAPACITY)) goto error; - if (!BUFFER_INIT(emitter, emitter->raw_output, RAW_OUTPUT_BUFFER_CAPACITY)) + if (!STRING_INIT(emitter, emitter->raw_output, RAW_OUTPUT_BUFFER_CAPACITY)) goto error; if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_CAPACITY)) goto error; @@ -387,8 +387,8 @@ yaml_emitter_delete(yaml_emitter_t *emitter) { assert(emitter); /* Non-NULL emitter object expected. */ - BUFFER_DEL(emitter, emitter->output); - BUFFER_DEL(emitter, emitter->raw_output); + STRING_DEL(emitter, emitter->output); + STRING_DEL(emitter, emitter->raw_output); STACK_DEL(emitter, emitter->states); while (!QUEUE_EMPTY(emitter, emitter->events)) { yaml_event_delete(&DEQUEUE(emitter, emitter->events)); diff --git a/src/dumper.c b/src/dumper.c index 203c6a7..dc3ad66 100644 --- a/src/dumper.c +++ b/src/dumper.c @@ -1,6 +1,8 @@ #include "yaml_private.h" +#if 0 + /* * API functions. */ @@ -392,3 +394,5 @@ yaml_emitter_dump_mapping(yaml_emitter_t *emitter, yaml_node_t *node, return 1; } +#endif + diff --git a/src/emitter.c b/src/emitter.c index 2b92868..68dfd9d 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -6,7 +6,7 @@ */ #define FLUSH(emitter) \ - ((emitter->buffer.pointer+5 < emitter->buffer.end) \ + ((emitter->output.pointer+5 < emitter->output.capacity) \ || yaml_emitter_flush(emitter)) /* @@ -15,7 +15,7 @@ #define PUT(emitter,value) \ (FLUSH(emitter) \ - && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \ + && (JOIN_OCTET(emitter->output,(yaml_char_t)(value)), \ emitter->column ++, \ 1)) @@ -26,12 +26,12 @@ #define PUT_BREAK(emitter) \ (FLUSH(emitter) \ && ((emitter->line_break == YAML_CR_BREAK ? \ - (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \ + JOIN_OCTET(emitter->output, (yaml_char_t) '\r') : \ emitter->line_break == YAML_LN_BREAK ? \ - (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \ + JOIN_OCTET(emitter->output, (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), \ + (JOIN_OCTET(emitter->output, (yaml_char_t) '\r'), \ + JOIN_OCTET(emitter->output, (yaml_char_t) '\n')) : 0), \ emitter->column = 0, \ emitter->line ++, \ 1)) @@ -42,7 +42,7 @@ #define WRITE(emitter,string) \ (FLUSH(emitter) \ - && (COPY(emitter->buffer,string), \ + && (COPY(emitter->output,string), \ emitter->column ++, \ 1)) @@ -56,7 +56,7 @@ (PUT_BREAK(emitter), \ string.pointer ++, \ 1) : \ - (COPY(emitter->buffer,string), \ + (COPY(emitter->output,string), \ emitter->column = 0, \ emitter->line ++, \ 1))) @@ -72,9 +72,6 @@ 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); @@ -135,7 +132,7 @@ yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, static int yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, - int root, int sequence, int mapping, int simple_key); + int is_root, int is_sequence, int is_mapping, int is_simple_key); static int yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event); @@ -260,19 +257,6 @@ 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. */ @@ -286,9 +270,11 @@ yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) } while (!yaml_emitter_need_more_events(emitter)) { - if (!yaml_emitter_analyze_event(emitter, emitter->events.head)) + if (!yaml_emitter_analyze_event(emitter, + emitter->events.list + emitter->events.head)) return 0; - if (!yaml_emitter_state_machine(emitter, emitter->events.head)) + if (!yaml_emitter_state_machine(emitter, + emitter->events.list + emitter->events.head)) return 0; yaml_event_delete(&DEQUEUE(emitter, emitter->events)); } @@ -310,12 +296,12 @@ yaml_emitter_need_more_events(yaml_emitter_t *emitter) { int level = 0; int accumulate = 0; - yaml_event_t *event; + size_t idx; if (QUEUE_EMPTY(emitter, emitter->events)) return 1; - switch (emitter->events.head->type) { + switch (emitter->events.list[emitter->events.head].type) { case YAML_DOCUMENT_START_EVENT: accumulate = 1; break; @@ -332,7 +318,8 @@ yaml_emitter_need_more_events(yaml_emitter_t *emitter) if (emitter->events.tail - emitter->events.head > accumulate) return 0; - for (event = emitter->events.head; event != emitter->events.tail; event ++) { + for (idx = emitter->events.head; idx < emitter->events.tail; idx ++) { + yaml_event_t *event = emitter->events.list+idx; switch (event->type) { case YAML_STREAM_START_EVENT: case YAML_DOCUMENT_START_EVENT: @@ -364,23 +351,22 @@ 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; + int idx; yaml_tag_directive_t copy = { NULL, NULL }; - for (tag_directive = emitter->tag_directives.start; - tag_directive != emitter->tag_directives.top; tag_directive ++) { + for (idx = 0; idx < emitter->tag_directives.length; idx ++) { + yaml_tag_directive_t *tag_directive = emitter->tag_directives.list+idx; 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"); + return EMITTER_ERROR_INIT(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; + MEMORY_ERROR_INIT(emitter); goto error; } @@ -477,7 +463,7 @@ yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event) return yaml_emitter_emit_block_mapping_value(emitter, event, 0); case YAML_EMIT_END_STATE: - return yaml_emitter_set_emitter_error(emitter, + return EMITTER_ERROR_INIT(emitter, "expected nothing after STREAM-END"); default: @@ -526,8 +512,8 @@ yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, emitter->line = 0; emitter->column = 0; - emitter->whitespace = 1; - emitter->indention = 1; + emitter->is_whitespace = 1; + emitter->is_indention = 1; if (emitter->encoding != YAML_UTF8_ENCODING) { if (!yaml_emitter_write_bom(emitter)) @@ -539,8 +525,7 @@ yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, return 1; } - return yaml_emitter_set_emitter_error(emitter, - "expected STREAM-START"); + return EMITTER_ERROR_INIT(emitter, "expected STREAM-START"); } /* @@ -559,7 +544,8 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, {NULL, NULL} }; yaml_tag_directive_t *tag_directive; - int implicit; + int is_implicit; + int idx; if (event->data.document_start.version_directive) { if (!yaml_emitter_analyze_version_directive(emitter, @@ -567,9 +553,8 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, return 0; } - for (tag_directive = event->data.document_start.tag_directives.start; - tag_directive != event->data.document_start.tag_directives.end; - tag_directive ++) { + for (idx = 0; idx < event->data.document_start.tag_directives.length; idx++) { + tag_directive = event->data.document_start.tag_directives.list+idx; if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive)) return 0; if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0)) @@ -582,13 +567,13 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, return 0; } - implicit = event->data.document_start.implicit; - if (!first || emitter->canonical) { - implicit = 0; + is_implicit = event->data.document_start.is_implicit; + if (!first || emitter->is_canonical) { + is_implicit = 0; } if (event->data.document_start.version_directive) { - implicit = 0; + is_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)) @@ -597,12 +582,11 @@ yaml_emitter_emit_document_start(yaml_emitter_t *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 (event->data.document_start.tag_directives.length) { + is_implicit = 0; + for (idx = 0; idx < event->data.document_start.tag_directives.length; + idx++) { + tag_directive = event->data.document_start.tag_directives.list+idx; if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0)) return 0; if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle, @@ -617,15 +601,15 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, } if (yaml_emitter_check_empty_document(emitter)) { - implicit = 0; + is_implicit = 0; } - if (!implicit) { + if (!is_implicit) { if (!yaml_emitter_write_indent(emitter)) return 0; if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0)) return 0; - if (emitter->canonical) { + if (emitter->is_canonical) { if (!yaml_emitter_write_indent(emitter)) return 0; } @@ -646,8 +630,7 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, return 1; } - return yaml_emitter_set_emitter_error(emitter, - "expected DOCUMENT-START or STREAM-END"); + return EMITTER_ERROR_INIT(emitter, "expected DOCUMENT-START or STREAM-END"); } /* @@ -676,7 +659,7 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter, { if (!yaml_emitter_write_indent(emitter)) return 0; - if (!event->data.document_end.implicit) { + if (!event->data.document_end.is_implicit) { if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) return 0; if (!yaml_emitter_write_indent(emitter)) @@ -697,8 +680,7 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter, return 1; } - return yaml_emitter_set_emitter_error(emitter, - "expected DOCUMENT-END"); + return EMITTER_ERROR_INIT(emitter, "expected DOCUMENT-END"); } /* @@ -723,7 +705,7 @@ yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, { emitter->flow_level --; emitter->indent = POP(emitter, emitter->indents); - if (emitter->canonical && !first) { + if (emitter->is_canonical && !first) { if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) return 0; if (!yaml_emitter_write_indent(emitter)) @@ -741,7 +723,7 @@ yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, return 0; } - if (emitter->canonical || emitter->column > emitter->best_width) { + if (emitter->is_canonical || emitter->column > emitter->best_width) { if (!yaml_emitter_write_indent(emitter)) return 0; } @@ -772,7 +754,7 @@ yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, { emitter->flow_level --; emitter->indent = POP(emitter, emitter->indents); - if (emitter->canonical && !first) { + if (emitter->is_canonical && !first) { if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) return 0; if (!yaml_emitter_write_indent(emitter)) @@ -789,12 +771,12 @@ yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) return 0; } - if (emitter->canonical || emitter->column > emitter->best_width) { + if (emitter->is_canonical || emitter->column > emitter->best_width) { if (!yaml_emitter_write_indent(emitter)) return 0; } - if (!emitter->canonical && yaml_emitter_check_simple_key(emitter)) + if (!emitter->is_canonical && yaml_emitter_check_simple_key(emitter)) { if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)) @@ -827,7 +809,7 @@ yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, return 0; } else { - if (emitter->canonical || emitter->column > emitter->best_width) { + if (emitter->is_canonical || emitter->column > emitter->best_width) { if (!yaml_emitter_write_indent(emitter)) return 0; } @@ -850,7 +832,7 @@ yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, if (first) { if (!yaml_emitter_increase_indent(emitter, 0, - (emitter->mapping_context && !emitter->indention))) + (emitter->is_mapping_context && !emitter->is_indention))) return 0; } @@ -949,12 +931,12 @@ yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, static int yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, - int root, int sequence, int mapping, int simple_key) + int is_root, int is_sequence, int is_mapping, int is_simple_key) { - emitter->root_context = root; - emitter->sequence_context = sequence; - emitter->mapping_context = mapping; - emitter->simple_key_context = simple_key; + emitter->is_root_context = is_root; + emitter->is_sequence_context = is_sequence; + emitter->is_mapping_context = is_mapping; + emitter->is_simple_key_context = is_simple_key; switch (event->type) { @@ -971,7 +953,7 @@ yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, return yaml_emitter_emit_mapping_start(emitter, event); default: - return yaml_emitter_set_emitter_error(emitter, + return EMITTER_ERROR_INIT(emitter, "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); } @@ -1027,7 +1009,7 @@ yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event) if (!yaml_emitter_process_tag(emitter)) return 0; - if (emitter->flow_level || emitter->canonical + if (emitter->flow_level || emitter->is_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; @@ -1051,7 +1033,7 @@ yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) if (!yaml_emitter_process_tag(emitter)) return 0; - if (emitter->flow_level || emitter->canonical + if (emitter->flow_level || emitter->is_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; @@ -1083,8 +1065,10 @@ 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); + return (emitter->events.list[emitter->events.head].type + == YAML_SEQUENCE_START_EVENT && + emitter->events.list[emitter->events.head+1].type + == YAML_SEQUENCE_END_EVENT); } /* @@ -1097,8 +1081,10 @@ 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); + return (emitter->events.list[emitter->events.head].type + == YAML_MAPPING_START_EVENT && + emitter->events.list[emitter->events.head+1].type + == YAML_MAPPING_END_EVENT); } /* @@ -1108,7 +1094,7 @@ yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter) static int yaml_emitter_check_simple_key(yaml_emitter_t *emitter) { - yaml_event_t *event = emitter->events.head; + yaml_event_t *event = emitter->events.list + emitter->events.head; size_t length = 0; switch (event->type) @@ -1118,7 +1104,7 @@ yaml_emitter_check_simple_key(yaml_emitter_t *emitter) break; case YAML_SCALAR_EVENT: - if (emitter->scalar_data.multiline) + if (emitter->scalar_data.is_multiline) return 0; length += emitter->anchor_data.anchor_length + emitter->tag_data.handle_length @@ -1162,47 +1148,47 @@ 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, + if (no_tag && !event->data.scalar.is_plain_implicit + && !event->data.scalar.is_quoted_implicit) { + return EMITTER_ERROR_INIT(emitter, "neither tag nor implicit flags are specified"); } if (style == YAML_ANY_SCALAR_STYLE) style = YAML_PLAIN_SCALAR_STYLE; - if (emitter->canonical) + if (emitter->is_canonical) style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; - if (emitter->simple_key_context && emitter->scalar_data.multiline) + if (emitter->is_simple_key_context && emitter->scalar_data.is_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)) + if ((emitter->flow_level && !emitter->scalar_data.is_flow_plain_allowed) + || (!emitter->flow_level && !emitter->scalar_data.is_block_plain_allowed)) style = YAML_SINGLE_QUOTED_SCALAR_STYLE; if (!emitter->scalar_data.length - && (emitter->flow_level || emitter->simple_key_context)) + && (emitter->flow_level || emitter->is_simple_key_context)) style = YAML_SINGLE_QUOTED_SCALAR_STYLE; - if (no_tag && !event->data.scalar.plain_implicit) + if (no_tag && !event->data.scalar.is_plain_implicit) style = YAML_SINGLE_QUOTED_SCALAR_STYLE; } if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE) { - if (!emitter->scalar_data.single_quoted_allowed) + if (!emitter->scalar_data.is_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) + if (!emitter->scalar_data.is_block_allowed + || emitter->flow_level || emitter->is_simple_key_context) style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; } - if (no_tag && !event->data.scalar.quoted_implicit + if (no_tag && !event->data.scalar.is_quoted_implicit && style != YAML_PLAIN_SCALAR_STYLE) { emitter->tag_data.handle = (yaml_char_t *)"!"; @@ -1225,7 +1211,7 @@ yaml_emitter_process_anchor(yaml_emitter_t *emitter) return 1; if (!yaml_emitter_write_indicator(emitter, - (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0)) + (emitter->anchor_data.is_alias ? "*" : "&"), 1, 0, 0)) return 0; return yaml_emitter_write_anchor(emitter, @@ -1279,17 +1265,17 @@ yaml_emitter_process_scalar(yaml_emitter_t *emitter) case YAML_PLAIN_SCALAR_STYLE: return yaml_emitter_write_plain_scalar(emitter, emitter->scalar_data.value, emitter->scalar_data.length, - !emitter->simple_key_context); + !emitter->is_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); + !emitter->is_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); + !emitter->is_simple_key_context); case YAML_LITERAL_SCALAR_STYLE: return yaml_emitter_write_literal_scalar(emitter, @@ -1315,8 +1301,7 @@ 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 EMITTER_ERROR_INIT(emitter, "incompatible %YAML directive"); } return 1; @@ -1335,34 +1320,30 @@ yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, yaml_string_t prefix = STRING(tag_directive.prefix, strlen((char *)tag_directive.prefix)); - if (handle.start == handle.end) { - return yaml_emitter_set_emitter_error(emitter, - "tag handle must not be empty"); + if (!handle.capacity) { + return EMITTER_ERROR_INIT(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.buffer[0] != '!') { + return EMITTER_ERROR_INIT(emitter, "tag handle must start with '!'"); } - if (handle.end[-1] != '!') { - return yaml_emitter_set_emitter_error(emitter, - "tag handle must end with '!'"); + if (handle.buffer[handle.capacity-1] != '!') { + return EMITTER_ERROR_INIT(emitter, "tag handle must end with '!'"); } handle.pointer ++; - while (handle.pointer < handle.end-1) { + while (handle.pointer < handle.capacity-1) { if (!IS_ALPHA(handle)) { - return yaml_emitter_set_emitter_error(emitter, + return EMITTER_ERROR_INIT(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"); + if (!prefix.capacity) { + return EMITTER_ERROR_INIT(emitter, "tag prefix must not be empty"); } return 1; @@ -1374,28 +1355,28 @@ yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, static int yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, - yaml_char_t *anchor, int alias) + yaml_char_t *anchor, int is_alias) { yaml_string_t string = STRING(anchor, strlen((char *)anchor)); - if (string.start == string.end) { - return yaml_emitter_set_emitter_error(emitter, alias ? + if (!string.capacity) { + return EMITTER_ERROR_INIT(emitter, is_alias ? "alias value must not be empty" : "anchor value must not be empty"); } - while (string.pointer != string.end) { + while (string.pointer < string.capacity) { if (!IS_ALPHA(string)) { - return yaml_emitter_set_emitter_error(emitter, alias ? + return EMITTER_ERROR_INIT(emitter, is_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; + emitter->anchor_data.anchor = string.buffer; + emitter->anchor_data.anchor_length = string.capacity; + emitter->anchor_data.is_alias = is_alias; return 1; } @@ -1409,32 +1390,30 @@ yaml_emitter_analyze_tag(yaml_emitter_t *emitter, yaml_char_t *tag) { yaml_string_t string = STRING(tag, strlen((char *)tag)); - yaml_tag_directive_t *tag_directive; + size_t idx; - if (string.start == string.end) { - return yaml_emitter_set_emitter_error(emitter, - "tag value must not be empty"); + if (!string.capacity) { + return EMITTER_ERROR_INIT(emitter, "tag value must not be empty"); } - for (tag_directive = emitter->tag_directives.start; - tag_directive != emitter->tag_directives.top; tag_directive ++) { + for (idx = 0; idx < emitter->tag_directives.length; idx ++) { + yaml_tag_directive_t *tag_directive = emitter->tag_directives.list+idx; 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, + if (prefix_length < string.capacity + && strncmp((char *)tag_directive->prefix, (char *)string.buffer, 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; + emitter->tag_data.suffix = string.buffer + prefix_length; + emitter->tag_data.suffix_length = string.capacity - prefix_length; return 1; } } - emitter->tag_data.suffix = string.start; - emitter->tag_data.suffix_length = string.end - string.start; + emitter->tag_data.suffix = string.buffer; + emitter->tag_data.suffix_length = string.capacity; return 1; } @@ -1473,13 +1452,13 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, emitter->scalar_data.value = value; emitter->scalar_data.length = length; - if (string.start == string.end) + if (!string.capacity) { - 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; + emitter->scalar_data.is_multiline = 0; + emitter->scalar_data.is_flow_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 1; + emitter->scalar_data.is_single_quoted_allowed = 1; + emitter->scalar_data.is_block_allowed = 0; return 1; } @@ -1497,9 +1476,9 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, preceeded_by_space = 1; followed_by_space = IS_BLANKZ_AT(string, WIDTH(string)); - while (string.pointer != string.end) + while (string.pointer < string.capacity) { - if (string.start == string.pointer) + if (!string.pointer) { if (CHECK(string, '#') || CHECK(string, ',') || CHECK(string, '[') || CHECK(string, ']') @@ -1547,7 +1526,7 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, } if (!IS_PRINTABLE(string) - || (!IS_ASCII(string) && !emitter->unicode)) { + || (!IS_ASCII(string) && !emitter->is_unicode)) { special_characters = 1; } @@ -1558,7 +1537,7 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, if (IS_SPACE(string)) { spaces = 1; - if (string.start == string.pointer) { + if (!string.pointer) { leading = 1; } } @@ -1569,7 +1548,7 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, mixed = 1; } breaks = 1; - if (string.start == string.pointer) { + if (!string.pointer) { leading = 1; } } @@ -1604,7 +1583,7 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, spaces = breaks = mixed = leading = 0; } - if ((spaces || breaks) && string.pointer == string.end-1) + if ((spaces || breaks) && string.pointer == string.capacity-1) { if (spaces && breaks) { mixed_breaks_spaces = 1; @@ -1625,53 +1604,53 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, preceeded_by_space = IS_BLANKZ(string); MOVE(string); - if (string.pointer != string.end) { + if (string.pointer < string.capacity) { followed_by_space = IS_BLANKZ_AT(string, WIDTH(string)); } } - emitter->scalar_data.multiline = line_breaks; + emitter->scalar_data.is_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; + emitter->scalar_data.is_flow_plain_allowed = 1; + emitter->scalar_data.is_block_plain_allowed = 1; + emitter->scalar_data.is_single_quoted_allowed = 1; + emitter->scalar_data.is_block_allowed = 1; if (leading_spaces || leading_breaks || trailing_spaces) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; - emitter->scalar_data.block_allowed = 0; + emitter->scalar_data.is_flow_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 0; + emitter->scalar_data.is_block_allowed = 0; } if (trailing_breaks) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; + emitter->scalar_data.is_flow_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 0; } if (inline_breaks_spaces) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; - emitter->scalar_data.single_quoted_allowed = 0; + emitter->scalar_data.is_flow_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 0; + emitter->scalar_data.is_single_quoted_allowed = 0; } if (mixed_breaks_spaces || 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; + emitter->scalar_data.is_flow_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 0; + emitter->scalar_data.is_single_quoted_allowed = 0; + emitter->scalar_data.is_block_allowed = 0; } if (line_breaks) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; + emitter->scalar_data.is_flow_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 0; } if (flow_indicators) { - emitter->scalar_data.flow_plain_allowed = 0; + emitter->scalar_data.is_flow_plain_allowed = 0; } if (block_indicators) { - emitter->scalar_data.block_plain_allowed = 0; + emitter->scalar_data.is_block_plain_allowed = 0; } return 1; @@ -1708,9 +1687,9 @@ yaml_emitter_analyze_event(yaml_emitter_t *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 (event->data.scalar.tag && (emitter->is_canonical || + (!event->data.scalar.is_plain_implicit + && !event->data.scalar.is_quoted_implicit))) { if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag)) return 0; } @@ -1725,8 +1704,8 @@ yaml_emitter_analyze_event(yaml_emitter_t *emitter, event->data.sequence_start.anchor, 0)) return 0; } - if (event->data.sequence_start.tag && (emitter->canonical || - !event->data.sequence_start.implicit)) { + if (event->data.sequence_start.tag && (emitter->is_canonical || + !event->data.sequence_start.is_implicit)) { if (!yaml_emitter_analyze_tag(emitter, event->data.sequence_start.tag)) return 0; @@ -1739,8 +1718,8 @@ yaml_emitter_analyze_event(yaml_emitter_t *emitter, event->data.mapping_start.anchor, 0)) return 0; } - if (event->data.mapping_start.tag && (emitter->canonical || - !event->data.mapping_start.implicit)) { + if (event->data.mapping_start.tag && (emitter->is_canonical || + !event->data.mapping_start.is_implicit)) { if (!yaml_emitter_analyze_tag(emitter, event->data.mapping_start.tag)) return 0; @@ -1761,9 +1740,9 @@ 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'; + JOIN_OCTET(emitter->output, (yaml_char_t) '\xEF'); + JOIN_OCTET(emitter->output, (yaml_char_t) '\xBB'); + JOIN_OCTET(emitter->output, (yaml_char_t) '\xBF'); return 1; } @@ -1773,8 +1752,8 @@ 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 (!emitter->is_indention || emitter->column > indent + || (emitter->column == indent && !emitter->is_whitespace)) { if (!PUT_BREAK(emitter)) return 0; } @@ -1782,8 +1761,8 @@ yaml_emitter_write_indent(yaml_emitter_t *emitter) if (!PUT(emitter, ' ')) return 0; } - emitter->whitespace = 1; - emitter->indention = 1; + emitter->is_whitespace = 1; + emitter->is_indention = 1; return 1; } @@ -1795,16 +1774,16 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter, { yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator)); - if (need_whitespace && !emitter->whitespace) { + if (need_whitespace && !emitter->is_whitespace) { if (!PUT(emitter, ' ')) return 0; } - while (string.pointer != string.end) { + while (string.pointer < string.capacity) { if (!WRITE(emitter, string)) return 0; } - emitter->whitespace = is_whitespace; - emitter->indention = (emitter->indention && is_indention); + emitter->is_whitespace = is_whitespace; + emitter->is_indention = (emitter->is_indention && is_indention); return 1; } @@ -1815,12 +1794,12 @@ yaml_emitter_write_anchor(yaml_emitter_t *emitter, { yaml_string_t string = STRING(value, length); - while (string.pointer != string.end) { + while (string.pointer < string.capacity) { if (!WRITE(emitter, string)) return 0; } - emitter->whitespace = 0; - emitter->indention = 0; + emitter->is_whitespace = 0; + emitter->is_indention = 0; return 1; } @@ -1831,16 +1810,16 @@ yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, { yaml_string_t string = STRING(value, length); - if (!emitter->whitespace) { + if (!emitter->is_whitespace) { if (!PUT(emitter, ' ')) return 0; } - while (string.pointer != string.end) { + while (string.pointer < string.capacity) { if (!WRITE(emitter, string)) return 0; } - emitter->whitespace = 0; - emitter->indention = 0; + emitter->is_whitespace = 0; + emitter->is_indention = 0; return 1; } @@ -1852,11 +1831,11 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter, { yaml_string_t string = STRING(value, length); - if (need_whitespace && !emitter->whitespace) { + if (need_whitespace && !emitter->is_whitespace) { if (!PUT(emitter, ' ')) return 0; } - while (string.pointer != string.end) { + while (string.pointer < string.capacity) { if (IS_ALPHA(string) || CHECK(string, ';') || CHECK(string, '/') || CHECK(string, '?') || CHECK(string, ':') @@ -1874,7 +1853,8 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter, int width = WIDTH(string); unsigned int value; while (width --) { - value = *(string.pointer++); + value = OCTET(string); + string.pointer ++; if (!PUT(emitter, '%')) return 0; if (!PUT(emitter, (value >> 4) + ((value >> 4) < 10 ? '0' : 'A' - 10))) @@ -1886,8 +1866,8 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter, } } - emitter->whitespace = 0; - emitter->indention = 0; + emitter->is_whitespace = 0; + emitter->is_indention = 0; return 1; } @@ -1900,11 +1880,11 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, int spaces = 0; int breaks = 0; - if (!emitter->whitespace) { + if (!emitter->is_whitespace) { if (!PUT(emitter, ' ')) return 0; } - while (string.pointer != string.end) + while (string.pointer < string.capacity) { if (IS_SPACE(string)) { @@ -1925,7 +1905,7 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, if (!PUT_BREAK(emitter)) return 0; } if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; + emitter->is_indention = 1; breaks = 1; } else @@ -1934,14 +1914,14 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indent(emitter)) return 0; } if (!WRITE(emitter, string)) return 0; - emitter->indention = 0; + emitter->is_indention = 0; spaces = 0; breaks = 0; } } - emitter->whitespace = 0; - emitter->indention = 0; + emitter->is_whitespace = 0; + emitter->is_indention = 0; return 1; } @@ -1957,14 +1937,14 @@ yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) return 0; - while (string.pointer != string.end) + while (string.pointer < string.capacity) { if (IS_SPACE(string)) { if (allow_breaks && !spaces && emitter->column > emitter->best_width - && string.pointer != string.start - && string.pointer != string.end - 1 + && string.pointer != 0 + && string.pointer != string.capacity - 1 && !IS_SPACE_AT(string, 1)) { if (!yaml_emitter_write_indent(emitter)) return 0; MOVE(string); @@ -1980,7 +1960,7 @@ yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, if (!PUT_BREAK(emitter)) return 0; } if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; + emitter->is_indention = 1; breaks = 1; } else @@ -1992,7 +1972,7 @@ yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, if (!PUT(emitter, '\'')) return 0; } if (!WRITE(emitter, string)) return 0; - emitter->indention = 0; + emitter->is_indention = 0; spaces = 0; breaks = 0; } @@ -2001,8 +1981,8 @@ yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0)) return 0; - emitter->whitespace = 0; - emitter->indention = 0; + emitter->is_whitespace = 0; + emitter->is_indention = 0; return 1; } @@ -2017,18 +1997,18 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) return 0; - while (string.pointer != string.end) + while (string.pointer < string.capacity) { - if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string)) + if (!IS_PRINTABLE(string) || (!emitter->is_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; + int idx; - octet = string.pointer[0]; + octet = OCTET(string); width = (octet & 0x80) == 0x00 ? 1 : (octet & 0xE0) == 0xC0 ? 2 : (octet & 0xF0) == 0xE0 ? 3 : @@ -2037,8 +2017,8 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, (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]; + for (idx = 1; idx < width; idx ++) { + octet = OCTET_AT(string, idx); value = (value << 6) + (octet & 0x3F); } string.pointer += width; @@ -2120,8 +2100,8 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, if (!PUT(emitter, 'U')) return 0; width = 8; } - for (k = (width-1)*4; k >= 0; k -= 4) { - int digit = (value >> k) & 0x0F; + for (idx = (width-1)*4; idx >= 0; idx -= 4) { + int digit = (value >> idx) & 0x0F; if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10))) return 0; } @@ -2132,8 +2112,8 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, { if (allow_breaks && !spaces && emitter->column > emitter->best_width - && string.pointer != string.start - && string.pointer != string.end - 1) { + && string.pointer != 0 + && string.pointer != string.capacity - 1) { if (!yaml_emitter_write_indent(emitter)) return 0; if (IS_SPACE_AT(string, 1)) { if (!PUT(emitter, '\\')) return 0; @@ -2155,8 +2135,8 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0)) return 0; - emitter->whitespace = 0; - emitter->indention = 0; + emitter->is_whitespace = 0; + emitter->is_indention = 0; return 1; } @@ -2165,19 +2145,19 @@ static int yaml_emitter_determine_chomping(yaml_emitter_t *emitter, yaml_string_t string) { - string.pointer = string.end; - if (string.start == string.pointer) + string.pointer = string.capacity; + if (!string.pointer) return -1; do { string.pointer --; - } while ((*string.pointer & 0xC0) == 0x80); + } while ((OCTET(string) & 0xC0) == 0x80); if (!IS_BREAK(string)) return -1; - if (string.start == string.pointer) + if (!string.pointer) return 0; do { string.pointer --; - } while ((*string.pointer & 0xC0) == 0x80); + } while ((OCTET(string) & 0xC0) == 0x80); if (!IS_BREAK(string)) return 0; return +1; @@ -2198,12 +2178,12 @@ yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indent(emitter)) return 0; - while (string.pointer != string.end) + while (string.pointer < string.capacity) { if (IS_BREAK(string)) { if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; + emitter->is_indention = 1; breaks = 1; } else @@ -2212,7 +2192,7 @@ yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indent(emitter)) return 0; } if (!WRITE(emitter, string)) return 0; - emitter->indention = 0; + emitter->is_indention = 0; breaks = 0; } } @@ -2235,7 +2215,7 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, if (!yaml_emitter_write_indent(emitter)) return 0; - while (string.pointer != string.end) + while (string.pointer < string.capacity) { if (IS_BREAK(string)) { @@ -2249,7 +2229,7 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, } } if (!WRITE_BREAK(emitter, string)) return 0; - emitter->indention = 1; + emitter->is_indention = 1; breaks = 1; } else @@ -2266,7 +2246,7 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, else { if (!WRITE(emitter, string)) return 0; } - emitter->indention = 0; + emitter->is_indention = 0; breaks = 0; } } diff --git a/src/loader.c b/src/loader.c index 3c96116..2e89157 100644 --- a/src/loader.c +++ b/src/loader.c @@ -1,6 +1,8 @@ #include "yaml_private.h" +#if 0 + /* * API functions. */ @@ -428,3 +430,5 @@ error: return 0; } +#endif + diff --git a/src/parser.c b/src/parser.c index 81122f6..dc9f82c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -46,18 +46,18 @@ */ #define PEEK_TOKEN(parser) \ - ((parser->token_available || yaml_parser_fetch_more_tokens(parser)) ? \ - parser->tokens.head : NULL) + ((parser->is_token_available || yaml_parser_fetch_more_tokens(parser)) ? \ + parser->tokens.list + parser->tokens.head : NULL) /* * Remove the next token from the queue (must be called after PEEK_TOKEN). */ #define SKIP_TOKEN(parser) \ - (parser->token_available = 0, \ + (parser->is_token_available = 0, \ parser->tokens_parsed ++, \ - parser->stream_end_produced = \ - (parser->tokens.head->type == YAML_STREAM_END_TOKEN), \ + parser->is_stream_end_produced = \ + (parser->tokens.list[parser->tokens.head].type == YAML_STREAM_END_TOKEN), \ parser->tokens.head ++) /* @@ -67,19 +67,6 @@ YAML_DECLARE(int) yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event); -/* - * Error handling. - */ - -static int -yaml_parser_set_parser_error(yaml_parser_t *parser, - const char *problem, yaml_mark_t problem_mark); - -static int -yaml_parser_set_parser_error_context(yaml_parser_t *parser, - const char *context, yaml_mark_t context_mark, - const char *problem, yaml_mark_t problem_mark); - /* * State functions. */ @@ -155,8 +142,9 @@ yaml_parser_process_empty_scalar(yaml_parser_t *parser, static int yaml_parser_process_directives(yaml_parser_t *parser, yaml_version_directive_t **version_directive_ref, - yaml_tag_directive_t **tag_directives_start_ref, - yaml_tag_directive_t **tag_directives_end_ref); + yaml_tag_directive_t **tag_directives_list_ref, + size_t *tag_directives_length_ref, + size_t *tag_directives_capacity_ref); static int yaml_parser_append_tag_directive(yaml_parser_t *parser, @@ -178,8 +166,8 @@ yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event) /* No events after the end of the stream or error. */ - if (parser->stream_end_produced || parser->error || - parser->state == YAML_PARSE_END_STATE) { + if (parser->is_stream_end_produced || parser->error.type + || parser->state == YAML_PARSE_END_STATE) { return 1; } @@ -188,36 +176,6 @@ yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event) return yaml_parser_state_machine(parser, event); } -/* - * Set parser error. - */ - -static int -yaml_parser_set_parser_error(yaml_parser_t *parser, - const char *problem, yaml_mark_t problem_mark) -{ - parser->error = YAML_PARSER_ERROR; - parser->problem = problem; - parser->problem_mark = problem_mark; - - return 0; -} - -static int -yaml_parser_set_parser_error_context(yaml_parser_t *parser, - const char *context, yaml_mark_t context_mark, - const char *problem, yaml_mark_t problem_mark) -{ - parser->error = YAML_PARSER_ERROR; - parser->context = context; - parser->context_mark = context_mark; - parser->problem = problem; - parser->problem_mark = problem_mark; - - return 0; -} - - /* * State dispatcher. */ @@ -318,7 +276,7 @@ yaml_parser_parse_stream_start(yaml_parser_t *parser, yaml_event_t *event) if (!token) return 0; if (token->type != YAML_STREAM_START_TOKEN) { - return yaml_parser_set_parser_error(parser, + return PARSER_ERROR_INIT(parser, "did not found expected ", token->start_mark); } @@ -345,9 +303,10 @@ yaml_parser_parse_document_start(yaml_parser_t *parser, yaml_event_t *event, yaml_token_t *token; yaml_version_directive_t *version_directive = NULL; struct { - yaml_tag_directive_t *start; - yaml_tag_directive_t *end; - } tag_directives = { NULL, NULL }; + yaml_tag_directive_t *list; + size_t length; + size_t capacity; + } tag_directives = { NULL, 0, 0 }; token = PEEK_TOKEN(parser); if (!token) return 0; @@ -370,12 +329,12 @@ yaml_parser_parse_document_start(yaml_parser_t *parser, yaml_event_t *event, token->type != YAML_DOCUMENT_START_TOKEN && token->type != YAML_STREAM_END_TOKEN) { - if (!yaml_parser_process_directives(parser, NULL, NULL, NULL)) + if (!yaml_parser_process_directives(parser, NULL, NULL, NULL, NULL)) return 0; if (!PUSH(parser, parser->states, YAML_PARSE_DOCUMENT_END_STATE)) return 0; parser->state = YAML_PARSE_BLOCK_NODE_STATE; - DOCUMENT_START_EVENT_INIT(*event, NULL, NULL, NULL, 1, + DOCUMENT_START_EVENT_INIT(*event, NULL, NULL, 0, 0, 1, token->start_mark, token->start_mark); return 1; } @@ -387,12 +346,13 @@ yaml_parser_parse_document_start(yaml_parser_t *parser, yaml_event_t *event, yaml_mark_t start_mark, end_mark; start_mark = token->start_mark; if (!yaml_parser_process_directives(parser, &version_directive, - &tag_directives.start, &tag_directives.end)) + &tag_directives.list, &tag_directives.length, + &tag_directives.capacity)) return 0; token = PEEK_TOKEN(parser); if (!token) goto error; if (token->type != YAML_DOCUMENT_START_TOKEN) { - yaml_parser_set_parser_error(parser, + PARSER_ERROR_INIT(parser, "did not found expected ", token->start_mark); goto error; } @@ -401,11 +361,13 @@ yaml_parser_parse_document_start(yaml_parser_t *parser, yaml_event_t *event, parser->state = YAML_PARSE_DOCUMENT_CONTENT_STATE; end_mark = token->end_mark; DOCUMENT_START_EVENT_INIT(*event, version_directive, - tag_directives.start, tag_directives.end, 0, + tag_directives.list, tag_directives.length, + tag_directives.capacity, 0, start_mark, end_mark); SKIP_TOKEN(parser); version_directive = NULL; - tag_directives.start = tag_directives.end = NULL; + tag_directives.list = NULL; + tag_directives.length = tag_directives.capacity = 0; return 1; } @@ -421,12 +383,12 @@ yaml_parser_parse_document_start(yaml_parser_t *parser, yaml_event_t *event, error: yaml_free(version_directive); - while (tag_directives.start != tag_directives.end) { - yaml_free(tag_directives.end[-1].handle); - yaml_free(tag_directives.end[-1].prefix); - tag_directives.end --; + while (!STACK_EMPTY(parser, tag_directives)) { + yaml_tag_directive_t tag_directive = POP(parser, tag_directives); + yaml_free(tag_directive.handle); + yaml_free(tag_directive.prefix); } - yaml_free(tag_directives.start); + STACK_DEL(parser, tag_directives); return 0; } @@ -598,16 +560,15 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, tag_handle = tag_suffix = NULL; } else { - yaml_tag_directive_t *tag_directive; - for (tag_directive = parser->tag_directives.start; - tag_directive != parser->tag_directives.top; - tag_directive ++) { + int idx; + for (idx = 0; idx < parser->tag_directives.length; idx++) { + yaml_tag_directive_t *tag_directive = parser->tag_directives.list + idx; if (strcmp((char *)tag_directive->handle, (char *)tag_handle) == 0) { size_t prefix_len = strlen((char *)tag_directive->prefix); size_t suffix_len = strlen((char *)tag_suffix); tag = yaml_malloc(prefix_len+suffix_len+1); if (!tag) { - parser->error = YAML_MEMORY_ERROR; + MEMORY_ERROR_INIT(parser); goto error; } memcpy(tag, tag_directive->prefix, prefix_len); @@ -620,7 +581,7 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, } } if (!tag) { - yaml_parser_set_parser_error_context(parser, + PARSER_ERROR_WITH_CONTEXT_INIT(parser, "while parsing a node", start_mark, "found undefined tag handle", tag_mark); goto error; @@ -687,7 +648,7 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, else if (anchor || tag) { yaml_char_t *value = yaml_malloc(1); if (!value) { - parser->error = YAML_MEMORY_ERROR; + MEMORY_ERROR_INIT(parser); goto error; } value[0] = '\0'; @@ -698,7 +659,7 @@ yaml_parser_parse_node(yaml_parser_t *parser, yaml_event_t *event, return 1; } else { - yaml_parser_set_parser_error_context(parser, + PARSER_ERROR_WITH_CONTEXT_INIT(parser, (block ? "while parsing a block node" : "while parsing a flow node"), start_mark, "did not found expected node content", token->start_mark); @@ -769,7 +730,7 @@ yaml_parser_parse_block_sequence_entry(yaml_parser_t *parser, else { - return yaml_parser_set_parser_error_context(parser, + return PARSER_ERROR_WITH_CONTEXT_INIT(parser, "while parsing a block collection", POP(parser, parser->marks), "did not found expected '-' indicator", token->start_mark); } @@ -879,7 +840,7 @@ yaml_parser_parse_block_mapping_key(yaml_parser_t *parser, else { - return yaml_parser_set_parser_error_context(parser, + return PARSER_ERROR_WITH_CONTEXT_INIT(parser, "while parsing a block mapping", POP(parser, parser->marks), "did not found expected key", token->start_mark); } @@ -973,7 +934,7 @@ yaml_parser_parse_flow_sequence_entry(yaml_parser_t *parser, if (!token) return 0; } else { - return yaml_parser_set_parser_error_context(parser, + return PARSER_ERROR_WITH_CONTEXT_INIT(parser, "while parsing a flow sequence", POP(parser, parser->marks), "did not found expected ',' or ']'", token->start_mark); } @@ -1125,7 +1086,7 @@ yaml_parser_parse_flow_mapping_key(yaml_parser_t *parser, if (!token) return 0; } else { - return yaml_parser_set_parser_error_context(parser, + return PARSER_ERROR_WITH_CONTEXT_INIT(parser, "while parsing a flow mapping", POP(parser, parser->marks), "did not found expected ',' or '}'", token->start_mark); } @@ -1214,8 +1175,7 @@ yaml_parser_process_empty_scalar(yaml_parser_t *parser, yaml_event_t *event, value = yaml_malloc(1); if (!value) { - parser->error = YAML_MEMORY_ERROR; - return 0; + return MEMORY_ERROR_INIT(parser); } value[0] = '\0'; @@ -1232,8 +1192,9 @@ yaml_parser_process_empty_scalar(yaml_parser_t *parser, yaml_event_t *event, static int yaml_parser_process_directives(yaml_parser_t *parser, yaml_version_directive_t **version_directive_ref, - yaml_tag_directive_t **tag_directives_start_ref, - yaml_tag_directive_t **tag_directives_end_ref) + yaml_tag_directive_t **tag_directives_list_ref, + size_t *tag_directives_length_ref, + size_t *tag_directives_capacity_ref) { yaml_tag_directive_t default_tag_directives[] = { {(yaml_char_t *)"!", (yaml_char_t *)"!"}, @@ -1243,13 +1204,13 @@ yaml_parser_process_directives(yaml_parser_t *parser, yaml_tag_directive_t *default_tag_directive; yaml_version_directive_t *version_directive = NULL; struct { - yaml_tag_directive_t *start; - yaml_tag_directive_t *end; - yaml_tag_directive_t *top; - } tag_directives = { NULL, NULL, NULL }; + yaml_tag_directive_t *list; + size_t length; + size_t capacity; + } tag_directives = { NULL, 0, 0 }; yaml_token_t *token; - if (!STACK_INIT(parser, tag_directives, INITIAL_STACK_SIZE)) + if (!STACK_INIT(parser, tag_directives, INITIAL_STACK_CAPACITY)) goto error; token = PEEK_TOKEN(parser); @@ -1260,19 +1221,19 @@ yaml_parser_process_directives(yaml_parser_t *parser, { if (token->type == YAML_VERSION_DIRECTIVE_TOKEN) { if (version_directive) { - yaml_parser_set_parser_error(parser, + PARSER_ERROR_INIT(parser, "found duplicate %YAML directive", token->start_mark); goto error; } if (token->data.version_directive.major != 1 || token->data.version_directive.minor != 1) { - yaml_parser_set_parser_error(parser, + PARSER_ERROR_INIT(parser, "found incompatible YAML document", token->start_mark); goto error; } version_directive = yaml_malloc(sizeof(yaml_version_directive_t)); if (!version_directive) { - parser->error = YAML_MEMORY_ERROR; + MEMORY_ERROR_INIT(parser); goto error; } version_directive->major = token->data.version_directive.major; @@ -1306,14 +1267,17 @@ yaml_parser_process_directives(yaml_parser_t *parser, if (version_directive_ref) { *version_directive_ref = version_directive; } - if (tag_directives_start_ref) { + if (tag_directives_list_ref) { if (STACK_EMPTY(parser, tag_directives)) { - *tag_directives_start_ref = *tag_directives_end_ref = NULL; + *tag_directives_list_ref = NULL; + *tag_directives_length_ref = 0; + *tag_directives_capacity_ref = 0; STACK_DEL(parser, tag_directives); } else { - *tag_directives_start_ref = tag_directives.start; - *tag_directives_end_ref = tag_directives.top; + *tag_directives_list_ref = tag_directives.list; + *tag_directives_length_ref = tag_directives.length; + *tag_directives_capacity_ref = tag_directives.capacity; } } else { @@ -1343,13 +1307,14 @@ yaml_parser_append_tag_directive(yaml_parser_t *parser, { yaml_tag_directive_t *tag_directive; yaml_tag_directive_t copy = { NULL, NULL }; + int idx; - for (tag_directive = parser->tag_directives.start; - tag_directive != parser->tag_directives.top; tag_directive ++) { + for (idx = 0; idx < parser->tag_directives.length; idx++) { + yaml_tag_directive_t *tag_directive = parser->tag_directives.list + idx; if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { if (allow_duplicates) return 1; - return yaml_parser_set_parser_error(parser, + return PARSER_ERROR_INIT(parser, "found duplicate %TAG directive", mark); } } @@ -1357,7 +1322,7 @@ yaml_parser_append_tag_directive(yaml_parser_t *parser, copy.handle = yaml_strdup(value.handle); copy.prefix = yaml_strdup(value.prefix); if (!copy.handle || !copy.prefix) { - parser->error = YAML_MEMORY_ERROR; + MEMORY_ERROR_INIT(parser); goto error; } diff --git a/src/reader.c b/src/reader.c index 2cf513d..c34c99a 100644 --- a/src/reader.c +++ b/src/reader.c @@ -35,7 +35,7 @@ yaml_parser_determine_encoding(yaml_parser_t *parser) { /* Ensure that we had enough bytes in the raw buffer. */ - while (!parser->is_eof && parser->raw_input.length < 3) { + while (!parser->is_eof && parser->raw_input.capacity < 3) { if (!yaml_parser_update_raw_buffer(parser)) { return 0; } @@ -43,19 +43,19 @@ yaml_parser_determine_encoding(yaml_parser_t *parser) /* Determine the encoding. */ - if (parser->raw_input.length >= 2 + if (parser->raw_input.capacity >= 2 && !memcmp(parser->raw_input.buffer, BOM_UTF16LE, 2)) { parser->encoding = YAML_UTF16LE_ENCODING; parser->raw_input.pointer = 2; parser->offset = 2; } - else if (parser->raw_input.length >= 2 + else if (parser->raw_input.capacity >= 2 && !memcmp(parser->raw_input.buffer, BOM_UTF16BE, 2)) { parser->encoding = YAML_UTF16BE_ENCODING; parser->raw_input.pointer = 2; parser->offset = 2; } - else if (parser->raw_input.length >= 3 + else if (parser->raw_input.capacity >= 3 && !memcmp(parser->raw_input.buffer, BOM_UTF8, 3)) { parser->encoding = YAML_UTF8_ENCODING; parser->raw_input.pointer = 3; @@ -80,7 +80,7 @@ yaml_parser_update_raw_buffer(yaml_parser_t *parser) /* Return if the raw buffer is full. */ if (parser->raw_input.pointer == 0 && - parser->raw_input.length == parser->raw_input.capacity) + parser->raw_input.capacity == RAW_INPUT_BUFFER_CAPACITY) return 1; /* Return on EOF. */ @@ -91,22 +91,23 @@ yaml_parser_update_raw_buffer(yaml_parser_t *parser) /* Move the remaining bytes in the raw buffer to the beginning. */ if (parser->raw_input.pointer > 0 && - parser->raw_input.pointer < parser->raw_input.length) { + parser->raw_input.pointer < parser->raw_input.capacity) { memmove(parser->raw_input.buffer, parser->raw_input.buffer + parser->raw_input.pointer, - parser->raw_input.length - parser->raw_input.pointer); + parser->raw_input.capacity - parser->raw_input.pointer); } + parser->raw_input.capacity -= parser->raw_input.pointer; parser->raw_input.pointer = 0; /* Call the read handler to fill the buffer. */ if (!parser->reader(parser->reader_data, - parser->raw_input.buffer + parser->raw_input.length, - parser->raw_input.capacity - parser->raw_input.length, + parser->raw_input.buffer + parser->raw_input.capacity, + RAW_INPUT_BUFFER_CAPACITY - parser->raw_input.capacity, &length)) { return READER_ERROR_INIT(parser, "Input error", parser->offset); } - parser->raw_input.length += length; + parser->raw_input.capacity += length; if (!length) { parser->is_eof = 1; } @@ -124,11 +125,13 @@ yaml_parser_update_raw_buffer(yaml_parser_t *parser) YAML_DECLARE(int) yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) { + size_t old_capacity; + assert(parser->reader); /* Read handler must be set. */ /* If the EOF flag is set and the raw buffer is empty, do nothing. */ - if (parser->is_eof && parser->raw_input.pointer == parser->raw_input.length) + if (parser->is_eof && parser->raw_input.pointer == parser->raw_input.capacity) return 1; /* Return if the buffer contains enough characters. */ @@ -146,17 +149,20 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Move the unread characters to the beginning of the buffer. */ if (parser->input.pointer > 0 && - parser->input.pointer < parser->input.length) { + parser->input.pointer < parser->input.capacity) { memmove(parser->input.buffer, parser->input.buffer + parser->input.pointer, - parser->input.length - parser->input.pointer); - parser->input.length -= parser->input.pointer; - parser->input.pointer = 0; + parser->input.capacity - parser->input.pointer); + parser->input.capacity -= parser->input.pointer; } - else if (parser->input.pointer == parser->input.length) { - parser->input.pointer = parser->input.length = 0; + else if (parser->input.pointer == parser->input.capacity) { + parser->input.capacity = 0; } + /* Set the pointer to the end of the buffer. */ + + parser->input.pointer = parser->input.capacity; + /* Fill the buffer until it has enough characters. */ while (parser->unread < length) @@ -167,18 +173,16 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Decode the raw buffer. */ - while (parser->raw_input.pointer != parser->raw_input.length) + while (parser->raw_input.pointer != parser->raw_input.capacity) { - unsigned char *raw_buffer = - parser->raw_input.buffer + parser->raw_input.pointer; size_t raw_unread = - parser->raw_input.length - parser->raw_input.pointer; + parser->raw_input.capacity - parser->raw_input.pointer; unsigned int value = 0, value2 = 0; int is_incomplete = 0; unsigned char octet; unsigned int width = 0; int low, high; - size_t k; + size_t idx; /* Decode the next character. */ @@ -208,7 +212,7 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Determine the length of the UTF-8 sequence. */ - octet = *raw_buffer; + octet = OCTET(parser->raw_input); width = (octet & 0x80) == 0x00 ? 1 : (octet & 0xE0) == 0xC0 ? 2 : (octet & 0xF0) == 0xE0 ? 3 : @@ -242,16 +246,16 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Check and decode the trailing octets. */ - for (k = 1; k < width; k ++) + for (idx = 1; idx < width; idx ++) { - octet = raw_buffer[k]; + octet = OCTET_AT(parser->raw_input, idx); /* Check if the octet is valid. */ if ((octet & 0xC0) != 0x80) return DECODER_ERROR_INIT(parser, "Invalid trailing UTF-8 octet", - parser->offset+k, octet); + parser->offset+idx, octet); /* Decode the octet. */ @@ -323,7 +327,8 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Get the character. */ - value = raw_buffer[low] + (raw_buffer[high] << 8); + value = OCTET_AT(parser->raw_input, low) + + (OCTET_AT(parser->raw_input, high) << 8); /* Check for unexpected low surrogate area. */ @@ -352,7 +357,8 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* Get the next character. */ - value2 = raw_buffer[low+2] + (raw_buffer[high+2] << 8); + value2 = OCTET_AT(parser->raw_input, low+2) + + (OCTET_AT(parser->raw_input, high+2) << 8); /* Check for a low surrogate area. */ @@ -406,39 +412,44 @@ yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) /* 0000 0000-0000 007F -> 0xxxxxxx */ if (value <= 0x7F) { - parser->input.buffer[parser->input.length++] = value; + JOIN_OCTET(parser->input, value); } /* 0000 0080-0000 07FF -> 110xxxxx 10xxxxxx */ else if (value <= 0x7FF) { - parser->input.buffer[parser->input.length++] = 0xC0 + (value >> 6); - parser->input.buffer[parser->input.length++] = 0x80 + (value & 0x3F); + JOIN_OCTET(parser->input, 0xC0 + (value >> 6)); + JOIN_OCTET(parser->input, 0x80 + (value & 0x3F)); } /* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */ else if (value <= 0xFFFF) { - parser->input.buffer[parser->input.length++] = 0xE0 + (value >> 12); - parser->input.buffer[parser->input.length++] = 0x80 + ((value >> 6) & 0x3F); - parser->input.buffer[parser->input.length++] = 0x80 + (value & 0x3F); + JOIN_OCTET(parser->input, 0xE0 + (value >> 12)); + JOIN_OCTET(parser->input, 0x80 + ((value >> 6) & 0x3F)); + JOIN_OCTET(parser->input, 0x80 + (value & 0x3F)); } /* 0001 0000-0010 FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ else { - parser->input.buffer[parser->input.length++] = 0xF0 + (value >> 18); - parser->input.buffer[parser->input.length++] = 0x80 + ((value >> 12) & 0x3F); - parser->input.buffer[parser->input.length++] = 0x80 + ((value >> 6) & 0x3F); - parser->input.buffer[parser->input.length++] = 0x80 + (value & 0x3F); + JOIN_OCTET(parser->input, 0xF0 + (value >> 18)); + JOIN_OCTET(parser->input, 0x80 + ((value >> 12) & 0x3F)); + JOIN_OCTET(parser->input, 0x80 + ((value >> 6) & 0x3F)); + JOIN_OCTET(parser->input, 0x80 + (value & 0x3F)); } parser->unread ++; } - /* On EOF, put NUL into the buffer and return. */ + /* On EOF, put NUL into the buffer and stop. */ if (parser->is_eof) { - parser->input.buffer[parser->input.length++] = '\0'; + JOIN_OCTET(parser->input, '\0'); parser->unread ++; - return 1; + break; } } + /* Swap the pointer with the end of the buffer. */ + + old_capacity = parser->input.capacity; + parser->input.capacity = parser->input.pointer; + parser->input.pointer = old_capacity; return 1; } diff --git a/src/scanner.c b/src/scanner.c index 4792dfd..f38fbae 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -495,21 +495,21 @@ (parser->mark.index ++, \ parser->mark.column ++, \ parser->unread --, \ - parser->buffer.pointer += WIDTH(parser->buffer)) + parser->input.pointer += WIDTH(parser->input)) #define SKIP_LINE(parser) \ - (IS_CRLF(parser->buffer) ? \ + (IS_CRLF(parser->input) ? \ (parser->mark.index += 2, \ parser->mark.column = 0, \ parser->mark.line ++, \ parser->unread -= 2, \ - parser->buffer.pointer += 2) : \ - IS_BREAK(parser->buffer) ? \ + parser->input.pointer += 2) : \ + IS_BREAK(parser->input) ? \ (parser->mark.index ++, \ parser->mark.column = 0, \ parser->mark.line ++, \ parser->unread --, \ - parser->buffer.pointer += WIDTH(parser->buffer)) : 0) + parser->input.pointer += WIDTH(parser->input)) : 0) /* * Copy a character to a string buffer and advance pointers. @@ -517,7 +517,7 @@ #define READ(parser,string) \ (STRING_EXTEND(parser,string) ? \ - (COPY(string,parser->buffer), \ + (COPY(string,parser->input), \ parser->mark.index ++, \ parser->mark.column ++, \ parser->unread --, \ @@ -529,37 +529,37 @@ #define READ_LINE(parser,string) \ (STRING_EXTEND(parser,string) ? \ - (((CHECK_AT(parser->buffer,'\r',0) \ - && CHECK_AT(parser->buffer,'\n',1)) ? /* CR LF -> LF */ \ - (*((string).pointer++) = (yaml_char_t) '\n', \ - parser->buffer.pointer += 2, \ + (((CHECK_AT(parser->input,'\r',0) \ + && CHECK_AT(parser->input,'\n',1)) ? /* CR LF -> LF */ \ + (JOIN_OCTET(string, (yaml_char_t) '\n'), \ + parser->input.pointer += 2, \ parser->mark.index += 2, \ parser->mark.column = 0, \ parser->mark.line ++, \ parser->unread -= 2) : \ - (CHECK_AT(parser->buffer,'\r',0) \ - || CHECK_AT(parser->buffer,'\n',0)) ? /* CR|LF -> LF */ \ - (*((string).pointer++) = (yaml_char_t) '\n', \ - parser->buffer.pointer ++, \ + (CHECK_AT(parser->input,'\r',0) \ + || CHECK_AT(parser->input,'\n',0)) ? /* CR|LF -> LF */ \ + (JOIN_OCTET(string,(yaml_char_t) '\n'), \ + parser->input.pointer ++, \ parser->mark.index ++, \ parser->mark.column = 0, \ parser->mark.line ++, \ parser->unread --) : \ - (CHECK_AT(parser->buffer,'\xC2',0) \ - && CHECK_AT(parser->buffer,'\x85',1)) ? /* NEL -> LF */ \ - (*((string).pointer++) = (yaml_char_t) '\n', \ - parser->buffer.pointer += 2, \ + (CHECK_AT(parser->input,'\xC2',0) \ + && CHECK_AT(parser->input,'\x85',1)) ? /* NEL -> LF */ \ + (JOIN_OCTET(string,(yaml_char_t) '\n'), \ + parser->input.pointer += 2, \ parser->mark.index ++, \ parser->mark.column = 0, \ parser->mark.line ++, \ parser->unread --) : \ - (CHECK_AT(parser->buffer,'\xE2',0) && \ - CHECK_AT(parser->buffer,'\x80',1) && \ - (CHECK_AT(parser->buffer,'\xA8',2) || \ - CHECK_AT(parser->buffer,'\xA9',2))) ? /* LS|PS -> LS|PS */ \ - (*((string).pointer++) = *(parser->buffer.pointer++), \ - *((string).pointer++) = *(parser->buffer.pointer++), \ - *((string).pointer++) = *(parser->buffer.pointer++), \ + (CHECK_AT(parser->input,'\xE2',0) && \ + CHECK_AT(parser->input,'\x80',1) && \ + (CHECK_AT(parser->input,'\xA8',2) || \ + CHECK_AT(parser->input,'\xA9',2))) ? /* LS|PS -> LS|PS */ \ + (COPY_OCTET(string,parser->input), \ + COPY_OCTET(string,parser->input), \ + COPY_OCTET(string,parser->input), \ parser->mark.index ++, \ parser->mark.column = 0, \ parser->mark.line ++, \ @@ -573,14 +573,6 @@ YAML_DECLARE(int) yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token); -/* - * Error handling. - */ - -static int -yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context, - yaml_mark_t context_mark, const char *problem); - /* * High-level token API. */ @@ -750,13 +742,13 @@ yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token) /* No tokens after STREAM-END or error. */ - if (parser->stream_end_produced || parser->error) { + if (parser->is_stream_end_produced || parser->error.type) { return 1; } /* Ensure that the tokens queue contains enough tokens. */ - if (!parser->token_available) { + if (!parser->is_token_available) { if (!yaml_parser_fetch_more_tokens(parser)) return 0; } @@ -764,33 +756,16 @@ yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token) /* Fetch the next token from the queue. */ *token = DEQUEUE(parser, parser->tokens); - parser->token_available = 0; + parser->is_token_available = 0; parser->tokens_parsed ++; if (token->type == YAML_STREAM_END_TOKEN) { - parser->stream_end_produced = 1; + parser->is_stream_end_produced = 1; } return 1; } -/* - * Set the scanner error and return 0. - */ - -static int -yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context, - yaml_mark_t context_mark, const char *problem) -{ - parser->error = YAML_SCANNER_ERROR; - parser->context = context; - parser->context_mark = context_mark; - parser->problem = problem; - parser->problem_mark = parser->mark; - - return 0; -} - /* * Ensure that the tokens queue contains at least one token which can be * returned to the Parser. @@ -819,16 +794,16 @@ yaml_parser_fetch_more_tokens(yaml_parser_t *parser) } else { - yaml_simple_key_t *simple_key; + size_t idx; /* Check if any potential simple key may occupy the head position. */ if (!yaml_parser_stale_simple_keys(parser)) return 0; - for (simple_key = parser->simple_keys.start; - simple_key != parser->simple_keys.top; simple_key++) { - if (simple_key->possible + for (idx = 0; idx < parser->simple_keys.length; idx++) { + yaml_simple_key_t *simple_key = parser->simple_keys.list + idx; + if (simple_key->is_possible && simple_key->token_number == parser->tokens_parsed) { need_more_tokens = 1; break; @@ -847,7 +822,7 @@ yaml_parser_fetch_more_tokens(yaml_parser_t *parser) return 0; } - parser->token_available = 1; + parser->is_token_available = 1; return 1; } @@ -859,6 +834,8 @@ yaml_parser_fetch_more_tokens(yaml_parser_t *parser) static int yaml_parser_fetch_next_token(yaml_parser_t *parser) { + yaml_mark_t start_mark = parser->mark; + /* Ensure that the buffer is initialized. */ if (!CACHE(parser, 1)) @@ -866,7 +843,7 @@ yaml_parser_fetch_next_token(yaml_parser_t *parser) /* Check if we just started scanning. Fetch STREAM-START then. */ - if (!parser->stream_start_produced) + if (!parser->is_stream_start_produced) return yaml_parser_fetch_stream_start(parser); /* Eat whitespaces and comments until we reach the next token. */ @@ -894,113 +871,113 @@ yaml_parser_fetch_next_token(yaml_parser_t *parser) /* Is it the end of the stream? */ - if (IS_Z(parser->buffer)) + if (IS_Z(parser->input)) return yaml_parser_fetch_stream_end(parser); /* Is it a directive? */ - if (parser->mark.column == 0 && CHECK(parser->buffer, '%')) + if (parser->mark.column == 0 && CHECK(parser->input, '%')) return yaml_parser_fetch_directive(parser); /* Is it the document start indicator? */ if (parser->mark.column == 0 - && CHECK_AT(parser->buffer, '-', 0) - && CHECK_AT(parser->buffer, '-', 1) - && CHECK_AT(parser->buffer, '-', 2) - && IS_BLANKZ_AT(parser->buffer, 3)) + && CHECK_AT(parser->input, '-', 0) + && CHECK_AT(parser->input, '-', 1) + && CHECK_AT(parser->input, '-', 2) + && IS_BLANKZ_AT(parser->input, 3)) return yaml_parser_fetch_document_indicator(parser, YAML_DOCUMENT_START_TOKEN); /* Is it the document end indicator? */ if (parser->mark.column == 0 - && CHECK_AT(parser->buffer, '.', 0) - && CHECK_AT(parser->buffer, '.', 1) - && CHECK_AT(parser->buffer, '.', 2) - && IS_BLANKZ_AT(parser->buffer, 3)) + && CHECK_AT(parser->input, '.', 0) + && CHECK_AT(parser->input, '.', 1) + && CHECK_AT(parser->input, '.', 2) + && IS_BLANKZ_AT(parser->input, 3)) return yaml_parser_fetch_document_indicator(parser, YAML_DOCUMENT_END_TOKEN); /* Is it the flow sequence start indicator? */ - if (CHECK(parser->buffer, '[')) + if (CHECK(parser->input, '[')) return yaml_parser_fetch_flow_collection_start(parser, YAML_FLOW_SEQUENCE_START_TOKEN); /* Is it the flow mapping start indicator? */ - if (CHECK(parser->buffer, '{')) + if (CHECK(parser->input, '{')) return yaml_parser_fetch_flow_collection_start(parser, YAML_FLOW_MAPPING_START_TOKEN); /* Is it the flow sequence end indicator? */ - if (CHECK(parser->buffer, ']')) + if (CHECK(parser->input, ']')) return yaml_parser_fetch_flow_collection_end(parser, YAML_FLOW_SEQUENCE_END_TOKEN); /* Is it the flow mapping end indicator? */ - if (CHECK(parser->buffer, '}')) + if (CHECK(parser->input, '}')) return yaml_parser_fetch_flow_collection_end(parser, YAML_FLOW_MAPPING_END_TOKEN); /* Is it the flow entry indicator? */ - if (CHECK(parser->buffer, ',')) + if (CHECK(parser->input, ',')) return yaml_parser_fetch_flow_entry(parser); /* Is it the block entry indicator? */ - if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1)) + if (CHECK(parser->input, '-') && IS_BLANKZ_AT(parser->input, 1)) return yaml_parser_fetch_block_entry(parser); /* Is it the key indicator? */ - if (CHECK(parser->buffer, '?') - && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1))) + if (CHECK(parser->input, '?') + && (parser->flow_level || IS_BLANKZ_AT(parser->input, 1))) return yaml_parser_fetch_key(parser); /* Is it the value indicator? */ - if (CHECK(parser->buffer, ':') - && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1))) + if (CHECK(parser->input, ':') + && (parser->flow_level || IS_BLANKZ_AT(parser->input, 1))) return yaml_parser_fetch_value(parser); /* Is it an alias? */ - if (CHECK(parser->buffer, '*')) + if (CHECK(parser->input, '*')) return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN); /* Is it an anchor? */ - if (CHECK(parser->buffer, '&')) + if (CHECK(parser->input, '&')) return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN); /* Is it a tag? */ - if (CHECK(parser->buffer, '!')) + if (CHECK(parser->input, '!')) return yaml_parser_fetch_tag(parser); /* Is it a literal scalar? */ - if (CHECK(parser->buffer, '|') && !parser->flow_level) + if (CHECK(parser->input, '|') && !parser->flow_level) return yaml_parser_fetch_block_scalar(parser, 1); /* Is it a folded scalar? */ - if (CHECK(parser->buffer, '>') && !parser->flow_level) + if (CHECK(parser->input, '>') && !parser->flow_level) return yaml_parser_fetch_block_scalar(parser, 0); /* Is it a single-quoted scalar? */ - if (CHECK(parser->buffer, '\'')) + if (CHECK(parser->input, '\'')) return yaml_parser_fetch_flow_scalar(parser, 1); /* Is it a double-quoted scalar? */ - if (CHECK(parser->buffer, '"')) + if (CHECK(parser->input, '"')) return yaml_parser_fetch_flow_scalar(parser, 0); /* @@ -1022,29 +999,29 @@ yaml_parser_fetch_next_token(yaml_parser_t *parser) * The last rule is more restrictive than the specification requires. */ - if (!(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '-') - || CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':') - || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '[') - || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{') - || CHECK(parser->buffer, '}') || CHECK(parser->buffer, '#') - || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '*') - || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '|') - || CHECK(parser->buffer, '>') || CHECK(parser->buffer, '\'') - || CHECK(parser->buffer, '"') || CHECK(parser->buffer, '%') - || CHECK(parser->buffer, '@') || CHECK(parser->buffer, '`')) || - (CHECK(parser->buffer, '-') && !IS_BLANK_AT(parser->buffer, 1)) || + if (!(IS_BLANKZ(parser->input) || CHECK(parser->input, '-') + || CHECK(parser->input, '?') || CHECK(parser->input, ':') + || CHECK(parser->input, ',') || CHECK(parser->input, '[') + || CHECK(parser->input, ']') || CHECK(parser->input, '{') + || CHECK(parser->input, '}') || CHECK(parser->input, '#') + || CHECK(parser->input, '&') || CHECK(parser->input, '*') + || CHECK(parser->input, '!') || CHECK(parser->input, '|') + || CHECK(parser->input, '>') || CHECK(parser->input, '\'') + || CHECK(parser->input, '"') || CHECK(parser->input, '%') + || CHECK(parser->input, '@') || CHECK(parser->input, '`')) || + (CHECK(parser->input, '-') && !IS_BLANK_AT(parser->input, 1)) || (!parser->flow_level && - (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':')) - && !IS_BLANKZ_AT(parser->buffer, 1))) + (CHECK(parser->input, '?') || CHECK(parser->input, ':')) + && !IS_BLANKZ_AT(parser->input, 1))) return yaml_parser_fetch_plain_scalar(parser); /* * If we don't determine the token type so far, it is an error. */ - return yaml_parser_set_scanner_error(parser, - "while scanning for the next token", parser->mark, - "found character that cannot start any token"); + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning for the next token", start_mark, + "found character that cannot start any token", parser->mark); } /* @@ -1055,13 +1032,14 @@ yaml_parser_fetch_next_token(yaml_parser_t *parser) static int yaml_parser_stale_simple_keys(yaml_parser_t *parser) { - yaml_simple_key_t *simple_key; + size_t idx; /* Check for a potential simple key for each flow level. */ - for (simple_key = parser->simple_keys.start; - simple_key != parser->simple_keys.top; simple_key ++) + for (idx = 0; idx < parser->simple_keys.length; idx ++) { + yaml_simple_key_t *simple_key = parser->simple_keys.list + idx; + /* * The specification requires that a simple key * @@ -1069,19 +1047,19 @@ yaml_parser_stale_simple_keys(yaml_parser_t *parser) * - is shorter than 1024 characters. */ - if (simple_key->possible + if (simple_key->is_possible && (simple_key->mark.line < parser->mark.line || simple_key->mark.index+1024 < parser->mark.index)) { /* Check if the potential simple key to be removed is required. */ - if (simple_key->required) { - return yaml_parser_set_scanner_error(parser, + if (simple_key->is_required) { + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, "while scanning a simple key", simple_key->mark, - "could not found expected ':'"); + "could not found expected ':'", parser->mark); } - simple_key->possible = 0; + simple_key->is_possible = 0; } } @@ -1102,7 +1080,7 @@ yaml_parser_save_simple_key(yaml_parser_t *parser) * level. */ - int required = (!parser->flow_level + int is_required = (!parser->flow_level && parser->indent == (int)parser->mark.column); /* @@ -1110,22 +1088,22 @@ yaml_parser_save_simple_key(yaml_parser_t *parser) * line. Therefore it is always allowed. But we add a check anyway. */ - assert(parser->simple_key_allowed || !required); /* Impossible. */ + assert(parser->is_simple_key_allowed || !is_required); /* Impossible. */ /* * If the current position may start a simple key, save it. */ - if (parser->simple_key_allowed) + if (parser->is_simple_key_allowed) { - yaml_simple_key_t simple_key = { 1, required, + yaml_simple_key_t simple_key = { 1, is_required, parser->tokens_parsed + parser->tokens.tail - parser->tokens.head, { 0, 0, 0 } }; simple_key.mark = parser->mark; if (!yaml_parser_remove_simple_key(parser)) return 0; - *(parser->simple_keys.top-1) = simple_key; + parser->simple_keys.list[parser->simple_keys.length-1] = simple_key; } return 1; @@ -1138,22 +1116,23 @@ yaml_parser_save_simple_key(yaml_parser_t *parser) static int yaml_parser_remove_simple_key(yaml_parser_t *parser) { - yaml_simple_key_t *simple_key = parser->simple_keys.top-1; + yaml_simple_key_t *simple_key = + parser->simple_keys.list + parser->simple_keys.length - 1; - if (simple_key->possible) + if (simple_key->is_possible) { /* If the key is required, it is an error. */ - if (simple_key->required) { - return yaml_parser_set_scanner_error(parser, + if (simple_key->is_required) { + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, "while scanning a simple key", simple_key->mark, - "could not found expected ':'"); + "could not found expected ':'", parser->mark); } } /* Remove the key from the stack. */ - simple_key->possible = 0; + simple_key->is_possible = 0; return 1; } @@ -1301,11 +1280,11 @@ yaml_parser_fetch_stream_start(yaml_parser_t *parser) /* A simple key is allowed at the beginning of the stream. */ - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; /* We have started. */ - parser->stream_start_produced = 1; + parser->is_stream_start_produced = 1; /* Create the STREAM-START token and append it to the queue. */ @@ -1344,7 +1323,7 @@ yaml_parser_fetch_stream_end(yaml_parser_t *parser) if (!yaml_parser_remove_simple_key(parser)) return 0; - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Create the STREAM-END token and append it to the queue. */ @@ -1375,7 +1354,7 @@ yaml_parser_fetch_directive(yaml_parser_t *parser) if (!yaml_parser_remove_simple_key(parser)) return 0; - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */ @@ -1413,7 +1392,7 @@ yaml_parser_fetch_document_indicator(yaml_parser_t *parser, if (!yaml_parser_remove_simple_key(parser)) return 0; - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Consume the token. */ @@ -1460,7 +1439,7 @@ yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser, /* A simple key may follow the indicators '[' and '{'. */ - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; /* Consume the token. */ @@ -1503,7 +1482,7 @@ yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser, /* No simple keys after the indicators ']' and '}'. */ - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Consume the token. */ @@ -1540,7 +1519,7 @@ yaml_parser_fetch_flow_entry(yaml_parser_t *parser) /* Simple keys are allowed after ','. */ - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; /* Consume the token. */ @@ -1574,9 +1553,10 @@ yaml_parser_fetch_block_entry(yaml_parser_t *parser) { /* Check if we are allowed to start a new entry. */ - if (!parser->simple_key_allowed) { - return yaml_parser_set_scanner_error(parser, NULL, parser->mark, - "block sequence entries are not allowed in this context"); + if (!parser->is_simple_key_allowed) { + return SCANNER_ERROR_INIT(parser, + "block sequence entries are not allowed in this context", + parser->mark); } /* Add the BLOCK-SEQUENCE-START token if needed. */ @@ -1601,7 +1581,7 @@ yaml_parser_fetch_block_entry(yaml_parser_t *parser) /* Simple keys are allowed after '-'. */ - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; /* Consume the token. */ @@ -1635,9 +1615,9 @@ yaml_parser_fetch_key(yaml_parser_t *parser) { /* Check if we are allowed to start a new key (not nessesary simple). */ - if (!parser->simple_key_allowed) { - return yaml_parser_set_scanner_error(parser, NULL, parser->mark, - "mapping keys are not allowed in this context"); + if (!parser->is_simple_key_allowed) { + return SCANNER_ERROR_INIT(parser, + "mapping keys are not allowed in this context", parser->mark); } /* Add the BLOCK-MAPPING-START token if needed. */ @@ -1654,7 +1634,7 @@ yaml_parser_fetch_key(yaml_parser_t *parser) /* Simple keys are allowed after '?' in the block context. */ - parser->simple_key_allowed = (!parser->flow_level); + parser->is_simple_key_allowed = (!parser->flow_level); /* Consume the token. */ @@ -1681,11 +1661,12 @@ yaml_parser_fetch_value(yaml_parser_t *parser) { yaml_mark_t start_mark, end_mark; yaml_token_t token; - yaml_simple_key_t *simple_key = parser->simple_keys.top-1; + yaml_simple_key_t *simple_key = + parser->simple_keys.list + parser->simple_keys.length - 1; /* Have we found a simple key? */ - if (simple_key->possible) + if (simple_key->is_possible) { /* Create the KEY token and insert it into the queue. */ @@ -1705,11 +1686,11 @@ yaml_parser_fetch_value(yaml_parser_t *parser) /* Remove the simple key. */ - simple_key->possible = 0; + simple_key->is_possible = 0; /* A simple key cannot follow another simple key. */ - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; } else { @@ -1721,9 +1702,10 @@ yaml_parser_fetch_value(yaml_parser_t *parser) { /* Check if we are allowed to start a complex value. */ - if (!parser->simple_key_allowed) { - return yaml_parser_set_scanner_error(parser, NULL, parser->mark, - "mapping values are not allowed in this context"); + if (!parser->is_simple_key_allowed) { + return SCANNER_ERROR_INIT(parser, + "mapping values are not allowed in this context", + parser->mark); } /* Add the BLOCK-MAPPING-START token if needed. */ @@ -1735,7 +1717,7 @@ yaml_parser_fetch_value(yaml_parser_t *parser) /* Simple keys after ':' are allowed in the block context. */ - parser->simple_key_allowed = (!parser->flow_level); + parser->is_simple_key_allowed = (!parser->flow_level); } /* Consume the token. */ @@ -1770,7 +1752,7 @@ yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type) /* A simple key cannot follow an anchor or an alias. */ - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Create the ALIAS or ANCHOR token and append it to the queue. */ @@ -1800,7 +1782,7 @@ yaml_parser_fetch_tag(yaml_parser_t *parser) /* A simple key cannot follow a tag. */ - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Create the TAG token and append it to the queue. */ @@ -1831,7 +1813,7 @@ yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal) /* A simple key may follow a block scalar. */ - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; /* Create the SCALAR token and append it to the queue. */ @@ -1862,7 +1844,7 @@ yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single) /* A simple key cannot follow a flow scalar. */ - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Create the SCALAR token and append it to the queue. */ @@ -1893,7 +1875,7 @@ yaml_parser_fetch_plain_scalar(yaml_parser_t *parser) /* A simple key cannot follow a flow scalar. */ - parser->simple_key_allowed = 0; + parser->is_simple_key_allowed = 0; /* Create the SCALAR token and append it to the queue. */ @@ -1923,7 +1905,7 @@ yaml_parser_scan_to_next_token(yaml_parser_t *parser) if (!CACHE(parser, 1)) return 0; - if (parser->mark.column == 0 && IS_BOM(parser->buffer)) + if (parser->mark.column == 0 && IS_BOM(parser->input)) SKIP(parser); /* @@ -1938,17 +1920,17 @@ yaml_parser_scan_to_next_token(yaml_parser_t *parser) if (!CACHE(parser, 1)) return 0; - while (CHECK(parser->buffer,' ') || - ((parser->flow_level || !parser->simple_key_allowed) && - CHECK(parser->buffer, '\t'))) { + while (CHECK(parser->input,' ') || + ((parser->flow_level || !parser->is_simple_key_allowed) && + CHECK(parser->input, '\t'))) { SKIP(parser); if (!CACHE(parser, 1)) return 0; } /* Eat a comment until a line break. */ - if (CHECK(parser->buffer, '#')) { - while (!IS_BREAKZ(parser->buffer)) { + if (CHECK(parser->input, '#')) { + while (!IS_BREAKZ(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) return 0; } @@ -1956,7 +1938,7 @@ yaml_parser_scan_to_next_token(yaml_parser_t *parser) /* If it is a line break, eat it. */ - if (IS_BREAK(parser->buffer)) + if (IS_BREAK(parser->input)) { if (!CACHE(parser, 2)) return 0; SKIP_LINE(parser); @@ -1964,7 +1946,7 @@ yaml_parser_scan_to_next_token(yaml_parser_t *parser) /* In the block context, a new line may start a simple key. */ if (!parser->flow_level) { - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; } } else @@ -2047,8 +2029,9 @@ yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token) else { - yaml_parser_set_scanner_error(parser, "while scanning a directive", - start_mark, "found uknown directive name"); + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a directive", start_mark, + "found uknown directive name", parser->mark); goto error; } @@ -2056,13 +2039,13 @@ yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token) if (!CACHE(parser, 1)) goto error; - while (IS_BLANK(parser->buffer)) { + while (IS_BLANK(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) goto error; } - if (CHECK(parser->buffer, '#')) { - while (!IS_BREAKZ(parser->buffer)) { + if (CHECK(parser->input, '#')) { + while (!IS_BREAKZ(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) goto error; } @@ -2070,15 +2053,16 @@ yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token) /* Check if we are at the end of the line. */ - if (!IS_BREAKZ(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a directive", - start_mark, "did not found expected comment or line break"); + if (!IS_BREAKZ(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a directive", start_mark, + "did not found expected comment or line break", parser->mark); goto error; } /* Eat a line break. */ - if (IS_BREAK(parser->buffer)) { + if (IS_BREAK(parser->input)) { if (!CACHE(parser, 2)) goto error; SKIP_LINE(parser); } @@ -2110,13 +2094,14 @@ yaml_parser_scan_directive_name(yaml_parser_t *parser, { yaml_string_t string = NULL_STRING; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; /* Consume the directive name. */ if (!CACHE(parser, 1)) goto error; - while (IS_ALPHA(parser->buffer)) + while (IS_ALPHA(parser->input)) { if (!READ(parser, string)) goto error; if (!CACHE(parser, 1)) goto error; @@ -2124,21 +2109,23 @@ yaml_parser_scan_directive_name(yaml_parser_t *parser, /* Check if the name is empty. */ - if (string.start == string.pointer) { - yaml_parser_set_scanner_error(parser, "while scanning a directive", - start_mark, "cannot found expected directive name"); + if (!string.pointer) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a directive", start_mark, + "cannot found expected directive name", parser->mark); goto error; } /* Check for an blank character after the name. */ - if (!IS_BLANKZ(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a directive", - start_mark, "found unexpected non-alphabetical character"); + if (!IS_BLANKZ(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a directive", start_mark, + "found unexpected non-alphabetical character", parser->mark); goto error; } - *name = string.start; + *name = string.buffer; return 1; @@ -2163,7 +2150,7 @@ yaml_parser_scan_version_directive_value(yaml_parser_t *parser, if (!CACHE(parser, 1)) return 0; - while (IS_BLANK(parser->buffer)) { + while (IS_BLANK(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) return 0; } @@ -2175,9 +2162,10 @@ yaml_parser_scan_version_directive_value(yaml_parser_t *parser, /* Eat '.'. */ - if (!CHECK(parser->buffer, '.')) { - return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", - start_mark, "did not find expected digit or '.' character"); + if (!CHECK(parser->input, '.')) { + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a %YAML directive", start_mark, + "did not find expected digit or '.' character", parser->mark); } SKIP(parser); @@ -2213,16 +2201,17 @@ yaml_parser_scan_version_directive_number(yaml_parser_t *parser, if (!CACHE(parser, 1)) return 0; - while (IS_DIGIT(parser->buffer)) + while (IS_DIGIT(parser->input)) { /* Check if the number is too long. */ if (++length > MAX_NUMBER_LENGTH) { - return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", - start_mark, "found extremely long version number"); + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a %YAML directive", start_mark, + "found extremely long version number", parser->mark); } - value = value*10 + AS_DIGIT(parser->buffer); + value = value*10 + AS_DIGIT(parser->input); SKIP(parser); @@ -2232,8 +2221,9 @@ yaml_parser_scan_version_directive_number(yaml_parser_t *parser, /* Check if the number was present. */ if (!length) { - return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", - start_mark, "did not find expected version number"); + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a %YAML directive", start_mark, + "did not find expected version number", parser->mark); } *number = value; @@ -2260,7 +2250,7 @@ yaml_parser_scan_tag_directive_value(yaml_parser_t *parser, if (!CACHE(parser, 1)) goto error; - while (IS_BLANK(parser->buffer)) { + while (IS_BLANK(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) goto error; } @@ -2274,15 +2264,16 @@ yaml_parser_scan_tag_directive_value(yaml_parser_t *parser, if (!CACHE(parser, 1)) goto error; - if (!IS_BLANK(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive", - start_mark, "did not find expected whitespace"); + if (!IS_BLANK(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a %TAG directive", start_mark, + "did not find expected whitespace", parser->mark); goto error; } /* Eat whitespaces. */ - while (IS_BLANK(parser->buffer)) { + while (IS_BLANK(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) goto error; } @@ -2296,9 +2287,10 @@ yaml_parser_scan_tag_directive_value(yaml_parser_t *parser, if (!CACHE(parser, 1)) goto error; - if (!IS_BLANKZ(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive", - start_mark, "did not find expected whitespace or line break"); + if (!IS_BLANKZ(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a %TAG directive", start_mark, + "did not find expected whitespace or line break", parser->mark); goto error; } @@ -2321,7 +2313,8 @@ yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token, yaml_mark_t start_mark, end_mark; yaml_string_t string = NULL_STRING; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; /* Eat the indicator character. */ @@ -2333,7 +2326,7 @@ yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token, if (!CACHE(parser, 1)) goto error; - while (IS_ALPHA(parser->buffer)) { + while (IS_ALPHA(parser->input)) { if (!READ(parser, string)) goto error; if (!CACHE(parser, 1)) goto error; length ++; @@ -2348,24 +2341,26 @@ yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token, * '?', ':', ',', ']', '}', '%', '@', '`'. */ - if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?') - || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',') - || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}') - || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@') - || CHECK(parser->buffer, '`'))) { - yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ? - "while scanning an anchor" : "while scanning an alias", start_mark, - "did not find expected alphabetic or numeric character"); + if (!length || !(IS_BLANKZ(parser->input) || CHECK(parser->input, '?') + || CHECK(parser->input, ':') || CHECK(parser->input, ',') + || CHECK(parser->input, ']') || CHECK(parser->input, '}') + || CHECK(parser->input, '%') || CHECK(parser->input, '@') + || CHECK(parser->input, '`'))) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, type == YAML_ANCHOR_TOKEN ? + "while scanning an anchor" : "while scanning an alias", + start_mark, + "did not find expected alphabetic or numeric character", + parser->mark); goto error; } /* Create a token. */ if (type == YAML_ANCHOR_TOKEN) { - ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark); + ANCHOR_TOKEN_INIT(*token, string.buffer, start_mark, end_mark); } else { - ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark); + ALIAS_TOKEN_INIT(*token, string.buffer, start_mark, end_mark); } return 1; @@ -2392,7 +2387,7 @@ yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) if (!CACHE(parser, 2)) goto error; - if (CHECK_AT(parser->buffer, '<', 1)) + if (CHECK_AT(parser->input, '<', 1)) { /* Set the handle to '' */ @@ -2412,9 +2407,10 @@ yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) /* Check for '>' and eat it. */ - if (!CHECK(parser->buffer, '>')) { - yaml_parser_set_scanner_error(parser, "while scanning a tag", - start_mark, "did not find the expected '>'"); + if (!CHECK(parser->input, '>')) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a tag", start_mark, + "did not find the expected '>'", parser->mark); goto error; } @@ -2470,9 +2466,10 @@ yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) if (!CACHE(parser, 1)) goto error; - if (!IS_BLANKZ(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a tag", - start_mark, "did not found expected whitespace or line break"); + if (!IS_BLANKZ(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a tag", start_mark, + "did not found expected whitespace or line break", parser->mark); goto error; } @@ -2500,16 +2497,17 @@ yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, { yaml_string_t string = NULL_STRING; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; /* Check the initial '!' character. */ if (!CACHE(parser, 1)) goto error; - if (!CHECK(parser->buffer, '!')) { - yaml_parser_set_scanner_error(parser, directive ? + if (!CHECK(parser->input, '!')) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, directive ? "while scanning a tag directive" : "while scanning a tag", - start_mark, "did not find expected '!'"); + start_mark, "did not find expected '!'", parser->mark); goto error; } @@ -2521,7 +2519,7 @@ yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, if (!CACHE(parser, 1)) goto error; - while (IS_ALPHA(parser->buffer)) + while (IS_ALPHA(parser->input)) { if (!READ(parser, string)) goto error; if (!CACHE(parser, 1)) goto error; @@ -2529,7 +2527,7 @@ yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, /* Check if the trailing character is '!' and copy it. */ - if (CHECK(parser->buffer, '!')) + if (CHECK(parser->input, '!')) { if (!READ(parser, string)) goto error; } @@ -2541,14 +2539,16 @@ yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, * URI. */ - if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) { - yaml_parser_set_scanner_error(parser, "while parsing a tag directive", - start_mark, "did not find expected '!'"); + if (directive && + !(string.buffer[0] == '!' && string.buffer[1] == '\0')) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while parsing a tag directive", start_mark, + "did not find expected '!'", parser->mark); goto error; } } - *handle = string.start; + *handle = string.buffer; return 1; @@ -2568,13 +2568,14 @@ yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive, size_t length = head ? strlen((char *)head) : 0; yaml_string_t string = NULL_STRING; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; /* Resize the string to include the head. */ - while (string.end - string.start <= (int)length) { - if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) { - parser->error = YAML_MEMORY_ERROR; + while (string.capacity <= length) { + if (!yaml_string_extend(&string.buffer, &string.capacity)) { + MEMORY_ERROR_INIT(parser); goto error; } } @@ -2586,7 +2587,7 @@ yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive, */ if (length > 1) { - memcpy(string.start, head+1, length-1); + memcpy(string.buffer, head+1, length-1); string.pointer += length-1; } @@ -2602,21 +2603,21 @@ yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive, * '%'. */ - while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';') - || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?') - || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@') - || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=') - || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$') - || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.') - || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~') - || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'') - || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')') - || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']') - || CHECK(parser->buffer, '%')) + while (IS_ALPHA(parser->input) || CHECK(parser->input, ';') + || CHECK(parser->input, '/') || CHECK(parser->input, '?') + || CHECK(parser->input, ':') || CHECK(parser->input, '@') + || CHECK(parser->input, '&') || CHECK(parser->input, '=') + || CHECK(parser->input, '+') || CHECK(parser->input, '$') + || CHECK(parser->input, ',') || CHECK(parser->input, '.') + || CHECK(parser->input, '!') || CHECK(parser->input, '~') + || CHECK(parser->input, '*') || CHECK(parser->input, '\'') + || CHECK(parser->input, '(') || CHECK(parser->input, ')') + || CHECK(parser->input, '[') || CHECK(parser->input, ']') + || CHECK(parser->input, '%')) { /* Check if it is a URI-escape sequence. */ - if (CHECK(parser->buffer, '%')) { + if (CHECK(parser->input, '%')) { if (!yaml_parser_scan_uri_escapes(parser, directive, start_mark, &string)) goto error; } @@ -2634,13 +2635,13 @@ yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive, if (!STRING_EXTEND(parser, string)) goto error; - yaml_parser_set_scanner_error(parser, directive ? + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, directive ? "while parsing a %TAG directive" : "while parsing a tag", - start_mark, "did not find expected tag URI"); + start_mark, "did not find expected tag URI", parser->mark); goto error; } - *uri = string.start; + *uri = string.buffer; return 1; @@ -2662,24 +2663,23 @@ yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive, /* Decode the required number of characters. */ do { - unsigned char octet = 0; /* Check for a URI-escaped octet. */ if (!CACHE(parser, 3)) return 0; - if (!(CHECK(parser->buffer, '%') - && IS_HEX_AT(parser->buffer, 1) - && IS_HEX_AT(parser->buffer, 2))) { - return yaml_parser_set_scanner_error(parser, directive ? + if (!(CHECK(parser->input, '%') + && IS_HEX_AT(parser->input, 1) + && IS_HEX_AT(parser->input, 2))) { + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, directive ? "while parsing a %TAG directive" : "while parsing a tag", - start_mark, "did not find URI escaped octet"); + start_mark, "did not find URI escaped octet", parser->mark); } /* Get the octet. */ - octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2); + octet = (AS_HEX_AT(parser->input, 1) << 4) + AS_HEX_AT(parser->input, 2); /* If it is the leading octet, determine the length of the UTF-8 sequence. */ @@ -2690,9 +2690,10 @@ yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive, (octet & 0xF0) == 0xE0 ? 3 : (octet & 0xF8) == 0xF0 ? 4 : 0; if (!width) { - return yaml_parser_set_scanner_error(parser, directive ? + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, directive ? "while parsing a %TAG directive" : "while parsing a tag", - start_mark, "found an incorrect leading UTF-8 octet"); + start_mark, "found an incorrect leading UTF-8 octet", + parser->mark); } } else @@ -2700,15 +2701,16 @@ yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive, /* Check if the trailing octet is correct. */ if ((octet & 0xC0) != 0x80) { - return yaml_parser_set_scanner_error(parser, directive ? + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, directive ? "while parsing a %TAG directive" : "while parsing a tag", - start_mark, "found an incorrect trailing UTF-8 octet"); + start_mark, "found an incorrect trailing UTF-8 octet", + parser->mark); } } /* Copy the octet and move the pointers. */ - *(string->pointer++) = octet; + JOIN_OCTET(*string, octet); SKIP(parser); SKIP(parser); SKIP(parser); @@ -2737,9 +2739,12 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, int leading_blank = 0; int trailing_blank = 0; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, leading_break, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_CAPACITY)) + goto error; /* Eat the indicator '|' or '>'. */ @@ -2753,11 +2758,11 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Check for a chomping indicator. */ - if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) + if (CHECK(parser->input, '+') || CHECK(parser->input, '-')) { /* Set the chomping method and eat the indicator. */ - chomping = CHECK(parser->buffer, '+') ? +1 : -1; + chomping = CHECK(parser->input, '+') ? +1 : -1; SKIP(parser); @@ -2765,19 +2770,20 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, if (!CACHE(parser, 1)) goto error; - if (IS_DIGIT(parser->buffer)) + if (IS_DIGIT(parser->input)) { /* Check that the intendation is greater than 0. */ - if (CHECK(parser->buffer, '0')) { - yaml_parser_set_scanner_error(parser, "while scanning a block scalar", - start_mark, "found an intendation indicator equal to 0"); + if (CHECK(parser->input, '0')) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a block scalar", start_mark, + "found an intendation indicator equal to 0", parser->mark); goto error; } /* Get the intendation level and eat the indicator. */ - increment = AS_DIGIT(parser->buffer); + increment = AS_DIGIT(parser->input); SKIP(parser); } @@ -2785,22 +2791,23 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Do the same as above, but in the opposite order. */ - else if (IS_DIGIT(parser->buffer)) + else if (IS_DIGIT(parser->input)) { - if (CHECK(parser->buffer, '0')) { - yaml_parser_set_scanner_error(parser, "while scanning a block scalar", - start_mark, "found an intendation indicator equal to 0"); + if (CHECK(parser->input, '0')) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a block scalar", start_mark, + "found an intendation indicator equal to 0", parser->mark); goto error; } - increment = AS_DIGIT(parser->buffer); + increment = AS_DIGIT(parser->input); SKIP(parser); if (!CACHE(parser, 1)) goto error; - if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) { - chomping = CHECK(parser->buffer, '+') ? +1 : -1; + if (CHECK(parser->input, '+') || CHECK(parser->input, '-')) { + chomping = CHECK(parser->input, '+') ? +1 : -1; SKIP(parser); } @@ -2810,13 +2817,13 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, if (!CACHE(parser, 1)) goto error; - while (IS_BLANK(parser->buffer)) { + while (IS_BLANK(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) goto error; } - if (CHECK(parser->buffer, '#')) { - while (!IS_BREAKZ(parser->buffer)) { + if (CHECK(parser->input, '#')) { + while (!IS_BREAKZ(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) goto error; } @@ -2824,15 +2831,16 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Check if we are at the end of the line. */ - if (!IS_BREAKZ(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a block scalar", - start_mark, "did not found expected comment or line break"); + if (!IS_BREAKZ(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a block scalar", start_mark, + "did not found expected comment or line break", parser->mark); goto error; } /* Eat a line break. */ - if (IS_BREAK(parser->buffer)) { + if (IS_BREAK(parser->input)) { if (!CACHE(parser, 2)) goto error; SKIP_LINE(parser); } @@ -2854,7 +2862,7 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, if (!CACHE(parser, 1)) goto error; - while ((int)parser->mark.column == indent && !IS_Z(parser->buffer)) + while ((int)parser->mark.column == indent && !IS_Z(parser->input)) { /* * We are at the beginning of a non-empty line. @@ -2862,18 +2870,18 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Is it a trailing whitespace? */ - trailing_blank = IS_BLANK(parser->buffer); + trailing_blank = IS_BLANK(parser->input); /* Check if we need to fold the leading line break. */ - if (!literal && (*leading_break.start == '\n') + if (!literal && (*leading_break.buffer == '\n') && !leading_blank && !trailing_blank) { /* Do we need to join the lines by space? */ - if (*trailing_breaks.start == '\0') { + if (*trailing_breaks.buffer == '\0') { if (!STRING_EXTEND(parser, string)) goto error; - *(string.pointer ++) = ' '; + JOIN_OCTET(string, ' '); } CLEAR(parser, leading_break); @@ -2890,11 +2898,11 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Is it a leading whitespace? */ - leading_blank = IS_BLANK(parser->buffer); + leading_blank = IS_BLANK(parser->input); /* Consume the current line. */ - while (!IS_BREAKZ(parser->buffer)) { + while (!IS_BREAKZ(parser->input)) { if (!READ(parser, string)) goto error; if (!CACHE(parser, 1)) goto error; } @@ -2922,7 +2930,7 @@ yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Create a token. */ - SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start, + SCALAR_TOKEN_INIT(*token, string.buffer, string.pointer, literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE, start_mark, end_mark); @@ -2962,7 +2970,7 @@ yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser, if (!CACHE(parser, 1)) return 0; while ((!*indent || (int)parser->mark.column < *indent) - && IS_SPACE(parser->buffer)) { + && IS_SPACE(parser->input)) { SKIP(parser); if (!CACHE(parser, 1)) return 0; } @@ -2973,14 +2981,16 @@ yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser, /* Check for a tab character messing the intendation. */ if ((!*indent || (int)parser->mark.column < *indent) - && IS_TAB(parser->buffer)) { - return yaml_parser_set_scanner_error(parser, "while scanning a block scalar", - start_mark, "found a tab character where an intendation space is expected"); + && IS_TAB(parser->input)) { + return SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a block scalar", start_mark, + "found a tab character where an intendation space is expected", + parser->mark); } /* Have we found a non-empty line? */ - if (!IS_BREAK(parser->buffer)) break; + if (!IS_BREAK(parser->input)) break; /* Consume the line break. */ @@ -3018,10 +3028,14 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, yaml_string_t whitespaces = NULL_STRING; int leading_blanks; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, leading_break, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_CAPACITY)) + goto error; /* Eat the left quote. */ @@ -3038,24 +3052,26 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, if (!CACHE(parser, 4)) goto error; if (parser->mark.column == 0 && - ((CHECK_AT(parser->buffer, '-', 0) && - CHECK_AT(parser->buffer, '-', 1) && - CHECK_AT(parser->buffer, '-', 2)) || - (CHECK_AT(parser->buffer, '.', 0) && - CHECK_AT(parser->buffer, '.', 1) && - CHECK_AT(parser->buffer, '.', 2))) && - IS_BLANKZ_AT(parser->buffer, 3)) + ((CHECK_AT(parser->input, '-', 0) && + CHECK_AT(parser->input, '-', 1) && + CHECK_AT(parser->input, '-', 2)) || + (CHECK_AT(parser->input, '.', 0) && + CHECK_AT(parser->input, '.', 1) && + CHECK_AT(parser->input, '.', 2))) && + IS_BLANKZ_AT(parser->input, 3)) { - yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar", - start_mark, "found unexpected document indicator"); + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a quoted scalar", start_mark, + "found unexpected document indicator", parser->mark); goto error; } /* Check for EOF. */ - if (IS_Z(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar", - start_mark, "found unexpected end of stream"); + if (IS_Z(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a quoted scalar", start_mark, + "found unexpected end of stream", parser->mark); goto error; } @@ -3065,30 +3081,30 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, leading_blanks = 0; - while (!IS_BLANKZ(parser->buffer)) + while (!IS_BLANKZ(parser->input)) { /* Check for an escaped single quote. */ - if (single && CHECK_AT(parser->buffer, '\'', 0) - && CHECK_AT(parser->buffer, '\'', 1)) + if (single && CHECK_AT(parser->input, '\'', 0) + && CHECK_AT(parser->input, '\'', 1)) { if (!STRING_EXTEND(parser, string)) goto error; - *(string.pointer++) = '\''; + JOIN_OCTET(string, '\''); SKIP(parser); SKIP(parser); } /* Check for the right quote. */ - else if (CHECK(parser->buffer, single ? '\'' : '"')) + else if (CHECK(parser->input, single ? '\'' : '"')) { break; } /* Check for an escaped line break. */ - else if (!single && CHECK(parser->buffer, '\\') - && IS_BREAK_AT(parser->buffer, 1)) + else if (!single && CHECK(parser->input, '\\') + && IS_BREAK_AT(parser->input, 1)) { if (!CACHE(parser, 3)) goto error; SKIP(parser); @@ -3099,7 +3115,7 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Check for an escape sequence. */ - else if (!single && CHECK(parser->buffer, '\\')) + else if (!single && CHECK(parser->input, '\\')) { size_t code_length = 0; @@ -3107,81 +3123,81 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Check the escape character. */ - switch (parser->buffer.pointer[1]) + switch (OCTET_AT(parser->input, 1)) { case '0': - *(string.pointer++) = '\0'; + JOIN_OCTET(string, '\0'); break; case 'a': - *(string.pointer++) = '\x07'; + JOIN_OCTET(string, '\x07'); break; case 'b': - *(string.pointer++) = '\x08'; + JOIN_OCTET(string, '\x08'); break; case 't': case '\t': - *(string.pointer++) = '\x09'; + JOIN_OCTET(string, '\x09'); break; case 'n': - *(string.pointer++) = '\x0A'; + JOIN_OCTET(string, '\x0A'); break; case 'v': - *(string.pointer++) = '\x0B'; + JOIN_OCTET(string, '\x0B'); break; case 'f': - *(string.pointer++) = '\x0C'; + JOIN_OCTET(string, '\x0C'); break; case 'r': - *(string.pointer++) = '\x0D'; + JOIN_OCTET(string, '\x0D'); break; case 'e': - *(string.pointer++) = '\x1B'; + JOIN_OCTET(string, '\x1B'); break; case ' ': - *(string.pointer++) = '\x20'; + JOIN_OCTET(string, '\x20'); break; case '"': - *(string.pointer++) = '"'; + JOIN_OCTET(string, '"'); break; case '\'': - *(string.pointer++) = '\''; + JOIN_OCTET(string, '\''); break; case '\\': - *(string.pointer++) = '\\'; + JOIN_OCTET(string, '\\'); break; case 'N': /* NEL (#x85) */ - *(string.pointer++) = '\xC2'; - *(string.pointer++) = '\x85'; + JOIN_OCTET(string, '\xC2'); + JOIN_OCTET(string, '\x85'); break; case '_': /* #xA0 */ - *(string.pointer++) = '\xC2'; - *(string.pointer++) = '\xA0'; + JOIN_OCTET(string, '\xC2'); + JOIN_OCTET(string, '\xA0'); break; case 'L': /* LS (#x2028) */ - *(string.pointer++) = '\xE2'; - *(string.pointer++) = '\x80'; - *(string.pointer++) = '\xA8'; + JOIN_OCTET(string, '\xE2'); + JOIN_OCTET(string, '\x80'); + JOIN_OCTET(string, '\xA8'); break; case 'P': /* PS (#x2029) */ - *(string.pointer++) = '\xE2'; - *(string.pointer++) = '\x80'; - *(string.pointer++) = '\xA9'; + JOIN_OCTET(string, '\xE2'); + JOIN_OCTET(string, '\x80'); + JOIN_OCTET(string, '\xA9'); break; case 'x': @@ -3197,8 +3213,9 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, break; default: - yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", - start_mark, "found unknown escape character"); + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while parsing a quoted scalar", start_mark, + "found unknown escape character", parser->mark); goto error; } @@ -3210,51 +3227,55 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, if (code_length) { unsigned int value = 0; - size_t k; + size_t idx; /* Scan the character value. */ if (!CACHE(parser, code_length)) goto error; - for (k = 0; k < code_length; k ++) { - if (!IS_HEX_AT(parser->buffer, k)) { - yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", - start_mark, "did not find expected hexdecimal number"); + for (idx = 0; idx < code_length; idx ++) { + if (!IS_HEX_AT(parser->input, idx)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while parsing a quoted scalar", start_mark, + "did not find expected hexdecimal number", + parser->mark); goto error; } - value = (value << 4) + AS_HEX_AT(parser->buffer, k); + value = (value << 4) + AS_HEX_AT(parser->input, idx); } /* Check the value and write the character. */ if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) { - yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", - start_mark, "found invalid Unicode character escape code"); + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while parsing a quoted scalar", start_mark, + "found invalid Unicode character escape code", + parser->mark); goto error; } if (value <= 0x7F) { - *(string.pointer++) = value; + JOIN_OCTET(string, value); } else if (value <= 0x7FF) { - *(string.pointer++) = 0xC0 + (value >> 6); - *(string.pointer++) = 0x80 + (value & 0x3F); + JOIN_OCTET(string, 0xC0 + (value >> 6)); + JOIN_OCTET(string, 0x80 + (value & 0x3F)); } else if (value <= 0xFFFF) { - *(string.pointer++) = 0xE0 + (value >> 12); - *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F); - *(string.pointer++) = 0x80 + (value & 0x3F); + JOIN_OCTET(string, 0xE0 + (value >> 12)); + JOIN_OCTET(string, 0x80 + ((value >> 6) & 0x3F)); + JOIN_OCTET(string, 0x80 + (value & 0x3F)); } else { - *(string.pointer++) = 0xF0 + (value >> 18); - *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F); - *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F); - *(string.pointer++) = 0x80 + (value & 0x3F); + JOIN_OCTET(string, 0xF0 + (value >> 18)); + JOIN_OCTET(string, 0x80 + ((value >> 12) & 0x3F)); + JOIN_OCTET(string, 0x80 + ((value >> 6) & 0x3F)); + JOIN_OCTET(string, 0x80 + (value & 0x3F)); } /* Advance the pointer. */ - for (k = 0; k < code_length; k ++) { + for (idx = 0; idx < code_length; idx ++) { SKIP(parser); } } @@ -3272,16 +3293,16 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Check if we are at the end of the scalar. */ - if (CHECK(parser->buffer, single ? '\'' : '"')) + if (CHECK(parser->input, single ? '\'' : '"')) break; /* Consume blank characters. */ if (!CACHE(parser, 1)) goto error; - while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)) + while (IS_BLANK(parser->input) || IS_BREAK(parser->input)) { - if (IS_BLANK(parser->buffer)) + if (IS_BLANK(parser->input)) { /* Consume a space or a tab character. */ @@ -3318,10 +3339,10 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, { /* Do we need to fold line breaks? */ - if (leading_break.start[0] == '\n') { - if (trailing_breaks.start[0] == '\0') { + if (leading_break.buffer[0] == '\n') { + if (trailing_breaks.buffer[0] == '\0') { if (!STRING_EXTEND(parser, string)) goto error; - *(string.pointer++) = ' '; + JOIN_OCTET(string, ' '); } else { if (!JOIN(parser, string, trailing_breaks)) goto error; @@ -3351,7 +3372,7 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, /* Create a token. */ - SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start, + SCALAR_TOKEN_INIT(*token, string.buffer, string.pointer, single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE, start_mark, end_mark); @@ -3386,10 +3407,14 @@ yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token) int leading_blanks = 0; int indent = parser->indent+1; - if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error; - if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error; + if (!STRING_INIT(parser, string, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, leading_break, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_CAPACITY)) + goto error; + if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_CAPACITY)) + goto error; start_mark = end_mark = parser->mark; @@ -3402,55 +3427,56 @@ yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token) if (!CACHE(parser, 4)) goto error; if (parser->mark.column == 0 && - ((CHECK_AT(parser->buffer, '-', 0) && - CHECK_AT(parser->buffer, '-', 1) && - CHECK_AT(parser->buffer, '-', 2)) || - (CHECK_AT(parser->buffer, '.', 0) && - CHECK_AT(parser->buffer, '.', 1) && - CHECK_AT(parser->buffer, '.', 2))) && - IS_BLANKZ_AT(parser->buffer, 3)) break; + ((CHECK_AT(parser->input, '-', 0) && + CHECK_AT(parser->input, '-', 1) && + CHECK_AT(parser->input, '-', 2)) || + (CHECK_AT(parser->input, '.', 0) && + CHECK_AT(parser->input, '.', 1) && + CHECK_AT(parser->input, '.', 2))) && + IS_BLANKZ_AT(parser->input, 3)) break; /* Check for a comment. */ - if (CHECK(parser->buffer, '#')) + if (CHECK(parser->input, '#')) break; /* Consume non-blank characters. */ - while (!IS_BLANKZ(parser->buffer)) + while (!IS_BLANKZ(parser->input)) { /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */ if (parser->flow_level - && CHECK(parser->buffer, ':') - && !IS_BLANKZ_AT(parser->buffer, 1)) { - yaml_parser_set_scanner_error(parser, "while scanning a plain scalar", - start_mark, "found unexpected ':'"); + && CHECK(parser->input, ':') + && !IS_BLANKZ_AT(parser->input, 1)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a plain scalar", start_mark, + "found unexpected ':'", parser->mark); goto error; } /* Check for indicators that may end a plain scalar. */ - if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1)) + if ((CHECK(parser->input, ':') && IS_BLANKZ_AT(parser->input, 1)) || (parser->flow_level && - (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':') - || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[') - || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{') - || CHECK(parser->buffer, '}')))) + (CHECK(parser->input, ',') || CHECK(parser->input, ':') + || CHECK(parser->input, '?') || CHECK(parser->input, '[') + || CHECK(parser->input, ']') || CHECK(parser->input, '{') + || CHECK(parser->input, '}')))) break; /* Check if we need to join whitespaces and breaks. */ - if (leading_blanks || whitespaces.start != whitespaces.pointer) + if (leading_blanks || whitespaces.pointer > 0) { if (leading_blanks) { /* Do we need to fold line breaks? */ - if (leading_break.start[0] == '\n') { - if (trailing_breaks.start[0] == '\0') { + if (leading_break.buffer[0] == '\n') { + if (trailing_breaks.buffer[0] == '\0') { if (!STRING_EXTEND(parser, string)) goto error; - *(string.pointer++) = ' '; + JOIN_OCTET(string, ' '); } else { if (!JOIN(parser, string, trailing_breaks)) goto error; @@ -3485,23 +3511,25 @@ yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token) /* Is it the end? */ - if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))) + if (!(IS_BLANK(parser->input) || IS_BREAK(parser->input))) break; /* Consume blank characters. */ if (!CACHE(parser, 1)) goto error; - while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)) + while (IS_BLANK(parser->input) || IS_BREAK(parser->input)) { - if (IS_BLANK(parser->buffer)) + if (IS_BLANK(parser->input)) { /* Check for tab character that abuse intendation. */ if (leading_blanks && (int)parser->mark.column < indent - && IS_TAB(parser->buffer)) { - yaml_parser_set_scanner_error(parser, "while scanning a plain scalar", - start_mark, "found a tab character that violate intendation"); + && IS_TAB(parser->input)) { + SCANNER_ERROR_WITH_CONTEXT_INIT(parser, + "while scanning a plain scalar", start_mark, + "found a tab character that violate intendation", + parser->mark); goto error; } @@ -3542,13 +3570,13 @@ yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token) /* Create a token. */ - SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start, + SCALAR_TOKEN_INIT(*token, string.buffer, string.pointer, YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark); - /* Note that we change the 'simple_key_allowed' flag. */ + /* Note that we change the 'is_simple_key_allowed' flag. */ if (leading_blanks) { - parser->simple_key_allowed = 1; + parser->is_simple_key_allowed = 1; } STRING_DEL(parser, leading_break); diff --git a/src/writer.c b/src/writer.c index 513d9ac..e03b0f6 100644 --- a/src/writer.c +++ b/src/writer.c @@ -5,25 +5,9 @@ * 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. */ @@ -34,31 +18,32 @@ 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->writer); /* 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) { + if (!emitter->output.pointer) { return 1; } + /* Switch the pointer to the beginning of the buffer. */ + + emitter->output.capacity = emitter->output.pointer; + emitter->output.pointer = 0; + /* 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; + if (emitter->writer(emitter->writer_data, + emitter->output.buffer, emitter->output.capacity)) { + emitter->offset += emitter->output.capacity; + emitter->output.capacity = OUTPUT_BUFFER_CAPACITY; return 1; } else { - return yaml_emitter_set_writer_error(emitter, "Write error"); + return WRITER_ERROR_INIT(emitter, "Write error", emitter->offset); } } @@ -67,12 +52,12 @@ yaml_emitter_flush(yaml_emitter_t *emitter) low = (emitter->encoding == YAML_UTF16LE_ENCODING ? 0 : 1); high = (emitter->encoding == YAML_UTF16LE_ENCODING ? 1 : 0); - while (emitter->buffer.pointer != emitter->buffer.last) + while (emitter->output.pointer != emitter->output.capacity) { unsigned char octet; unsigned int width; unsigned int value; - size_t k; + size_t idx; /* * See the "reader.c" code for more details on UTF-8 encoding. Note @@ -81,7 +66,7 @@ yaml_emitter_flush(yaml_emitter_t *emitter) /* Read the next UTF-8 character. */ - octet = emitter->buffer.pointer[0]; + octet = OCTET(emitter->output); width = (octet & 0x80) == 0x00 ? 1 : (octet & 0xE0) == 0xC0 ? 2 : @@ -93,49 +78,48 @@ yaml_emitter_flush(yaml_emitter_t *emitter) (octet & 0xF0) == 0xE0 ? octet & 0x0F : (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; - for (k = 1; k < width; k ++) { - octet = emitter->buffer.pointer[k]; + for (idx = 1; idx < width; idx ++) { + octet = OCTET_AT(emitter->output, idx); value = (value << 6) + (octet & 0x3F); } - emitter->buffer.pointer += width; + emitter->output.pointer += width; /* Write the character. */ if (value < 0x10000) { - emitter->raw_buffer.last[high] = value >> 8; - emitter->raw_buffer.last[low] = value & 0xFF; + OCTET_AT(emitter->raw_output, high) = value >> 8; + OCTET_AT(emitter->raw_output, low) = value & 0xFF; - emitter->raw_buffer.last += 2; + emitter->raw_output.pointer += 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; + OCTET_AT(emitter->raw_output, high) = 0xD8 + (value >> 18); + OCTET_AT(emitter->raw_output, low) = (value >> 10) & 0xFF; + OCTET_AT(emitter->raw_output, high+2) = 0xDC + ((value >> 8) & 0xFF); + OCTET_AT(emitter->raw_output, low+2) = value & 0xFF; - emitter->raw_buffer.last += 4; + emitter->raw_output.pointer += 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; + if (emitter->writer(emitter->writer_data, + emitter->raw_output.buffer, emitter->raw_output.pointer)) { + emitter->output.pointer = 0; + emitter->output.capacity = OUTPUT_BUFFER_CAPACITY; + emitter->offset += emitter->raw_output.pointer; + emitter->raw_output.pointer = 0; return 1; } else { - return yaml_emitter_set_writer_error(emitter, "Write error"); + return WRITER_ERROR_INIT(emitter, "Write error", emitter->offset); } } diff --git a/src/yaml_private.h b/src/yaml_private.h index 61f071e..b2d55a3 100644 --- a/src/yaml_private.h +++ b/src/yaml_private.h @@ -71,7 +71,7 @@ yaml_strdup(const yaml_char_t *); #define DUMPING_ERROR_INIT(error,error_type,error_problem) \ (memset(&(error), 0, sizeof(error)), \ (error).type = (error_type), \ - (error).type.dumping.problem = (error_problem), \ + (error).data.dumping.problem = (error_problem), \ 0) #define MEMORY_ERROR_INIT(self) \ @@ -104,7 +104,7 @@ yaml_strdup(const yaml_char_t *); #define WRITER_ERROR_INIT(self,problem,offset) \ WRITING_ERROR_INIT((self)->error,YAML_WRITER_ERROR,problem,offset) -#define EMITTER_ERROR_INIT(self,context,problem) \ +#define EMITTER_ERROR_INIT(self,problem) \ DUMPING_ERROR_INIT((self)->error,YAML_EMITTER_ERROR,problem) #define SERIALIZER_ERROR_INIT(self,context) \ @@ -148,22 +148,6 @@ yaml_strdup(const yaml_char_t *); #define INITIAL_QUEUE_CAPACITY 16 #define INITIAL_STRING_CAPACITY 16 -/* - * Input/output buffer management. - */ - -#define STORAGE_INIT(self,storage,storage_capacity) \ - (((storage).buffer = yaml_malloc(storage_capacity)) ? \ - ((storage).pointer = (storage).length = 0, \ - (buffer).capacity = (storage_capacity) \ - 1) : \ - ((self)->error.type = YAML_MEMORY_ERROR, \ - 0)) - -#define STORAGE_DEL(self,storage) \ - (yaml_free((storage).buffer), \ - (storage).pointer = (storage).length = (storage).capacity = 0) - /* * String management. */ @@ -182,7 +166,7 @@ yaml_string_join( yaml_char_t **base_buffer, size_t *base_pointer, size_t *base_capacity, yaml_char_t *adj_buffer, size_t adj_pointer, size_t adj_capacity); -#define NULL_STRING { NULL, NULL, NULL } +#define NULL_STRING { NULL, 0, 0 } #define STRING(string,capacity) { (string), 0, (capacity) } @@ -460,6 +444,13 @@ yaml_string_join( #define MOVE(string) ((string).pointer += WIDTH((string))) +/* + * Write a single octet and bump the pointer. + */ + +#define JOIN_OCTET(string,octet) \ + ((string).buffer[(string).pointer++] = (octet)) + /* * Copy a single octet and bump the pointers. */ @@ -863,7 +854,6 @@ struct yaml_parser_s { struct { yaml_char_t *buffer; size_t pointer; - size_t length; size_t capacity; } input; @@ -874,7 +864,6 @@ struct yaml_parser_s { struct { unsigned char *buffer; size_t pointer; - size_t length; size_t capacity; } raw_input; @@ -1068,7 +1057,6 @@ struct yaml_emitter_s { struct { yaml_char_t *buffer; size_t pointer; - size_t length; size_t capacity; } output; @@ -1076,10 +1064,12 @@ struct yaml_emitter_s { struct { yaml_char_t *buffer; size_t pointer; - size_t length; size_t capacity; } raw_output; + /* The offset of the current position (in bytes). */ + size_t offset; + /* The stream encoding. */ yaml_encoding_t encoding; @@ -1150,9 +1140,9 @@ struct yaml_emitter_s { /* The current column. */ int column; /* If the last character was a whitespace? */ - int whitespace; + int is_whitespace; /* If the last character was an indentation character (' ', '-', '?', ':')? */ - int indention; + int is_indention; /* Anchor analysis. */ struct { diff --git a/tests/run-scanner.c b/tests/run-scanner.c index 2c79e7c..2483dd2 100644 --- a/tests/run-scanner.c +++ b/tests/run-scanner.c @@ -21,7 +21,7 @@ main(int argc, char *argv[]) for (number = 1; number < argc; number ++) { FILE *file; - yaml_parser_t parser; + yaml_parser_t *parser; yaml_token_t token; int done = 0; int count = 0; @@ -33,25 +33,25 @@ main(int argc, char *argv[]) file = fopen(argv[number], "rb"); assert(file); - assert(yaml_parser_initialize(&parser)); + assert((parser = yaml_parser_new())); - yaml_parser_set_input_file(&parser, file); + yaml_parser_set_file_reader(parser, file); while (!done) { - if (!yaml_parser_scan(&parser, &token)) { + if (!yaml_parser_scan(parser, &token)) { error = 1; break; } done = (token.type == YAML_STREAM_END_TOKEN); - yaml_token_delete(&token); + yaml_token_destroy(&token); count ++; } - yaml_parser_delete(&parser); + yaml_parser_delete(parser); assert(!fclose(file)); -- cgit v1.2.1