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 * Check the octet at the specified position.
494 #define CHECK_AT(parser,octet,offset) \
495 (parser->buffer.pointer[offset] == (yaml_char_t)(octet))
498 * Check the current octet in the buffer.
501 #define CHECK(parser,octet) CHECK_AT(parser,(octet),0)
504 * Check if the character at the specified position is an alphabetical
505 * character, a digit, '_', or '-'.
508 #define IS_ALPHA_AT(parser,offset) \
509 ((parser->buffer.pointer[offset] >= (yaml_char_t) '0' && \
510 parser->buffer.pointer[offset] <= (yaml_char_t) '9') || \
511 (parser->buffer.pointer[offset] >= (yaml_char_t) 'A' && \
512 parser->buffer.pointer[offset] <= (yaml_char_t) 'Z') || \
513 (parser->buffer.pointer[offset] >= (yaml_char_t) 'a' && \
514 parser->buffer.pointer[offset] <= (yaml_char_t) 'z') || \
515 parser->buffer.pointer[offset] == '_' || \
516 parser->buffer.pointer[offset] == '-')
518 #define IS_ALPHA(parser) IS_ALPHA_AT(parser,0)
521 * Check if the character at the specified position is a digit.
524 #define IS_DIGIT_AT(parser,offset) \
525 ((parser->buffer.pointer[offset] >= (yaml_char_t) '0' && \
526 parser->buffer.pointer[offset] <= (yaml_char_t) '9'))
528 #define IS_DIGIT(parser) IS_DIGIT_AT(parser,0)
531 * Get the value of a digit.
534 #define AS_DIGIT_AT(parser,offset) \
535 (parser->buffer.pointer[offset] - (yaml_char_t) '0')
537 #define AS_DIGIT(parser) AS_DIGIT_AT(parser,0)
540 * Check if the character at the specified position is a hex-digit.
543 #define IS_HEX_AT(parser,offset) \
544 ((parser->buffer.pointer[offset] >= (yaml_char_t) '0' && \
545 parser->buffer.pointer[offset] <= (yaml_char_t) '9') || \
546 (parser->buffer.pointer[offset] >= (yaml_char_t) 'A' && \
547 parser->buffer.pointer[offset] <= (yaml_char_t) 'F') || \
548 (parser->buffer.pointer[offset] >= (yaml_char_t) 'a' && \
549 parser->buffer.pointer[offset] <= (yaml_char_t) 'f'))
551 #define IS_HEX(parser) IS_HEX_AT(parser,0)
554 * Get the value of a hex-digit.
557 #define AS_HEX_AT(parser,offset) \
558 ((parser->buffer.pointer[offset] >= (yaml_char_t) 'A' && \
559 parser->buffer.pointer[offset] <= (yaml_char_t) 'F') ? \
560 (parser->buffer.pointer[offset] - (yaml_char_t) 'A' + 10) : \
561 (parser->buffer.pointer[offset] >= (yaml_char_t) 'a' && \
562 parser->buffer.pointer[offset] <= (yaml_char_t) 'f') ? \
563 (parser->buffer.pointer[offset] - (yaml_char_t) 'a' + 10) : \
564 (parser->buffer.pointer[offset] - (yaml_char_t) '0'))
566 #define AS_HEX(parser) AS_HEX_AT(parser,0)
569 * Check if the character at the specified position is NUL.
572 #define IS_Z_AT(parser,offset) CHECK_AT(parser,'\0',(offset))
574 #define IS_Z(parser) IS_Z_AT(parser,0)
577 * Check if the character at the specified position is BOM.
580 #define IS_BOM_AT(parser,offset) \
581 (CHECK_AT(parser,'\xEF',(offset)) \
582 && CHECK_AT(parser,'\xBB',(offset)+1) \
583 && CHECK_AT(parser,'\xBF',(offset)+1)) /* BOM (#xFEFF) */
585 #define IS_BOM(parser) IS_BOM_AT(parser,0)
588 * Check if the character at the specified position is space.
591 #define IS_SPACE_AT(parser,offset) CHECK_AT(parser,' ',(offset))
593 #define IS_SPACE(parser) IS_SPACE_AT(parser,0)
596 * Check if the character at the specified position is tab.
599 #define IS_TAB_AT(parser,offset) CHECK_AT(parser,'\t',(offset))
601 #define IS_TAB(parser) IS_TAB_AT(parser,0)
604 * Check if the character at the specified position is blank (space or tab).
607 #define IS_BLANK_AT(parser,offset) \
608 (IS_SPACE_AT(parser,(offset)) || IS_TAB_AT(parser,(offset)))
610 #define IS_BLANK(parser) IS_BLANK_AT(parser,0)
613 * Check if the character at the specified position is a line break.
616 #define IS_BREAK_AT(parser,offset) \
617 (CHECK_AT(parser,'\r',(offset)) /* CR (#xD)*/ \
618 || CHECK_AT(parser,'\n',(offset)) /* LF (#xA) */ \
619 || (CHECK_AT(parser,'\xC2',(offset)) \
620 && CHECK_AT(parser,'\x85',(offset)+1)) /* NEL (#x85) */ \
621 || (CHECK_AT(parser,'\xE2',(offset)) \
622 && CHECK_AT(parser,'\x80',(offset)+1) \
623 && CHECK_AT(parser,'\xA8',(offset)+2)) /* LS (#x2028) */ \
624 || (CHECK_AT(parser,'\xE2',(offset)) \
625 && CHECK_AT(parser,'\x80',(offset)+1) \
626 && CHECK_AT(parser,'\xA9',(offset)+2))) /* PS (#x2029) */
628 #define IS_BREAK(parser) IS_BREAK_AT(parser,0)
630 #define IS_CRLF_AT(parser,offset) \
631 (CHECK_AT(parser,'\r',(offset)) && CHECK_AT(parser,'\n',(offset)+1))
633 #define IS_CRLF(parser) IS_CRLF_AT(parser,0)
636 * Check if the character is a line break or NUL.
639 #define IS_BREAKZ_AT(parser,offset) \
640 (IS_BREAK_AT(parser,(offset)) || IS_Z_AT(parser,(offset)))
642 #define IS_BREAKZ(parser) IS_BREAKZ_AT(parser,0)
645 * Check if the character is a line break, space, or NUL.
648 #define IS_SPACEZ_AT(parser,offset) \
649 (IS_SPACE_AT(parser,(offset)) || IS_BREAKZ_AT(parser,(offset)))
651 #define IS_SPACEZ(parser) IS_SPACEZ_AT(parser,0)
654 * Check if the character is a line break, space, tab, or NUL.
657 #define IS_BLANKZ_AT(parser,offset) \
658 (IS_BLANK_AT(parser,(offset)) || IS_BREAKZ_AT(parser,(offset)))
660 #define IS_BLANKZ(parser) IS_BLANKZ_AT(parser,0)
663 * Determine the width of the character.
666 #define WIDTH_AT(parser,offset) \
667 ((parser->buffer.pointer[offset] & 0x80) == 0x00 ? 1 : \
668 (parser->buffer.pointer[offset] & 0xE0) == 0xC0 ? 2 : \
669 (parser->buffer.pointer[offset] & 0xF0) == 0xE0 ? 3 : \
670 (parser->buffer.pointer[offset] & 0xF8) == 0xF0 ? 4 : 0)
672 #define WIDTH(parser) WIDTH_AT(parser,0)
675 * Advance the buffer pointer.
678 #define SKIP(parser) \
679 (parser->mark.index ++, \
680 parser->mark.column ++, \
682 parser->buffer.pointer += WIDTH(parser))
684 #define SKIP_LINE(parser) \
686 (parser->mark.index += 2, \
687 parser->mark.column = 0, \
688 parser->mark.line ++, \
689 parser->unread -= 2, \
690 parser->buffer.pointer += 2) : \
692 (parser->mark.index ++, \
693 parser->mark.column = 0, \
694 parser->mark.line ++, \
696 parser->buffer.pointer += WIDTH(parser)) : 0)
699 * Copy a character to a string buffer and advance pointers.
702 #define READ(parser,string) \
703 (STRING_EXTEND(parser,string) ? \
704 (((*parser->buffer.pointer & 0x80) == 0x00 ? \
705 (*((string).pointer++) = *(parser->buffer.pointer++)) : \
706 (*parser->buffer.pointer & 0xE0) == 0xC0 ? \
707 (*((string).pointer++) = *(parser->buffer.pointer++), \
708 *((string).pointer++) = *(parser->buffer.pointer++)) : \
709 (*parser->buffer.pointer & 0xF0) == 0xE0 ? \
710 (*((string).pointer++) = *(parser->buffer.pointer++), \
711 *((string).pointer++) = *(parser->buffer.pointer++), \
712 *((string).pointer++) = *(parser->buffer.pointer++)) : \
713 (*parser->buffer.pointer & 0xF8) == 0xF0 ? \
714 (*((string).pointer++) = *(parser->buffer.pointer++), \
715 *((string).pointer++) = *(parser->buffer.pointer++), \
716 *((string).pointer++) = *(parser->buffer.pointer++), \
717 *((string).pointer++) = *(parser->buffer.pointer++)) : 0), \
718 parser->mark.index ++, \
719 parser->mark.column ++, \
724 * Copy a line break character to a string buffer and advance pointers.
727 #define READ_LINE(parser,string) \
728 (STRING_EXTEND(parser,string) ? \
729 (((CHECK_AT(parser,'\r',0) && CHECK_AT(parser,'\n',1)) ? /* CR LF -> LF */ \
730 (*((string).pointer++) = (yaml_char_t) '\n', \
731 parser->buffer.pointer += 2, \
732 parser->mark.index += 2, \
733 parser->mark.column = 0, \
734 parser->mark.line ++, \
735 parser->unread -= 2) : \
736 (CHECK_AT(parser,'\r',0) || CHECK_AT(parser,'\n',0)) ? /* CR|LF -> LF */ \
737 (*((string).pointer++) = (yaml_char_t) '\n', \
738 parser->buffer.pointer ++, \
739 parser->mark.index ++, \
740 parser->mark.column = 0, \
741 parser->mark.line ++, \
742 parser->unread --) : \
743 (CHECK_AT(parser,'\xC2',0) && CHECK_AT(parser,'\x85',1)) ? /* NEL -> LF */ \
744 (*((string).pointer++) = (yaml_char_t) '\n', \
745 parser->buffer.pointer += 2, \
746 parser->mark.index ++, \
747 parser->mark.column = 0, \
748 parser->mark.line ++, \
749 parser->unread --) : \
750 (CHECK_AT(parser,'\xE2',0) && \
751 CHECK_AT(parser,'\x80',1) && \
752 (CHECK_AT(parser,'\xA8',2) || \
753 CHECK_AT(parser,'\xA9',2))) ? /* LS|PS -> LS|PS */ \
754 (*((string).pointer++) = *(parser->buffer.pointer++), \
755 *((string).pointer++) = *(parser->buffer.pointer++), \
756 *((string).pointer++) = *(parser->buffer.pointer++), \
757 parser->mark.index ++, \
758 parser->mark.column = 0, \
759 parser->mark.line ++, \
760 parser->unread --) : 0), \
764 * Public API declarations.
768 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
775 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
776 yaml_mark_t context_mark, const char *problem);
779 * High-level token API.
783 yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
786 yaml_parser_fetch_next_token(yaml_parser_t *parser);
789 * Potential simple keys.
793 yaml_parser_stale_simple_keys(yaml_parser_t *parser);
796 yaml_parser_save_simple_key(yaml_parser_t *parser);
799 yaml_parser_remove_simple_key(yaml_parser_t *parser);
802 yaml_parser_increase_flow_level(yaml_parser_t *parser);
805 yaml_parser_decrease_flow_level(yaml_parser_t *parser);
808 * Indentation treatment.
812 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
813 int number, yaml_token_type_t type, yaml_mark_t mark);
816 yaml_parser_unroll_indent(yaml_parser_t *parser, int column);
823 yaml_parser_fetch_stream_start(yaml_parser_t *parser);
826 yaml_parser_fetch_stream_end(yaml_parser_t *parser);
829 yaml_parser_fetch_directive(yaml_parser_t *parser);
832 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
833 yaml_token_type_t type);
836 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
837 yaml_token_type_t type);
840 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
841 yaml_token_type_t type);
844 yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
847 yaml_parser_fetch_block_entry(yaml_parser_t *parser);
850 yaml_parser_fetch_key(yaml_parser_t *parser);
853 yaml_parser_fetch_value(yaml_parser_t *parser);
856 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
859 yaml_parser_fetch_tag(yaml_parser_t *parser);
862 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
865 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
868 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
875 yaml_parser_scan_to_next_token(yaml_parser_t *parser);
878 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
881 yaml_parser_scan_directive_name(yaml_parser_t *parser,
882 yaml_mark_t start_mark, yaml_char_t **name);
885 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
886 yaml_mark_t start_mark, int *major, int *minor);
889 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
890 yaml_mark_t start_mark, int *number);
893 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
894 yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
897 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
898 yaml_token_type_t type);
901 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
904 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
905 yaml_mark_t start_mark, yaml_char_t **handle);
908 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
909 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
912 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
913 yaml_mark_t start_mark, yaml_string_t *string);
916 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
920 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
921 int *indent, yaml_string_t *breaks,
922 yaml_mark_t start_mark, yaml_mark_t *end_mark);
925 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
929 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
932 * Get the next token.
936 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
938 assert(parser); /* Non-NULL parser object is expected. */
939 assert(token); /* Non-NULL token object is expected. */
941 /* Erase the token object. */
943 memset(token, 0, sizeof(yaml_token_t));
945 /* No tokens after STREAM-END or error. */
947 if (parser->stream_end_produced || parser->error) {
951 /* Ensure that the tokens queue contains enough tokens. */
953 if (!parser->token_available) {
954 if (!yaml_parser_fetch_more_tokens(parser))
958 /* Fetch the next token from the queue. */
960 *token = DEQUEUE(parser, parser->tokens);
961 parser->token_available = 0;
962 parser->tokens_parsed ++;
964 if (token->type == YAML_STREAM_END_TOKEN) {
965 parser->stream_end_produced = 1;
972 * Set the scanner error and return 0.
976 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
977 yaml_mark_t context_mark, const char *problem)
979 parser->error = YAML_SCANNER_ERROR;
980 parser->context = context;
981 parser->context_mark = context_mark;
982 parser->problem = problem;
983 parser->problem_mark = parser->mark;
989 * Ensure that the tokens queue contains at least one token which can be
990 * returned to the Parser.
994 yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
996 int need_more_tokens;
998 /* While we need more tokens to fetch, do it. */
1003 * Check if we really need to fetch more tokens.
1006 need_more_tokens = 0;
1008 if (parser->tokens.head == parser->tokens.tail)
1010 /* Queue is empty. */
1012 need_more_tokens = 1;
1016 yaml_simple_key_t *simple_key;
1018 /* Check if any potential simple key may occupy the head position. */
1020 if (!yaml_parser_stale_simple_keys(parser))
1023 for (simple_key = parser->simple_keys.start;
1024 simple_key != parser->simple_keys.top; simple_key++) {
1025 if (simple_key->possible
1026 && simple_key->token_number == parser->tokens_parsed) {
1027 need_more_tokens = 1;
1033 /* We are finished. */
1035 if (!need_more_tokens)
1038 /* Fetch the next token. */
1040 if (!yaml_parser_fetch_next_token(parser))
1044 parser->token_available = 1;
1050 * The dispatcher for token fetchers.
1054 yaml_parser_fetch_next_token(yaml_parser_t *parser)
1056 /* Ensure that the buffer is initialized. */
1058 if (!CACHE(parser, 1))
1061 /* Check if we just started scanning. Fetch STREAM-START then. */
1063 if (!parser->stream_start_produced)
1064 return yaml_parser_fetch_stream_start(parser);
1066 /* Eat whitespaces and comments until we reach the next token. */
1068 if (!yaml_parser_scan_to_next_token(parser))
1071 /* Remove obsolete potential simple keys. */
1073 if (!yaml_parser_stale_simple_keys(parser))
1076 /* Check the indentation level against the current column. */
1078 if (!yaml_parser_unroll_indent(parser, parser->mark.column))
1082 * Ensure that the buffer contains at least 4 characters. 4 is the length
1083 * of the longest indicators ('--- ' and '... ').
1086 if (!CACHE(parser, 4))
1089 /* Is it the end of the stream? */
1092 return yaml_parser_fetch_stream_end(parser);
1094 /* Is it a directive? */
1096 if (parser->mark.column == 0 && CHECK(parser, '%'))
1097 return yaml_parser_fetch_directive(parser);
1099 /* Is it the document start indicator? */
1101 if (parser->mark.column == 0
1102 && CHECK_AT(parser, '-', 0)
1103 && CHECK_AT(parser, '-', 1)
1104 && CHECK_AT(parser, '-', 2)
1105 && IS_BLANKZ_AT(parser, 3))
1106 return yaml_parser_fetch_document_indicator(parser,
1107 YAML_DOCUMENT_START_TOKEN);
1109 /* Is it the document end indicator? */
1111 if (parser->mark.column == 0
1112 && CHECK_AT(parser, '.', 0)
1113 && CHECK_AT(parser, '.', 1)
1114 && CHECK_AT(parser, '.', 2)
1115 && IS_BLANKZ_AT(parser, 3))
1116 return yaml_parser_fetch_document_indicator(parser,
1117 YAML_DOCUMENT_END_TOKEN);
1119 /* Is it the flow sequence start indicator? */
1121 if (CHECK(parser, '['))
1122 return yaml_parser_fetch_flow_collection_start(parser,
1123 YAML_FLOW_SEQUENCE_START_TOKEN);
1125 /* Is it the flow mapping start indicator? */
1127 if (CHECK(parser, '{'))
1128 return yaml_parser_fetch_flow_collection_start(parser,
1129 YAML_FLOW_MAPPING_START_TOKEN);
1131 /* Is it the flow sequence end indicator? */
1133 if (CHECK(parser, ']'))
1134 return yaml_parser_fetch_flow_collection_end(parser,
1135 YAML_FLOW_SEQUENCE_END_TOKEN);
1137 /* Is it the flow mapping end indicator? */
1139 if (CHECK(parser, '}'))
1140 return yaml_parser_fetch_flow_collection_end(parser,
1141 YAML_FLOW_MAPPING_END_TOKEN);
1143 /* Is it the flow entry indicator? */
1145 if (CHECK(parser, ','))
1146 return yaml_parser_fetch_flow_entry(parser);
1148 /* Is it the block entry indicator? */
1150 if (CHECK(parser, '-') && IS_BLANKZ_AT(parser, 1))
1151 return yaml_parser_fetch_block_entry(parser);
1153 /* Is it the key indicator? */
1155 if (CHECK(parser, '?') && (parser->flow_level || IS_BLANKZ_AT(parser, 1)))
1156 return yaml_parser_fetch_key(parser);
1158 /* Is it the value indicator? */
1160 if (CHECK(parser, ':') && (parser->flow_level || IS_BLANKZ_AT(parser, 1)))
1161 return yaml_parser_fetch_value(parser);
1163 /* Is it an alias? */
1165 if (CHECK(parser, '*'))
1166 return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
1168 /* Is it an anchor? */
1170 if (CHECK(parser, '&'))
1171 return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
1175 if (CHECK(parser, '!'))
1176 return yaml_parser_fetch_tag(parser);
1178 /* Is it a literal scalar? */
1180 if (CHECK(parser, '|') && !parser->flow_level)
1181 return yaml_parser_fetch_block_scalar(parser, 1);
1183 /* Is it a folded scalar? */
1185 if (CHECK(parser, '>') && !parser->flow_level)
1186 return yaml_parser_fetch_block_scalar(parser, 0);
1188 /* Is it a single-quoted scalar? */
1190 if (CHECK(parser, '\''))
1191 return yaml_parser_fetch_flow_scalar(parser, 1);
1193 /* Is it a double-quoted scalar? */
1195 if (CHECK(parser, '"'))
1196 return yaml_parser_fetch_flow_scalar(parser, 0);
1199 * Is it a plain scalar?
1201 * A plain scalar may start with any non-blank characters except
1203 * '-', '?', ':', ',', '[', ']', '{', '}',
1204 * '#', '&', '*', '!', '|', '>', '\'', '\"',
1207 * In the block context (and, for the '-' indicator, in the flow context
1208 * too), it may also start with the characters
1212 * if it is followed by a non-space character.
1214 * The last rule is more restrictive than the specification requires.
1217 if (!(IS_BLANKZ(parser) || CHECK(parser, '-') || CHECK(parser, '?')
1218 || CHECK(parser, ':') || CHECK(parser, ',') || CHECK(parser, '[')
1219 || CHECK(parser, ']') || CHECK(parser, '{') || CHECK(parser, '}')
1220 || CHECK(parser, '#') || CHECK(parser, '&') || CHECK(parser, '*')
1221 || CHECK(parser, '!') || CHECK(parser, '|') || CHECK(parser, '>')
1222 || CHECK(parser, '\'') || CHECK(parser, '"') || CHECK(parser, '%')
1223 || CHECK(parser, '@') || CHECK(parser, '`')) ||
1224 (CHECK(parser, '-') && !IS_BLANK_AT(parser, 1)) ||
1225 (!parser->flow_level &&
1226 (CHECK(parser, '?') || CHECK(parser, ':')) && !IS_BLANKZ_AT(parser, 1)))
1227 return yaml_parser_fetch_plain_scalar(parser);
1230 * If we don't determine the token type so far, it is an error.
1233 return yaml_parser_set_scanner_error(parser,
1234 "while scanning for the next token", parser->mark,
1235 "found character that cannot start any token");
1239 * Check the list of potential simple keys and remove the positions that
1240 * cannot contain simple keys anymore.
1244 yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1246 yaml_simple_key_t *simple_key;
1248 /* Check for a potential simple key for each flow level. */
1250 for (simple_key = parser->simple_keys.start;
1251 simple_key != parser->simple_keys.top; simple_key ++)
1254 * The specification requires that a simple key
1256 * - is limited to a single line,
1257 * - is shorter than 1024 characters.
1260 if (simple_key->possible
1261 && (simple_key->mark.line < parser->mark.line
1262 || simple_key->mark.index+1024 < parser->mark.index)) {
1264 /* Check if the potential simple key to be removed is required. */
1266 if (simple_key->required) {
1267 return yaml_parser_set_scanner_error(parser,
1268 "while scanning a simple key", simple_key->mark,
1269 "could not found expected ':'");
1272 simple_key->possible = 0;
1280 * Check if a simple key may start at the current position and add it if
1285 yaml_parser_save_simple_key(yaml_parser_t *parser)
1288 * A simple key is required at the current position if the scanner is in
1289 * the block context and the current column coincides with the indentation
1293 int required = (!parser->flow_level
1294 && parser->indent == parser->mark.column);
1297 * A simple key is required only when it is the first token in the current
1298 * line. Therefore it is always allowed. But we add a check anyway.
1301 assert(parser->simple_key_allowed || !required); /* Impossible. */
1304 * If the current position may start a simple key, save it.
1307 if (parser->simple_key_allowed)
1309 yaml_simple_key_t simple_key = { 1, required,
1310 parser->tokens_parsed + parser->tokens.tail - parser->tokens.head,
1313 if (!yaml_parser_remove_simple_key(parser)) return 0;
1315 *(parser->simple_keys.top-1) = simple_key;
1322 * Remove a potential simple key at the current flow level.
1326 yaml_parser_remove_simple_key(yaml_parser_t *parser)
1328 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1330 if (simple_key->possible)
1332 /* If the key is required, it is an error. */
1334 if (simple_key->required) {
1335 return yaml_parser_set_scanner_error(parser,
1336 "while scanning a simple key", simple_key->mark,
1337 "could not found expected ':'");
1341 /* Remove the key from the stack. */
1343 simple_key->possible = 0;
1349 * Increase the flow level and resize the simple key list if needed.
1353 yaml_parser_increase_flow_level(yaml_parser_t *parser)
1355 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1357 /* Reset the simple key on the next level. */
1359 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1362 /* Increase the flow level. */
1364 parser->flow_level++;
1370 * Decrease the flow level.
1374 yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1376 if (parser->flow_level) {
1377 parser->flow_level --;
1378 POP(parser, parser->simple_keys);
1385 * Push the current indentation level to the stack and set the new level
1386 * the current column is greater than the indentation level. In this case,
1387 * append or insert the specified token into the token queue.
1392 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1393 int number, yaml_token_type_t type, yaml_mark_t mark)
1397 /* In the flow context, do nothing. */
1399 if (parser->flow_level)
1402 if (parser->indent < column)
1405 * Push the current indentation level to the stack and set the new
1406 * indentation level.
1409 if (!PUSH(parser, parser->indents, parser->indent))
1412 parser->indent = column;
1414 /* Create a token and insert it into the queue. */
1416 TOKEN_INIT(token, type, mark, mark);
1419 if (!ENQUEUE(parser, parser->tokens, token))
1423 if (!QUEUE_INSERT(parser,
1424 parser->tokens, number - parser->tokens_parsed, token))
1433 * Pop indentation levels from the indents stack until the current level
1434 * becomes less or equal to the column. For each intendation level, append
1435 * the BLOCK-END token.
1440 yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1444 /* In the flow context, do nothing. */
1446 if (parser->flow_level)
1449 /* Loop through the intendation levels in the stack. */
1451 while (parser->indent > column)
1453 /* Create a token and append it to the queue. */
1455 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1457 if (!ENQUEUE(parser, parser->tokens, token))
1460 /* Pop the indentation level. */
1462 parser->indent = POP(parser, parser->indents);
1469 * Initialize the scanner and produce the STREAM-START token.
1473 yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1475 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1478 /* Set the initial indentation. */
1480 parser->indent = -1;
1482 /* Initialize the simple key stack. */
1484 if (!PUSH(parser, parser->simple_keys, simple_key))
1487 /* A simple key is allowed at the beginning of the stream. */
1489 parser->simple_key_allowed = 1;
1491 /* We have started. */
1493 parser->stream_start_produced = 1;
1495 /* Create the STREAM-START token and append it to the queue. */
1497 STREAM_START_TOKEN_INIT(token, parser->encoding,
1498 parser->mark, parser->mark);
1500 if (!ENQUEUE(parser, parser->tokens, token))
1507 * Produce the STREAM-END token and shut down the scanner.
1511 yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1515 /* Reset the indentation level. */
1517 if (!yaml_parser_unroll_indent(parser, -1))
1520 /* Reset simple keys. */
1522 if (!yaml_parser_remove_simple_key(parser))
1525 parser->simple_key_allowed = 0;
1527 /* Create the STREAM-END token and append it to the queue. */
1529 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1531 if (!ENQUEUE(parser, parser->tokens, token))
1538 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1542 yaml_parser_fetch_directive(yaml_parser_t *parser)
1546 /* Reset the indentation level. */
1548 if (!yaml_parser_unroll_indent(parser, -1))
1551 /* Reset simple keys. */
1553 if (!yaml_parser_remove_simple_key(parser))
1556 parser->simple_key_allowed = 0;
1558 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1560 if (!yaml_parser_scan_directive(parser, &token))
1563 /* Append the token to the queue. */
1565 if (!ENQUEUE(parser, parser->tokens, token)) {
1566 yaml_token_delete(&token);
1574 * Produce the DOCUMENT-START or DOCUMENT-END token.
1578 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1579 yaml_token_type_t type)
1581 yaml_mark_t start_mark, end_mark;
1584 /* Reset the indentation level. */
1586 if (!yaml_parser_unroll_indent(parser, -1))
1589 /* Reset simple keys. */
1591 if (!yaml_parser_remove_simple_key(parser))
1594 parser->simple_key_allowed = 0;
1596 /* Consume the token. */
1598 start_mark = parser->mark;
1604 end_mark = parser->mark;
1606 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1608 TOKEN_INIT(token, type, start_mark, end_mark);
1610 /* Append the token to the queue. */
1612 if (!ENQUEUE(parser, parser->tokens, token))
1619 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1623 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1624 yaml_token_type_t type)
1626 yaml_mark_t start_mark, end_mark;
1629 /* The indicators '[' and '{' may start a simple key. */
1631 if (!yaml_parser_save_simple_key(parser))
1634 /* Increase the flow level. */
1636 if (!yaml_parser_increase_flow_level(parser))
1639 /* A simple key may follow the indicators '[' and '{'. */
1641 parser->simple_key_allowed = 1;
1643 /* Consume the token. */
1645 start_mark = parser->mark;
1647 end_mark = parser->mark;
1649 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1651 TOKEN_INIT(token, type, start_mark, end_mark);
1653 /* Append the token to the queue. */
1655 if (!ENQUEUE(parser, parser->tokens, token))
1662 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1666 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1667 yaml_token_type_t type)
1669 yaml_mark_t start_mark, end_mark;
1672 /* Reset any potential simple key on the current flow level. */
1674 if (!yaml_parser_remove_simple_key(parser))
1677 /* Decrease the flow level. */
1679 if (!yaml_parser_decrease_flow_level(parser))
1682 /* No simple keys after the indicators ']' and '}'. */
1684 parser->simple_key_allowed = 0;
1686 /* Consume the token. */
1688 start_mark = parser->mark;
1690 end_mark = parser->mark;
1692 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1694 TOKEN_INIT(token, type, start_mark, end_mark);
1696 /* Append the token to the queue. */
1698 if (!ENQUEUE(parser, parser->tokens, token))
1705 * Produce the FLOW-ENTRY token.
1709 yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1711 yaml_mark_t start_mark, end_mark;
1714 /* Reset any potential simple keys on the current flow level. */
1716 if (!yaml_parser_remove_simple_key(parser))
1719 /* Simple keys are allowed after ','. */
1721 parser->simple_key_allowed = 1;
1723 /* Consume the token. */
1725 start_mark = parser->mark;
1727 end_mark = parser->mark;
1729 /* Create the FLOW-ENTRY token and append it to the queue. */
1731 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1733 if (!ENQUEUE(parser, parser->tokens, token))
1740 * Produce the BLOCK-ENTRY token.
1744 yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1746 yaml_mark_t start_mark, end_mark;
1749 /* Check if the scanner is in the block context. */
1751 if (!parser->flow_level)
1753 /* Check if we are allowed to start a new entry. */
1755 if (!parser->simple_key_allowed) {
1756 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1757 "block sequence entries are not allowed in this context");
1760 /* Add the BLOCK-SEQUENCE-START token if needed. */
1762 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1763 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1769 * It is an error for the '-' indicator to occur in the flow context,
1770 * but we let the Parser detect and report about it because the Parser
1771 * is able to point to the context.
1775 /* Reset any potential simple keys on the current flow level. */
1777 if (!yaml_parser_remove_simple_key(parser))
1780 /* Simple keys are allowed after '-'. */
1782 parser->simple_key_allowed = 1;
1784 /* Consume the token. */
1786 start_mark = parser->mark;
1788 end_mark = parser->mark;
1790 /* Create the BLOCK-ENTRY token and append it to the queue. */
1792 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1794 if (!ENQUEUE(parser, parser->tokens, token))
1801 * Produce the KEY token.
1805 yaml_parser_fetch_key(yaml_parser_t *parser)
1807 yaml_mark_t start_mark, end_mark;
1810 /* In the block context, additional checks are required. */
1812 if (!parser->flow_level)
1814 /* Check if we are allowed to start a new key (not nessesary simple). */
1816 if (!parser->simple_key_allowed) {
1817 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1818 "mapping keys are not allowed in this context");
1821 /* Add the BLOCK-MAPPING-START token if needed. */
1823 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1824 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1828 /* Reset any potential simple keys on the current flow level. */
1830 if (!yaml_parser_remove_simple_key(parser))
1833 /* Simple keys are allowed after '?' in the block context. */
1835 parser->simple_key_allowed = (!parser->flow_level);
1837 /* Consume the token. */
1839 start_mark = parser->mark;
1841 end_mark = parser->mark;
1843 /* Create the KEY token and append it to the queue. */
1845 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1847 if (!ENQUEUE(parser, parser->tokens, token))
1854 * Produce the VALUE token.
1858 yaml_parser_fetch_value(yaml_parser_t *parser)
1860 yaml_mark_t start_mark, end_mark;
1862 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1864 /* Have we found a simple key? */
1866 if (simple_key->possible)
1869 /* Create the KEY token and insert it into the queue. */
1871 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1873 if (!QUEUE_INSERT(parser, parser->tokens,
1874 simple_key->token_number - parser->tokens_parsed, token))
1877 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1879 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1880 simple_key->token_number,
1881 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1884 /* Remove the simple key. */
1886 simple_key->possible = 0;
1888 /* A simple key cannot follow another simple key. */
1890 parser->simple_key_allowed = 0;
1894 /* The ':' indicator follows a complex key. */
1896 /* In the block context, extra checks are required. */
1898 if (!parser->flow_level)
1900 /* Check if we are allowed to start a complex value. */
1902 if (!parser->simple_key_allowed) {
1903 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1904 "mapping values are not allowed in this context");
1907 /* Add the BLOCK-MAPPING-START token if needed. */
1909 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1910 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1914 /* Simple keys after ':' are allowed in the block context. */
1916 parser->simple_key_allowed = (!parser->flow_level);
1919 /* Consume the token. */
1921 start_mark = parser->mark;
1923 end_mark = parser->mark;
1925 /* Create the VALUE token and append it to the queue. */
1927 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1929 if (!ENQUEUE(parser, parser->tokens, token))
1936 * Produce the ALIAS or ANCHOR token.
1940 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1944 /* An anchor or an alias could be a simple key. */
1946 if (!yaml_parser_save_simple_key(parser))
1949 /* A simple key cannot follow an anchor or an alias. */
1951 parser->simple_key_allowed = 0;
1953 /* Create the ALIAS or ANCHOR token and append it to the queue. */
1955 if (!yaml_parser_scan_anchor(parser, &token, type))
1958 if (!ENQUEUE(parser, parser->tokens, token)) {
1959 yaml_token_delete(&token);
1966 * Produce the TAG token.
1970 yaml_parser_fetch_tag(yaml_parser_t *parser)
1974 /* A tag could be a simple key. */
1976 if (!yaml_parser_save_simple_key(parser))
1979 /* A simple key cannot follow a tag. */
1981 parser->simple_key_allowed = 0;
1983 /* Create the TAG token and append it to the queue. */
1985 if (!yaml_parser_scan_tag(parser, &token))
1988 if (!ENQUEUE(parser, parser->tokens, token)) {
1989 yaml_token_delete(&token);
1997 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
2001 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
2005 /* Remove any potential simple keys. */
2007 if (!yaml_parser_remove_simple_key(parser))
2010 /* A simple key may follow a block scalar. */
2012 parser->simple_key_allowed = 1;
2014 /* Create the SCALAR token and append it to the queue. */
2016 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
2019 if (!ENQUEUE(parser, parser->tokens, token)) {
2020 yaml_token_delete(&token);
2028 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
2032 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
2036 /* A plain scalar could be a simple key. */
2038 if (!yaml_parser_save_simple_key(parser))
2041 /* A simple key cannot follow a flow scalar. */
2043 parser->simple_key_allowed = 0;
2045 /* Create the SCALAR token and append it to the queue. */
2047 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
2050 if (!ENQUEUE(parser, parser->tokens, token)) {
2051 yaml_token_delete(&token);
2059 * Produce the SCALAR(...,plain) token.
2063 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
2067 /* A plain scalar could be a simple key. */
2069 if (!yaml_parser_save_simple_key(parser))
2072 /* A simple key cannot follow a flow scalar. */
2074 parser->simple_key_allowed = 0;
2076 /* Create the SCALAR token and append it to the queue. */
2078 if (!yaml_parser_scan_plain_scalar(parser, &token))
2081 if (!ENQUEUE(parser, parser->tokens, token)) {
2082 yaml_token_delete(&token);
2090 * Eat whitespaces and comments until the next token is found.
2094 yaml_parser_scan_to_next_token(yaml_parser_t *parser)
2096 /* Until the next token is not found. */
2100 /* Allow the BOM mark to start a line. */
2102 if (!CACHE(parser, 1)) return 0;
2104 if (parser->mark.column == 0 && IS_BOM(parser))
2112 * - in the flow context;
2113 * - in the block context, but not at the beginning of the line or
2114 * after '-', '?', or ':' (complex value).
2117 if (!CACHE(parser, 1)) return 0;
2119 while (CHECK(parser,' ') ||
2120 ((parser->flow_level || !parser->simple_key_allowed) &&
2121 CHECK(parser, '\t'))) {
2123 if (!CACHE(parser, 1)) return 0;
2126 /* Eat a comment until a line break. */
2128 if (CHECK(parser, '#')) {
2129 while (!IS_BREAKZ(parser)) {
2131 if (!CACHE(parser, 1)) return 0;
2135 /* If it is a line break, eat it. */
2137 if (IS_BREAK(parser))
2139 if (!CACHE(parser, 2)) return 0;
2142 /* In the block context, a new line may start a simple key. */
2144 if (!parser->flow_level) {
2145 parser->simple_key_allowed = 1;
2150 /* We have found a token. */
2160 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
2163 * %YAML 1.1 # a comment \n
2164 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2165 * %TAG !yaml! tag:yaml.org,2002: \n
2166 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2170 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
2172 yaml_mark_t start_mark, end_mark;
2173 yaml_char_t *name = NULL;
2175 yaml_char_t *handle = NULL, *prefix = NULL;
2179 start_mark = parser->mark;
2183 /* Scan the directive name. */
2185 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2188 /* Is it a YAML directive? */
2190 if (strcmp((char *)name, "YAML") == 0)
2192 /* Scan the VERSION directive value. */
2194 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2198 end_mark = parser->mark;
2200 /* Create a VERSION-DIRECTIVE token. */
2202 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2203 start_mark, end_mark);
2206 /* Is it a TAG directive? */
2208 else if (strcmp((char *)name, "TAG") == 0)
2210 /* Scan the TAG directive value. */
2212 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2216 end_mark = parser->mark;
2218 /* Create a TAG-DIRECTIVE token. */
2220 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2221 start_mark, end_mark);
2224 /* Unknown directive. */
2228 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2229 start_mark, "found uknown directive name");
2233 /* Eat the rest of the line including any comments. */
2235 if (!CACHE(parser, 1)) goto error;
2237 while (IS_BLANK(parser)) {
2239 if (!CACHE(parser, 1)) goto error;
2242 if (CHECK(parser, '#')) {
2243 while (!IS_BREAKZ(parser)) {
2245 if (!CACHE(parser, 1)) goto error;
2249 /* Check if we are at the end of the line. */
2251 if (!IS_BREAKZ(parser)) {
2252 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2253 start_mark, "did not found expected comment or line break");
2257 /* Eat a line break. */
2259 if (IS_BREAK(parser)) {
2260 if (!CACHE(parser, 2)) goto error;
2276 * Scan the directive name.
2279 * %YAML 1.1 # a comment \n
2281 * %TAG !yaml! tag:yaml.org,2002: \n
2286 yaml_parser_scan_directive_name(yaml_parser_t *parser,
2287 yaml_mark_t start_mark, yaml_char_t **name)
2289 yaml_string_t string = NULL_STRING;
2291 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2293 /* Consume the directive name. */
2295 if (!CACHE(parser, 1)) goto error;
2297 while (IS_ALPHA(parser))
2299 if (!READ(parser, string)) goto error;
2300 if (!CACHE(parser, 1)) goto error;
2303 /* Check if the name is empty. */
2305 if (string.start == string.pointer) {
2306 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2307 start_mark, "cannot found expected directive name");
2311 /* Check for an blank character after the name. */
2313 if (!IS_BLANKZ(parser)) {
2314 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2315 start_mark, "found unexpected non-alphabetical character");
2319 *name = string.start;
2324 STRING_DEL(parser, string);
2329 * Scan the value of VERSION-DIRECTIVE.
2332 * %YAML 1.1 # a comment \n
2337 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2338 yaml_mark_t start_mark, int *major, int *minor)
2340 /* Eat whitespaces. */
2342 if (!CACHE(parser, 1)) return 0;
2344 while (IS_BLANK(parser)) {
2346 if (!CACHE(parser, 1)) return 0;
2349 /* Consume the major version number. */
2351 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2356 if (!CHECK(parser, '.')) {
2357 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2358 start_mark, "did not find expected digit or '.' character");
2363 /* Consume the minor version number. */
2365 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2371 #define MAX_NUMBER_LENGTH 9
2374 * Scan the version number of VERSION-DIRECTIVE.
2377 * %YAML 1.1 # a comment \n
2379 * %YAML 1.1 # a comment \n
2384 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2385 yaml_mark_t start_mark, int *number)
2390 /* Repeat while the next character is digit. */
2392 if (!CACHE(parser, 1)) return 0;
2394 while (IS_DIGIT(parser))
2396 /* Check if the number is too long. */
2398 if (++length > MAX_NUMBER_LENGTH) {
2399 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2400 start_mark, "found extremely long version number");
2403 value = value*10 + AS_DIGIT(parser);
2407 if (!CACHE(parser, 1)) return 0;
2410 /* Check if the number was present. */
2413 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2414 start_mark, "did not find expected version number");
2423 * Scan the value of a TAG-DIRECTIVE token.
2426 * %TAG !yaml! tag:yaml.org,2002: \n
2427 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2431 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2432 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2434 yaml_char_t *handle_value = NULL;
2435 yaml_char_t *prefix_value = NULL;
2437 /* Eat whitespaces. */
2439 if (!CACHE(parser, 1)) goto error;
2441 while (IS_BLANK(parser)) {
2443 if (!CACHE(parser, 1)) goto error;
2446 /* Scan a handle. */
2448 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2451 /* Expect a whitespace. */
2453 if (!CACHE(parser, 1)) goto error;
2455 if (!IS_BLANK(parser)) {
2456 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2457 start_mark, "did not find expected whitespace");
2461 /* Eat whitespaces. */
2463 while (IS_BLANK(parser)) {
2465 if (!CACHE(parser, 1)) goto error;
2468 /* Scan a prefix. */
2470 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2473 /* Expect a whitespace or line break. */
2475 if (!CACHE(parser, 1)) goto error;
2477 if (!IS_BLANKZ(parser)) {
2478 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2479 start_mark, "did not find expected whitespace or line break");
2483 *handle = handle_value;
2484 *prefix = prefix_value;
2489 yaml_free(handle_value);
2490 yaml_free(prefix_value);
2495 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2496 yaml_token_type_t type)
2499 yaml_mark_t start_mark, end_mark;
2500 yaml_string_t string = NULL_STRING;
2502 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2504 /* Eat the indicator character. */
2506 start_mark = parser->mark;
2510 /* Consume the value. */
2512 if (!CACHE(parser, 1)) goto error;
2514 while (IS_ALPHA(parser)) {
2515 if (!READ(parser, string)) goto error;
2516 if (!CACHE(parser, 1)) goto error;
2520 end_mark = parser->mark;
2523 * Check if length of the anchor is greater than 0 and it is followed by
2524 * a whitespace character or one of the indicators:
2526 * '?', ':', ',', ']', '}', '%', '@', '`'.
2529 if (!length || !(IS_BLANKZ(parser) || CHECK(parser, '?') || CHECK(parser, ':') ||
2530 CHECK(parser, ',') || CHECK(parser, ']') || CHECK(parser, '}') ||
2531 CHECK(parser, '%') || CHECK(parser, '@') || CHECK(parser, '`'))) {
2532 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2533 "while scanning an anchor" : "while scanning an alias", start_mark,
2534 "did not find expected alphabetic or numeric character");
2538 /* Create a token. */
2540 if (type == YAML_ANCHOR_TOKEN) {
2541 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2544 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2550 STRING_DEL(parser, string);
2559 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2561 yaml_char_t *handle = NULL;
2562 yaml_char_t *suffix = NULL;
2563 yaml_mark_t start_mark, end_mark;
2565 start_mark = parser->mark;
2567 /* Check if the tag is in the canonical form. */
2569 if (!CACHE(parser, 2)) goto error;
2571 if (CHECK_AT(parser, '<', 1))
2573 /* Set the handle to '' */
2575 handle = yaml_malloc(1);
2576 if (!handle) goto error;
2584 /* Consume the tag value. */
2586 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2589 /* Check for '>' and eat it. */
2591 if (!CHECK(parser, '>')) {
2592 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2593 start_mark, "did not find the expected '>'");
2601 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2603 /* First, try to scan a handle. */
2605 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2608 /* Check if it is, indeed, handle. */
2610 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2612 /* Scan the suffix now. */
2614 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2619 /* It wasn't a handle after all. Scan the rest of the tag. */
2621 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2624 /* Set the handle to '!'. */
2627 handle = yaml_malloc(2);
2628 if (!handle) goto error;
2633 * A special case: the '!' tag. Set the handle to '' and the
2637 if (suffix[0] == '\0') {
2638 yaml_char_t *tmp = handle;
2645 /* Check the character which ends the tag. */
2647 if (!CACHE(parser, 1)) goto error;
2649 if (!IS_BLANKZ(parser)) {
2650 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2651 start_mark, "did not found expected whitespace or line break");
2655 end_mark = parser->mark;
2657 /* Create a token. */
2659 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2670 * Scan a tag handle.
2674 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2675 yaml_mark_t start_mark, yaml_char_t **handle)
2677 yaml_string_t string = NULL_STRING;
2679 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2681 /* Check the initial '!' character. */
2683 if (!CACHE(parser, 1)) goto error;
2685 if (!CHECK(parser, '!')) {
2686 yaml_parser_set_scanner_error(parser, directive ?
2687 "while scanning a tag directive" : "while scanning a tag",
2688 start_mark, "did not find expected '!'");
2692 /* Copy the '!' character. */
2694 if (!READ(parser, string)) goto error;
2696 /* Copy all subsequent alphabetical and numerical characters. */
2698 if (!CACHE(parser, 1)) goto error;
2700 while (IS_ALPHA(parser))
2702 if (!READ(parser, string)) goto error;
2703 if (!CACHE(parser, 1)) goto error;
2706 /* Check if the trailing character is '!' and copy it. */
2708 if (CHECK(parser, '!'))
2710 if (!READ(parser, string)) goto error;
2715 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2716 * directive, it's an error. If it's a tag token, it must be a part of
2720 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2721 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2722 start_mark, "did not find expected '!'");
2727 *handle = string.start;
2732 STRING_DEL(parser, string);
2741 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2742 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2744 size_t length = head ? strlen((char *)head) : 0;
2745 yaml_string_t string = NULL_STRING;
2747 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2749 /* Resize the string to include the head. */
2751 while (string.end - string.start <= length) {
2752 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2753 parser->error = YAML_MEMORY_ERROR;
2759 * Copy the head if needed.
2761 * Note that we don't copy the leading '!' character.
2765 memcpy(string.start, head+1, length-1);
2766 string.pointer += length-1;
2771 if (!CACHE(parser, 1)) goto error;
2774 * The set of characters that may appear in URI is as follows:
2776 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2777 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2781 while (IS_ALPHA(parser) || CHECK(parser, ';') || CHECK(parser, '/') ||
2782 CHECK(parser, '?') || CHECK(parser, ':') || CHECK(parser, '@') ||
2783 CHECK(parser, '&') || CHECK(parser, '=') || CHECK(parser, '+') ||
2784 CHECK(parser, '$') || CHECK(parser, ',') || CHECK(parser, '.') ||
2785 CHECK(parser, '!') || CHECK(parser, '~') || CHECK(parser, '*') ||
2786 CHECK(parser, '\'') || CHECK(parser, '(') || CHECK(parser, ')') ||
2787 CHECK(parser, '[') || CHECK(parser, ']') || CHECK(parser, '%'))
2789 /* Check if it is a URI-escape sequence. */
2791 if (CHECK(parser, '%')) {
2792 if (!yaml_parser_scan_uri_escapes(parser,
2793 directive, start_mark, &string)) goto error;
2796 if (!READ(parser, string)) goto error;
2800 if (!CACHE(parser, 1)) goto error;
2803 /* Check if the tag is non-empty. */
2806 if (!STRING_EXTEND(parser, string))
2809 yaml_parser_set_scanner_error(parser, directive ?
2810 "while parsing a %TAG directive" : "while parsing a tag",
2811 start_mark, "did not find expected tag URI");
2815 *uri = string.start;
2820 STRING_DEL(parser, string);
2825 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2829 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2830 yaml_mark_t start_mark, yaml_string_t *string)
2834 /* Decode the required number of characters. */
2838 unsigned char octet = 0;
2840 /* Check for a URI-escaped octet. */
2842 if (!CACHE(parser, 3)) return 0;
2844 if (!(CHECK(parser, '%') && IS_HEX_AT(parser, 1) && IS_HEX_AT(parser, 2))) {
2845 return yaml_parser_set_scanner_error(parser, directive ?
2846 "while parsing a %TAG directive" : "while parsing a tag",
2847 start_mark, "did not find URI escaped octet");
2850 /* Get the octet. */
2852 octet = (AS_HEX_AT(parser, 1) << 4) + AS_HEX_AT(parser, 2);
2854 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2858 width = (octet & 0x80) == 0x00 ? 1 :
2859 (octet & 0xE0) == 0xC0 ? 2 :
2860 (octet & 0xF0) == 0xE0 ? 3 :
2861 (octet & 0xF8) == 0xF0 ? 4 : 0;
2863 return yaml_parser_set_scanner_error(parser, directive ?
2864 "while parsing a %TAG directive" : "while parsing a tag",
2865 start_mark, "found an incorrect leading UTF-8 octet");
2870 /* Check if the trailing octet is correct. */
2872 if ((octet & 0xC0) != 0x80) {
2873 return yaml_parser_set_scanner_error(parser, directive ?
2874 "while parsing a %TAG directive" : "while parsing a tag",
2875 start_mark, "found an incorrect trailing UTF-8 octet");
2879 /* Copy the octet and move the pointers. */
2881 *(string->pointer++) = octet;
2892 * Scan a block scalar.
2896 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2899 yaml_mark_t start_mark;
2900 yaml_mark_t end_mark;
2901 yaml_string_t string = NULL_STRING;
2902 yaml_string_t leading_break = NULL_STRING;
2903 yaml_string_t trailing_breaks = NULL_STRING;
2907 int leading_blank = 0;
2908 int trailing_blank = 0;
2910 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2911 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2912 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2914 /* Eat the indicator '|' or '>'. */
2916 start_mark = parser->mark;
2920 /* Scan the additional block scalar indicators. */
2922 if (!CACHE(parser, 1)) goto error;
2924 /* Check for a chomping indicator. */
2926 if (CHECK(parser, '+') || CHECK(parser, '-'))
2928 /* Set the chomping method and eat the indicator. */
2930 chomping = CHECK(parser, '+') ? +1 : -1;
2934 /* Check for an indentation indicator. */
2936 if (!CACHE(parser, 1)) goto error;
2938 if (IS_DIGIT(parser))
2940 /* Check that the intendation is greater than 0. */
2942 if (CHECK(parser, '0')) {
2943 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2944 start_mark, "found an intendation indicator equal to 0");
2948 /* Get the intendation level and eat the indicator. */
2950 increment = AS_DIGIT(parser);
2956 /* Do the same as above, but in the opposite order. */
2958 else if (IS_DIGIT(parser))
2960 if (CHECK(parser, '0')) {
2961 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2962 start_mark, "found an intendation indicator equal to 0");
2966 increment = AS_DIGIT(parser);
2970 if (!CACHE(parser, 1)) goto error;
2972 if (CHECK(parser, '+') || CHECK(parser, '-')) {
2973 chomping = CHECK(parser, '+') ? +1 : -1;
2979 /* Eat whitespaces and comments to the end of the line. */
2981 if (!CACHE(parser, 1)) goto error;
2983 while (IS_BLANK(parser)) {
2985 if (!CACHE(parser, 1)) goto error;
2988 if (CHECK(parser, '#')) {
2989 while (!IS_BREAKZ(parser)) {
2991 if (!CACHE(parser, 1)) goto error;
2995 /* Check if we are at the end of the line. */
2997 if (!IS_BREAKZ(parser)) {
2998 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2999 start_mark, "did not found expected comment or line break");
3003 /* Eat a line break. */
3005 if (IS_BREAK(parser)) {
3006 if (!CACHE(parser, 2)) goto error;
3010 end_mark = parser->mark;
3012 /* Set the intendation level if it was specified. */
3015 indent = parser->indent >= 0 ? parser->indent+increment : increment;
3018 /* Scan the leading line breaks and determine the indentation level if needed. */
3020 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
3021 start_mark, &end_mark)) goto error;
3023 /* Scan the block scalar content. */
3025 if (!CACHE(parser, 1)) goto error;
3027 while (parser->mark.column == indent && !IS_Z(parser))
3030 * We are at the beginning of a non-empty line.
3033 /* Is it a trailing whitespace? */
3035 trailing_blank = IS_BLANK(parser);
3037 /* Check if we need to fold the leading line break. */
3039 if (!literal && (*leading_break.start == '\n')
3040 && !leading_blank && !trailing_blank)
3042 /* Do we need to join the lines by space? */
3044 if (*trailing_breaks.start == '\0') {
3045 if (!STRING_EXTEND(parser, string)) goto error;
3046 *(string.pointer ++) = ' ';
3049 CLEAR(parser, leading_break);
3052 if (!JOIN(parser, string, leading_break)) goto error;
3053 CLEAR(parser, leading_break);
3056 /* Append the remaining line breaks. */
3058 if (!JOIN(parser, string, trailing_breaks)) goto error;
3059 CLEAR(parser, trailing_breaks);
3061 /* Is it a leading whitespace? */
3063 leading_blank = IS_BLANK(parser);
3065 /* Consume the current line. */
3067 while (!IS_BREAKZ(parser)) {
3068 if (!READ(parser, string)) goto error;
3069 if (!CACHE(parser, 1)) goto error;
3072 /* Consume the line break. */
3074 if (!CACHE(parser, 2)) goto error;
3076 if (!READ_LINE(parser, leading_break)) goto error;
3078 /* Eat the following intendation spaces and line breaks. */
3080 if (!yaml_parser_scan_block_scalar_breaks(parser,
3081 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
3084 /* Chomp the tail. */
3086 if (chomping != -1) {
3087 if (!JOIN(parser, string, leading_break)) goto error;
3089 if (chomping == 1) {
3090 if (!JOIN(parser, string, trailing_breaks)) goto error;
3093 /* Create a token. */
3095 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3096 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
3097 start_mark, end_mark);
3099 STRING_DEL(parser, leading_break);
3100 STRING_DEL(parser, trailing_breaks);
3105 STRING_DEL(parser, string);
3106 STRING_DEL(parser, leading_break);
3107 STRING_DEL(parser, trailing_breaks);
3113 * Scan intendation spaces and line breaks for a block scalar. Determine the
3114 * intendation level if needed.
3118 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
3119 int *indent, yaml_string_t *breaks,
3120 yaml_mark_t start_mark, yaml_mark_t *end_mark)
3124 *end_mark = parser->mark;
3126 /* Eat the intendation spaces and line breaks. */
3130 /* Eat the intendation spaces. */
3132 if (!CACHE(parser, 1)) return 0;
3134 while ((!*indent || parser->mark.column < *indent) && IS_SPACE(parser)) {
3136 if (!CACHE(parser, 1)) return 0;
3139 if (parser->mark.column > max_indent)
3140 max_indent = parser->mark.column;
3142 /* Check for a tab character messing the intendation. */
3144 if ((!*indent || parser->mark.column < *indent) && IS_TAB(parser)) {
3145 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
3146 start_mark, "found a tab character where an intendation space is expected");
3149 /* Have we found a non-empty line? */
3151 if (!IS_BREAK(parser)) break;
3153 /* Consume the line break. */
3155 if (!CACHE(parser, 2)) return 0;
3156 if (!READ_LINE(parser, *breaks)) return 0;
3157 *end_mark = parser->mark;
3160 /* Determine the indentation level if needed. */
3163 *indent = max_indent;
3164 if (*indent < parser->indent + 1)
3165 *indent = parser->indent + 1;
3174 * Scan a quoted scalar.
3178 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3181 yaml_mark_t start_mark;
3182 yaml_mark_t end_mark;
3183 yaml_string_t string = NULL_STRING;
3184 yaml_string_t leading_break = NULL_STRING;
3185 yaml_string_t trailing_breaks = NULL_STRING;
3186 yaml_string_t whitespaces = NULL_STRING;
3189 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3190 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3191 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3192 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3194 /* Eat the left quote. */
3196 start_mark = parser->mark;
3200 /* Consume the content of the quoted scalar. */
3204 /* Check that there are no document indicators at the beginning of the line. */
3206 if (!CACHE(parser, 4)) goto error;
3208 if (parser->mark.column == 0 &&
3209 ((CHECK_AT(parser, '-', 0) &&
3210 CHECK_AT(parser, '-', 1) &&
3211 CHECK_AT(parser, '-', 2)) ||
3212 (CHECK_AT(parser, '.', 0) &&
3213 CHECK_AT(parser, '.', 1) &&
3214 CHECK_AT(parser, '.', 2))) &&
3215 IS_BLANKZ_AT(parser, 3))
3217 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3218 start_mark, "found unexpected document indicator");
3222 /* Check for EOF. */
3225 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3226 start_mark, "found unexpected end of stream");
3230 /* Consume non-blank characters. */
3232 if (!CACHE(parser, 2)) goto error;
3236 while (!IS_BLANKZ(parser))
3238 /* Check for an escaped single quote. */
3240 if (single && CHECK_AT(parser, '\'', 0) && CHECK_AT(parser, '\'', 1))
3242 if (!STRING_EXTEND(parser, string)) goto error;
3243 *(string.pointer++) = '\'';
3248 /* Check for the right quote. */
3250 else if (CHECK(parser, single ? '\'' : '"'))
3255 /* Check for an escaped line break. */
3257 else if (!single && CHECK(parser, '\\') && IS_BREAK_AT(parser, 1))
3259 if (!CACHE(parser, 3)) goto error;
3266 /* Check for an escape sequence. */
3268 else if (!single && CHECK(parser, '\\'))
3270 int code_length = 0;
3272 if (!STRING_EXTEND(parser, string)) goto error;
3274 /* Check the escape character. */
3276 switch (parser->buffer.pointer[1])
3279 *(string.pointer++) = '\0';
3283 *(string.pointer++) = '\x07';
3287 *(string.pointer++) = '\x08';
3292 *(string.pointer++) = '\x09';
3296 *(string.pointer++) = '\x0A';
3300 *(string.pointer++) = '\x0B';
3304 *(string.pointer++) = '\x0C';
3308 *(string.pointer++) = '\x0D';
3312 *(string.pointer++) = '\x1B';
3316 *(string.pointer++) = '\x20';
3320 *(string.pointer++) = '"';
3324 *(string.pointer++) = '\'';
3328 *(string.pointer++) = '\\';
3331 case 'N': /* NEL (#x85) */
3332 *(string.pointer++) = '\xC2';
3333 *(string.pointer++) = '\x85';
3336 case '_': /* #xA0 */
3337 *(string.pointer++) = '\xC2';
3338 *(string.pointer++) = '\xA0';
3341 case 'L': /* LS (#x2028) */
3342 *(string.pointer++) = '\xE2';
3343 *(string.pointer++) = '\x80';
3344 *(string.pointer++) = '\xA8';
3347 case 'P': /* PS (#x2029) */
3348 *(string.pointer++) = '\xE2';
3349 *(string.pointer++) = '\x80';
3350 *(string.pointer++) = '\xA9';
3366 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3367 start_mark, "found unknown escape character");
3374 /* Consume an arbitrary escape code. */
3378 unsigned int value = 0;
3381 /* Scan the character value. */
3383 if (!CACHE(parser, code_length)) goto error;
3385 for (k = 0; k < code_length; k ++) {
3386 if (!IS_HEX_AT(parser, k)) {
3387 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3388 start_mark, "did not find expected hexdecimal number");
3391 value = (value << 4) + AS_HEX_AT(parser, k);
3394 /* Check the value and write the character. */
3396 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3397 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3398 start_mark, "found invalid Unicode character escape code");
3402 if (value <= 0x7F) {
3403 *(string.pointer++) = value;
3405 else if (value <= 0x7FF) {
3406 *(string.pointer++) = 0xC0 + (value >> 6);
3407 *(string.pointer++) = 0x80 + (value & 0x3F);
3409 else if (value <= 0xFFFF) {
3410 *(string.pointer++) = 0xE0 + (value >> 12);
3411 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3412 *(string.pointer++) = 0x80 + (value & 0x3F);
3415 *(string.pointer++) = 0xF0 + (value >> 18);
3416 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3417 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3418 *(string.pointer++) = 0x80 + (value & 0x3F);
3421 /* Advance the pointer. */
3423 for (k = 0; k < code_length; k ++) {
3431 /* It is a non-escaped non-blank character. */
3433 if (!READ(parser, string)) goto error;
3436 if (!CACHE(parser, 2)) goto error;
3439 /* Check if we are at the end of the scalar. */
3441 if (CHECK(parser, single ? '\'' : '"'))
3444 /* Consume blank characters. */
3446 if (!CACHE(parser, 1)) goto error;
3448 while (IS_BLANK(parser) || IS_BREAK(parser))
3450 if (IS_BLANK(parser))
3452 /* Consume a space or a tab character. */
3454 if (!leading_blanks) {
3455 if (!READ(parser, whitespaces)) goto error;
3463 if (!CACHE(parser, 2)) goto error;
3465 /* Check if it is a first line break. */
3467 if (!leading_blanks)
3469 CLEAR(parser, whitespaces);
3470 if (!READ_LINE(parser, leading_break)) goto error;
3475 if (!READ_LINE(parser, trailing_breaks)) goto error;
3478 if (!CACHE(parser, 1)) goto error;
3481 /* Join the whitespaces or fold line breaks. */
3485 /* Do we need to fold line breaks? */
3487 if (leading_break.start[0] == '\n') {
3488 if (trailing_breaks.start[0] == '\0') {
3489 if (!STRING_EXTEND(parser, string)) goto error;
3490 *(string.pointer++) = ' ';
3493 if (!JOIN(parser, string, trailing_breaks)) goto error;
3494 CLEAR(parser, trailing_breaks);
3496 CLEAR(parser, leading_break);
3499 if (!JOIN(parser, string, leading_break)) goto error;
3500 if (!JOIN(parser, string, trailing_breaks)) goto error;
3501 CLEAR(parser, leading_break);
3502 CLEAR(parser, trailing_breaks);
3507 if (!JOIN(parser, string, whitespaces)) goto error;
3508 CLEAR(parser, whitespaces);
3512 /* Eat the right quote. */
3516 end_mark = parser->mark;
3518 /* Create a token. */
3520 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3521 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3522 start_mark, end_mark);
3524 STRING_DEL(parser, leading_break);
3525 STRING_DEL(parser, trailing_breaks);
3526 STRING_DEL(parser, whitespaces);
3531 STRING_DEL(parser, string);
3532 STRING_DEL(parser, leading_break);
3533 STRING_DEL(parser, trailing_breaks);
3534 STRING_DEL(parser, whitespaces);
3540 * Scan a plain scalar.
3544 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3546 yaml_mark_t start_mark;
3547 yaml_mark_t end_mark;
3548 yaml_string_t string = NULL_STRING;
3549 yaml_string_t leading_break = NULL_STRING;
3550 yaml_string_t trailing_breaks = NULL_STRING;
3551 yaml_string_t whitespaces = NULL_STRING;
3552 int leading_blanks = 0;
3553 int indent = parser->indent+1;
3555 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3556 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3557 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3558 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3560 start_mark = end_mark = parser->mark;
3562 /* Consume the content of the plain scalar. */
3566 /* Check for a document indicator. */
3568 if (!CACHE(parser, 4)) goto error;
3570 if (parser->mark.column == 0 &&
3571 ((CHECK_AT(parser, '-', 0) &&
3572 CHECK_AT(parser, '-', 1) &&
3573 CHECK_AT(parser, '-', 2)) ||
3574 (CHECK_AT(parser, '.', 0) &&
3575 CHECK_AT(parser, '.', 1) &&
3576 CHECK_AT(parser, '.', 2))) &&
3577 IS_BLANKZ_AT(parser, 3)) break;
3579 /* Check for a comment. */
3581 if (CHECK(parser, '#'))
3584 /* Consume non-blank characters. */
3586 while (!IS_BLANKZ(parser))
3588 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
3590 if (parser->flow_level && CHECK(parser, ':') && !IS_BLANKZ_AT(parser, 1)) {
3591 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3592 start_mark, "found unexpected ':'");
3596 /* Check for indicators that may end a plain scalar. */
3598 if ((CHECK(parser, ':') && IS_BLANKZ_AT(parser, 1)) ||
3599 (parser->flow_level &&
3600 (CHECK(parser, ',') || CHECK(parser, ':') ||
3601 CHECK(parser, '?') || CHECK(parser, '[') ||
3602 CHECK(parser, ']') || CHECK(parser, '{') ||
3603 CHECK(parser, '}'))))
3606 /* Check if we need to join whitespaces and breaks. */
3608 if (leading_blanks || whitespaces.start != whitespaces.pointer)
3612 /* Do we need to fold line breaks? */
3614 if (leading_break.start[0] == '\n') {
3615 if (trailing_breaks.start[0] == '\0') {
3616 if (!STRING_EXTEND(parser, string)) goto error;
3617 *(string.pointer++) = ' ';
3620 if (!JOIN(parser, string, trailing_breaks)) goto error;
3621 CLEAR(parser, trailing_breaks);
3623 CLEAR(parser, leading_break);
3626 if (!JOIN(parser, string, leading_break)) goto error;
3627 if (!JOIN(parser, string, trailing_breaks)) goto error;
3628 CLEAR(parser, leading_break);
3629 CLEAR(parser, trailing_breaks);
3636 if (!JOIN(parser, string, whitespaces)) goto error;
3637 CLEAR(parser, whitespaces);
3641 /* Copy the character. */
3643 if (!READ(parser, string)) goto error;
3645 end_mark = parser->mark;
3647 if (!CACHE(parser, 2)) goto error;
3650 /* Is it the end? */
3652 if (!(IS_BLANK(parser) || IS_BREAK(parser)))
3655 /* Consume blank characters. */
3657 if (!CACHE(parser, 1)) goto error;
3659 while (IS_BLANK(parser) || IS_BREAK(parser))
3661 if (IS_BLANK(parser))
3663 /* Check for tab character that abuse intendation. */
3665 if (leading_blanks && parser->mark.column < indent && IS_TAB(parser)) {
3666 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3667 start_mark, "found a tab character that violate intendation");
3671 /* Consume a space or a tab character. */
3673 if (!leading_blanks) {
3674 if (!READ(parser, whitespaces)) goto error;
3682 if (!CACHE(parser, 2)) goto error;
3684 /* Check if it is a first line break. */
3686 if (!leading_blanks)
3688 CLEAR(parser, whitespaces);
3689 if (!READ_LINE(parser, leading_break)) goto error;
3694 if (!READ_LINE(parser, trailing_breaks)) goto error;
3697 if (!CACHE(parser, 1)) goto error;
3700 /* Check intendation level. */
3702 if (!parser->flow_level && parser->mark.column < indent)
3706 /* Create a token. */
3708 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3709 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3711 /* Note that we change the 'simple_key_allowed' flag. */
3713 if (leading_blanks) {
3714 parser->simple_key_allowed = 1;
3717 STRING_DEL(parser, leading_break);
3718 STRING_DEL(parser, trailing_breaks);
3719 STRING_DEL(parser, whitespaces);
3724 STRING_DEL(parser, string);
3725 STRING_DEL(parser, leading_break);
3726 STRING_DEL(parser, trailing_breaks);
3727 STRING_DEL(parser, whitespaces);