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 /* No tokens after STREAM-END or error. */
943 if (parser->stream_end_produced || parser->error) {
944 memset(token, 0, sizeof(yaml_token_t));
949 /* Ensure that the tokens queue contains enough tokens. */
951 if (!parser->token_available) {
952 if (!yaml_parser_fetch_more_tokens(parser))
956 /* Fetch the next token from the queue. */
958 *token = DEQUEUE(parser, parser->tokens);
959 parser->token_available = 0;
960 parser->tokens_parsed ++;
962 if (token->type == YAML_STREAM_END_TOKEN) {
963 parser->stream_end_produced = 1;
970 * Set the scanner error and return 0.
974 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
975 yaml_mark_t context_mark, const char *problem)
977 parser->error = YAML_SCANNER_ERROR;
978 parser->context = context;
979 parser->context_mark = context_mark;
980 parser->problem = problem;
981 parser->problem_mark = parser->mark;
987 * Ensure that the tokens queue contains at least one token which can be
988 * returned to the Parser.
992 yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
994 int need_more_tokens;
996 /* While we need more tokens to fetch, do it. */
1001 * Check if we really need to fetch more tokens.
1004 need_more_tokens = 0;
1006 if (parser->tokens.head == parser->tokens.tail)
1008 /* Queue is empty. */
1010 need_more_tokens = 1;
1014 yaml_simple_key_t *simple_key;
1016 /* Check if any potential simple key may occupy the head position. */
1018 if (!yaml_parser_stale_simple_keys(parser))
1021 for (simple_key = parser->simple_keys.start;
1022 simple_key != parser->simple_keys.top; simple_key++) {
1023 if (simple_key->possible
1024 && simple_key->token_number == parser->tokens_parsed) {
1025 need_more_tokens = 1;
1031 /* We are finished. */
1033 if (!need_more_tokens)
1036 /* Fetch the next token. */
1038 if (!yaml_parser_fetch_next_token(parser))
1042 parser->token_available = 1;
1048 * The dispatcher for token fetchers.
1052 yaml_parser_fetch_next_token(yaml_parser_t *parser)
1054 /* Ensure that the buffer is initialized. */
1056 if (!CACHE(parser, 1))
1059 /* Check if we just started scanning. Fetch STREAM-START then. */
1061 if (!parser->stream_start_produced)
1062 return yaml_parser_fetch_stream_start(parser);
1064 /* Eat whitespaces and comments until we reach the next token. */
1066 if (!yaml_parser_scan_to_next_token(parser))
1069 /* Remove obsolete potential simple keys. */
1071 if (!yaml_parser_stale_simple_keys(parser))
1074 /* Check the indentation level against the current column. */
1076 if (!yaml_parser_unroll_indent(parser, parser->mark.column))
1080 * Ensure that the buffer contains at least 4 characters. 4 is the length
1081 * of the longest indicators ('--- ' and '... ').
1084 if (!CACHE(parser, 4))
1087 /* Is it the end of the stream? */
1090 return yaml_parser_fetch_stream_end(parser);
1092 /* Is it a directive? */
1094 if (parser->mark.column == 0 && CHECK(parser, '%'))
1095 return yaml_parser_fetch_directive(parser);
1097 /* Is it the document start indicator? */
1099 if (parser->mark.column == 0
1100 && CHECK_AT(parser, '-', 0)
1101 && CHECK_AT(parser, '-', 1)
1102 && CHECK_AT(parser, '-', 2)
1103 && IS_BLANKZ_AT(parser, 3))
1104 return yaml_parser_fetch_document_indicator(parser,
1105 YAML_DOCUMENT_START_TOKEN);
1107 /* Is it the document end indicator? */
1109 if (parser->mark.column == 0
1110 && CHECK_AT(parser, '.', 0)
1111 && CHECK_AT(parser, '.', 1)
1112 && CHECK_AT(parser, '.', 2)
1113 && IS_BLANKZ_AT(parser, 3))
1114 return yaml_parser_fetch_document_indicator(parser,
1115 YAML_DOCUMENT_END_TOKEN);
1117 /* Is it the flow sequence start indicator? */
1119 if (CHECK(parser, '['))
1120 return yaml_parser_fetch_flow_collection_start(parser,
1121 YAML_FLOW_SEQUENCE_START_TOKEN);
1123 /* Is it the flow mapping start indicator? */
1125 if (CHECK(parser, '{'))
1126 return yaml_parser_fetch_flow_collection_start(parser,
1127 YAML_FLOW_MAPPING_START_TOKEN);
1129 /* Is it the flow sequence end indicator? */
1131 if (CHECK(parser, ']'))
1132 return yaml_parser_fetch_flow_collection_end(parser,
1133 YAML_FLOW_SEQUENCE_END_TOKEN);
1135 /* Is it the flow mapping end indicator? */
1137 if (CHECK(parser, '}'))
1138 return yaml_parser_fetch_flow_collection_end(parser,
1139 YAML_FLOW_MAPPING_END_TOKEN);
1141 /* Is it the flow entry indicator? */
1143 if (CHECK(parser, ','))
1144 return yaml_parser_fetch_flow_entry(parser);
1146 /* Is it the block entry indicator? */
1148 if (CHECK(parser, '-') && IS_BLANKZ_AT(parser, 1))
1149 return yaml_parser_fetch_block_entry(parser);
1151 /* Is it the key indicator? */
1153 if (CHECK(parser, '?') && (parser->flow_level || IS_BLANKZ_AT(parser, 1)))
1154 return yaml_parser_fetch_key(parser);
1156 /* Is it the value indicator? */
1158 if (CHECK(parser, ':') && (parser->flow_level || IS_BLANKZ_AT(parser, 1)))
1159 return yaml_parser_fetch_value(parser);
1161 /* Is it an alias? */
1163 if (CHECK(parser, '*'))
1164 return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
1166 /* Is it an anchor? */
1168 if (CHECK(parser, '&'))
1169 return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
1173 if (CHECK(parser, '!'))
1174 return yaml_parser_fetch_tag(parser);
1176 /* Is it a literal scalar? */
1178 if (CHECK(parser, '|') && !parser->flow_level)
1179 return yaml_parser_fetch_block_scalar(parser, 1);
1181 /* Is it a folded scalar? */
1183 if (CHECK(parser, '>') && !parser->flow_level)
1184 return yaml_parser_fetch_block_scalar(parser, 0);
1186 /* Is it a single-quoted scalar? */
1188 if (CHECK(parser, '\''))
1189 return yaml_parser_fetch_flow_scalar(parser, 1);
1191 /* Is it a double-quoted scalar? */
1193 if (CHECK(parser, '"'))
1194 return yaml_parser_fetch_flow_scalar(parser, 0);
1197 * Is it a plain scalar?
1199 * A plain scalar may start with any non-blank characters except
1201 * '-', '?', ':', ',', '[', ']', '{', '}',
1202 * '#', '&', '*', '!', '|', '>', '\'', '\"',
1205 * In the block context (and, for the '-' indicator, in the flow context
1206 * too), it may also start with the characters
1210 * if it is followed by a non-space character.
1212 * The last rule is more restrictive than the specification requires.
1215 if (!(IS_BLANKZ(parser) || CHECK(parser, '-') || CHECK(parser, '?')
1216 || CHECK(parser, ':') || CHECK(parser, ',') || CHECK(parser, '[')
1217 || CHECK(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, '`')) ||
1222 (CHECK(parser, '-') && !IS_BLANK_AT(parser, 1)) ||
1223 (!parser->flow_level &&
1224 (CHECK(parser, '?') || CHECK(parser, ':')) && !IS_BLANKZ_AT(parser, 1)))
1225 return yaml_parser_fetch_plain_scalar(parser);
1228 * If we don't determine the token type so far, it is an error.
1231 return yaml_parser_set_scanner_error(parser,
1232 "while scanning for the next token", parser->mark,
1233 "found character that cannot start any token");
1237 * Check the list of potential simple keys and remove the positions that
1238 * cannot contain simple keys anymore.
1242 yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1244 yaml_simple_key_t *simple_key;
1246 /* Check for a potential simple key for each flow level. */
1248 for (simple_key = parser->simple_keys.start;
1249 simple_key != parser->simple_keys.top; simple_key ++)
1252 * The specification requires that a simple key
1254 * - is limited to a single line,
1255 * - is shorter than 1024 characters.
1258 if (simple_key->possible
1259 && (simple_key->mark.line < parser->mark.line
1260 || simple_key->mark.index+1024 < parser->mark.index)) {
1262 /* Check if the potential simple key to be removed is required. */
1264 if (simple_key->required) {
1265 return yaml_parser_set_scanner_error(parser,
1266 "while scanning a simple key", simple_key->mark,
1267 "could not found expected ':'");
1270 simple_key->possible = 0;
1278 * Check if a simple key may start at the current position and add it if
1283 yaml_parser_save_simple_key(yaml_parser_t *parser)
1286 * A simple key is required at the current position if the scanner is in
1287 * the block context and the current column coincides with the indentation
1291 int required = (!parser->flow_level
1292 && parser->indent == parser->mark.column);
1295 * A simple key is required only when it is the first token in the current
1296 * line. Therefore it is always allowed. But we add a check anyway.
1299 assert(parser->simple_key_allowed || !required); /* Impossible. */
1302 * If the current position may start a simple key, save it.
1305 if (parser->simple_key_allowed)
1307 yaml_simple_key_t simple_key = { 1, required,
1308 parser->tokens_parsed + parser->tokens.tail - parser->tokens.head,
1311 if (!yaml_parser_remove_simple_key(parser)) return 0;
1313 *(parser->simple_keys.top-1) = simple_key;
1320 * Remove a potential simple key at the current flow level.
1324 yaml_parser_remove_simple_key(yaml_parser_t *parser)
1326 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1328 if (simple_key->possible)
1330 /* If the key is required, it is an error. */
1332 if (simple_key->required) {
1333 return yaml_parser_set_scanner_error(parser,
1334 "while scanning a simple key", simple_key->mark,
1335 "could not found expected ':'");
1339 /* Remove the key from the stack. */
1341 simple_key->possible = 0;
1347 * Increase the flow level and resize the simple key list if needed.
1351 yaml_parser_increase_flow_level(yaml_parser_t *parser)
1353 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1355 /* Reset the simple key on the next level. */
1357 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1360 /* Increase the flow level. */
1362 parser->flow_level++;
1368 * Decrease the flow level.
1372 yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1374 if (parser->flow_level) {
1375 parser->flow_level --;
1376 POP(parser, parser->simple_keys);
1383 * Push the current indentation level to the stack and set the new level
1384 * the current column is greater than the indentation level. In this case,
1385 * append or insert the specified token into the token queue.
1390 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1391 int number, yaml_token_type_t type, yaml_mark_t mark)
1395 /* In the flow context, do nothing. */
1397 if (parser->flow_level)
1400 if (parser->indent < column)
1403 * Push the current indentation level to the stack and set the new
1404 * indentation level.
1407 if (!PUSH(parser, parser->indents, parser->indent))
1410 parser->indent = column;
1412 /* Create a token and insert it into the queue. */
1414 TOKEN_INIT(token, type, mark, mark);
1417 if (!ENQUEUE(parser, parser->tokens, token))
1421 if (!QUEUE_INSERT(parser,
1422 parser->tokens, number - parser->tokens_parsed, token))
1431 * Pop indentation levels from the indents stack until the current level
1432 * becomes less or equal to the column. For each intendation level, append
1433 * the BLOCK-END token.
1438 yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1442 /* In the flow context, do nothing. */
1444 if (parser->flow_level)
1447 /* Loop through the intendation levels in the stack. */
1449 while (parser->indent > column)
1451 /* Create a token and append it to the queue. */
1453 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1455 if (!ENQUEUE(parser, parser->tokens, token))
1458 /* Pop the indentation level. */
1460 parser->indent = POP(parser, parser->indents);
1467 * Initialize the scanner and produce the STREAM-START token.
1471 yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1473 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1476 /* Set the initial indentation. */
1478 parser->indent = -1;
1480 /* Initialize the simple key stack. */
1482 if (!PUSH(parser, parser->simple_keys, simple_key))
1485 /* A simple key is allowed at the beginning of the stream. */
1487 parser->simple_key_allowed = 1;
1489 /* We have started. */
1491 parser->stream_start_produced = 1;
1493 /* Create the STREAM-START token and append it to the queue. */
1495 STREAM_START_TOKEN_INIT(token, parser->encoding,
1496 parser->mark, parser->mark);
1498 if (!ENQUEUE(parser, parser->tokens, token))
1505 * Produce the STREAM-END token and shut down the scanner.
1509 yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1513 /* Reset the indentation level. */
1515 if (!yaml_parser_unroll_indent(parser, -1))
1518 /* Reset simple keys. */
1520 if (!yaml_parser_remove_simple_key(parser))
1523 parser->simple_key_allowed = 0;
1525 /* Create the STREAM-END token and append it to the queue. */
1527 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1529 if (!ENQUEUE(parser, parser->tokens, token))
1536 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1540 yaml_parser_fetch_directive(yaml_parser_t *parser)
1544 /* Reset the indentation level. */
1546 if (!yaml_parser_unroll_indent(parser, -1))
1549 /* Reset simple keys. */
1551 if (!yaml_parser_remove_simple_key(parser))
1554 parser->simple_key_allowed = 0;
1556 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1558 if (!yaml_parser_scan_directive(parser, &token))
1561 /* Append the token to the queue. */
1563 if (!ENQUEUE(parser, parser->tokens, token)) {
1564 yaml_token_delete(&token);
1572 * Produce the DOCUMENT-START or DOCUMENT-END token.
1576 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1577 yaml_token_type_t type)
1579 yaml_mark_t start_mark, end_mark;
1582 /* Reset the indentation level. */
1584 if (!yaml_parser_unroll_indent(parser, -1))
1587 /* Reset simple keys. */
1589 if (!yaml_parser_remove_simple_key(parser))
1592 parser->simple_key_allowed = 0;
1594 /* Consume the token. */
1596 start_mark = parser->mark;
1602 end_mark = parser->mark;
1604 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1606 TOKEN_INIT(token, type, start_mark, end_mark);
1608 /* Append the token to the queue. */
1610 if (!ENQUEUE(parser, parser->tokens, token))
1617 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1621 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1622 yaml_token_type_t type)
1624 yaml_mark_t start_mark, end_mark;
1627 /* The indicators '[' and '{' may start a simple key. */
1629 if (!yaml_parser_save_simple_key(parser))
1632 /* Increase the flow level. */
1634 if (!yaml_parser_increase_flow_level(parser))
1637 /* A simple key may follow the indicators '[' and '{'. */
1639 parser->simple_key_allowed = 1;
1641 /* Consume the token. */
1643 start_mark = parser->mark;
1645 end_mark = parser->mark;
1647 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1649 TOKEN_INIT(token, type, start_mark, end_mark);
1651 /* Append the token to the queue. */
1653 if (!ENQUEUE(parser, parser->tokens, token))
1660 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1664 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1665 yaml_token_type_t type)
1667 yaml_mark_t start_mark, end_mark;
1670 /* Reset any potential simple key on the current flow level. */
1672 if (!yaml_parser_remove_simple_key(parser))
1675 /* Decrease the flow level. */
1677 if (!yaml_parser_decrease_flow_level(parser))
1680 /* No simple keys after the indicators ']' and '}'. */
1682 parser->simple_key_allowed = 0;
1684 /* Consume the token. */
1686 start_mark = parser->mark;
1688 end_mark = parser->mark;
1690 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1692 TOKEN_INIT(token, type, start_mark, end_mark);
1694 /* Append the token to the queue. */
1696 if (!ENQUEUE(parser, parser->tokens, token))
1703 * Produce the FLOW-ENTRY token.
1707 yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1709 yaml_mark_t start_mark, end_mark;
1712 /* Reset any potential simple keys on the current flow level. */
1714 if (!yaml_parser_remove_simple_key(parser))
1717 /* Simple keys are allowed after ','. */
1719 parser->simple_key_allowed = 1;
1721 /* Consume the token. */
1723 start_mark = parser->mark;
1725 end_mark = parser->mark;
1727 /* Create the FLOW-ENTRY token and append it to the queue. */
1729 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1731 if (!ENQUEUE(parser, parser->tokens, token))
1738 * Produce the BLOCK-ENTRY token.
1742 yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1744 yaml_mark_t start_mark, end_mark;
1747 /* Check if the scanner is in the block context. */
1749 if (!parser->flow_level)
1751 /* Check if we are allowed to start a new entry. */
1753 if (!parser->simple_key_allowed) {
1754 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1755 "block sequence entries are not allowed in this context");
1758 /* Add the BLOCK-SEQUENCE-START token if needed. */
1760 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1761 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1767 * It is an error for the '-' indicator to occur in the flow context,
1768 * but we let the Parser detect and report about it because the Parser
1769 * is able to point to the context.
1773 /* Reset any potential simple keys on the current flow level. */
1775 if (!yaml_parser_remove_simple_key(parser))
1778 /* Simple keys are allowed after '-'. */
1780 parser->simple_key_allowed = 1;
1782 /* Consume the token. */
1784 start_mark = parser->mark;
1786 end_mark = parser->mark;
1788 /* Create the BLOCK-ENTRY token and append it to the queue. */
1790 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1792 if (!ENQUEUE(parser, parser->tokens, token))
1799 * Produce the KEY token.
1803 yaml_parser_fetch_key(yaml_parser_t *parser)
1805 yaml_mark_t start_mark, end_mark;
1808 /* In the block context, additional checks are required. */
1810 if (!parser->flow_level)
1812 /* Check if we are allowed to start a new key (not nessesary simple). */
1814 if (!parser->simple_key_allowed) {
1815 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1816 "mapping keys are not allowed in this context");
1819 /* Add the BLOCK-MAPPING-START token if needed. */
1821 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1822 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1826 /* Reset any potential simple keys on the current flow level. */
1828 if (!yaml_parser_remove_simple_key(parser))
1831 /* Simple keys are allowed after '?' in the block context. */
1833 parser->simple_key_allowed = (!parser->flow_level);
1835 /* Consume the token. */
1837 start_mark = parser->mark;
1839 end_mark = parser->mark;
1841 /* Create the KEY token and append it to the queue. */
1843 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1845 if (!ENQUEUE(parser, parser->tokens, token))
1852 * Produce the VALUE token.
1856 yaml_parser_fetch_value(yaml_parser_t *parser)
1858 yaml_mark_t start_mark, end_mark;
1860 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1862 /* Have we found a simple key? */
1864 if (simple_key->possible)
1867 /* Create the KEY token and insert it into the queue. */
1869 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1871 if (!QUEUE_INSERT(parser, parser->tokens,
1872 simple_key->token_number - parser->tokens_parsed, token))
1875 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1877 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1878 simple_key->token_number,
1879 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1882 /* Remove the simple key. */
1884 simple_key->possible = 0;
1886 /* A simple key cannot follow another simple key. */
1888 parser->simple_key_allowed = 0;
1892 /* The ':' indicator follows a complex key. */
1894 /* In the block context, extra checks are required. */
1896 if (!parser->flow_level)
1898 /* Check if we are allowed to start a complex value. */
1900 if (!parser->simple_key_allowed) {
1901 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1902 "mapping values are not allowed in this context");
1905 /* Add the BLOCK-MAPPING-START token if needed. */
1907 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1908 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1912 /* Simple keys after ':' are allowed in the block context. */
1914 parser->simple_key_allowed = (!parser->flow_level);
1917 /* Consume the token. */
1919 start_mark = parser->mark;
1921 end_mark = parser->mark;
1923 /* Create the VALUE token and append it to the queue. */
1925 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1927 if (!ENQUEUE(parser, parser->tokens, token))
1934 * Produce the ALIAS or ANCHOR token.
1938 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1942 /* An anchor or an alias could be a simple key. */
1944 if (!yaml_parser_save_simple_key(parser))
1947 /* A simple key cannot follow an anchor or an alias. */
1949 parser->simple_key_allowed = 0;
1951 /* Create the ALIAS or ANCHOR token and append it to the queue. */
1953 if (!yaml_parser_scan_anchor(parser, &token, type))
1956 if (!ENQUEUE(parser, parser->tokens, token)) {
1957 yaml_token_delete(&token);
1964 * Produce the TAG token.
1968 yaml_parser_fetch_tag(yaml_parser_t *parser)
1972 /* A tag could be a simple key. */
1974 if (!yaml_parser_save_simple_key(parser))
1977 /* A simple key cannot follow a tag. */
1979 parser->simple_key_allowed = 0;
1981 /* Create the TAG token and append it to the queue. */
1983 if (!yaml_parser_scan_tag(parser, &token))
1986 if (!ENQUEUE(parser, parser->tokens, token)) {
1987 yaml_token_delete(&token);
1995 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1999 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
2003 /* Remove any potential simple keys. */
2005 if (!yaml_parser_remove_simple_key(parser))
2008 /* A simple key may follow a block scalar. */
2010 parser->simple_key_allowed = 1;
2012 /* Create the SCALAR token and append it to the queue. */
2014 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
2017 if (!ENQUEUE(parser, parser->tokens, token)) {
2018 yaml_token_delete(&token);
2026 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
2030 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
2034 /* A plain scalar could be a simple key. */
2036 if (!yaml_parser_save_simple_key(parser))
2039 /* A simple key cannot follow a flow scalar. */
2041 parser->simple_key_allowed = 0;
2043 /* Create the SCALAR token and append it to the queue. */
2045 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
2048 if (!ENQUEUE(parser, parser->tokens, token)) {
2049 yaml_token_delete(&token);
2057 * Produce the SCALAR(...,plain) token.
2061 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
2065 /* A plain scalar could be a simple key. */
2067 if (!yaml_parser_save_simple_key(parser))
2070 /* A simple key cannot follow a flow scalar. */
2072 parser->simple_key_allowed = 0;
2074 /* Create the SCALAR token and append it to the queue. */
2076 if (!yaml_parser_scan_plain_scalar(parser, &token))
2079 if (!ENQUEUE(parser, parser->tokens, token)) {
2080 yaml_token_delete(&token);
2088 * Eat whitespaces and comments until the next token is found.
2092 yaml_parser_scan_to_next_token(yaml_parser_t *parser)
2094 /* Until the next token is not found. */
2098 /* Allow the BOM mark to start a line. */
2100 if (!CACHE(parser, 1)) return 0;
2102 if (parser->mark.column == 0 && IS_BOM(parser))
2110 * - in the flow context;
2111 * - in the block context, but not at the beginning of the line or
2112 * after '-', '?', or ':' (complex value).
2115 if (!CACHE(parser, 1)) return 0;
2117 while (CHECK(parser,' ') ||
2118 ((parser->flow_level || !parser->simple_key_allowed) &&
2119 CHECK(parser, '\t'))) {
2121 if (!CACHE(parser, 1)) return 0;
2124 /* Eat a comment until a line break. */
2126 if (CHECK(parser, '#')) {
2127 while (!IS_BREAKZ(parser)) {
2129 if (!CACHE(parser, 1)) return 0;
2133 /* If it is a line break, eat it. */
2135 if (IS_BREAK(parser))
2137 if (!CACHE(parser, 2)) return 0;
2140 /* In the block context, a new line may start a simple key. */
2142 if (!parser->flow_level) {
2143 parser->simple_key_allowed = 1;
2148 /* We have found a token. */
2158 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
2161 * %YAML 1.1 # a comment \n
2162 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2163 * %TAG !yaml! tag:yaml.org,2002: \n
2164 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2168 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
2170 yaml_mark_t start_mark, end_mark;
2171 yaml_char_t *name = NULL;
2173 yaml_char_t *handle = NULL, *prefix = NULL;
2177 start_mark = parser->mark;
2181 /* Scan the directive name. */
2183 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2186 /* Is it a YAML directive? */
2188 if (strcmp((char *)name, "YAML") == 0)
2190 /* Scan the VERSION directive value. */
2192 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2196 end_mark = parser->mark;
2198 /* Create a VERSION-DIRECTIVE token. */
2200 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2201 start_mark, end_mark);
2204 /* Is it a TAG directive? */
2206 else if (strcmp((char *)name, "TAG") == 0)
2208 /* Scan the TAG directive value. */
2210 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2214 end_mark = parser->mark;
2216 /* Create a TAG-DIRECTIVE token. */
2218 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2219 start_mark, end_mark);
2222 /* Unknown directive. */
2226 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2227 start_mark, "found uknown directive name");
2231 /* Eat the rest of the line including any comments. */
2233 if (!CACHE(parser, 1)) goto error;
2235 while (IS_BLANK(parser)) {
2237 if (!CACHE(parser, 1)) goto error;
2240 if (CHECK(parser, '#')) {
2241 while (!IS_BREAKZ(parser)) {
2243 if (!CACHE(parser, 1)) goto error;
2247 /* Check if we are at the end of the line. */
2249 if (!IS_BREAKZ(parser)) {
2250 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2251 start_mark, "did not found expected comment or line break");
2255 /* Eat a line break. */
2257 if (IS_BREAK(parser)) {
2258 if (!CACHE(parser, 2)) goto error;
2274 * Scan the directive name.
2277 * %YAML 1.1 # a comment \n
2279 * %TAG !yaml! tag:yaml.org,2002: \n
2284 yaml_parser_scan_directive_name(yaml_parser_t *parser,
2285 yaml_mark_t start_mark, yaml_char_t **name)
2287 yaml_string_t string = NULL_STRING;
2289 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2291 /* Consume the directive name. */
2293 if (!CACHE(parser, 1)) goto error;
2295 while (IS_ALPHA(parser))
2297 if (!READ(parser, string)) goto error;
2298 if (!CACHE(parser, 1)) goto error;
2301 /* Check if the name is empty. */
2303 if (string.start == string.pointer) {
2304 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2305 start_mark, "cannot found expected directive name");
2309 /* Check for an blank character after the name. */
2311 if (!IS_BLANKZ(parser)) {
2312 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2313 start_mark, "found unexpected non-alphabetical character");
2317 *name = string.start;
2322 STRING_DEL(parser, string);
2327 * Scan the value of VERSION-DIRECTIVE.
2330 * %YAML 1.1 # a comment \n
2335 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2336 yaml_mark_t start_mark, int *major, int *minor)
2338 /* Eat whitespaces. */
2340 if (!CACHE(parser, 1)) return 0;
2342 while (IS_BLANK(parser)) {
2344 if (!CACHE(parser, 1)) return 0;
2347 /* Consume the major version number. */
2349 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2354 if (!CHECK(parser, '.')) {
2355 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2356 start_mark, "did not find expected digit or '.' character");
2361 /* Consume the minor version number. */
2363 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2369 #define MAX_NUMBER_LENGTH 9
2372 * Scan the version number of VERSION-DIRECTIVE.
2375 * %YAML 1.1 # a comment \n
2377 * %YAML 1.1 # a comment \n
2382 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2383 yaml_mark_t start_mark, int *number)
2388 /* Repeat while the next character is digit. */
2390 if (!CACHE(parser, 1)) return 0;
2392 while (IS_DIGIT(parser))
2394 /* Check if the number is too long. */
2396 if (++length > MAX_NUMBER_LENGTH) {
2397 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2398 start_mark, "found extremely long version number");
2401 value = value*10 + AS_DIGIT(parser);
2405 if (!CACHE(parser, 1)) return 0;
2408 /* Check if the number was present. */
2411 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2412 start_mark, "did not find expected version number");
2421 * Scan the value of a TAG-DIRECTIVE token.
2424 * %TAG !yaml! tag:yaml.org,2002: \n
2425 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2429 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2430 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2432 yaml_char_t *handle_value = NULL;
2433 yaml_char_t *prefix_value = NULL;
2435 /* Eat whitespaces. */
2437 if (!CACHE(parser, 1)) goto error;
2439 while (IS_BLANK(parser)) {
2441 if (!CACHE(parser, 1)) goto error;
2444 /* Scan a handle. */
2446 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2449 /* Expect a whitespace. */
2451 if (!CACHE(parser, 1)) goto error;
2453 if (!IS_BLANK(parser)) {
2454 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2455 start_mark, "did not find expected whitespace");
2459 /* Eat whitespaces. */
2461 while (IS_BLANK(parser)) {
2463 if (!CACHE(parser, 1)) goto error;
2466 /* Scan a prefix. */
2468 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2471 /* Expect a whitespace or line break. */
2473 if (!CACHE(parser, 1)) goto error;
2475 if (!IS_BLANKZ(parser)) {
2476 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2477 start_mark, "did not find expected whitespace or line break");
2481 *handle = handle_value;
2482 *prefix = prefix_value;
2487 yaml_free(handle_value);
2488 yaml_free(prefix_value);
2493 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2494 yaml_token_type_t type)
2497 yaml_mark_t start_mark, end_mark;
2498 yaml_string_t string = NULL_STRING;
2500 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2502 /* Eat the indicator character. */
2504 start_mark = parser->mark;
2508 /* Consume the value. */
2510 if (!CACHE(parser, 1)) goto error;
2512 while (IS_ALPHA(parser)) {
2513 if (!READ(parser, string)) goto error;
2514 if (!CACHE(parser, 1)) goto error;
2518 end_mark = parser->mark;
2521 * Check if length of the anchor is greater than 0 and it is followed by
2522 * a whitespace character or one of the indicators:
2524 * '?', ':', ',', ']', '}', '%', '@', '`'.
2527 if (!length || !(IS_BLANKZ(parser) || CHECK(parser, '?') || CHECK(parser, ':') ||
2528 CHECK(parser, ',') || CHECK(parser, ']') || CHECK(parser, '}') ||
2529 CHECK(parser, '%') || CHECK(parser, '@') || CHECK(parser, '`'))) {
2530 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2531 "while scanning an anchor" : "while scanning an alias", start_mark,
2532 "did not find expected alphabetic or numeric character");
2536 /* Create a token. */
2538 if (type == YAML_ANCHOR_TOKEN) {
2539 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2542 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2548 STRING_DEL(parser, string);
2557 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2559 yaml_char_t *handle = NULL;
2560 yaml_char_t *suffix = NULL;
2561 yaml_mark_t start_mark, end_mark;
2563 start_mark = parser->mark;
2565 /* Check if the tag is in the canonical form. */
2567 if (!CACHE(parser, 2)) goto error;
2569 if (CHECK_AT(parser, '<', 1))
2571 /* Set the handle to '' */
2573 handle = yaml_malloc(1);
2574 if (!handle) goto error;
2582 /* Consume the tag value. */
2584 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2587 /* Check for '>' and eat it. */
2589 if (!CHECK(parser, '>')) {
2590 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2591 start_mark, "did not find the expected '>'");
2599 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2601 /* First, try to scan a handle. */
2603 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2606 /* Check if it is, indeed, handle. */
2608 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2610 /* Scan the suffix now. */
2612 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2617 /* It wasn't a handle after all. Scan the rest of the tag. */
2619 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2622 /* Set the handle to '!'. */
2625 handle = yaml_malloc(2);
2626 if (!handle) goto error;
2631 * A special case: the '!' tag. Set the handle to '' and the
2635 if (suffix[0] == '\0') {
2636 yaml_char_t *tmp = handle;
2643 /* Check the character which ends the tag. */
2645 if (!CACHE(parser, 1)) goto error;
2647 if (!IS_BLANKZ(parser)) {
2648 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2649 start_mark, "did not found expected whitespace or line break");
2653 end_mark = parser->mark;
2655 /* Create a token. */
2657 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2668 * Scan a tag handle.
2672 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2673 yaml_mark_t start_mark, yaml_char_t **handle)
2675 yaml_string_t string = NULL_STRING;
2677 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2679 /* Check the initial '!' character. */
2681 if (!CACHE(parser, 1)) goto error;
2683 if (!CHECK(parser, '!')) {
2684 yaml_parser_set_scanner_error(parser, directive ?
2685 "while scanning a tag directive" : "while scanning a tag",
2686 start_mark, "did not find expected '!'");
2690 /* Copy the '!' character. */
2692 if (!READ(parser, string)) goto error;
2694 /* Copy all subsequent alphabetical and numerical characters. */
2696 if (!CACHE(parser, 1)) goto error;
2698 while (IS_ALPHA(parser))
2700 if (!READ(parser, string)) goto error;
2701 if (!CACHE(parser, 1)) goto error;
2704 /* Check if the trailing character is '!' and copy it. */
2706 if (CHECK(parser, '!'))
2708 if (!READ(parser, string)) goto error;
2713 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2714 * directive, it's an error. If it's a tag token, it must be a part of
2718 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2719 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2720 start_mark, "did not find expected '!'");
2725 *handle = string.start;
2730 STRING_DEL(parser, string);
2739 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2740 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2742 size_t length = head ? strlen((char *)head) : 0;
2743 yaml_string_t string = NULL_STRING;
2745 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2747 /* Resize the string to include the head. */
2749 while (string.end - string.start <= length) {
2750 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2751 parser->error = YAML_MEMORY_ERROR;
2757 * Copy the head if needed.
2759 * Note that we don't copy the leading '!' character.
2763 memcpy(string.start, head+1, length-1);
2764 string.pointer += length-1;
2769 if (!CACHE(parser, 1)) goto error;
2772 * The set of characters that may appear in URI is as follows:
2774 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2775 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2779 while (IS_ALPHA(parser) || CHECK(parser, ';') || CHECK(parser, '/') ||
2780 CHECK(parser, '?') || CHECK(parser, ':') || CHECK(parser, '@') ||
2781 CHECK(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, '%'))
2787 /* Check if it is a URI-escape sequence. */
2789 if (CHECK(parser, '%')) {
2790 if (!yaml_parser_scan_uri_escapes(parser,
2791 directive, start_mark, &string)) goto error;
2794 if (!READ(parser, string)) goto error;
2798 if (!CACHE(parser, 1)) goto error;
2801 /* Check if the tag is non-empty. */
2804 if (!STRING_EXTEND(parser, string))
2807 yaml_parser_set_scanner_error(parser, directive ?
2808 "while parsing a %TAG directive" : "while parsing a tag",
2809 start_mark, "did not find expected tag URI");
2813 *uri = string.start;
2818 STRING_DEL(parser, string);
2823 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2827 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2828 yaml_mark_t start_mark, yaml_string_t *string)
2832 /* Decode the required number of characters. */
2836 unsigned char octet = 0;
2838 /* Check for a URI-escaped octet. */
2840 if (!CACHE(parser, 3)) return 0;
2842 if (!(CHECK(parser, '%') && IS_HEX_AT(parser, 1) && IS_HEX_AT(parser, 2))) {
2843 return yaml_parser_set_scanner_error(parser, directive ?
2844 "while parsing a %TAG directive" : "while parsing a tag",
2845 start_mark, "did not find URI escaped octet");
2848 /* Get the octet. */
2850 octet = (AS_HEX_AT(parser, 1) << 4) + AS_HEX_AT(parser, 2);
2852 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2856 width = (octet & 0x80) == 0x00 ? 1 :
2857 (octet & 0xE0) == 0xC0 ? 2 :
2858 (octet & 0xF0) == 0xE0 ? 3 :
2859 (octet & 0xF8) == 0xF0 ? 4 : 0;
2861 return yaml_parser_set_scanner_error(parser, directive ?
2862 "while parsing a %TAG directive" : "while parsing a tag",
2863 start_mark, "found an incorrect leading UTF-8 octet");
2868 /* Check if the trailing octet is correct. */
2870 if ((octet & 0xC0) != 0x80) {
2871 return yaml_parser_set_scanner_error(parser, directive ?
2872 "while parsing a %TAG directive" : "while parsing a tag",
2873 start_mark, "found an incorrect trailing UTF-8 octet");
2877 /* Copy the octet and move the pointers. */
2879 *(string->pointer++) = octet;
2890 * Scan a block scalar.
2894 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2897 yaml_mark_t start_mark;
2898 yaml_mark_t end_mark;
2899 yaml_string_t string = NULL_STRING;
2900 yaml_string_t leading_break = NULL_STRING;
2901 yaml_string_t trailing_breaks = NULL_STRING;
2905 int leading_blank = 0;
2906 int trailing_blank = 0;
2908 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2909 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2910 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2912 /* Eat the indicator '|' or '>'. */
2914 start_mark = parser->mark;
2918 /* Scan the additional block scalar indicators. */
2920 if (!CACHE(parser, 1)) goto error;
2922 /* Check for a chomping indicator. */
2924 if (CHECK(parser, '+') || CHECK(parser, '-'))
2926 /* Set the chomping method and eat the indicator. */
2928 chomping = CHECK(parser, '+') ? +1 : -1;
2932 /* Check for an indentation indicator. */
2934 if (!CACHE(parser, 1)) goto error;
2936 if (IS_DIGIT(parser))
2938 /* Check that the intendation is greater than 0. */
2940 if (CHECK(parser, '0')) {
2941 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2942 start_mark, "found an intendation indicator equal to 0");
2946 /* Get the intendation level and eat the indicator. */
2948 increment = AS_DIGIT(parser);
2954 /* Do the same as above, but in the opposite order. */
2956 else if (IS_DIGIT(parser))
2958 if (CHECK(parser, '0')) {
2959 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2960 start_mark, "found an intendation indicator equal to 0");
2964 increment = AS_DIGIT(parser);
2968 if (!CACHE(parser, 1)) goto error;
2970 if (CHECK(parser, '+') || CHECK(parser, '-')) {
2971 chomping = CHECK(parser, '+') ? +1 : -1;
2977 /* Eat whitespaces and comments to the end of the line. */
2979 if (!CACHE(parser, 1)) goto error;
2981 while (IS_BLANK(parser)) {
2983 if (!CACHE(parser, 1)) goto error;
2986 if (CHECK(parser, '#')) {
2987 while (!IS_BREAKZ(parser)) {
2989 if (!CACHE(parser, 1)) goto error;
2993 /* Check if we are at the end of the line. */
2995 if (!IS_BREAKZ(parser)) {
2996 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2997 start_mark, "did not found expected comment or line break");
3001 /* Eat a line break. */
3003 if (IS_BREAK(parser)) {
3004 if (!CACHE(parser, 2)) goto error;
3008 end_mark = parser->mark;
3010 /* Set the intendation level if it was specified. */
3013 indent = parser->indent >= 0 ? parser->indent+increment : increment;
3016 /* Scan the leading line breaks and determine the indentation level if needed. */
3018 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
3019 start_mark, &end_mark)) goto error;
3021 /* Scan the block scalar content. */
3023 if (!CACHE(parser, 1)) goto error;
3025 while (parser->mark.column == indent && !IS_Z(parser))
3028 * We are at the beginning of a non-empty line.
3031 /* Is it a trailing whitespace? */
3033 trailing_blank = IS_BLANK(parser);
3035 /* Check if we need to fold the leading line break. */
3037 if (!literal && (*leading_break.start == '\n')
3038 && !leading_blank && !trailing_blank)
3040 /* Do we need to join the lines by space? */
3042 if (*trailing_breaks.start == '\0') {
3043 if (!STRING_EXTEND(parser, string)) goto error;
3044 *(string.pointer ++) = ' ';
3047 CLEAR(parser, leading_break);
3050 if (!JOIN(parser, string, leading_break)) goto error;
3051 CLEAR(parser, leading_break);
3054 /* Append the remaining line breaks. */
3056 if (!JOIN(parser, string, trailing_breaks)) goto error;
3057 CLEAR(parser, trailing_breaks);
3059 /* Is it a leading whitespace? */
3061 leading_blank = IS_BLANK(parser);
3063 /* Consume the current line. */
3065 while (!IS_BREAKZ(parser)) {
3066 if (!READ(parser, string)) goto error;
3067 if (!CACHE(parser, 1)) goto error;
3070 /* Consume the line break. */
3072 if (!CACHE(parser, 2)) goto error;
3074 if (!READ_LINE(parser, leading_break)) goto error;
3076 /* Eat the following intendation spaces and line breaks. */
3078 if (!yaml_parser_scan_block_scalar_breaks(parser,
3079 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
3082 /* Chomp the tail. */
3084 if (chomping != -1) {
3085 if (!JOIN(parser, string, leading_break)) goto error;
3087 if (chomping == 1) {
3088 if (!JOIN(parser, string, trailing_breaks)) goto error;
3091 /* Create a token. */
3093 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3094 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
3095 start_mark, end_mark);
3097 STRING_DEL(parser, leading_break);
3098 STRING_DEL(parser, trailing_breaks);
3103 STRING_DEL(parser, string);
3104 STRING_DEL(parser, leading_break);
3105 STRING_DEL(parser, trailing_breaks);
3111 * Scan intendation spaces and line breaks for a block scalar. Determine the
3112 * intendation level if needed.
3116 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
3117 int *indent, yaml_string_t *breaks,
3118 yaml_mark_t start_mark, yaml_mark_t *end_mark)
3122 *end_mark = parser->mark;
3124 /* Eat the intendation spaces and line breaks. */
3128 /* Eat the intendation spaces. */
3130 if (!CACHE(parser, 1)) return 0;
3132 while ((!*indent || parser->mark.column < *indent) && IS_SPACE(parser)) {
3134 if (!CACHE(parser, 1)) return 0;
3137 if (parser->mark.column > max_indent)
3138 max_indent = parser->mark.column;
3140 /* Check for a tab character messing the intendation. */
3142 if ((!*indent || parser->mark.column < *indent) && IS_TAB(parser)) {
3143 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
3144 start_mark, "found a tab character where an intendation space is expected");
3147 /* Have we found a non-empty line? */
3149 if (!IS_BREAK(parser)) break;
3151 /* Consume the line break. */
3153 if (!CACHE(parser, 2)) return 0;
3154 if (!READ_LINE(parser, *breaks)) return 0;
3155 *end_mark = parser->mark;
3158 /* Determine the indentation level if needed. */
3161 *indent = max_indent;
3162 if (*indent < parser->indent + 1)
3163 *indent = parser->indent + 1;
3172 * Scan a quoted scalar.
3176 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3179 yaml_mark_t start_mark;
3180 yaml_mark_t end_mark;
3181 yaml_string_t string = NULL_STRING;
3182 yaml_string_t leading_break = NULL_STRING;
3183 yaml_string_t trailing_breaks = NULL_STRING;
3184 yaml_string_t whitespaces = NULL_STRING;
3187 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3188 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3189 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3190 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3192 /* Eat the left quote. */
3194 start_mark = parser->mark;
3198 /* Consume the content of the quoted scalar. */
3202 /* Check that there are no document indicators at the beginning of the line. */
3204 if (!CACHE(parser, 4)) goto error;
3206 if (parser->mark.column == 0 &&
3207 ((CHECK_AT(parser, '-', 0) &&
3208 CHECK_AT(parser, '-', 1) &&
3209 CHECK_AT(parser, '-', 2)) ||
3210 (CHECK_AT(parser, '.', 0) &&
3211 CHECK_AT(parser, '.', 1) &&
3212 CHECK_AT(parser, '.', 2))) &&
3213 IS_BLANKZ_AT(parser, 3))
3215 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3216 start_mark, "found unexpected document indicator");
3220 /* Check for EOF. */
3223 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3224 start_mark, "found unexpected end of stream");
3228 /* Consume non-blank characters. */
3230 if (!CACHE(parser, 2)) goto error;
3234 while (!IS_BLANKZ(parser))
3236 /* Check for an escaped single quote. */
3238 if (single && CHECK_AT(parser, '\'', 0) && CHECK_AT(parser, '\'', 1))
3240 if (!STRING_EXTEND(parser, string)) goto error;
3241 *(string.pointer++) = '\'';
3246 /* Check for the right quote. */
3248 else if (CHECK(parser, single ? '\'' : '"'))
3253 /* Check for an escaped line break. */
3255 else if (!single && CHECK(parser, '\\') && IS_BREAK_AT(parser, 1))
3257 if (!CACHE(parser, 3)) goto error;
3264 /* Check for an escape sequence. */
3266 else if (!single && CHECK(parser, '\\'))
3268 int code_length = 0;
3270 if (!STRING_EXTEND(parser, string)) goto error;
3272 /* Check the escape character. */
3274 switch (parser->buffer.pointer[1])
3277 *(string.pointer++) = '\0';
3281 *(string.pointer++) = '\x07';
3285 *(string.pointer++) = '\x08';
3290 *(string.pointer++) = '\x09';
3294 *(string.pointer++) = '\x0A';
3298 *(string.pointer++) = '\x0B';
3302 *(string.pointer++) = '\x0C';
3306 *(string.pointer++) = '\x0D';
3310 *(string.pointer++) = '\x1B';
3314 *(string.pointer++) = '\x20';
3318 *(string.pointer++) = '"';
3322 *(string.pointer++) = '\'';
3326 *(string.pointer++) = '\\';
3329 case 'N': /* NEL (#x85) */
3330 *(string.pointer++) = '\xC2';
3331 *(string.pointer++) = '\x85';
3334 case '_': /* #xA0 */
3335 *(string.pointer++) = '\xC2';
3336 *(string.pointer++) = '\xA0';
3339 case 'L': /* LS (#x2028) */
3340 *(string.pointer++) = '\xE2';
3341 *(string.pointer++) = '\x80';
3342 *(string.pointer++) = '\xA8';
3345 case 'P': /* PS (#x2029) */
3346 *(string.pointer++) = '\xE2';
3347 *(string.pointer++) = '\x80';
3348 *(string.pointer++) = '\xA9';
3364 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3365 start_mark, "found unknown escape character");
3372 /* Consume an arbitrary escape code. */
3376 unsigned int value = 0;
3379 /* Scan the character value. */
3381 if (!CACHE(parser, code_length)) goto error;
3383 for (k = 0; k < code_length; k ++) {
3384 if (!IS_HEX_AT(parser, k)) {
3385 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3386 start_mark, "did not find expected hexdecimal number");
3389 value = (value << 4) + AS_HEX_AT(parser, k);
3392 /* Check the value and write the character. */
3394 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3395 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3396 start_mark, "found invalid Unicode character escape code");
3400 if (value <= 0x7F) {
3401 *(string.pointer++) = value;
3403 else if (value <= 0x7FF) {
3404 *(string.pointer++) = 0xC0 + (value >> 6);
3405 *(string.pointer++) = 0x80 + (value & 0x3F);
3407 else if (value <= 0xFFFF) {
3408 *(string.pointer++) = 0xE0 + (value >> 12);
3409 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3410 *(string.pointer++) = 0x80 + (value & 0x3F);
3413 *(string.pointer++) = 0xF0 + (value >> 18);
3414 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3415 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3416 *(string.pointer++) = 0x80 + (value & 0x3F);
3419 /* Advance the pointer. */
3421 for (k = 0; k < code_length; k ++) {
3429 /* It is a non-escaped non-blank character. */
3431 if (!READ(parser, string)) goto error;
3434 if (!CACHE(parser, 2)) goto error;
3437 /* Check if we are at the end of the scalar. */
3439 if (CHECK(parser, single ? '\'' : '"'))
3442 /* Consume blank characters. */
3444 if (!CACHE(parser, 1)) goto error;
3446 while (IS_BLANK(parser) || IS_BREAK(parser))
3448 if (IS_BLANK(parser))
3450 /* Consume a space or a tab character. */
3452 if (!leading_blanks) {
3453 if (!READ(parser, whitespaces)) goto error;
3461 if (!CACHE(parser, 2)) goto error;
3463 /* Check if it is a first line break. */
3465 if (!leading_blanks)
3467 CLEAR(parser, whitespaces);
3468 if (!READ_LINE(parser, leading_break)) goto error;
3473 if (!READ_LINE(parser, trailing_breaks)) goto error;
3476 if (!CACHE(parser, 1)) goto error;
3479 /* Join the whitespaces or fold line breaks. */
3483 /* Do we need to fold line breaks? */
3485 if (leading_break.start[0] == '\n') {
3486 if (trailing_breaks.start[0] == '\0') {
3487 if (!STRING_EXTEND(parser, string)) goto error;
3488 *(string.pointer++) = ' ';
3491 if (!JOIN(parser, string, trailing_breaks)) goto error;
3492 CLEAR(parser, trailing_breaks);
3494 CLEAR(parser, leading_break);
3497 if (!JOIN(parser, string, leading_break)) goto error;
3498 if (!JOIN(parser, string, trailing_breaks)) goto error;
3499 CLEAR(parser, leading_break);
3500 CLEAR(parser, trailing_breaks);
3505 if (!JOIN(parser, string, whitespaces)) goto error;
3506 CLEAR(parser, whitespaces);
3510 /* Eat the right quote. */
3514 end_mark = parser->mark;
3516 /* Create a token. */
3518 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3519 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3520 start_mark, end_mark);
3522 STRING_DEL(parser, leading_break);
3523 STRING_DEL(parser, trailing_breaks);
3524 STRING_DEL(parser, whitespaces);
3529 STRING_DEL(parser, string);
3530 STRING_DEL(parser, leading_break);
3531 STRING_DEL(parser, trailing_breaks);
3532 STRING_DEL(parser, whitespaces);
3538 * Scan a plain scalar.
3542 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3544 yaml_mark_t start_mark;
3545 yaml_mark_t end_mark;
3546 yaml_string_t string = NULL_STRING;
3547 yaml_string_t leading_break = NULL_STRING;
3548 yaml_string_t trailing_breaks = NULL_STRING;
3549 yaml_string_t whitespaces = NULL_STRING;
3550 int leading_blanks = 0;
3551 int indent = parser->indent+1;
3553 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3554 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3555 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3556 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3558 start_mark = end_mark = parser->mark;
3560 /* Consume the content of the plain scalar. */
3564 /* Check for a document indicator. */
3566 if (!CACHE(parser, 4)) goto error;
3568 if (parser->mark.column == 0 &&
3569 ((CHECK_AT(parser, '-', 0) &&
3570 CHECK_AT(parser, '-', 1) &&
3571 CHECK_AT(parser, '-', 2)) ||
3572 (CHECK_AT(parser, '.', 0) &&
3573 CHECK_AT(parser, '.', 1) &&
3574 CHECK_AT(parser, '.', 2))) &&
3575 IS_BLANKZ_AT(parser, 3)) break;
3577 /* Check for a comment. */
3579 if (CHECK(parser, '#'))
3582 /* Consume non-blank characters. */
3584 while (!IS_BLANKZ(parser))
3586 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
3588 if (parser->flow_level && CHECK(parser, ':') && !IS_BLANKZ_AT(parser, 1)) {
3589 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3590 start_mark, "found unexpected ':'");
3594 /* Check for indicators that may end a plain scalar. */
3596 if ((CHECK(parser, ':') && IS_BLANKZ_AT(parser, 1)) ||
3597 (parser->flow_level &&
3598 (CHECK(parser, ',') || CHECK(parser, ':') ||
3599 CHECK(parser, '?') || CHECK(parser, '[') ||
3600 CHECK(parser, ']') || CHECK(parser, '{') ||
3601 CHECK(parser, '}'))))
3604 /* Check if we need to join whitespaces and breaks. */
3606 if (leading_blanks || whitespaces.start != whitespaces.pointer)
3610 /* Do we need to fold line breaks? */
3612 if (leading_break.start[0] == '\n') {
3613 if (trailing_breaks.start[0] == '\0') {
3614 if (!STRING_EXTEND(parser, string)) goto error;
3615 *(string.pointer++) = ' ';
3618 if (!JOIN(parser, string, trailing_breaks)) goto error;
3619 CLEAR(parser, trailing_breaks);
3621 CLEAR(parser, leading_break);
3624 if (!JOIN(parser, string, leading_break)) goto error;
3625 if (!JOIN(parser, string, trailing_breaks)) goto error;
3626 CLEAR(parser, leading_break);
3627 CLEAR(parser, trailing_breaks);
3634 if (!JOIN(parser, string, whitespaces)) goto error;
3635 CLEAR(parser, whitespaces);
3639 /* Copy the character. */
3641 if (!READ(parser, string)) goto error;
3643 end_mark = parser->mark;
3645 if (!CACHE(parser, 2)) goto error;
3648 /* Is it the end? */
3650 if (!(IS_BLANK(parser) || IS_BREAK(parser)))
3653 /* Consume blank characters. */
3655 if (!CACHE(parser, 1)) goto error;
3657 while (IS_BLANK(parser) || IS_BREAK(parser))
3659 if (IS_BLANK(parser))
3661 /* Check for tab character that abuse intendation. */
3663 if (leading_blanks && parser->mark.column < indent && IS_TAB(parser)) {
3664 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3665 start_mark, "found a tab character that violate intendation");
3669 /* Consume a space or a tab character. */
3671 if (!leading_blanks) {
3672 if (!READ(parser, whitespaces)) goto error;
3680 if (!CACHE(parser, 2)) goto error;
3682 /* Check if it is a first line break. */
3684 if (!leading_blanks)
3686 CLEAR(parser, whitespaces);
3687 if (!READ_LINE(parser, leading_break)) goto error;
3692 if (!READ_LINE(parser, trailing_breaks)) goto error;
3695 if (!CACHE(parser, 1)) goto error;
3698 /* Check intendation level. */
3700 if (!parser->flow_level && parser->mark.column < indent)
3704 /* Create a token. */
3706 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3707 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3709 /* Note that we change the 'simple_key_allowed' flag. */
3711 if (leading_blanks) {
3712 parser->simple_key_allowed = 1;
3715 STRING_DEL(parser, leading_break);
3716 STRING_DEL(parser, trailing_breaks);
3717 STRING_DEL(parser, whitespaces);
3722 STRING_DEL(parser, string);
3723 STRING_DEL(parser, leading_break);
3724 STRING_DEL(parser, trailing_breaks);
3725 STRING_DEL(parser, whitespaces);