Commits

Kirill Simonov committed 7839bbf

Completed the first phase of API refactoring.

Comments (0)

Files changed (10)

         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;
 {
     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));
     }
         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;
 {
     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));
 
 #include "yaml_private.h"
 
+#if 0
+
 /*
  * API functions.
  */
     return 1;
 }
 
+#endif
+
  */
 
 #define FLUSH(emitter)                                                          \
-    ((emitter->buffer.pointer+5 < emitter->buffer.end)                          \
+    ((emitter->output.pointer+5 < emitter->output.capacity)                     \
      || yaml_emitter_flush(emitter))
 
 /*
 
 #define PUT(emitter,value)                                                      \
     (FLUSH(emitter)                                                             \
-     && (*(emitter->buffer.pointer++) = (yaml_char_t)(value),                   \
+     && (JOIN_OCTET(emitter->output,(yaml_char_t)(value)),                      \
          emitter->column ++,                                                    \
          1))
 
 #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))
 
 #define WRITE(emitter,string)                                                   \
     (FLUSH(emitter)                                                             \
-     && (COPY(emitter->buffer,string),                                          \
+     && (COPY(emitter->output,string),                                          \
          emitter->column ++,                                                    \
          1))
 
          (PUT_BREAK(emitter),                                                   \
           string.pointer ++,                                                    \
           1) :                                                                  \
-         (COPY(emitter->buffer,string),                                         \
+         (COPY(emitter->output,string),                                         \
           emitter->column = 0,                                                  \
           emitter->line ++,                                                     \
           1)))
  */
 
 static int
-yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem);
-
-static int
 yaml_emitter_need_more_events(yaml_emitter_t *emitter);
 
 static int
 
 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);
         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.
  */
 
     }
 
     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));
     }
 {
     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;
     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:
 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;
     }
 
             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:
 
         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))
         return 1;
     }
 
-    return yaml_emitter_set_emitter_error(emitter,
-            "expected STREAM-START");
+    return EMITTER_ERROR_INIT(emitter, "expected STREAM-START");
 }
 
 /*
             {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,
                 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))
                 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))
                 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,
         }
 
         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;
             }
         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");
 }
 
 /*
     {
         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))
         return 1;
     }
 
-    return yaml_emitter_set_emitter_error(emitter,
-            "expected DOCUMENT-END");
+    return EMITTER_ERROR_INIT(emitter, "expected DOCUMENT-END");
 }
 
 /*
     {
         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))
             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;
     }
     {
         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))
         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))
             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;
         }
     if (first)
     {
         if (!yaml_emitter_increase_indent(emitter, 0,
-                    (emitter->mapping_context && !emitter->indention)))
+                    (emitter->is_mapping_context && !emitter->is_indention)))
             return 0;
     }
 
 
 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)
     {
             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");
     }
 
     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;
     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;
     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);
 }
 
 /*
     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);
 }
 
 /*
 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)
             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
     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 *)"!";
         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,
         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,
         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;
     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;
 
 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;
 }
         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;
 }
     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;
     }
     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, ']')
         }
 
         if (!IS_PRINTABLE(string)
-                || (!IS_ASCII(string) && !emitter->unicode)) {
+                || (!IS_ASCII(string) && !emitter->is_unicode)) {
             special_characters = 1;
         }
 
         if (IS_SPACE(string))
         {
             spaces = 1;
-            if (string.start == string.pointer) {
+            if (!string.pointer) {
                 leading = 1;
             }
         }
                 mixed = 1;
             }
             breaks = 1;
-            if (string.start == string.pointer) {
+            if (!string.pointer) {
                 leading = 1;
             }
         }
             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;
 
         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;
                             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;
             }
                             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;
                             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;
 {
     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;
 }
 {
     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;
     }
 
         if (!PUT(emitter, ' ')) return 0;
     }
 
-    emitter->whitespace = 1;
-    emitter->indention = 1;
+    emitter->is_whitespace = 1;
+    emitter->is_indention = 1;
 
     return 1;
 }
 {
     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;
 }
 {
     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;
 }
 {
     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;
 }
 {
     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, ':')
             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)))
         }
     }
 
-    emitter->whitespace = 0;
-    emitter->indention = 0;
+    emitter->is_whitespace = 0;
+    emitter->is_indention = 0;
 
     return 1;
 }
     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))
         {
                 if (!PUT_BREAK(emitter)) return 0;
             }
             if (!WRITE_BREAK(emitter, string)) return 0;
-            emitter->indention = 1;
+            emitter->is_indention = 1;
             breaks = 1;
         }
         else
                 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;
 }
     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);
                 if (!PUT_BREAK(emitter)) return 0;
             }
             if (!WRITE_BREAK(emitter, string)) return 0;
-            emitter->indention = 1;
+            emitter->is_indention = 1;
             breaks = 1;
         }
         else
                 if (!PUT(emitter, '\'')) return 0;
             }
             if (!WRITE(emitter, string)) return 0;
-            emitter->indention = 0;
+            emitter->is_indention = 0;
             spaces = 0;
             breaks = 0;
         }
     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;
 }
     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 :
                     (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;
                         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;
                     }
         {
             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;
     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;
 }
 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;
     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
                 if (!yaml_emitter_write_indent(emitter)) return 0;
             }
             if (!WRITE(emitter, string)) return 0;
-            emitter->indention = 0;
+            emitter->is_indention = 0;
             breaks = 0;
         }
     }
     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
             else {
                 if (!WRITE(emitter, string)) return 0;
             }
-            emitter->indention = 0;
+            emitter->is_indention = 0;
             breaks = 0;
         }
     }
 
 #include "yaml_private.h"
 
+#if 0
+
 /*
  * API functions.
  */
     return 0;
 }
 
+#endif
+
  */
 
 #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 ++)
 
 /*
 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.
  */
 
 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,
 
     /* 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;
     }
 
 }
 
 /*
- * 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.
  */
 
     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 <stream-start>", token->start_mark);
     }
 
     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;
             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;
     }
         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 <document start>", token->start_mark);
             goto error;
         }
         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;
     }
 
 
 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;
 }
 
                 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);
                     }
                 }
                 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;
             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';
                 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);
 
     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);
     }
 
     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);
     }
                 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);
             }
                 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);
             }
 
     value = yaml_malloc(1);
     if (!value) {
-        parser->error = YAML_MEMORY_ERROR;
-        return 0;
+        return MEMORY_ERROR_INIT(parser);
     }
     value[0] = '\0';
 
 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 *)"!"},
     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);
     {
         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;
     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 {
 {
     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);
         }
     }
     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;
     }
 
 {
     /* 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;
         }
 
     /* 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;
     /* 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. */
     /* 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;
     }
 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. */
     /* 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)
 
         /* 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. */
 
 
                     /* 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 :
 
                     /* 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. */
 
 
                     /* 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. */
 
 
                         /* 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. */
 
 
             /* 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;
 }
      (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.
 
 #define READ(parser,string)                                                     \
      (STRING_EXTEND(parser,string) ?                                            \
-         (COPY(string,parser->buffer),                                          \
+         (COPY(string,parser->input),                                           \
           parser->mark.index ++,                                                \
           parser->mark.column ++,                                               \
           parser->unread --,                                                    \
 
 #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 ++,                                                     \
 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.
  */
 
 
     /* 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;
     }
     /* 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.
  */
         }
         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;
             return 0;
     }
 
-    parser->token_available = 1;
+    parser->is_token_available = 1;
 
     return 1;
 }
 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))
 
     /* 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. */
 
     /* 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? */