6 * The following notes assume that you are familiar with the YAML specification
7 * (http://yaml.org/spec/cvs/current.html). We mostly follow it, although in
8 * some cases we are less restrictive that it requires.
10 * The process of transforming a YAML stream into a sequence of events is
11 * divided on two steps: Scanning and Parsing.
13 * The Scanner transforms the input stream into a sequence of tokens, while the
14 * parser transform the sequence of tokens produced by the Scanner into a
15 * sequence of parsing events.
17 * The Scanner is rather clever and complicated. The Parser, on the contrary,
18 * is a straightforward implementation of a recursive-descendant parser (or,
19 * LL(1) parser, as it is usually called).
21 * Actually there are two issues of Scanning that might be called "clever", the
22 * rest is quite straightforward. The issues are "block collection start" and
23 * "simple keys". Both issues are explained below in details.
25 * Here the Scanning step is explained and implemented. We start with the list
26 * of all the tokens produced by the Scanner together with short descriptions.
30 * STREAM-START(encoding) # The stream start.
31 * STREAM-END # The stream end.
32 * VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
33 * TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
34 * DOCUMENT-START # '---'
35 * DOCUMENT-END # '...'
36 * BLOCK-SEQUENCE-START # Indentation increase denoting a block
37 * BLOCK-MAPPING-START # sequence or a block mapping.
38 * BLOCK-END # Indentation decrease.
39 * FLOW-SEQUENCE-START # '['
40 * FLOW-SEQUENCE-END # ']'
41 * BLOCK-SEQUENCE-START # '{'
42 * BLOCK-SEQUENCE-END # '}'
45 * KEY # '?' or nothing (simple keys).
47 * ALIAS(anchor) # '*anchor'
48 * ANCHOR(anchor) # '&anchor'
49 * TAG(handle,suffix) # '!handle!suffix'
50 * SCALAR(value,style) # A scalar.
52 * The following two tokens are "virtual" tokens denoting the beginning and the
55 * STREAM-START(encoding)
58 * We pass the information about the input stream encoding with the
61 * The next two tokens are responsible for tags:
63 * VERSION-DIRECTIVE(major,minor)
64 * TAG-DIRECTIVE(handle,prefix)
70 * %TAG !yaml! tag:yaml.org,2002:
73 * The correspoding sequence of tokens:
76 * VERSION-DIRECTIVE(1,1)
77 * TAG-DIRECTIVE("!","!foo")
78 * TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
82 * Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
85 * The document start and end indicators are represented by:
90 * Note that if a YAML stream contains an implicit document (without '---'
91 * and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
94 * In the following examples, we present whole documents together with the
97 * 1. An implicit document:
103 * STREAM-START(utf-8)
104 * SCALAR("a scalar",single-quoted)
107 * 2. An explicit document:
115 * STREAM-START(utf-8)
117 * SCALAR("a scalar",single-quoted)
121 * 3. Several documents in a stream:
127 * 'yet another scalar'
131 * STREAM-START(utf-8)
132 * SCALAR("a scalar",single-quoted)
134 * SCALAR("another scalar",single-quoted)
136 * SCALAR("yet another scalar",single-quoted)
139 * We have already introduced the SCALAR token above. The following tokens are
140 * used to describe aliases, anchors, tag, and scalars:
145 * SCALAR(value,style)
147 * The following series of examples illustrate the usage of these tokens:
149 * 1. A recursive sequence:
155 * STREAM-START(utf-8)
157 * FLOW-SEQUENCE-START
162 * 2. A tagged scalar:
164 * !!float "3.14" # A good approximation.
168 * STREAM-START(utf-8)
170 * SCALAR("3.14",double-quoted)
173 * 3. Various scalar styles:
175 * --- # Implicit empty plain scalars do not produce tokens.
177 * --- 'a single-quoted scalar'
178 * --- "a double-quoted scalar"
187 * STREAM-START(utf-8)
190 * SCALAR("a plain scalar",plain)
192 * SCALAR("a single-quoted scalar",single-quoted)
194 * SCALAR("a double-quoted scalar",double-quoted)
196 * SCALAR("a literal scalar",literal)
198 * SCALAR("a folded scalar",folded)
201 * Now it's time to review collection-related tokens. We will start with
204 * FLOW-SEQUENCE-START
212 * The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
213 * FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
214 * correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
215 * indicators '?' and ':', which are used for denoting mapping keys and values,
216 * are represented by the KEY and VALUE tokens.
218 * The following examples show flow collections:
220 * 1. A flow sequence:
222 * [item 1, item 2, item 3]
226 * STREAM-START(utf-8)
227 * FLOW-SEQUENCE-START
228 * SCALAR("item 1",plain)
230 * SCALAR("item 2",plain)
232 * SCALAR("item 3",plain)
239 * a simple key: a value, # Note that the KEY token is produced.
240 * ? a complex key: another value,
245 * STREAM-START(utf-8)
248 * SCALAR("a simple key",plain)
250 * SCALAR("a value",plain)
253 * SCALAR("a complex key",plain)
255 * SCALAR("another value",plain)
260 * A simple key is a key which is not denoted by the '?' indicator. Note that
261 * the Scanner still produce the KEY token whenever it encounters a simple key.
263 * For scanning block collections, the following tokens are used (note that we
264 * repeat KEY and VALUE here):
266 * BLOCK-SEQUENCE-START
267 * BLOCK-MAPPING-START
273 * The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
274 * increase that precedes a block collection (cf. the INDENT token in Python).
275 * The token BLOCK-END denote indentation decrease that ends a block collection
276 * (cf. the DEDENT token in Python). However YAML has some syntax pecularities
277 * that makes detections of these tokens more complex.
279 * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
280 * '-', '?', and ':' correspondingly.
282 * The following examples show how the tokens BLOCK-SEQUENCE-START,
283 * BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
285 * 1. Block sequences:
298 * STREAM-START(utf-8)
299 * BLOCK-SEQUENCE-START
301 * SCALAR("item 1",plain)
303 * SCALAR("item 2",plain)
305 * BLOCK-SEQUENCE-START
307 * SCALAR("item 3.1",plain)
309 * SCALAR("item 3.2",plain)
312 * BLOCK-MAPPING-START
314 * SCALAR("key 1",plain)
316 * SCALAR("value 1",plain)
318 * SCALAR("key 2",plain)
320 * SCALAR("value 2",plain)
327 * a simple key: a value # The KEY token is produced here.
339 * STREAM-START(utf-8)
340 * BLOCK-MAPPING-START
342 * SCALAR("a simple key",plain)
344 * SCALAR("a value",plain)
346 * SCALAR("a complex key",plain)
348 * SCALAR("another value",plain)
350 * SCALAR("a mapping",plain)
351 * BLOCK-MAPPING-START
353 * SCALAR("key 1",plain)
355 * SCALAR("value 1",plain)
357 * SCALAR("key 2",plain)
359 * SCALAR("value 2",plain)
362 * SCALAR("a sequence",plain)
364 * BLOCK-SEQUENCE-START
366 * SCALAR("item 1",plain)
368 * SCALAR("item 2",plain)
373 * YAML does not always require to start a new block collection from a new
374 * line. If the current line contains only '-', '?', and ':' indicators, a new
375 * block collection may start at the current line. The following examples
376 * illustrate this case:
378 * 1. Collections in a sequence:
389 * STREAM-START(utf-8)
390 * BLOCK-SEQUENCE-START
392 * BLOCK-SEQUENCE-START
394 * SCALAR("item 1",plain)
396 * SCALAR("item 2",plain)
399 * BLOCK-MAPPING-START
401 * SCALAR("key 1",plain)
403 * SCALAR("value 1",plain)
405 * SCALAR("key 2",plain)
407 * SCALAR("value 2",plain)
410 * BLOCK-MAPPING-START
412 * SCALAR("complex key")
414 * SCALAR("complex value")
419 * 2. Collections in a mapping:
430 * STREAM-START(utf-8)
431 * BLOCK-MAPPING-START
433 * SCALAR("a sequence",plain)
435 * BLOCK-SEQUENCE-START
437 * SCALAR("item 1",plain)
439 * SCALAR("item 2",plain)
442 * SCALAR("a mapping",plain)
444 * BLOCK-MAPPING-START
446 * SCALAR("key 1",plain)
448 * SCALAR("value 1",plain)
450 * SCALAR("key 2",plain)
452 * SCALAR("value 2",plain)
457 * YAML also permits non-indented sequences if they are included into a block
458 * mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
461 * - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
466 * STREAM-START(utf-8)
467 * BLOCK-MAPPING-START
469 * SCALAR("key",plain)
472 * SCALAR("item 1",plain)
474 * SCALAR("item 2",plain)
478 #include "yaml_private.h"
481 * Ensure that the buffer contains the required number of characters.
482 * Return 1 on success, 0 on failure (reader error or memory error).
485 #define CACHE(parser,length) \
486 (parser->unread >= (length) \
488 : yaml_parser_update_buffer(parser, (length)))
491 * Advance the buffer pointer.
494 #define SKIP(parser) \
495 (parser->mark.index ++, \
496 parser->mark.column ++, \
498 parser->buffer.pointer += WIDTH(parser->buffer))
500 #define SKIP_LINE(parser) \
501 (IS_CRLF(parser->buffer) ? \
502 (parser->mark.index += 2, \
503 parser->mark.column = 0, \
504 parser->mark.line ++, \
505 parser->unread -= 2, \
506 parser->buffer.pointer += 2) : \
507 IS_BREAK(parser->buffer) ? \
508 (parser->mark.index ++, \
509 parser->mark.column = 0, \
510 parser->mark.line ++, \
512 parser->buffer.pointer += WIDTH(parser->buffer)) : 0)
515 * Copy a character to a string buffer and advance pointers.
518 #define READ(parser,string) \
519 (STRING_EXTEND(parser,string) ? \
520 (COPY(string,parser->buffer), \
521 parser->mark.index ++, \
522 parser->mark.column ++, \
527 * Copy a line break character to a string buffer and advance pointers.
530 #define READ_LINE(parser,string) \
531 (STRING_EXTEND(parser,string) ? \
532 (((CHECK_AT(parser->buffer,'\r',0) \
533 && CHECK_AT(parser->buffer,'\n',1)) ? /* CR LF -> LF */ \
534 (*((string).pointer++) = (yaml_char_t) '\n', \
535 parser->buffer.pointer += 2, \
536 parser->mark.index += 2, \
537 parser->mark.column = 0, \
538 parser->mark.line ++, \
539 parser->unread -= 2) : \
540 (CHECK_AT(parser->buffer,'\r',0) \
541 || CHECK_AT(parser->buffer,'\n',0)) ? /* CR|LF -> LF */ \
542 (*((string).pointer++) = (yaml_char_t) '\n', \
543 parser->buffer.pointer ++, \
544 parser->mark.index ++, \
545 parser->mark.column = 0, \
546 parser->mark.line ++, \
547 parser->unread --) : \
548 (CHECK_AT(parser->buffer,'\xC2',0) \
549 && CHECK_AT(parser->buffer,'\x85',1)) ? /* NEL -> LF */ \
550 (*((string).pointer++) = (yaml_char_t) '\n', \
551 parser->buffer.pointer += 2, \
552 parser->mark.index ++, \
553 parser->mark.column = 0, \
554 parser->mark.line ++, \
555 parser->unread --) : \
556 (CHECK_AT(parser->buffer,'\xE2',0) && \
557 CHECK_AT(parser->buffer,'\x80',1) && \
558 (CHECK_AT(parser->buffer,'\xA8',2) || \
559 CHECK_AT(parser->buffer,'\xA9',2))) ? /* LS|PS -> LS|PS */ \
560 (*((string).pointer++) = *(parser->buffer.pointer++), \
561 *((string).pointer++) = *(parser->buffer.pointer++), \
562 *((string).pointer++) = *(parser->buffer.pointer++), \
563 parser->mark.index ++, \
564 parser->mark.column = 0, \
565 parser->mark.line ++, \
566 parser->unread --) : 0), \
570 * Public API declarations.
574 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
581 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
582 yaml_mark_t context_mark, const char *problem);
585 * High-level token API.
589 yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
592 yaml_parser_fetch_next_token(yaml_parser_t *parser);
595 * Potential simple keys.
599 yaml_parser_stale_simple_keys(yaml_parser_t *parser);
602 yaml_parser_save_simple_key(yaml_parser_t *parser);
605 yaml_parser_remove_simple_key(yaml_parser_t *parser);
608 yaml_parser_increase_flow_level(yaml_parser_t *parser);
611 yaml_parser_decrease_flow_level(yaml_parser_t *parser);
614 * Indentation treatment.
618 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
619 int number, yaml_token_type_t type, yaml_mark_t mark);
622 yaml_parser_unroll_indent(yaml_parser_t *parser, int column);
629 yaml_parser_fetch_stream_start(yaml_parser_t *parser);
632 yaml_parser_fetch_stream_end(yaml_parser_t *parser);
635 yaml_parser_fetch_directive(yaml_parser_t *parser);
638 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
639 yaml_token_type_t type);
642 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
643 yaml_token_type_t type);
646 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
647 yaml_token_type_t type);
650 yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
653 yaml_parser_fetch_block_entry(yaml_parser_t *parser);
656 yaml_parser_fetch_key(yaml_parser_t *parser);
659 yaml_parser_fetch_value(yaml_parser_t *parser);
662 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
665 yaml_parser_fetch_tag(yaml_parser_t *parser);
668 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
671 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
674 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
681 yaml_parser_scan_to_next_token(yaml_parser_t *parser);
684 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
687 yaml_parser_scan_directive_name(yaml_parser_t *parser,
688 yaml_mark_t start_mark, yaml_char_t **name);
691 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
692 yaml_mark_t start_mark, int *major, int *minor);
695 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
696 yaml_mark_t start_mark, int *number);
699 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
700 yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
703 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
704 yaml_token_type_t type);
707 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
710 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
711 yaml_mark_t start_mark, yaml_char_t **handle);
714 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
715 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
718 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
719 yaml_mark_t start_mark, yaml_string_t *string);
722 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
726 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
727 int *indent, yaml_string_t *breaks,
728 yaml_mark_t start_mark, yaml_mark_t *end_mark);
731 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
735 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
738 * Get the next token.
742 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
744 assert(parser); /* Non-NULL parser object is expected. */
745 assert(token); /* Non-NULL token object is expected. */
747 /* Erase the token object. */
749 memset(token, 0, sizeof(yaml_token_t));
751 /* No tokens after STREAM-END or error. */
753 if (parser->stream_end_produced || parser->error) {
757 /* Ensure that the tokens queue contains enough tokens. */
759 if (!parser->token_available) {
760 if (!yaml_parser_fetch_more_tokens(parser))
764 /* Fetch the next token from the queue. */
766 *token = DEQUEUE(parser, parser->tokens);
767 parser->token_available = 0;
768 parser->tokens_parsed ++;
770 if (token->type == YAML_STREAM_END_TOKEN) {
771 parser->stream_end_produced = 1;
778 * Set the scanner error and return 0.
782 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
783 yaml_mark_t context_mark, const char *problem)
785 parser->error = YAML_SCANNER_ERROR;
786 parser->context = context;
787 parser->context_mark = context_mark;
788 parser->problem = problem;
789 parser->problem_mark = parser->mark;
795 * Ensure that the tokens queue contains at least one token which can be
796 * returned to the Parser.
800 yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
802 int need_more_tokens;
804 /* While we need more tokens to fetch, do it. */
809 * Check if we really need to fetch more tokens.
812 need_more_tokens = 0;
814 if (parser->tokens.head == parser->tokens.tail)
816 /* Queue is empty. */
818 need_more_tokens = 1;
822 yaml_simple_key_t *simple_key;
824 /* Check if any potential simple key may occupy the head position. */
826 if (!yaml_parser_stale_simple_keys(parser))
829 for (simple_key = parser->simple_keys.start;
830 simple_key != parser->simple_keys.top; simple_key++) {
831 if (simple_key->possible
832 && simple_key->token_number == parser->tokens_parsed) {
833 need_more_tokens = 1;
839 /* We are finished. */
841 if (!need_more_tokens)
844 /* Fetch the next token. */
846 if (!yaml_parser_fetch_next_token(parser))
850 parser->token_available = 1;
856 * The dispatcher for token fetchers.
860 yaml_parser_fetch_next_token(yaml_parser_t *parser)
862 /* Ensure that the buffer is initialized. */
864 if (!CACHE(parser, 1))
867 /* Check if we just started scanning. Fetch STREAM-START then. */
869 if (!parser->stream_start_produced)
870 return yaml_parser_fetch_stream_start(parser);
872 /* Eat whitespaces and comments until we reach the next token. */
874 if (!yaml_parser_scan_to_next_token(parser))
877 /* Remove obsolete potential simple keys. */
879 if (!yaml_parser_stale_simple_keys(parser))
882 /* Check the indentation level against the current column. */
884 if (!yaml_parser_unroll_indent(parser, parser->mark.column))
888 * Ensure that the buffer contains at least 4 characters. 4 is the length
889 * of the longest indicators ('--- ' and '... ').
892 if (!CACHE(parser, 4))
895 /* Is it the end of the stream? */
897 if (IS_Z(parser->buffer))
898 return yaml_parser_fetch_stream_end(parser);
900 /* Is it a directive? */
902 if (parser->mark.column == 0 && CHECK(parser->buffer, '%'))
903 return yaml_parser_fetch_directive(parser);
905 /* Is it the document start indicator? */
907 if (parser->mark.column == 0
908 && CHECK_AT(parser->buffer, '-', 0)
909 && CHECK_AT(parser->buffer, '-', 1)
910 && CHECK_AT(parser->buffer, '-', 2)
911 && IS_BLANKZ_AT(parser->buffer, 3))
912 return yaml_parser_fetch_document_indicator(parser,
913 YAML_DOCUMENT_START_TOKEN);
915 /* Is it the document end indicator? */
917 if (parser->mark.column == 0
918 && CHECK_AT(parser->buffer, '.', 0)
919 && CHECK_AT(parser->buffer, '.', 1)
920 && CHECK_AT(parser->buffer, '.', 2)
921 && IS_BLANKZ_AT(parser->buffer, 3))
922 return yaml_parser_fetch_document_indicator(parser,
923 YAML_DOCUMENT_END_TOKEN);
925 /* Is it the flow sequence start indicator? */
927 if (CHECK(parser->buffer, '['))
928 return yaml_parser_fetch_flow_collection_start(parser,
929 YAML_FLOW_SEQUENCE_START_TOKEN);
931 /* Is it the flow mapping start indicator? */
933 if (CHECK(parser->buffer, '{'))
934 return yaml_parser_fetch_flow_collection_start(parser,
935 YAML_FLOW_MAPPING_START_TOKEN);
937 /* Is it the flow sequence end indicator? */
939 if (CHECK(parser->buffer, ']'))
940 return yaml_parser_fetch_flow_collection_end(parser,
941 YAML_FLOW_SEQUENCE_END_TOKEN);
943 /* Is it the flow mapping end indicator? */
945 if (CHECK(parser->buffer, '}'))
946 return yaml_parser_fetch_flow_collection_end(parser,
947 YAML_FLOW_MAPPING_END_TOKEN);
949 /* Is it the flow entry indicator? */
951 if (CHECK(parser->buffer, ','))
952 return yaml_parser_fetch_flow_entry(parser);
954 /* Is it the block entry indicator? */
956 if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1))
957 return yaml_parser_fetch_block_entry(parser);
959 /* Is it the key indicator? */
961 if (CHECK(parser->buffer, '?')
962 && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
963 return yaml_parser_fetch_key(parser);
965 /* Is it the value indicator? */
967 if (CHECK(parser->buffer, ':')
968 && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
969 return yaml_parser_fetch_value(parser);
971 /* Is it an alias? */
973 if (CHECK(parser->buffer, '*'))
974 return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
976 /* Is it an anchor? */
978 if (CHECK(parser->buffer, '&'))
979 return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
983 if (CHECK(parser->buffer, '!'))
984 return yaml_parser_fetch_tag(parser);
986 /* Is it a literal scalar? */
988 if (CHECK(parser->buffer, '|') && !parser->flow_level)
989 return yaml_parser_fetch_block_scalar(parser, 1);
991 /* Is it a folded scalar? */
993 if (CHECK(parser->buffer, '>') && !parser->flow_level)
994 return yaml_parser_fetch_block_scalar(parser, 0);
996 /* Is it a single-quoted scalar? */
998 if (CHECK(parser->buffer, '\''))
999 return yaml_parser_fetch_flow_scalar(parser, 1);
1001 /* Is it a double-quoted scalar? */
1003 if (CHECK(parser->buffer, '"'))
1004 return yaml_parser_fetch_flow_scalar(parser, 0);
1007 * Is it a plain scalar?
1009 * A plain scalar may start with any non-blank characters except
1011 * '-', '?', ':', ',', '[', ']', '{', '}',
1012 * '#', '&', '*', '!', '|', '>', '\'', '\"',
1015 * In the block context (and, for the '-' indicator, in the flow context
1016 * too), it may also start with the characters
1020 * if it is followed by a non-space character.
1022 * The last rule is more restrictive than the specification requires.
1025 if (!(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '-')
1026 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':')
1027 || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '[')
1028 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
1029 || CHECK(parser->buffer, '}') || CHECK(parser->buffer, '#')
1030 || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '*')
1031 || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '|')
1032 || CHECK(parser->buffer, '>') || CHECK(parser->buffer, '\'')
1033 || CHECK(parser->buffer, '"') || CHECK(parser->buffer, '%')
1034 || CHECK(parser->buffer, '@') || CHECK(parser->buffer, '`')) ||
1035 (CHECK(parser->buffer, '-') && !IS_BLANK_AT(parser->buffer, 1)) ||
1036 (!parser->flow_level &&
1037 (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':'))
1038 && !IS_BLANKZ_AT(parser->buffer, 1)))
1039 return yaml_parser_fetch_plain_scalar(parser);
1042 * If we don't determine the token type so far, it is an error.
1045 return yaml_parser_set_scanner_error(parser,
1046 "while scanning for the next token", parser->mark,
1047 "found character that cannot start any token");
1051 * Check the list of potential simple keys and remove the positions that
1052 * cannot contain simple keys anymore.
1056 yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1058 yaml_simple_key_t *simple_key;
1060 /* Check for a potential simple key for each flow level. */
1062 for (simple_key = parser->simple_keys.start;
1063 simple_key != parser->simple_keys.top; simple_key ++)
1066 * The specification requires that a simple key
1068 * - is limited to a single line,
1069 * - is shorter than 1024 characters.
1072 if (simple_key->possible
1073 && (simple_key->mark.line < parser->mark.line
1074 || simple_key->mark.index+1024 < parser->mark.index)) {
1076 /* Check if the potential simple key to be removed is required. */
1078 if (simple_key->required) {
1079 return yaml_parser_set_scanner_error(parser,
1080 "while scanning a simple key", simple_key->mark,
1081 "could not find expected ':'");
1084 simple_key->possible = 0;
1092 * Check if a simple key may start at the current position and add it if
1097 yaml_parser_save_simple_key(yaml_parser_t *parser)
1100 * A simple key is required at the current position if the scanner is in
1101 * the block context and the current column coincides with the indentation
1105 int required = (!parser->flow_level
1106 && parser->indent == (int)parser->mark.column);
1109 * A simple key is required only when it is the first token in the current
1110 * line. Therefore it is always allowed. But we add a check anyway.
1113 assert(parser->simple_key_allowed || !required); /* Impossible. */
1116 * If the current position may start a simple key, save it.
1119 if (parser->simple_key_allowed)
1121 yaml_simple_key_t simple_key;
1122 simple_key.possible = 1;
1123 simple_key.required = required;
1124 simple_key.token_number =
1125 parser->tokens_parsed + (parser->tokens.tail - parser->tokens.head);
1126 simple_key.mark = parser->mark;
1128 if (!yaml_parser_remove_simple_key(parser)) return 0;
1130 *(parser->simple_keys.top-1) = simple_key;
1137 * Remove a potential simple key at the current flow level.
1141 yaml_parser_remove_simple_key(yaml_parser_t *parser)
1143 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1145 if (simple_key->possible)
1147 /* If the key is required, it is an error. */
1149 if (simple_key->required) {
1150 return yaml_parser_set_scanner_error(parser,
1151 "while scanning a simple key", simple_key->mark,
1152 "could not find expected ':'");
1156 /* Remove the key from the stack. */
1158 simple_key->possible = 0;
1164 * Increase the flow level and resize the simple key list if needed.
1168 yaml_parser_increase_flow_level(yaml_parser_t *parser)
1170 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1172 /* Reset the simple key on the next level. */
1174 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1177 /* Increase the flow level. */
1179 parser->flow_level++;
1185 * Decrease the flow level.
1189 yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1191 yaml_simple_key_t dummy_key; /* Used to eliminate a compiler warning. */
1193 if (parser->flow_level) {
1194 parser->flow_level --;
1195 dummy_key = POP(parser, parser->simple_keys);
1202 * Push the current indentation level to the stack and set the new level
1203 * the current column is greater than the indentation level. In this case,
1204 * append or insert the specified token into the token queue.
1209 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1210 int number, yaml_token_type_t type, yaml_mark_t mark)
1214 /* In the flow context, do nothing. */
1216 if (parser->flow_level)
1219 if (parser->indent < column)
1222 * Push the current indentation level to the stack and set the new
1223 * indentation level.
1226 if (!PUSH(parser, parser->indents, parser->indent))
1229 parser->indent = column;
1231 /* Create a token and insert it into the queue. */
1233 TOKEN_INIT(token, type, mark, mark);
1236 if (!ENQUEUE(parser, parser->tokens, token))
1240 if (!QUEUE_INSERT(parser,
1241 parser->tokens, number - parser->tokens_parsed, token))
1250 * Pop indentation levels from the indents stack until the current level
1251 * becomes less or equal to the column. For each intendation level, append
1252 * the BLOCK-END token.
1257 yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1261 /* In the flow context, do nothing. */
1263 if (parser->flow_level)
1266 /* Loop through the intendation levels in the stack. */
1268 while (parser->indent > column)
1270 /* Create a token and append it to the queue. */
1272 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1274 if (!ENQUEUE(parser, parser->tokens, token))
1277 /* Pop the indentation level. */
1279 parser->indent = POP(parser, parser->indents);
1286 * Initialize the scanner and produce the STREAM-START token.
1290 yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1292 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1295 /* Set the initial indentation. */
1297 parser->indent = -1;
1299 /* Initialize the simple key stack. */
1301 if (!PUSH(parser, parser->simple_keys, simple_key))
1304 /* A simple key is allowed at the beginning of the stream. */
1306 parser->simple_key_allowed = 1;
1308 /* We have started. */
1310 parser->stream_start_produced = 1;
1312 /* Create the STREAM-START token and append it to the queue. */
1314 STREAM_START_TOKEN_INIT(token, parser->encoding,
1315 parser->mark, parser->mark);
1317 if (!ENQUEUE(parser, parser->tokens, token))
1324 * Produce the STREAM-END token and shut down the scanner.
1328 yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1332 /* Force new line. */
1334 if (parser->mark.column != 0) {
1335 parser->mark.column = 0;
1336 parser->mark.line ++;
1339 /* Reset the indentation level. */
1341 if (!yaml_parser_unroll_indent(parser, -1))
1344 /* Reset simple keys. */
1346 if (!yaml_parser_remove_simple_key(parser))
1349 parser->simple_key_allowed = 0;
1351 /* Create the STREAM-END token and append it to the queue. */
1353 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1355 if (!ENQUEUE(parser, parser->tokens, token))
1362 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1366 yaml_parser_fetch_directive(yaml_parser_t *parser)
1370 /* Reset the indentation level. */
1372 if (!yaml_parser_unroll_indent(parser, -1))
1375 /* Reset simple keys. */
1377 if (!yaml_parser_remove_simple_key(parser))
1380 parser->simple_key_allowed = 0;
1382 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1384 if (!yaml_parser_scan_directive(parser, &token))
1387 /* Append the token to the queue. */
1389 if (!ENQUEUE(parser, parser->tokens, token)) {
1390 yaml_token_delete(&token);
1398 * Produce the DOCUMENT-START or DOCUMENT-END token.
1402 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1403 yaml_token_type_t type)
1405 yaml_mark_t start_mark, end_mark;
1408 /* Reset the indentation level. */
1410 if (!yaml_parser_unroll_indent(parser, -1))
1413 /* Reset simple keys. */
1415 if (!yaml_parser_remove_simple_key(parser))
1418 parser->simple_key_allowed = 0;
1420 /* Consume the token. */
1422 start_mark = parser->mark;
1428 end_mark = parser->mark;
1430 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1432 TOKEN_INIT(token, type, start_mark, end_mark);
1434 /* Append the token to the queue. */
1436 if (!ENQUEUE(parser, parser->tokens, token))
1443 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1447 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1448 yaml_token_type_t type)
1450 yaml_mark_t start_mark, end_mark;
1453 /* The indicators '[' and '{' may start a simple key. */
1455 if (!yaml_parser_save_simple_key(parser))
1458 /* Increase the flow level. */
1460 if (!yaml_parser_increase_flow_level(parser))
1463 /* A simple key may follow the indicators '[' and '{'. */
1465 parser->simple_key_allowed = 1;
1467 /* Consume the token. */
1469 start_mark = parser->mark;
1471 end_mark = parser->mark;
1473 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1475 TOKEN_INIT(token, type, start_mark, end_mark);
1477 /* Append the token to the queue. */
1479 if (!ENQUEUE(parser, parser->tokens, token))
1486 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1490 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1491 yaml_token_type_t type)
1493 yaml_mark_t start_mark, end_mark;
1496 /* Reset any potential simple key on the current flow level. */
1498 if (!yaml_parser_remove_simple_key(parser))
1501 /* Decrease the flow level. */
1503 if (!yaml_parser_decrease_flow_level(parser))
1506 /* No simple keys after the indicators ']' and '}'. */
1508 parser->simple_key_allowed = 0;
1510 /* Consume the token. */
1512 start_mark = parser->mark;
1514 end_mark = parser->mark;
1516 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1518 TOKEN_INIT(token, type, start_mark, end_mark);
1520 /* Append the token to the queue. */
1522 if (!ENQUEUE(parser, parser->tokens, token))
1529 * Produce the FLOW-ENTRY token.
1533 yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1535 yaml_mark_t start_mark, end_mark;
1538 /* Reset any potential simple keys on the current flow level. */
1540 if (!yaml_parser_remove_simple_key(parser))
1543 /* Simple keys are allowed after ','. */
1545 parser->simple_key_allowed = 1;
1547 /* Consume the token. */
1549 start_mark = parser->mark;
1551 end_mark = parser->mark;
1553 /* Create the FLOW-ENTRY token and append it to the queue. */
1555 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1557 if (!ENQUEUE(parser, parser->tokens, token))
1564 * Produce the BLOCK-ENTRY token.
1568 yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1570 yaml_mark_t start_mark, end_mark;
1573 /* Check if the scanner is in the block context. */
1575 if (!parser->flow_level)
1577 /* Check if we are allowed to start a new entry. */
1579 if (!parser->simple_key_allowed) {
1580 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1581 "block sequence entries are not allowed in this context");
1584 /* Add the BLOCK-SEQUENCE-START token if needed. */
1586 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1587 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1593 * It is an error for the '-' indicator to occur in the flow context,
1594 * but we let the Parser detect and report about it because the Parser
1595 * is able to point to the context.
1599 /* Reset any potential simple keys on the current flow level. */
1601 if (!yaml_parser_remove_simple_key(parser))
1604 /* Simple keys are allowed after '-'. */
1606 parser->simple_key_allowed = 1;
1608 /* Consume the token. */
1610 start_mark = parser->mark;
1612 end_mark = parser->mark;
1614 /* Create the BLOCK-ENTRY token and append it to the queue. */
1616 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1618 if (!ENQUEUE(parser, parser->tokens, token))
1625 * Produce the KEY token.
1629 yaml_parser_fetch_key(yaml_parser_t *parser)
1631 yaml_mark_t start_mark, end_mark;
1634 /* In the block context, additional checks are required. */
1636 if (!parser->flow_level)
1638 /* Check if we are allowed to start a new key (not nessesary simple). */
1640 if (!parser->simple_key_allowed) {
1641 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1642 "mapping keys are not allowed in this context");
1645 /* Add the BLOCK-MAPPING-START token if needed. */
1647 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1648 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1652 /* Reset any potential simple keys on the current flow level. */
1654 if (!yaml_parser_remove_simple_key(parser))
1657 /* Simple keys are allowed after '?' in the block context. */
1659 parser->simple_key_allowed = (!parser->flow_level);
1661 /* Consume the token. */
1663 start_mark = parser->mark;
1665 end_mark = parser->mark;
1667 /* Create the KEY token and append it to the queue. */
1669 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1671 if (!ENQUEUE(parser, parser->tokens, token))
1678 * Produce the VALUE token.
1682 yaml_parser_fetch_value(yaml_parser_t *parser)
1684 yaml_mark_t start_mark, end_mark;
1686 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1688 /* Have we found a simple key? */
1690 if (simple_key->possible)
1693 /* Create the KEY token and insert it into the queue. */
1695 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1697 if (!QUEUE_INSERT(parser, parser->tokens,
1698 simple_key->token_number - parser->tokens_parsed, token))
1701 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1703 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1704 simple_key->token_number,
1705 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1708 /* Remove the simple key. */
1710 simple_key->possible = 0;
1712 /* A simple key cannot follow another simple key. */
1714 parser->simple_key_allowed = 0;
1718 /* The ':' indicator follows a complex key. */
1720 /* In the block context, extra checks are required. */
1722 if (!parser->flow_level)
1724 /* Check if we are allowed to start a complex value. */
1726 if (!parser->simple_key_allowed) {
1727 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1728 "mapping values are not allowed in this context");
1731 /* Add the BLOCK-MAPPING-START token if needed. */
1733 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1734 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1738 /* Simple keys after ':' are allowed in the block context. */
1740 parser->simple_key_allowed = (!parser->flow_level);
1743 /* Consume the token. */
1745 start_mark = parser->mark;
1747 end_mark = parser->mark;
1749 /* Create the VALUE token and append it to the queue. */
1751 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1753 if (!ENQUEUE(parser, parser->tokens, token))
1760 * Produce the ALIAS or ANCHOR token.
1764 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1768 /* An anchor or an alias could be a simple key. */
1770 if (!yaml_parser_save_simple_key(parser))
1773 /* A simple key cannot follow an anchor or an alias. */
1775 parser->simple_key_allowed = 0;
1777 /* Create the ALIAS or ANCHOR token and append it to the queue. */
1779 if (!yaml_parser_scan_anchor(parser, &token, type))
1782 if (!ENQUEUE(parser, parser->tokens, token)) {
1783 yaml_token_delete(&token);
1790 * Produce the TAG token.
1794 yaml_parser_fetch_tag(yaml_parser_t *parser)
1798 /* A tag could be a simple key. */
1800 if (!yaml_parser_save_simple_key(parser))
1803 /* A simple key cannot follow a tag. */
1805 parser->simple_key_allowed = 0;
1807 /* Create the TAG token and append it to the queue. */
1809 if (!yaml_parser_scan_tag(parser, &token))
1812 if (!ENQUEUE(parser, parser->tokens, token)) {
1813 yaml_token_delete(&token);
1821 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1825 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1829 /* Remove any potential simple keys. */
1831 if (!yaml_parser_remove_simple_key(parser))
1834 /* A simple key may follow a block scalar. */
1836 parser->simple_key_allowed = 1;
1838 /* Create the SCALAR token and append it to the queue. */
1840 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1843 if (!ENQUEUE(parser, parser->tokens, token)) {
1844 yaml_token_delete(&token);
1852 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1856 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1860 /* A plain scalar could be a simple key. */
1862 if (!yaml_parser_save_simple_key(parser))
1865 /* A simple key cannot follow a flow scalar. */
1867 parser->simple_key_allowed = 0;
1869 /* Create the SCALAR token and append it to the queue. */
1871 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1874 if (!ENQUEUE(parser, parser->tokens, token)) {
1875 yaml_token_delete(&token);
1883 * Produce the SCALAR(...,plain) token.
1887 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1891 /* A plain scalar could be a simple key. */
1893 if (!yaml_parser_save_simple_key(parser))
1896 /* A simple key cannot follow a flow scalar. */
1898 parser->simple_key_allowed = 0;
1900 /* Create the SCALAR token and append it to the queue. */
1902 if (!yaml_parser_scan_plain_scalar(parser, &token))
1905 if (!ENQUEUE(parser, parser->tokens, token)) {
1906 yaml_token_delete(&token);
1914 * Eat whitespaces and comments until the next token is found.
1918 yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1920 /* Until the next token is not found. */
1924 /* Allow the BOM mark to start a line. */
1926 if (!CACHE(parser, 1)) return 0;
1928 if (parser->mark.column == 0 && IS_BOM(parser->buffer))
1936 * - in the flow context;
1937 * - in the block context, but not at the beginning of the line or
1938 * after '-', '?', or ':' (complex value).
1941 if (!CACHE(parser, 1)) return 0;
1943 while (CHECK(parser->buffer,' ') ||
1944 ((parser->flow_level || !parser->simple_key_allowed) &&
1945 CHECK(parser->buffer, '\t'))) {
1947 if (!CACHE(parser, 1)) return 0;
1950 /* Eat a comment until a line break. */
1952 if (CHECK(parser->buffer, '#')) {
1953 while (!IS_BREAKZ(parser->buffer)) {
1955 if (!CACHE(parser, 1)) return 0;
1959 /* If it is a line break, eat it. */
1961 if (IS_BREAK(parser->buffer))
1963 if (!CACHE(parser, 2)) return 0;
1966 /* In the block context, a new line may start a simple key. */
1968 if (!parser->flow_level) {
1969 parser->simple_key_allowed = 1;
1974 /* We have found a token. */
1984 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1987 * %YAML 1.1 # a comment \n
1988 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1989 * %TAG !yaml! tag:yaml.org,2002: \n
1990 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1994 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
1996 yaml_mark_t start_mark, end_mark;
1997 yaml_char_t *name = NULL;
1999 yaml_char_t *handle = NULL, *prefix = NULL;
2003 start_mark = parser->mark;
2007 /* Scan the directive name. */
2009 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2012 /* Is it a YAML directive? */
2014 if (strcmp((char *)name, "YAML") == 0)
2016 /* Scan the VERSION directive value. */
2018 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2022 end_mark = parser->mark;
2024 /* Create a VERSION-DIRECTIVE token. */
2026 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2027 start_mark, end_mark);
2030 /* Is it a TAG directive? */
2032 else if (strcmp((char *)name, "TAG") == 0)
2034 /* Scan the TAG directive value. */
2036 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2040 end_mark = parser->mark;
2042 /* Create a TAG-DIRECTIVE token. */
2044 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2045 start_mark, end_mark);
2048 /* Unknown directive. */
2052 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2053 start_mark, "found uknown directive name");
2057 /* Eat the rest of the line including any comments. */
2059 if (!CACHE(parser, 1)) goto error;
2061 while (IS_BLANK(parser->buffer)) {
2063 if (!CACHE(parser, 1)) goto error;
2066 if (CHECK(parser->buffer, '#')) {
2067 while (!IS_BREAKZ(parser->buffer)) {
2069 if (!CACHE(parser, 1)) goto error;
2073 /* Check if we are at the end of the line. */
2075 if (!IS_BREAKZ(parser->buffer)) {
2076 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2077 start_mark, "did not find expected comment or line break");
2081 /* Eat a line break. */
2083 if (IS_BREAK(parser->buffer)) {
2084 if (!CACHE(parser, 2)) goto error;
2100 * Scan the directive name.
2103 * %YAML 1.1 # a comment \n
2105 * %TAG !yaml! tag:yaml.org,2002: \n
2110 yaml_parser_scan_directive_name(yaml_parser_t *parser,
2111 yaml_mark_t start_mark, yaml_char_t **name)
2113 yaml_string_t string = NULL_STRING;
2115 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2117 /* Consume the directive name. */
2119 if (!CACHE(parser, 1)) goto error;
2121 while (IS_ALPHA(parser->buffer))
2123 if (!READ(parser, string)) goto error;
2124 if (!CACHE(parser, 1)) goto error;
2127 /* Check if the name is empty. */
2129 if (string.start == string.pointer) {
2130 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2131 start_mark, "could not find expected directive name");
2135 /* Check for an blank character after the name. */
2137 if (!IS_BLANKZ(parser->buffer)) {
2138 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2139 start_mark, "found unexpected non-alphabetical character");
2143 *name = string.start;
2148 STRING_DEL(parser, string);
2153 * Scan the value of VERSION-DIRECTIVE.
2156 * %YAML 1.1 # a comment \n
2161 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2162 yaml_mark_t start_mark, int *major, int *minor)
2164 /* Eat whitespaces. */
2166 if (!CACHE(parser, 1)) return 0;
2168 while (IS_BLANK(parser->buffer)) {
2170 if (!CACHE(parser, 1)) return 0;
2173 /* Consume the major version number. */
2175 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2180 if (!CHECK(parser->buffer, '.')) {
2181 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2182 start_mark, "did not find expected digit or '.' character");
2187 /* Consume the minor version number. */
2189 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2195 #define MAX_NUMBER_LENGTH 9
2198 * Scan the version number of VERSION-DIRECTIVE.
2201 * %YAML 1.1 # a comment \n
2203 * %YAML 1.1 # a comment \n
2208 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2209 yaml_mark_t start_mark, int *number)
2214 /* Repeat while the next character is digit. */
2216 if (!CACHE(parser, 1)) return 0;
2218 while (IS_DIGIT(parser->buffer))
2220 /* Check if the number is too long. */
2222 if (++length > MAX_NUMBER_LENGTH) {
2223 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2224 start_mark, "found extremely long version number");
2227 value = value*10 + AS_DIGIT(parser->buffer);
2231 if (!CACHE(parser, 1)) return 0;
2234 /* Check if the number was present. */
2237 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2238 start_mark, "did not find expected version number");
2247 * Scan the value of a TAG-DIRECTIVE token.
2250 * %TAG !yaml! tag:yaml.org,2002: \n
2251 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2255 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2256 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2258 yaml_char_t *handle_value = NULL;
2259 yaml_char_t *prefix_value = NULL;
2261 /* Eat whitespaces. */
2263 if (!CACHE(parser, 1)) goto error;
2265 while (IS_BLANK(parser->buffer)) {
2267 if (!CACHE(parser, 1)) goto error;
2270 /* Scan a handle. */
2272 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2275 /* Expect a whitespace. */
2277 if (!CACHE(parser, 1)) goto error;
2279 if (!IS_BLANK(parser->buffer)) {
2280 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2281 start_mark, "did not find expected whitespace");
2285 /* Eat whitespaces. */
2287 while (IS_BLANK(parser->buffer)) {
2289 if (!CACHE(parser, 1)) goto error;
2292 /* Scan a prefix. */
2294 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2297 /* Expect a whitespace or line break. */
2299 if (!CACHE(parser, 1)) goto error;
2301 if (!IS_BLANKZ(parser->buffer)) {
2302 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2303 start_mark, "did not find expected whitespace or line break");
2307 *handle = handle_value;
2308 *prefix = prefix_value;
2313 yaml_free(handle_value);
2314 yaml_free(prefix_value);
2319 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2320 yaml_token_type_t type)
2323 yaml_mark_t start_mark, end_mark;
2324 yaml_string_t string = NULL_STRING;
2326 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2328 /* Eat the indicator character. */
2330 start_mark = parser->mark;
2334 /* Consume the value. */
2336 if (!CACHE(parser, 1)) goto error;
2338 while (IS_ALPHA(parser->buffer)) {
2339 if (!READ(parser, string)) goto error;
2340 if (!CACHE(parser, 1)) goto error;
2344 end_mark = parser->mark;
2347 * Check if length of the anchor is greater than 0 and it is followed by
2348 * a whitespace character or one of the indicators:
2350 * '?', ':', ',', ']', '}', '%', '@', '`'.
2353 if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2354 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2355 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2356 || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2357 || CHECK(parser->buffer, '`'))) {
2358 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2359 "while scanning an anchor" : "while scanning an alias", start_mark,
2360 "did not find expected alphabetic or numeric character");
2364 /* Create a token. */
2366 if (type == YAML_ANCHOR_TOKEN) {
2367 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2370 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2376 STRING_DEL(parser, string);
2385 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2387 yaml_char_t *handle = NULL;
2388 yaml_char_t *suffix = NULL;
2389 yaml_mark_t start_mark, end_mark;
2391 start_mark = parser->mark;
2393 /* Check if the tag is in the canonical form. */
2395 if (!CACHE(parser, 2)) goto error;
2397 if (CHECK_AT(parser->buffer, '<', 1))
2399 /* Set the handle to '' */
2401 handle = yaml_malloc(1);
2402 if (!handle) goto error;
2410 /* Consume the tag value. */
2412 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2415 /* Check for '>' and eat it. */
2417 if (!CHECK(parser->buffer, '>')) {
2418 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2419 start_mark, "did not find the expected '>'");
2427 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2429 /* First, try to scan a handle. */
2431 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2434 /* Check if it is, indeed, handle. */
2436 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2438 /* Scan the suffix now. */
2440 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2445 /* It wasn't a handle after all. Scan the rest of the tag. */
2447 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2450 /* Set the handle to '!'. */
2453 handle = yaml_malloc(2);
2454 if (!handle) goto error;
2459 * A special case: the '!' tag. Set the handle to '' and the
2463 if (suffix[0] == '\0') {
2464 yaml_char_t *tmp = handle;
2471 /* Check the character which ends the tag. */
2473 if (!CACHE(parser, 1)) goto error;
2475 if (!IS_BLANKZ(parser->buffer)) {
2476 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2477 start_mark, "did not find expected whitespace or line break");
2481 end_mark = parser->mark;
2483 /* Create a token. */
2485 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2496 * Scan a tag handle.
2500 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2501 yaml_mark_t start_mark, yaml_char_t **handle)
2503 yaml_string_t string = NULL_STRING;
2505 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2507 /* Check the initial '!' character. */
2509 if (!CACHE(parser, 1)) goto error;
2511 if (!CHECK(parser->buffer, '!')) {
2512 yaml_parser_set_scanner_error(parser, directive ?
2513 "while scanning a tag directive" : "while scanning a tag",
2514 start_mark, "did not find expected '!'");
2518 /* Copy the '!' character. */
2520 if (!READ(parser, string)) goto error;
2522 /* Copy all subsequent alphabetical and numerical characters. */
2524 if (!CACHE(parser, 1)) goto error;
2526 while (IS_ALPHA(parser->buffer))
2528 if (!READ(parser, string)) goto error;
2529 if (!CACHE(parser, 1)) goto error;
2532 /* Check if the trailing character is '!' and copy it. */
2534 if (CHECK(parser->buffer, '!'))
2536 if (!READ(parser, string)) goto error;
2541 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2542 * directive, it's an error. If it's a tag token, it must be a part of
2546 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2547 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2548 start_mark, "did not find expected '!'");
2553 *handle = string.start;
2558 STRING_DEL(parser, string);
2567 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2568 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2570 size_t length = head ? strlen((char *)head) : 0;
2571 yaml_string_t string = NULL_STRING;
2573 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2575 /* Resize the string to include the head. */
2577 while (string.end - string.start <= (int)length) {
2578 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2579 parser->error = YAML_MEMORY_ERROR;
2585 * Copy the head if needed.
2587 * Note that we don't copy the leading '!' character.
2591 memcpy(string.start, head+1, length-1);
2592 string.pointer += length-1;
2597 if (!CACHE(parser, 1)) goto error;
2600 * The set of characters that may appear in URI is as follows:
2602 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2603 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2607 while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';')
2608 || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?')
2609 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@')
2610 || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=')
2611 || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$')
2612 || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.')
2613 || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
2614 || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
2615 || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
2616 || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2617 || CHECK(parser->buffer, '%'))
2619 /* Check if it is a URI-escape sequence. */
2621 if (CHECK(parser->buffer, '%')) {
2622 if (!yaml_parser_scan_uri_escapes(parser,
2623 directive, start_mark, &string)) goto error;
2626 if (!READ(parser, string)) goto error;
2630 if (!CACHE(parser, 1)) goto error;
2633 /* Check if the tag is non-empty. */
2636 if (!STRING_EXTEND(parser, string))
2639 yaml_parser_set_scanner_error(parser, directive ?
2640 "while parsing a %TAG directive" : "while parsing a tag",
2641 start_mark, "did not find expected tag URI");
2645 *uri = string.start;
2650 STRING_DEL(parser, string);
2655 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2659 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2660 yaml_mark_t start_mark, yaml_string_t *string)
2664 /* Decode the required number of characters. */
2668 unsigned char octet = 0;
2670 /* Check for a URI-escaped octet. */
2672 if (!CACHE(parser, 3)) return 0;
2674 if (!(CHECK(parser->buffer, '%')
2675 && IS_HEX_AT(parser->buffer, 1)
2676 && IS_HEX_AT(parser->buffer, 2))) {
2677 return yaml_parser_set_scanner_error(parser, directive ?
2678 "while parsing a %TAG directive" : "while parsing a tag",
2679 start_mark, "did not find URI escaped octet");
2682 /* Get the octet. */
2684 octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
2686 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2690 width = (octet & 0x80) == 0x00 ? 1 :
2691 (octet & 0xE0) == 0xC0 ? 2 :
2692 (octet & 0xF0) == 0xE0 ? 3 :
2693 (octet & 0xF8) == 0xF0 ? 4 : 0;
2695 return yaml_parser_set_scanner_error(parser, directive ?
2696 "while parsing a %TAG directive" : "while parsing a tag",
2697 start_mark, "found an incorrect leading UTF-8 octet");
2702 /* Check if the trailing octet is correct. */
2704 if ((octet & 0xC0) != 0x80) {
2705 return yaml_parser_set_scanner_error(parser, directive ?
2706 "while parsing a %TAG directive" : "while parsing a tag",
2707 start_mark, "found an incorrect trailing UTF-8 octet");
2711 /* Copy the octet and move the pointers. */
2713 *(string->pointer++) = octet;
2724 * Scan a block scalar.
2728 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2731 yaml_mark_t start_mark;
2732 yaml_mark_t end_mark;
2733 yaml_string_t string = NULL_STRING;
2734 yaml_string_t leading_break = NULL_STRING;
2735 yaml_string_t trailing_breaks = NULL_STRING;
2739 int leading_blank = 0;
2740 int trailing_blank = 0;
2742 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2743 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2744 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2746 /* Eat the indicator '|' or '>'. */
2748 start_mark = parser->mark;
2752 /* Scan the additional block scalar indicators. */
2754 if (!CACHE(parser, 1)) goto error;
2756 /* Check for a chomping indicator. */
2758 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
2760 /* Set the chomping method and eat the indicator. */
2762 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2766 /* Check for an indentation indicator. */
2768 if (!CACHE(parser, 1)) goto error;
2770 if (IS_DIGIT(parser->buffer))
2772 /* Check that the intendation is greater than 0. */
2774 if (CHECK(parser->buffer, '0')) {
2775 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2776 start_mark, "found an intendation indicator equal to 0");
2780 /* Get the intendation level and eat the indicator. */
2782 increment = AS_DIGIT(parser->buffer);
2788 /* Do the same as above, but in the opposite order. */
2790 else if (IS_DIGIT(parser->buffer))
2792 if (CHECK(parser->buffer, '0')) {
2793 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2794 start_mark, "found an intendation indicator equal to 0");
2798 increment = AS_DIGIT(parser->buffer);
2802 if (!CACHE(parser, 1)) goto error;
2804 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2805 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2811 /* Eat whitespaces and comments to the end of the line. */
2813 if (!CACHE(parser, 1)) goto error;
2815 while (IS_BLANK(parser->buffer)) {
2817 if (!CACHE(parser, 1)) goto error;
2820 if (CHECK(parser->buffer, '#')) {
2821 while (!IS_BREAKZ(parser->buffer)) {
2823 if (!CACHE(parser, 1)) goto error;
2827 /* Check if we are at the end of the line. */
2829 if (!IS_BREAKZ(parser->buffer)) {
2830 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2831 start_mark, "did not find expected comment or line break");
2835 /* Eat a line break. */
2837 if (IS_BREAK(parser->buffer)) {
2838 if (!CACHE(parser, 2)) goto error;
2842 end_mark = parser->mark;
2844 /* Set the intendation level if it was specified. */
2847 indent = parser->indent >= 0 ? parser->indent+increment : increment;
2850 /* Scan the leading line breaks and determine the indentation level if needed. */
2852 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
2853 start_mark, &end_mark)) goto error;
2855 /* Scan the block scalar content. */
2857 if (!CACHE(parser, 1)) goto error;
2859 while ((int)parser->mark.column == indent && !IS_Z(parser->buffer))
2862 * We are at the beginning of a non-empty line.
2865 /* Is it a trailing whitespace? */
2867 trailing_blank = IS_BLANK(parser->buffer);
2869 /* Check if we need to fold the leading line break. */
2871 if (!literal && (*leading_break.start == '\n')
2872 && !leading_blank && !trailing_blank)
2874 /* Do we need to join the lines by space? */
2876 if (*trailing_breaks.start == '\0') {
2877 if (!STRING_EXTEND(parser, string)) goto error;
2878 *(string.pointer ++) = ' ';
2881 CLEAR(parser, leading_break);
2884 if (!JOIN(parser, string, leading_break)) goto error;
2885 CLEAR(parser, leading_break);
2888 /* Append the remaining line breaks. */
2890 if (!JOIN(parser, string, trailing_breaks)) goto error;
2891 CLEAR(parser, trailing_breaks);
2893 /* Is it a leading whitespace? */
2895 leading_blank = IS_BLANK(parser->buffer);
2897 /* Consume the current line. */
2899 while (!IS_BREAKZ(parser->buffer)) {
2900 if (!READ(parser, string)) goto error;
2901 if (!CACHE(parser, 1)) goto error;
2904 /* Consume the line break. */
2906 if (!CACHE(parser, 2)) goto error;
2908 if (!READ_LINE(parser, leading_break)) goto error;
2910 /* Eat the following intendation spaces and line breaks. */
2912 if (!yaml_parser_scan_block_scalar_breaks(parser,
2913 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
2916 /* Chomp the tail. */
2918 if (chomping != -1) {
2919 if (!JOIN(parser, string, leading_break)) goto error;
2921 if (chomping == 1) {
2922 if (!JOIN(parser, string, trailing_breaks)) goto error;
2925 /* Create a token. */
2927 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
2928 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2929 start_mark, end_mark);
2931 STRING_DEL(parser, leading_break);
2932 STRING_DEL(parser, trailing_breaks);
2937 STRING_DEL(parser, string);
2938 STRING_DEL(parser, leading_break);
2939 STRING_DEL(parser, trailing_breaks);
2945 * Scan intendation spaces and line breaks for a block scalar. Determine the
2946 * intendation level if needed.
2950 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2951 int *indent, yaml_string_t *breaks,
2952 yaml_mark_t start_mark, yaml_mark_t *end_mark)
2956 *end_mark = parser->mark;
2958 /* Eat the intendation spaces and line breaks. */
2962 /* Eat the intendation spaces. */
2964 if (!CACHE(parser, 1)) return 0;
2966 while ((!*indent || (int)parser->mark.column < *indent)
2967 && IS_SPACE(parser->buffer)) {
2969 if (!CACHE(parser, 1)) return 0;
2972 if ((int)parser->mark.column > max_indent)
2973 max_indent = (int)parser->mark.column;
2975 /* Check for a tab character messing the intendation. */
2977 if ((!*indent || (int)parser->mark.column < *indent)
2978 && IS_TAB(parser->buffer)) {
2979 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2980 start_mark, "found a tab character where an intendation space is expected");
2983 /* Have we found a non-empty line? */
2985 if (!IS_BREAK(parser->buffer)) break;
2987 /* Consume the line break. */
2989 if (!CACHE(parser, 2)) return 0;
2990 if (!READ_LINE(parser, *breaks)) return 0;
2991 *end_mark = parser->mark;
2994 /* Determine the indentation level if needed. */
2997 *indent = max_indent;
2998 if (*indent < parser->indent + 1)
2999 *indent = parser->indent + 1;
3008 * Scan a quoted scalar.
3012 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3015 yaml_mark_t start_mark;
3016 yaml_mark_t end_mark;
3017 yaml_string_t string = NULL_STRING;
3018 yaml_string_t leading_break = NULL_STRING;
3019 yaml_string_t trailing_breaks = NULL_STRING;
3020 yaml_string_t whitespaces = NULL_STRING;
3023 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3024 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3025 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3026 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3028 /* Eat the left quote. */
3030 start_mark = parser->mark;
3034 /* Consume the content of the quoted scalar. */
3038 /* Check that there are no document indicators at the beginning of the line. */
3040 if (!CACHE(parser, 4)) goto error;
3042 if (parser->mark.column == 0 &&
3043 ((CHECK_AT(parser->buffer, '-', 0) &&
3044 CHECK_AT(parser->buffer, '-', 1) &&
3045 CHECK_AT(parser->buffer, '-', 2)) ||
3046 (CHECK_AT(parser->buffer, '.', 0) &&
3047 CHECK_AT(parser->buffer, '.', 1) &&
3048 CHECK_AT(parser->buffer, '.', 2))) &&
3049 IS_BLANKZ_AT(parser->buffer, 3))
3051 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3052 start_mark, "found unexpected document indicator");
3056 /* Check for EOF. */
3058 if (IS_Z(parser->buffer)) {
3059 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3060 start_mark, "found unexpected end of stream");
3064 /* Consume non-blank characters. */
3066 if (!CACHE(parser, 2)) goto error;
3070 while (!IS_BLANKZ(parser->buffer))
3072 /* Check for an escaped single quote. */
3074 if (single && CHECK_AT(parser->buffer, '\'', 0)
3075 && CHECK_AT(parser->buffer, '\'', 1))
3077 if (!STRING_EXTEND(parser, string)) goto error;
3078 *(string.pointer++) = '\'';
3083 /* Check for the right quote. */
3085 else if (CHECK(parser->buffer, single ? '\'' : '"'))
3090 /* Check for an escaped line break. */
3092 else if (!single && CHECK(parser->buffer, '\\')
3093 && IS_BREAK_AT(parser->buffer, 1))
3095 if (!CACHE(parser, 3)) goto error;
3102 /* Check for an escape sequence. */
3104 else if (!single && CHECK(parser->buffer, '\\'))
3106 size_t code_length = 0;
3108 if (!STRING_EXTEND(parser, string)) goto error;
3110 /* Check the escape character. */
3112 switch (parser->buffer.pointer[1])
3115 *(string.pointer++) = '\0';
3119 *(string.pointer++) = '\x07';
3123 *(string.pointer++) = '\x08';
3128 *(string.pointer++) = '\x09';
3132 *(string.pointer++) = '\x0A';
3136 *(string.pointer++) = '\x0B';
3140 *(string.pointer++) = '\x0C';
3144 *(string.pointer++) = '\x0D';
3148 *(string.pointer++) = '\x1B';
3152 *(string.pointer++) = '\x20';
3156 *(string.pointer++) = '"';
3160 *(string.pointer++) = '\'';
3164 *(string.pointer++) = '\\';
3167 case 'N': /* NEL (#x85) */
3168 *(string.pointer++) = '\xC2';
3169 *(string.pointer++) = '\x85';
3172 case '_': /* #xA0 */
3173 *(string.pointer++) = '\xC2';
3174 *(string.pointer++) = '\xA0';
3177 case 'L': /* LS (#x2028) */
3178 *(string.pointer++) = '\xE2';
3179 *(string.pointer++) = '\x80';
3180 *(string.pointer++) = '\xA8';
3183 case 'P': /* PS (#x2029) */
3184 *(string.pointer++) = '\xE2';
3185 *(string.pointer++) = '\x80';
3186 *(string.pointer++) = '\xA9';
3202 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3203 start_mark, "found unknown escape character");
3210 /* Consume an arbitrary escape code. */
3214 unsigned int value = 0;
3217 /* Scan the character value. */
3219 if (!CACHE(parser, code_length)) goto error;
3221 for (k = 0; k < code_length; k ++) {
3222 if (!IS_HEX_AT(parser->buffer, k)) {
3223 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3224 start_mark, "did not find expected hexdecimal number");
3227 value = (value << 4) + AS_HEX_AT(parser->buffer, k);
3230 /* Check the value and write the character. */
3232 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3233 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3234 start_mark, "found invalid Unicode character escape code");
3238 if (value <= 0x7F) {
3239 *(string.pointer++) = value;
3241 else if (value <= 0x7FF) {
3242 *(string.pointer++) = 0xC0 + (value >> 6);
3243 *(string.pointer++) = 0x80 + (value & 0x3F);
3245 else if (value <= 0xFFFF) {
3246 *(string.pointer++) = 0xE0 + (value >> 12);
3247 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3248 *(string.pointer++) = 0x80 + (value & 0x3F);
3251 *(string.pointer++) = 0xF0 + (value >> 18);
3252 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3253 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3254 *(string.pointer++) = 0x80 + (value & 0x3F);
3257 /* Advance the pointer. */
3259 for (k = 0; k < code_length; k ++) {
3267 /* It is a non-escaped non-blank character. */
3269 if (!READ(parser, string)) goto error;
3272 if (!CACHE(parser, 2)) goto error;
3275 /* Check if we are at the end of the scalar. */
3277 if (CHECK(parser->buffer, single ? '\'' : '"'))
3280 /* Consume blank characters. */
3282 if (!CACHE(parser, 1)) goto error;
3284 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3286 if (IS_BLANK(parser->buffer))
3288 /* Consume a space or a tab character. */
3290 if (!leading_blanks) {
3291 if (!READ(parser, whitespaces)) goto error;
3299 if (!CACHE(parser, 2)) goto error;
3301 /* Check if it is a first line break. */
3303 if (!leading_blanks)
3305 CLEAR(parser, whitespaces);
3306 if (!READ_LINE(parser, leading_break)) goto error;
3311 if (!READ_LINE(parser, trailing_breaks)) goto error;
3314 if (!CACHE(parser, 1)) goto error;
3317 /* Join the whitespaces or fold line breaks. */
3321 /* Do we need to fold line breaks? */
3323 if (leading_break.start[0] == '\n') {
3324 if (trailing_breaks.start[0] == '\0') {
3325 if (!STRING_EXTEND(parser, string)) goto error;
3326 *(string.pointer++) = ' ';
3329 if (!JOIN(parser, string, trailing_breaks)) goto error;
3330 CLEAR(parser, trailing_breaks);
3332 CLEAR(parser, leading_break);
3335 if (!JOIN(parser, string, leading_break)) goto error;
3336 if (!JOIN(parser, string, trailing_breaks)) goto error;
3337 CLEAR(parser, leading_break);
3338 CLEAR(parser, trailing_breaks);
3343 if (!JOIN(parser, string, whitespaces)) goto error;
3344 CLEAR(parser, whitespaces);
3348 /* Eat the right quote. */
3352 end_mark = parser->mark;
3354 /* Create a token. */
3356 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3357 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3358 start_mark, end_mark);
3360 STRING_DEL(parser, leading_break);
3361 STRING_DEL(parser, trailing_breaks);
3362 STRING_DEL(parser, whitespaces);
3367 STRING_DEL(parser, string);
3368 STRING_DEL(parser, leading_break);
3369 STRING_DEL(parser, trailing_breaks);
3370 STRING_DEL(parser, whitespaces);
3376 * Scan a plain scalar.
3380 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3382 yaml_mark_t start_mark;
3383 yaml_mark_t end_mark;
3384 yaml_string_t string = NULL_STRING;
3385 yaml_string_t leading_break = NULL_STRING;
3386 yaml_string_t trailing_breaks = NULL_STRING;
3387 yaml_string_t whitespaces = NULL_STRING;
3388 int leading_blanks = 0;
3389 int indent = parser->indent+1;
3391 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3392 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3393 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3394 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3396 start_mark = end_mark = parser->mark;
3398 /* Consume the content of the plain scalar. */
3402 /* Check for a document indicator. */
3404 if (!CACHE(parser, 4)) goto error;
3406 if (parser->mark.column == 0 &&
3407 ((CHECK_AT(parser->buffer, '-', 0) &&
3408 CHECK_AT(parser->buffer, '-', 1) &&
3409 CHECK_AT(parser->buffer, '-', 2)) ||
3410 (CHECK_AT(parser->buffer, '.', 0) &&
3411 CHECK_AT(parser->buffer, '.', 1) &&
3412 CHECK_AT(parser->buffer, '.', 2))) &&
3413 IS_BLANKZ_AT(parser->buffer, 3)) break;
3415 /* Check for a comment. */
3417 if (CHECK(parser->buffer, '#'))
3420 /* Consume non-blank characters. */
3422 while (!IS_BLANKZ(parser->buffer))
3424 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
3426 if (parser->flow_level
3427 && CHECK(parser->buffer, ':')
3428 && !IS_BLANKZ_AT(parser->buffer, 1)) {
3429 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3430 start_mark, "found unexpected ':'");
3434 /* Check for indicators that may end a plain scalar. */
3436 if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3437 || (parser->flow_level &&
3438 (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':')
3439 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
3440 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3441 || CHECK(parser->buffer, '}'))))
3444 /* Check if we need to join whitespaces and breaks. */
3446 if (leading_blanks || whitespaces.start != whitespaces.pointer)
3450 /* Do we need to fold line breaks? */
3452 if (leading_break.start[0] == '\n') {
3453 if (trailing_breaks.start[0] == '\0') {
3454 if (!STRING_EXTEND(parser, string)) goto error;
3455 *(string.pointer++) = ' ';
3458 if (!JOIN(parser, string, trailing_breaks)) goto error;
3459 CLEAR(parser, trailing_breaks);
3461 CLEAR(parser, leading_break);
3464 if (!JOIN(parser, string, leading_break)) goto error;
3465 if (!JOIN(parser, string, trailing_breaks)) goto error;
3466 CLEAR(parser, leading_break);
3467 CLEAR(parser, trailing_breaks);
3474 if (!JOIN(parser, string, whitespaces)) goto error;
3475 CLEAR(parser, whitespaces);
3479 /* Copy the character. */
3481 if (!READ(parser, string)) goto error;
3483 end_mark = parser->mark;
3485 if (!CACHE(parser, 2)) goto error;
3488 /* Is it the end? */
3490 if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
3493 /* Consume blank characters. */
3495 if (!CACHE(parser, 1)) goto error;
3497 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3499 if (IS_BLANK(parser->buffer))
3501 /* Check for tab character that abuse intendation. */
3503 if (leading_blanks && (int)parser->mark.column < indent
3504 && IS_TAB(parser->buffer)) {
3505 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3506 start_mark, "found a tab character that violate intendation");
3510 /* Consume a space or a tab character. */
3512 if (!leading_blanks) {
3513 if (!READ(parser, whitespaces)) goto error;
3521 if (!CACHE(parser, 2)) goto error;
3523 /* Check if it is a first line break. */
3525 if (!leading_blanks)
3527 CLEAR(parser, whitespaces);
3528 if (!READ_LINE(parser, leading_break)) goto error;
3533 if (!READ_LINE(parser, trailing_breaks)) goto error;
3536 if (!CACHE(parser, 1)) goto error;
3539 /* Check intendation level. */
3541 if (!parser->flow_level && (int)parser->mark.column < indent)
3545 /* Create a token. */
3547 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3548 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3550 /* Note that we change the 'simple_key_allowed' flag. */
3552 if (leading_blanks) {
3553 parser->simple_key_allowed = 1;
3556 STRING_DEL(parser, leading_break);
3557 STRING_DEL(parser, trailing_breaks);
3558 STRING_DEL(parser, whitespaces);
3563 STRING_DEL(parser, string);
3564 STRING_DEL(parser, leading_break);
3565 STRING_DEL(parser, trailing_breaks);
3566 STRING_DEL(parser, whitespaces);