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 * Token initializers.
767 #define TOKEN_INIT(token,token_type,token_start_mark,token_end_mark) \
768 (memset(&(token), 0, sizeof(yaml_token_t)), \
769 (token).type = (token_type), \
770 (token).start_mark = (token_start_mark), \
771 (token).end_mark = (token_end_mark))
773 #define STREAM_START_TOKEN_INIT(token,token_encoding,start_mark,end_mark) \
774 (TOKEN_INIT((token),YAML_STREAM_START_TOKEN,(start_mark),(end_mark)), \
775 (token).data.stream_start.encoding = (token_encoding))
777 #define STREAM_END_TOKEN_INIT(token,start_mark,end_mark) \
778 (TOKEN_INIT((token),YAML_STREAM_END_TOKEN,(start_mark),(end_mark)))
780 #define ALIAS_TOKEN_INIT(token,token_value,start_mark,end_mark) \
781 (TOKEN_INIT((token),YAML_ALIAS_TOKEN,(start_mark),(end_mark)), \
782 (token).data.alias.value = (token_value))
784 #define ANCHOR_TOKEN_INIT(token,token_value,start_mark,end_mark) \
785 (TOKEN_INIT((token),YAML_ANCHOR_TOKEN,(start_mark),(end_mark)), \
786 (token).data.anchor.value = (token_value))
788 #define TAG_TOKEN_INIT(token,token_handle,token_suffix,start_mark,end_mark) \
789 (TOKEN_INIT((token),YAML_TAG_TOKEN,(start_mark),(end_mark)), \
790 (token).data.tag.handle = (token_handle), \
791 (token).data.tag.suffix = (token_suffix))
793 #define SCALAR_TOKEN_INIT(token,token_value,token_length,token_style,start_mark,end_mark) \
794 (TOKEN_INIT((token),YAML_SCALAR_TOKEN,(start_mark),(end_mark)), \
795 (token).data.scalar.value = (token_value), \
796 (token).data.scalar.length = (token_length), \
797 (token).data.scalar.style = (token_style))
799 #define VERSION_DIRECTIVE_TOKEN_INIT(token,token_major,token_minor,start_mark,end_mark) \
800 (TOKEN_INIT((token),YAML_VERSION_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \
801 (token).data.version_directive.major = (token_major), \
802 (token).data.version_directive.minor = (token_minor))
804 #define TAG_DIRECTIVE_TOKEN_INIT(token,token_handle,token_prefix,start_mark,end_mark) \
805 (TOKEN_INIT((token),YAML_TAG_DIRECTIVE_TOKEN,(start_mark),(end_mark)), \
806 (token).data.tag_directive.handle = (token_handle), \
807 (token).data.tag_directive.prefix = (token_prefix))
810 * Public API declarations.
814 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
821 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
822 yaml_mark_t context_mark, const char *problem);
825 * High-level token API.
829 yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
832 yaml_parser_fetch_next_token(yaml_parser_t *parser);
835 * Potential simple keys.
839 yaml_parser_stale_simple_keys(yaml_parser_t *parser);
842 yaml_parser_save_simple_key(yaml_parser_t *parser);
845 yaml_parser_remove_simple_key(yaml_parser_t *parser);
848 yaml_parser_increase_flow_level(yaml_parser_t *parser);
851 yaml_parser_decrease_flow_level(yaml_parser_t *parser);
854 * Indentation treatment.
858 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
859 int number, yaml_token_type_t type, yaml_mark_t mark);
862 yaml_parser_unroll_indent(yaml_parser_t *parser, int column);
869 yaml_parser_fetch_stream_start(yaml_parser_t *parser);
872 yaml_parser_fetch_stream_end(yaml_parser_t *parser);
875 yaml_parser_fetch_directive(yaml_parser_t *parser);
878 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
879 yaml_token_type_t type);
882 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
883 yaml_token_type_t type);
886 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
887 yaml_token_type_t type);
890 yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
893 yaml_parser_fetch_block_entry(yaml_parser_t *parser);
896 yaml_parser_fetch_key(yaml_parser_t *parser);
899 yaml_parser_fetch_value(yaml_parser_t *parser);
902 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
905 yaml_parser_fetch_tag(yaml_parser_t *parser);
908 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
911 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
914 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
921 yaml_parser_scan_to_next_token(yaml_parser_t *parser);
924 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
927 yaml_parser_scan_directive_name(yaml_parser_t *parser,
928 yaml_mark_t start_mark, yaml_char_t **name);
931 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
932 yaml_mark_t start_mark, int *major, int *minor);
935 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
936 yaml_mark_t start_mark, int *number);
939 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
940 yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
943 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
944 yaml_token_type_t type);
947 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
950 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
951 yaml_mark_t start_mark, yaml_char_t **handle);
954 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
955 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
958 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
959 yaml_mark_t start_mark, yaml_string_t *string);
962 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
966 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
967 int *indent, yaml_string_t *breaks,
968 yaml_mark_t start_mark, yaml_mark_t *end_mark);
971 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
975 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
978 * Get the next token.
982 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
984 assert(parser); /* Non-NULL parser object is expected. */
985 assert(token); /* Non-NULL token object is expected. */
987 /* No tokens after STREAM-END or error. */
989 if (parser->stream_end_produced || parser->error) {
990 memset(token, 0, sizeof(yaml_token_t));
995 /* Ensure that the tokens queue contains enough tokens. */
997 if (!parser->token_available) {
998 if (!yaml_parser_fetch_more_tokens(parser))
1002 /* Fetch the next token from the queue. */
1004 *token = DEQUEUE(parser, parser->tokens);
1005 parser->token_available = 0;
1006 parser->tokens_parsed ++;
1008 if (token->type == YAML_STREAM_END_TOKEN) {
1009 parser->stream_end_produced = 1;
1016 * Set the scanner error and return 0.
1020 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
1021 yaml_mark_t context_mark, const char *problem)
1023 parser->error = YAML_SCANNER_ERROR;
1024 parser->context = context;
1025 parser->context_mark = context_mark;
1026 parser->problem = problem;
1027 parser->problem_mark = parser->mark;
1033 * Ensure that the tokens queue contains at least one token which can be
1034 * returned to the Parser.
1038 yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
1040 int need_more_tokens;
1042 /* While we need more tokens to fetch, do it. */
1047 * Check if we really need to fetch more tokens.
1050 need_more_tokens = 0;
1052 if (parser->tokens.head == parser->tokens.tail)
1054 /* Queue is empty. */
1056 need_more_tokens = 1;
1060 yaml_simple_key_t *simple_key;
1062 /* Check if any potential simple key may occupy the head position. */
1064 if (!yaml_parser_stale_simple_keys(parser))
1067 for (simple_key = parser->simple_keys.start;
1068 simple_key != parser->simple_keys.top; simple_key++) {
1069 if (simple_key->possible
1070 && simple_key->token_number == parser->tokens_parsed) {
1071 need_more_tokens = 1;
1077 /* We are finished. */
1079 if (!need_more_tokens)
1082 /* Fetch the next token. */
1084 if (!yaml_parser_fetch_next_token(parser))
1088 parser->token_available = 1;
1094 * The dispatcher for token fetchers.
1098 yaml_parser_fetch_next_token(yaml_parser_t *parser)
1100 /* Ensure that the buffer is initialized. */
1102 if (!CACHE(parser, 1))
1105 /* Check if we just started scanning. Fetch STREAM-START then. */
1107 if (!parser->stream_start_produced)
1108 return yaml_parser_fetch_stream_start(parser);
1110 /* Eat whitespaces and comments until we reach the next token. */
1112 if (!yaml_parser_scan_to_next_token(parser))
1115 /* Remove obsolete potential simple keys. */
1117 if (!yaml_parser_stale_simple_keys(parser))
1120 /* Check the indentation level against the current column. */
1122 if (!yaml_parser_unroll_indent(parser, parser->mark.column))
1126 * Ensure that the buffer contains at least 4 characters. 4 is the length
1127 * of the longest indicators ('--- ' and '... ').
1130 if (!CACHE(parser, 4))
1133 /* Is it the end of the stream? */
1136 return yaml_parser_fetch_stream_end(parser);
1138 /* Is it a directive? */
1140 if (parser->mark.column == 0 && CHECK(parser, '%'))
1141 return yaml_parser_fetch_directive(parser);
1143 /* Is it the document start indicator? */
1145 if (parser->mark.column == 0
1146 && CHECK_AT(parser, '-', 0)
1147 && CHECK_AT(parser, '-', 1)
1148 && CHECK_AT(parser, '-', 2)
1149 && IS_BLANKZ_AT(parser, 3))
1150 return yaml_parser_fetch_document_indicator(parser,
1151 YAML_DOCUMENT_START_TOKEN);
1153 /* Is it the document end indicator? */
1155 if (parser->mark.column == 0
1156 && CHECK_AT(parser, '.', 0)
1157 && CHECK_AT(parser, '.', 1)
1158 && CHECK_AT(parser, '.', 2)
1159 && IS_BLANKZ_AT(parser, 3))
1160 return yaml_parser_fetch_document_indicator(parser,
1161 YAML_DOCUMENT_END_TOKEN);
1163 /* Is it the flow sequence start indicator? */
1165 if (CHECK(parser, '['))
1166 return yaml_parser_fetch_flow_collection_start(parser,
1167 YAML_FLOW_SEQUENCE_START_TOKEN);
1169 /* Is it the flow mapping start indicator? */
1171 if (CHECK(parser, '{'))
1172 return yaml_parser_fetch_flow_collection_start(parser,
1173 YAML_FLOW_MAPPING_START_TOKEN);
1175 /* Is it the flow sequence end indicator? */
1177 if (CHECK(parser, ']'))
1178 return yaml_parser_fetch_flow_collection_end(parser,
1179 YAML_FLOW_SEQUENCE_END_TOKEN);
1181 /* Is it the flow mapping end indicator? */
1183 if (CHECK(parser, '}'))
1184 return yaml_parser_fetch_flow_collection_end(parser,
1185 YAML_FLOW_MAPPING_END_TOKEN);
1187 /* Is it the flow entry indicator? */
1189 if (CHECK(parser, ','))
1190 return yaml_parser_fetch_flow_entry(parser);
1192 /* Is it the block entry indicator? */
1194 if (CHECK(parser, '-') && IS_BLANKZ_AT(parser, 1))
1195 return yaml_parser_fetch_block_entry(parser);
1197 /* Is it the key indicator? */
1199 if (CHECK(parser, '?') && (parser->flow_level || IS_BLANKZ_AT(parser, 1)))
1200 return yaml_parser_fetch_key(parser);
1202 /* Is it the value indicator? */
1204 if (CHECK(parser, ':') && (parser->flow_level || IS_BLANKZ_AT(parser, 1)))
1205 return yaml_parser_fetch_value(parser);
1207 /* Is it an alias? */
1209 if (CHECK(parser, '*'))
1210 return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
1212 /* Is it an anchor? */
1214 if (CHECK(parser, '&'))
1215 return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
1219 if (CHECK(parser, '!'))
1220 return yaml_parser_fetch_tag(parser);
1222 /* Is it a literal scalar? */
1224 if (CHECK(parser, '|') && !parser->flow_level)
1225 return yaml_parser_fetch_block_scalar(parser, 1);
1227 /* Is it a folded scalar? */
1229 if (CHECK(parser, '>') && !parser->flow_level)
1230 return yaml_parser_fetch_block_scalar(parser, 0);
1232 /* Is it a single-quoted scalar? */
1234 if (CHECK(parser, '\''))
1235 return yaml_parser_fetch_flow_scalar(parser, 1);
1237 /* Is it a double-quoted scalar? */
1239 if (CHECK(parser, '"'))
1240 return yaml_parser_fetch_flow_scalar(parser, 0);
1243 * Is it a plain scalar?
1245 * A plain scalar may start with any non-blank characters except
1247 * '-', '?', ':', ',', '[', ']', '{', '}',
1248 * '#', '&', '*', '!', '|', '>', '\'', '\"',
1251 * In the block context (and, for the '-' indicator, in the flow context
1252 * too), it may also start with the characters
1256 * if it is followed by a non-space character.
1258 * The last rule is more restrictive than the specification requires.
1261 if (!(IS_BLANKZ(parser) || CHECK(parser, '-') || CHECK(parser, '?')
1262 || CHECK(parser, ':') || CHECK(parser, ',') || CHECK(parser, '[')
1263 || CHECK(parser, ']') || CHECK(parser, '{') || CHECK(parser, '}')
1264 || CHECK(parser, '#') || CHECK(parser, '&') || CHECK(parser, '*')
1265 || CHECK(parser, '!') || CHECK(parser, '|') || CHECK(parser, '>')
1266 || CHECK(parser, '\'') || CHECK(parser, '"') || CHECK(parser, '%')
1267 || CHECK(parser, '@') || CHECK(parser, '`')) ||
1268 (CHECK(parser, '-') && !IS_BLANK_AT(parser, 1)) ||
1269 (!parser->flow_level &&
1270 (CHECK(parser, '?') || CHECK(parser, ':')) && !IS_BLANKZ_AT(parser, 1)))
1271 return yaml_parser_fetch_plain_scalar(parser);
1274 * If we don't determine the token type so far, it is an error.
1277 return yaml_parser_set_scanner_error(parser,
1278 "while scanning for the next token", parser->mark,
1279 "found character that cannot start any token");
1283 * Check the list of potential simple keys and remove the positions that
1284 * cannot contain simple keys anymore.
1288 yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1290 yaml_simple_key_t *simple_key;
1292 /* Check for a potential simple key for each flow level. */
1294 for (simple_key = parser->simple_keys.start;
1295 simple_key != parser->simple_keys.top; simple_key ++)
1298 * The specification requires that a simple key
1300 * - is limited to a single line,
1301 * - is shorter than 1024 characters.
1304 if (simple_key->possible
1305 && (simple_key->mark.line < parser->mark.line
1306 || simple_key->mark.index+1024 < parser->mark.index)) {
1308 /* Check if the potential simple key to be removed is required. */
1310 if (simple_key->required) {
1311 return yaml_parser_set_scanner_error(parser,
1312 "while scanning a simple key", simple_key->mark,
1313 "could not found expected ':'");
1316 simple_key->possible = 0;
1324 * Check if a simple key may start at the current position and add it if
1329 yaml_parser_save_simple_key(yaml_parser_t *parser)
1332 * A simple key is required at the current position if the scanner is in
1333 * the block context and the current column coincides with the indentation
1337 int required = (!parser->flow_level
1338 && parser->indent == parser->mark.column);
1341 * A simple key is required only when it is the first token in the current
1342 * line. Therefore it is always allowed. But we add a check anyway.
1345 assert(parser->simple_key_allowed || !required); /* Impossible. */
1348 * If the current position may start a simple key, save it.
1351 if (parser->simple_key_allowed)
1353 yaml_simple_key_t simple_key = { 1, required,
1354 parser->tokens_parsed + parser->tokens.tail - parser->tokens.head,
1357 if (!yaml_parser_remove_simple_key(parser)) return 0;
1359 *(parser->simple_keys.top-1) = simple_key;
1366 * Remove a potential simple key at the current flow level.
1370 yaml_parser_remove_simple_key(yaml_parser_t *parser)
1372 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1374 if (simple_key->possible)
1376 /* If the key is required, it is an error. */
1378 if (simple_key->required) {
1379 return yaml_parser_set_scanner_error(parser,
1380 "while scanning a simple key", simple_key->mark,
1381 "could not found expected ':'");
1385 /* Remove the key from the stack. */
1387 simple_key->possible = 0;
1393 * Increase the flow level and resize the simple key list if needed.
1397 yaml_parser_increase_flow_level(yaml_parser_t *parser)
1399 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1401 /* Reset the simple key on the next level. */
1403 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1406 /* Increase the flow level. */
1408 parser->flow_level++;
1414 * Decrease the flow level.
1418 yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1420 if (parser->flow_level) {
1421 parser->flow_level --;
1422 POP(parser, parser->simple_keys);
1429 * Push the current indentation level to the stack and set the new level
1430 * the current column is greater than the indentation level. In this case,
1431 * append or insert the specified token into the token queue.
1436 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1437 int number, yaml_token_type_t type, yaml_mark_t mark)
1441 /* In the flow context, do nothing. */
1443 if (parser->flow_level)
1446 if (parser->indent < column)
1449 * Push the current indentation level to the stack and set the new
1450 * indentation level.
1453 if (!PUSH(parser, parser->indents, parser->indent))
1456 parser->indent = column;
1458 /* Create a token and insert it into the queue. */
1460 TOKEN_INIT(token, type, mark, mark);
1463 if (!ENQUEUE(parser, parser->tokens, token))
1467 if (!QUEUE_INSERT(parser,
1468 parser->tokens, number - parser->tokens_parsed, token))
1477 * Pop indentation levels from the indents stack until the current level
1478 * becomes less or equal to the column. For each intendation level, append
1479 * the BLOCK-END token.
1484 yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1488 /* In the flow context, do nothing. */
1490 if (parser->flow_level)
1493 /* Loop through the intendation levels in the stack. */
1495 while (parser->indent > column)
1497 /* Create a token and append it to the queue. */
1499 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1501 if (!ENQUEUE(parser, parser->tokens, token))
1504 /* Pop the indentation level. */
1506 parser->indent = POP(parser, parser->indents);
1513 * Initialize the scanner and produce the STREAM-START token.
1517 yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1519 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1522 /* Set the initial indentation. */
1524 parser->indent = -1;
1526 /* Initialize the simple key stack. */
1528 if (!PUSH(parser, parser->simple_keys, simple_key))
1531 /* A simple key is allowed at the beginning of the stream. */
1533 parser->simple_key_allowed = 1;
1535 /* We have started. */
1537 parser->stream_start_produced = 1;
1539 /* Create the STREAM-START token and append it to the queue. */
1541 STREAM_START_TOKEN_INIT(token, parser->encoding,
1542 parser->mark, parser->mark);
1544 if (!ENQUEUE(parser, parser->tokens, token))
1551 * Produce the STREAM-END token and shut down the scanner.
1555 yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1559 /* Reset the indentation level. */
1561 if (!yaml_parser_unroll_indent(parser, -1))
1564 /* Reset simple keys. */
1566 if (!yaml_parser_remove_simple_key(parser))
1569 parser->simple_key_allowed = 0;
1571 /* Create the STREAM-END token and append it to the queue. */
1573 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1575 if (!ENQUEUE(parser, parser->tokens, token))
1582 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1586 yaml_parser_fetch_directive(yaml_parser_t *parser)
1590 /* Reset the indentation level. */
1592 if (!yaml_parser_unroll_indent(parser, -1))
1595 /* Reset simple keys. */
1597 if (!yaml_parser_remove_simple_key(parser))
1600 parser->simple_key_allowed = 0;
1602 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1604 if (!yaml_parser_scan_directive(parser, &token))
1607 /* Append the token to the queue. */
1609 if (!ENQUEUE(parser, parser->tokens, token)) {
1610 yaml_token_delete(&token);
1618 * Produce the DOCUMENT-START or DOCUMENT-END token.
1622 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1623 yaml_token_type_t type)
1625 yaml_mark_t start_mark, end_mark;
1628 /* Reset the indentation level. */
1630 if (!yaml_parser_unroll_indent(parser, -1))
1633 /* Reset simple keys. */
1635 if (!yaml_parser_remove_simple_key(parser))
1638 parser->simple_key_allowed = 0;
1640 /* Consume the token. */
1642 start_mark = parser->mark;
1648 end_mark = parser->mark;
1650 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1652 TOKEN_INIT(token, type, start_mark, end_mark);
1654 /* Append the token to the queue. */
1656 if (!ENQUEUE(parser, parser->tokens, token))
1663 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1667 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1668 yaml_token_type_t type)
1670 yaml_mark_t start_mark, end_mark;
1673 /* The indicators '[' and '{' may start a simple key. */
1675 if (!yaml_parser_save_simple_key(parser))
1678 /* Increase the flow level. */
1680 if (!yaml_parser_increase_flow_level(parser))
1683 /* A simple key may follow the indicators '[' and '{'. */
1685 parser->simple_key_allowed = 1;
1687 /* Consume the token. */
1689 start_mark = parser->mark;
1691 end_mark = parser->mark;
1693 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1695 TOKEN_INIT(token, type, start_mark, end_mark);
1697 /* Append the token to the queue. */
1699 if (!ENQUEUE(parser, parser->tokens, token))
1706 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1710 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1711 yaml_token_type_t type)
1713 yaml_mark_t start_mark, end_mark;
1716 /* Reset any potential simple key on the current flow level. */
1718 if (!yaml_parser_remove_simple_key(parser))
1721 /* Decrease the flow level. */
1723 if (!yaml_parser_decrease_flow_level(parser))
1726 /* No simple keys after the indicators ']' and '}'. */
1728 parser->simple_key_allowed = 0;
1730 /* Consume the token. */
1732 start_mark = parser->mark;
1734 end_mark = parser->mark;
1736 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1738 TOKEN_INIT(token, type, start_mark, end_mark);
1740 /* Append the token to the queue. */
1742 if (!ENQUEUE(parser, parser->tokens, token))
1749 * Produce the FLOW-ENTRY token.
1753 yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1755 yaml_mark_t start_mark, end_mark;
1758 /* Reset any potential simple keys on the current flow level. */
1760 if (!yaml_parser_remove_simple_key(parser))
1763 /* Simple keys are allowed after ','. */
1765 parser->simple_key_allowed = 1;
1767 /* Consume the token. */
1769 start_mark = parser->mark;
1771 end_mark = parser->mark;
1773 /* Create the FLOW-ENTRY token and append it to the queue. */
1775 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1777 if (!ENQUEUE(parser, parser->tokens, token))
1784 * Produce the BLOCK-ENTRY token.
1788 yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1790 yaml_mark_t start_mark, end_mark;
1793 /* Check if the scanner is in the block context. */
1795 if (!parser->flow_level)
1797 /* Check if we are allowed to start a new entry. */
1799 if (!parser->simple_key_allowed) {
1800 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1801 "block sequence entries are not allowed in this context");
1804 /* Add the BLOCK-SEQUENCE-START token if needed. */
1806 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1807 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1813 * It is an error for the '-' indicator to occur in the flow context,
1814 * but we let the Parser detect and report about it because the Parser
1815 * is able to point to the context.
1819 /* Reset any potential simple keys on the current flow level. */
1821 if (!yaml_parser_remove_simple_key(parser))
1824 /* Simple keys are allowed after '-'. */
1826 parser->simple_key_allowed = 1;
1828 /* Consume the token. */
1830 start_mark = parser->mark;
1832 end_mark = parser->mark;
1834 /* Create the BLOCK-ENTRY token and append it to the queue. */
1836 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1838 if (!ENQUEUE(parser, parser->tokens, token))
1845 * Produce the KEY token.
1849 yaml_parser_fetch_key(yaml_parser_t *parser)
1851 yaml_mark_t start_mark, end_mark;
1854 /* In the block context, additional checks are required. */
1856 if (!parser->flow_level)
1858 /* Check if we are allowed to start a new key (not nessesary simple). */
1860 if (!parser->simple_key_allowed) {
1861 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1862 "mapping keys are not allowed in this context");
1865 /* Add the BLOCK-MAPPING-START token if needed. */
1867 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1868 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1872 /* Reset any potential simple keys on the current flow level. */
1874 if (!yaml_parser_remove_simple_key(parser))
1877 /* Simple keys are allowed after '?' in the block context. */
1879 parser->simple_key_allowed = (!parser->flow_level);
1881 /* Consume the token. */
1883 start_mark = parser->mark;
1885 end_mark = parser->mark;
1887 /* Create the KEY token and append it to the queue. */
1889 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1891 if (!ENQUEUE(parser, parser->tokens, token))
1898 * Produce the VALUE token.
1902 yaml_parser_fetch_value(yaml_parser_t *parser)
1904 yaml_mark_t start_mark, end_mark;
1906 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1908 /* Have we found a simple key? */
1910 if (simple_key->possible)
1913 /* Create the KEY token and insert it into the queue. */
1915 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1917 if (!QUEUE_INSERT(parser, parser->tokens,
1918 simple_key->token_number - parser->tokens_parsed, token))
1921 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1923 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1924 simple_key->token_number,
1925 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1928 /* Remove the simple key. */
1930 simple_key->possible = 0;
1932 /* A simple key cannot follow another simple key. */
1934 parser->simple_key_allowed = 0;
1938 /* The ':' indicator follows a complex key. */
1940 /* In the block context, extra checks are required. */
1942 if (!parser->flow_level)
1944 /* Check if we are allowed to start a complex value. */
1946 if (!parser->simple_key_allowed) {
1947 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1948 "mapping values are not allowed in this context");
1951 /* Add the BLOCK-MAPPING-START token if needed. */
1953 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1954 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1958 /* Simple keys after ':' are allowed in the block context. */
1960 parser->simple_key_allowed = (!parser->flow_level);
1963 /* Consume the token. */
1965 start_mark = parser->mark;
1967 end_mark = parser->mark;
1969 /* Create the VALUE token and append it to the queue. */
1971 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1973 if (!ENQUEUE(parser, parser->tokens, token))
1980 * Produce the ALIAS or ANCHOR token.
1984 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1988 /* An anchor or an alias could be a simple key. */
1990 if (!yaml_parser_save_simple_key(parser))
1993 /* A simple key cannot follow an anchor or an alias. */
1995 parser->simple_key_allowed = 0;
1997 /* Create the ALIAS or ANCHOR token and append it to the queue. */
1999 if (!yaml_parser_scan_anchor(parser, &token, type))
2002 if (!ENQUEUE(parser, parser->tokens, token)) {
2003 yaml_token_delete(&token);
2010 * Produce the TAG token.
2014 yaml_parser_fetch_tag(yaml_parser_t *parser)
2018 /* A tag could be a simple key. */
2020 if (!yaml_parser_save_simple_key(parser))
2023 /* A simple key cannot follow a tag. */
2025 parser->simple_key_allowed = 0;
2027 /* Create the TAG token and append it to the queue. */
2029 if (!yaml_parser_scan_tag(parser, &token))
2032 if (!ENQUEUE(parser, parser->tokens, token)) {
2033 yaml_token_delete(&token);
2041 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
2045 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
2049 /* Remove any potential simple keys. */
2051 if (!yaml_parser_remove_simple_key(parser))
2054 /* A simple key may follow a block scalar. */
2056 parser->simple_key_allowed = 1;
2058 /* Create the SCALAR token and append it to the queue. */
2060 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
2063 if (!ENQUEUE(parser, parser->tokens, token)) {
2064 yaml_token_delete(&token);
2072 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
2076 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
2080 /* A plain scalar could be a simple key. */
2082 if (!yaml_parser_save_simple_key(parser))
2085 /* A simple key cannot follow a flow scalar. */
2087 parser->simple_key_allowed = 0;
2089 /* Create the SCALAR token and append it to the queue. */
2091 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
2094 if (!ENQUEUE(parser, parser->tokens, token)) {
2095 yaml_token_delete(&token);
2103 * Produce the SCALAR(...,plain) token.
2107 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
2111 /* A plain scalar could be a simple key. */
2113 if (!yaml_parser_save_simple_key(parser))
2116 /* A simple key cannot follow a flow scalar. */
2118 parser->simple_key_allowed = 0;
2120 /* Create the SCALAR token and append it to the queue. */
2122 if (!yaml_parser_scan_plain_scalar(parser, &token))
2125 if (!ENQUEUE(parser, parser->tokens, token)) {
2126 yaml_token_delete(&token);
2134 * Eat whitespaces and comments until the next token is found.
2138 yaml_parser_scan_to_next_token(yaml_parser_t *parser)
2140 /* Until the next token is not found. */
2144 /* Allow the BOM mark to start a line. */
2146 if (!CACHE(parser, 1)) return 0;
2148 if (parser->mark.column == 0 && IS_BOM(parser))
2156 * - in the flow context;
2157 * - in the block context, but not at the beginning of the line or
2158 * after '-', '?', or ':' (complex value).
2161 if (!CACHE(parser, 1)) return 0;
2163 while (CHECK(parser,' ') ||
2164 ((parser->flow_level || !parser->simple_key_allowed) &&
2165 CHECK(parser, '\t'))) {
2167 if (!CACHE(parser, 1)) return 0;
2170 /* Eat a comment until a line break. */
2172 if (CHECK(parser, '#')) {
2173 while (!IS_BREAKZ(parser)) {
2175 if (!CACHE(parser, 1)) return 0;
2179 /* If it is a line break, eat it. */
2181 if (IS_BREAK(parser))
2183 if (!CACHE(parser, 2)) return 0;
2186 /* In the block context, a new line may start a simple key. */
2188 if (!parser->flow_level) {
2189 parser->simple_key_allowed = 1;
2194 /* We have found a token. */
2204 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
2207 * %YAML 1.1 # a comment \n
2208 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2209 * %TAG !yaml! tag:yaml.org,2002: \n
2210 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2214 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
2216 yaml_mark_t start_mark, end_mark;
2217 yaml_char_t *name = NULL;
2219 yaml_char_t *handle = NULL, *prefix = NULL;
2223 start_mark = parser->mark;
2227 /* Scan the directive name. */
2229 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2232 /* Is it a YAML directive? */
2234 if (strcmp((char *)name, "YAML") == 0)
2236 /* Scan the VERSION directive value. */
2238 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2242 end_mark = parser->mark;
2244 /* Create a VERSION-DIRECTIVE token. */
2246 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2247 start_mark, end_mark);
2250 /* Is it a TAG directive? */
2252 else if (strcmp((char *)name, "TAG") == 0)
2254 /* Scan the TAG directive value. */
2256 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2260 end_mark = parser->mark;
2262 /* Create a TAG-DIRECTIVE token. */
2264 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2265 start_mark, end_mark);
2268 /* Unknown directive. */
2272 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2273 start_mark, "found uknown directive name");
2277 /* Eat the rest of the line including any comments. */
2279 if (!CACHE(parser, 1)) goto error;
2281 while (IS_BLANK(parser)) {
2283 if (!CACHE(parser, 1)) goto error;
2286 if (CHECK(parser, '#')) {
2287 while (!IS_BREAKZ(parser)) {
2289 if (!CACHE(parser, 1)) goto error;
2293 /* Check if we are at the end of the line. */
2295 if (!IS_BREAKZ(parser)) {
2296 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2297 start_mark, "did not found expected comment or line break");
2301 /* Eat a line break. */
2303 if (IS_BREAK(parser)) {
2304 if (!CACHE(parser, 2)) goto error;
2320 * Scan the directive name.
2323 * %YAML 1.1 # a comment \n
2325 * %TAG !yaml! tag:yaml.org,2002: \n
2330 yaml_parser_scan_directive_name(yaml_parser_t *parser,
2331 yaml_mark_t start_mark, yaml_char_t **name)
2333 yaml_string_t string = NULL_STRING;
2335 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2337 /* Consume the directive name. */
2339 if (!CACHE(parser, 1)) goto error;
2341 while (IS_ALPHA(parser))
2343 if (!READ(parser, string)) goto error;
2344 if (!CACHE(parser, 1)) goto error;
2347 /* Check if the name is empty. */
2349 if (string.start == string.pointer) {
2350 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2351 start_mark, "cannot found expected directive name");
2355 /* Check for an blank character after the name. */
2357 if (!IS_BLANKZ(parser)) {
2358 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2359 start_mark, "found unexpected non-alphabetical character");
2363 *name = string.start;
2368 STRING_DEL(parser, string);
2373 * Scan the value of VERSION-DIRECTIVE.
2376 * %YAML 1.1 # a comment \n
2381 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2382 yaml_mark_t start_mark, int *major, int *minor)
2384 /* Eat whitespaces. */
2386 if (!CACHE(parser, 1)) return 0;
2388 while (IS_BLANK(parser)) {
2390 if (!CACHE(parser, 1)) return 0;
2393 /* Consume the major version number. */
2395 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2400 if (!CHECK(parser, '.')) {
2401 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2402 start_mark, "did not find expected digit or '.' character");
2407 /* Consume the minor version number. */
2409 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2415 #define MAX_NUMBER_LENGTH 9
2418 * Scan the version number of VERSION-DIRECTIVE.
2421 * %YAML 1.1 # a comment \n
2423 * %YAML 1.1 # a comment \n
2428 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2429 yaml_mark_t start_mark, int *number)
2434 /* Repeat while the next character is digit. */
2436 if (!CACHE(parser, 1)) return 0;
2438 while (IS_DIGIT(parser))
2440 /* Check if the number is too long. */
2442 if (++length > MAX_NUMBER_LENGTH) {
2443 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2444 start_mark, "found extremely long version number");
2447 value = value*10 + AS_DIGIT(parser);
2451 if (!CACHE(parser, 1)) return 0;
2454 /* Check if the number was present. */
2457 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2458 start_mark, "did not find expected version number");
2467 * Scan the value of a TAG-DIRECTIVE token.
2470 * %TAG !yaml! tag:yaml.org,2002: \n
2471 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2475 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2476 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2478 yaml_char_t *handle_value = NULL;
2479 yaml_char_t *prefix_value = NULL;
2481 /* Eat whitespaces. */
2483 if (!CACHE(parser, 1)) goto error;
2485 while (IS_BLANK(parser)) {
2487 if (!CACHE(parser, 1)) goto error;
2490 /* Scan a handle. */
2492 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2495 /* Expect a whitespace. */
2497 if (!CACHE(parser, 1)) goto error;
2499 if (!IS_BLANK(parser)) {
2500 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2501 start_mark, "did not find expected whitespace");
2505 /* Eat whitespaces. */
2507 while (IS_BLANK(parser)) {
2509 if (!CACHE(parser, 1)) goto error;
2512 /* Scan a prefix. */
2514 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2517 /* Expect a whitespace or line break. */
2519 if (!CACHE(parser, 1)) goto error;
2521 if (!IS_BLANKZ(parser)) {
2522 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2523 start_mark, "did not find expected whitespace or line break");
2527 *handle = handle_value;
2528 *prefix = prefix_value;
2533 yaml_free(handle_value);
2534 yaml_free(prefix_value);
2539 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2540 yaml_token_type_t type)
2543 yaml_mark_t start_mark, end_mark;
2544 yaml_string_t string = NULL_STRING;
2546 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2548 /* Eat the indicator character. */
2550 start_mark = parser->mark;
2554 /* Consume the value. */
2556 if (!CACHE(parser, 1)) goto error;
2558 while (IS_ALPHA(parser)) {
2559 if (!READ(parser, string)) goto error;
2560 if (!CACHE(parser, 1)) goto error;
2564 end_mark = parser->mark;
2567 * Check if length of the anchor is greater than 0 and it is followed by
2568 * a whitespace character or one of the indicators:
2570 * '?', ':', ',', ']', '}', '%', '@', '`'.
2573 if (!length || !(IS_BLANKZ(parser) || CHECK(parser, '?') || CHECK(parser, ':') ||
2574 CHECK(parser, ',') || CHECK(parser, ']') || CHECK(parser, '}') ||
2575 CHECK(parser, '%') || CHECK(parser, '@') || CHECK(parser, '`'))) {
2576 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2577 "while scanning an anchor" : "while scanning an alias", start_mark,
2578 "did not find expected alphabetic or numeric character");
2582 /* Create a token. */
2584 if (type == YAML_ANCHOR_TOKEN) {
2585 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2588 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2594 STRING_DEL(parser, string);
2603 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2605 yaml_char_t *handle = NULL;
2606 yaml_char_t *suffix = NULL;
2607 yaml_mark_t start_mark, end_mark;
2609 start_mark = parser->mark;
2611 /* Check if the tag is in the canonical form. */
2613 if (!CACHE(parser, 2)) goto error;
2615 if (CHECK_AT(parser, '<', 1))
2617 /* Set the handle to '' */
2619 handle = yaml_malloc(1);
2620 if (!handle) goto error;
2628 /* Consume the tag value. */
2630 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2633 /* Check for '>' and eat it. */
2635 if (!CHECK(parser, '>')) {
2636 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2637 start_mark, "did not find the expected '>'");
2645 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2647 /* First, try to scan a handle. */
2649 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2652 /* Check if it is, indeed, handle. */
2654 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2656 /* Scan the suffix now. */
2658 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2663 /* It wasn't a handle after all. Scan the rest of the tag. */
2665 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2668 /* Set the handle to '!'. */
2671 handle = yaml_malloc(2);
2672 if (!handle) goto error;
2677 * A special case: the '!' tag. Set the handle to '' and the
2681 if (suffix[0] == '\0') {
2682 yaml_char_t *tmp = handle;
2689 /* Check the character which ends the tag. */
2691 if (!CACHE(parser, 1)) goto error;
2693 if (!IS_BLANKZ(parser)) {
2694 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2695 start_mark, "did not found expected whitespace or line break");
2699 end_mark = parser->mark;
2701 /* Create a token. */
2703 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2714 * Scan a tag handle.
2718 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2719 yaml_mark_t start_mark, yaml_char_t **handle)
2721 yaml_string_t string = NULL_STRING;
2723 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2725 /* Check the initial '!' character. */
2727 if (!CACHE(parser, 1)) goto error;
2729 if (!CHECK(parser, '!')) {
2730 yaml_parser_set_scanner_error(parser, directive ?
2731 "while scanning a tag directive" : "while scanning a tag",
2732 start_mark, "did not find expected '!'");
2736 /* Copy the '!' character. */
2738 if (!READ(parser, string)) goto error;
2740 /* Copy all subsequent alphabetical and numerical characters. */
2742 if (!CACHE(parser, 1)) goto error;
2744 while (IS_ALPHA(parser))
2746 if (!READ(parser, string)) goto error;
2747 if (!CACHE(parser, 1)) goto error;
2750 /* Check if the trailing character is '!' and copy it. */
2752 if (CHECK(parser, '!'))
2754 if (!READ(parser, string)) goto error;
2759 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2760 * directive, it's an error. If it's a tag token, it must be a part of
2764 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2765 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2766 start_mark, "did not find expected '!'");
2771 *handle = string.start;
2776 STRING_DEL(parser, string);
2785 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2786 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2788 size_t length = head ? strlen((char *)head) : 0;
2789 yaml_string_t string = NULL_STRING;
2791 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2793 /* Resize the string to include the head. */
2795 while (string.end - string.start <= length) {
2796 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2797 parser->error = YAML_MEMORY_ERROR;
2803 * Copy the head if needed.
2805 * Note that we don't copy the leading '!' character.
2809 memcpy(string.start, head+1, length-1);
2810 string.pointer += length-1;
2815 if (!CACHE(parser, 1)) goto error;
2818 * The set of characters that may appear in URI is as follows:
2820 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2821 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2825 while (IS_ALPHA(parser) || CHECK(parser, ';') || CHECK(parser, '/') ||
2826 CHECK(parser, '?') || CHECK(parser, ':') || CHECK(parser, '@') ||
2827 CHECK(parser, '&') || CHECK(parser, '=') || CHECK(parser, '+') ||
2828 CHECK(parser, '$') || CHECK(parser, ',') || CHECK(parser, '.') ||
2829 CHECK(parser, '!') || CHECK(parser, '~') || CHECK(parser, '*') ||
2830 CHECK(parser, '\'') || CHECK(parser, '(') || CHECK(parser, ')') ||
2831 CHECK(parser, '[') || CHECK(parser, ']') || CHECK(parser, '%'))
2833 /* Check if it is a URI-escape sequence. */
2835 if (CHECK(parser, '%')) {
2836 if (!yaml_parser_scan_uri_escapes(parser,
2837 directive, start_mark, &string)) goto error;
2840 if (!READ(parser, string)) goto error;
2844 if (!CACHE(parser, 1)) goto error;
2847 /* Check if the tag is non-empty. */
2850 if (!STRING_EXTEND(parser, string))
2853 yaml_parser_set_scanner_error(parser, directive ?
2854 "while parsing a %TAG directive" : "while parsing a tag",
2855 start_mark, "did not find expected tag URI");
2859 *uri = string.start;
2864 STRING_DEL(parser, string);
2869 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2873 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2874 yaml_mark_t start_mark, yaml_string_t *string)
2878 /* Decode the required number of characters. */
2882 unsigned char octet = 0;
2884 /* Check for a URI-escaped octet. */
2886 if (!CACHE(parser, 3)) return 0;
2888 if (!(CHECK(parser, '%') && IS_HEX_AT(parser, 1) && IS_HEX_AT(parser, 2))) {
2889 return yaml_parser_set_scanner_error(parser, directive ?
2890 "while parsing a %TAG directive" : "while parsing a tag",
2891 start_mark, "did not find URI escaped octet");
2894 /* Get the octet. */
2896 octet = (AS_HEX_AT(parser, 1) << 4) + AS_HEX_AT(parser, 2);
2898 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2902 width = (octet & 0x80) == 0x00 ? 1 :
2903 (octet & 0xE0) == 0xC0 ? 2 :
2904 (octet & 0xF0) == 0xE0 ? 3 :
2905 (octet & 0xF8) == 0xF0 ? 4 : 0;
2907 return yaml_parser_set_scanner_error(parser, directive ?
2908 "while parsing a %TAG directive" : "while parsing a tag",
2909 start_mark, "found an incorrect leading UTF-8 octet");
2914 /* Check if the trailing octet is correct. */
2916 if ((octet & 0xC0) != 0x80) {
2917 return yaml_parser_set_scanner_error(parser, directive ?
2918 "while parsing a %TAG directive" : "while parsing a tag",
2919 start_mark, "found an incorrect trailing UTF-8 octet");
2923 /* Copy the octet and move the pointers. */
2925 *(string->pointer++) = octet;
2936 * Scan a block scalar.
2940 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2943 yaml_mark_t start_mark;
2944 yaml_mark_t end_mark;
2945 yaml_string_t string = NULL_STRING;
2946 yaml_string_t leading_break = NULL_STRING;
2947 yaml_string_t trailing_breaks = NULL_STRING;
2951 int leading_blank = 0;
2952 int trailing_blank = 0;
2954 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2955 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2956 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2958 /* Eat the indicator '|' or '>'. */
2960 start_mark = parser->mark;
2964 /* Scan the additional block scalar indicators. */
2966 if (!CACHE(parser, 1)) goto error;
2968 /* Check for a chomping indicator. */
2970 if (CHECK(parser, '+') || CHECK(parser, '-'))
2972 /* Set the chomping method and eat the indicator. */
2974 chomping = CHECK(parser, '+') ? +1 : -1;
2978 /* Check for an indentation indicator. */
2980 if (!CACHE(parser, 1)) goto error;
2982 if (IS_DIGIT(parser))
2984 /* Check that the intendation is greater than 0. */
2986 if (CHECK(parser, '0')) {
2987 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2988 start_mark, "found an intendation indicator equal to 0");
2992 /* Get the intendation level and eat the indicator. */
2994 increment = AS_DIGIT(parser);
3000 /* Do the same as above, but in the opposite order. */
3002 else if (IS_DIGIT(parser))
3004 if (CHECK(parser, '0')) {
3005 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
3006 start_mark, "found an intendation indicator equal to 0");
3010 increment = AS_DIGIT(parser);
3014 if (!CACHE(parser, 1)) goto error;
3016 if (CHECK(parser, '+') || CHECK(parser, '-')) {
3017 chomping = CHECK(parser, '+') ? +1 : -1;
3023 /* Eat whitespaces and comments to the end of the line. */
3025 if (!CACHE(parser, 1)) goto error;
3027 while (IS_BLANK(parser)) {
3029 if (!CACHE(parser, 1)) goto error;
3032 if (CHECK(parser, '#')) {
3033 while (!IS_BREAKZ(parser)) {
3035 if (!CACHE(parser, 1)) goto error;
3039 /* Check if we are at the end of the line. */
3041 if (!IS_BREAKZ(parser)) {
3042 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
3043 start_mark, "did not found expected comment or line break");
3047 /* Eat a line break. */
3049 if (IS_BREAK(parser)) {
3050 if (!CACHE(parser, 2)) goto error;
3054 end_mark = parser->mark;
3056 /* Set the intendation level if it was specified. */
3059 indent = parser->indent >= 0 ? parser->indent+increment : increment;
3062 /* Scan the leading line breaks and determine the indentation level if needed. */
3064 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
3065 start_mark, &end_mark)) goto error;
3067 /* Scan the block scalar content. */
3069 if (!CACHE(parser, 1)) goto error;
3071 while (parser->mark.column == indent && !IS_Z(parser))
3074 * We are at the beginning of a non-empty line.
3077 /* Is it a trailing whitespace? */
3079 trailing_blank = IS_BLANK(parser);
3081 /* Check if we need to fold the leading line break. */
3083 if (!literal && (*leading_break.start == '\n')
3084 && !leading_blank && !trailing_blank)
3086 /* Do we need to join the lines by space? */
3088 if (*trailing_breaks.start == '\0') {
3089 if (!STRING_EXTEND(parser, string)) goto error;
3090 *(string.pointer ++) = ' ';
3093 CLEAR(parser, leading_break);
3096 if (!JOIN(parser, string, leading_break)) goto error;
3097 CLEAR(parser, leading_break);
3100 /* Append the remaining line breaks. */
3102 if (!JOIN(parser, string, trailing_breaks)) goto error;
3103 CLEAR(parser, trailing_breaks);
3105 /* Is it a leading whitespace? */
3107 leading_blank = IS_BLANK(parser);
3109 /* Consume the current line. */
3111 while (!IS_BREAKZ(parser)) {
3112 if (!READ(parser, string)) goto error;
3113 if (!CACHE(parser, 1)) goto error;
3116 /* Consume the line break. */
3118 if (!CACHE(parser, 2)) goto error;
3120 if (!READ_LINE(parser, leading_break)) goto error;
3122 /* Eat the following intendation spaces and line breaks. */
3124 if (!yaml_parser_scan_block_scalar_breaks(parser,
3125 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
3128 /* Chomp the tail. */
3130 if (chomping != -1) {
3131 if (!JOIN(parser, string, leading_break)) goto error;
3133 if (chomping == 1) {
3134 if (!JOIN(parser, string, trailing_breaks)) goto error;
3137 /* Create a token. */
3139 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3140 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
3141 start_mark, end_mark);
3143 STRING_DEL(parser, leading_break);
3144 STRING_DEL(parser, trailing_breaks);
3149 STRING_DEL(parser, string);
3150 STRING_DEL(parser, leading_break);
3151 STRING_DEL(parser, trailing_breaks);
3157 * Scan intendation spaces and line breaks for a block scalar. Determine the
3158 * intendation level if needed.
3162 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
3163 int *indent, yaml_string_t *breaks,
3164 yaml_mark_t start_mark, yaml_mark_t *end_mark)
3168 *end_mark = parser->mark;
3170 /* Eat the intendation spaces and line breaks. */
3174 /* Eat the intendation spaces. */
3176 if (!CACHE(parser, 1)) return 0;
3178 while ((!*indent || parser->mark.column < *indent) && IS_SPACE(parser)) {
3180 if (!CACHE(parser, 1)) return 0;
3183 if (parser->mark.column > max_indent)
3184 max_indent = parser->mark.column;
3186 /* Check for a tab character messing the intendation. */
3188 if ((!*indent || parser->mark.column < *indent) && IS_TAB(parser)) {
3189 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
3190 start_mark, "found a tab character where an intendation space is expected");
3193 /* Have we found a non-empty line? */
3195 if (!IS_BREAK(parser)) break;
3197 /* Consume the line break. */
3199 if (!CACHE(parser, 2)) return 0;
3200 if (!READ_LINE(parser, *breaks)) return 0;
3201 *end_mark = parser->mark;
3204 /* Determine the indentation level if needed. */
3207 *indent = max_indent;
3208 if (*indent < parser->indent + 1)
3209 *indent = parser->indent + 1;
3218 * Scan a quoted scalar.
3222 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3225 yaml_mark_t start_mark;
3226 yaml_mark_t end_mark;
3227 yaml_string_t string = NULL_STRING;
3228 yaml_string_t leading_break = NULL_STRING;
3229 yaml_string_t trailing_breaks = NULL_STRING;
3230 yaml_string_t whitespaces = NULL_STRING;
3233 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3234 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3235 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3236 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3238 /* Eat the left quote. */
3240 start_mark = parser->mark;
3244 /* Consume the content of the quoted scalar. */
3248 /* Check that there are no document indicators at the beginning of the line. */
3250 if (!CACHE(parser, 4)) goto error;
3252 if (parser->mark.column == 0 &&
3253 ((CHECK_AT(parser, '-', 0) &&
3254 CHECK_AT(parser, '-', 1) &&
3255 CHECK_AT(parser, '-', 2)) ||
3256 (CHECK_AT(parser, '.', 0) &&
3257 CHECK_AT(parser, '.', 1) &&
3258 CHECK_AT(parser, '.', 2))) &&
3259 IS_BLANKZ_AT(parser, 3))
3261 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3262 start_mark, "found unexpected document indicator");
3266 /* Check for EOF. */
3269 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3270 start_mark, "found unexpected end of stream");
3274 /* Consume non-blank characters. */
3276 if (!CACHE(parser, 2)) goto error;
3280 while (!IS_BLANKZ(parser))
3282 /* Check for an escaped single quote. */
3284 if (single && CHECK_AT(parser, '\'', 0) && CHECK_AT(parser, '\'', 1))
3286 if (!STRING_EXTEND(parser, string)) goto error;
3287 *(string.pointer++) = '\'';
3292 /* Check for the right quote. */
3294 else if (CHECK(parser, single ? '\'' : '"'))
3299 /* Check for an escaped line break. */
3301 else if (!single && CHECK(parser, '\\') && IS_BREAK_AT(parser, 1))
3303 if (!CACHE(parser, 3)) goto error;
3310 /* Check for an escape sequence. */
3312 else if (!single && CHECK(parser, '\\'))
3314 int code_length = 0;
3316 if (!STRING_EXTEND(parser, string)) goto error;
3318 /* Check the escape character. */
3320 switch (parser->buffer.pointer[1])
3323 *(string.pointer++) = '\0';
3327 *(string.pointer++) = '\x07';
3331 *(string.pointer++) = '\x08';
3336 *(string.pointer++) = '\x09';
3340 *(string.pointer++) = '\x0A';
3344 *(string.pointer++) = '\x0B';
3348 *(string.pointer++) = '\x0C';
3352 *(string.pointer++) = '\x0D';
3356 *(string.pointer++) = '\x1B';
3360 *(string.pointer++) = '\x20';
3364 *(string.pointer++) = '"';
3368 *(string.pointer++) = '\'';
3372 *(string.pointer++) = '\\';
3375 case 'N': /* NEL (#x85) */
3376 *(string.pointer++) = '\xC2';
3377 *(string.pointer++) = '\x85';
3380 case '_': /* #xA0 */
3381 *(string.pointer++) = '\xC2';
3382 *(string.pointer++) = '\xA0';
3385 case 'L': /* LS (#x2028) */
3386 *(string.pointer++) = '\xE2';
3387 *(string.pointer++) = '\x80';
3388 *(string.pointer++) = '\xA8';
3391 case 'P': /* PS (#x2029) */
3392 *(string.pointer++) = '\xE2';
3393 *(string.pointer++) = '\x80';
3394 *(string.pointer++) = '\xA9';
3410 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3411 start_mark, "found unknown escape character");
3418 /* Consume an arbitrary escape code. */
3422 unsigned int value = 0;
3425 /* Scan the character value. */
3427 if (!CACHE(parser, code_length)) goto error;
3429 for (k = 0; k < code_length; k ++) {
3430 if (!IS_HEX_AT(parser, k)) {
3431 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3432 start_mark, "did not find expected hexdecimal number");
3435 value = (value << 4) + AS_HEX_AT(parser, k);
3438 /* Check the value and write the character. */
3440 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3441 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3442 start_mark, "found invalid Unicode character escape code");
3446 if (value <= 0x7F) {
3447 *(string.pointer++) = value;
3449 else if (value <= 0x7FF) {
3450 *(string.pointer++) = 0xC0 + (value >> 6);
3451 *(string.pointer++) = 0x80 + (value & 0x3F);
3453 else if (value <= 0xFFFF) {
3454 *(string.pointer++) = 0xE0 + (value >> 12);
3455 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3456 *(string.pointer++) = 0x80 + (value & 0x3F);
3459 *(string.pointer++) = 0xF0 + (value >> 18);
3460 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3461 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3462 *(string.pointer++) = 0x80 + (value & 0x3F);
3465 /* Advance the pointer. */
3467 for (k = 0; k < code_length; k ++) {
3475 /* It is a non-escaped non-blank character. */
3477 if (!READ(parser, string)) goto error;
3480 if (!CACHE(parser, 2)) goto error;
3483 /* Check if we are at the end of the scalar. */
3485 if (CHECK(parser, single ? '\'' : '"'))
3488 /* Consume blank characters. */
3490 if (!CACHE(parser, 1)) goto error;
3492 while (IS_BLANK(parser) || IS_BREAK(parser))
3494 if (IS_BLANK(parser))
3496 /* Consume a space or a tab character. */
3498 if (!leading_blanks) {
3499 if (!READ(parser, whitespaces)) goto error;
3507 if (!CACHE(parser, 2)) goto error;
3509 /* Check if it is a first line break. */
3511 if (!leading_blanks)
3513 CLEAR(parser, whitespaces);
3514 if (!READ_LINE(parser, leading_break)) goto error;
3519 if (!READ_LINE(parser, trailing_breaks)) goto error;
3522 if (!CACHE(parser, 1)) goto error;
3525 /* Join the whitespaces or fold line breaks. */
3529 /* Do we need to fold line breaks? */
3531 if (leading_break.start[0] == '\n') {
3532 if (trailing_breaks.start[0] == '\0') {
3533 if (!STRING_EXTEND(parser, string)) goto error;
3534 *(string.pointer++) = ' ';
3537 if (!JOIN(parser, string, trailing_breaks)) goto error;
3538 CLEAR(parser, trailing_breaks);
3540 CLEAR(parser, leading_break);
3543 if (!JOIN(parser, string, leading_break)) goto error;
3544 if (!JOIN(parser, string, trailing_breaks)) goto error;
3545 CLEAR(parser, leading_break);
3546 CLEAR(parser, trailing_breaks);
3551 if (!JOIN(parser, string, whitespaces)) goto error;
3552 CLEAR(parser, whitespaces);
3556 /* Eat the right quote. */
3560 end_mark = parser->mark;
3562 /* Create a token. */
3564 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3565 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3566 start_mark, end_mark);
3568 STRING_DEL(parser, leading_break);
3569 STRING_DEL(parser, trailing_breaks);
3570 STRING_DEL(parser, whitespaces);
3575 STRING_DEL(parser, string);
3576 STRING_DEL(parser, leading_break);
3577 STRING_DEL(parser, trailing_breaks);
3578 STRING_DEL(parser, whitespaces);
3584 * Scan a plain scalar.
3588 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3590 yaml_mark_t start_mark;
3591 yaml_mark_t end_mark;
3592 yaml_string_t string = NULL_STRING;
3593 yaml_string_t leading_break = NULL_STRING;
3594 yaml_string_t trailing_breaks = NULL_STRING;
3595 yaml_string_t whitespaces = NULL_STRING;
3596 int leading_blanks = 0;
3597 int indent = parser->indent+1;
3599 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3600 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3601 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3602 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3604 start_mark = parser->mark;
3606 /* Consume the content of the plain scalar. */
3610 /* Check for a document indicator. */
3612 if (!CACHE(parser, 4)) goto error;
3614 if (parser->mark.column == 0 &&
3615 ((CHECK_AT(parser, '-', 0) &&
3616 CHECK_AT(parser, '-', 1) &&
3617 CHECK_AT(parser, '-', 2)) ||
3618 (CHECK_AT(parser, '.', 0) &&
3619 CHECK_AT(parser, '.', 1) &&
3620 CHECK_AT(parser, '.', 2))) &&
3621 IS_BLANKZ_AT(parser, 3)) break;
3623 /* Check for a comment. */
3625 if (CHECK(parser, '#'))
3628 /* Consume non-blank characters. */
3630 while (!IS_BLANKZ(parser))
3632 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
3634 if (parser->flow_level && CHECK(parser, ':') && !IS_BLANKZ_AT(parser, 1)) {
3635 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3636 start_mark, "found unexpected ':'");
3640 /* Check for indicators that may end a plain scalar. */
3642 if ((CHECK(parser, ':') && IS_BLANKZ_AT(parser, 1)) ||
3643 (parser->flow_level &&
3644 (CHECK(parser, ',') || CHECK(parser, ':') ||
3645 CHECK(parser, '?') || CHECK(parser, '[') ||
3646 CHECK(parser, ']') || CHECK(parser, '{') ||
3647 CHECK(parser, '}'))))
3650 /* Check if we need to join whitespaces and breaks. */
3652 if (leading_blanks || whitespaces.start != whitespaces.pointer)
3656 /* Do we need to fold line breaks? */
3658 if (leading_break.start[0] == '\n') {
3659 if (trailing_breaks.start[0] == '\0') {
3660 if (!STRING_EXTEND(parser, string)) goto error;
3661 *(string.pointer++) = ' ';
3664 if (!JOIN(parser, string, trailing_breaks)) goto error;
3665 CLEAR(parser, trailing_breaks);
3667 CLEAR(parser, leading_break);
3670 if (!JOIN(parser, string, leading_break)) goto error;
3671 if (!JOIN(parser, string, trailing_breaks)) goto error;
3672 CLEAR(parser, leading_break);
3673 CLEAR(parser, trailing_breaks);
3680 if (!JOIN(parser, string, whitespaces)) goto error;
3681 CLEAR(parser, whitespaces);
3685 /* Copy the character. */
3687 if (!READ(parser, string)) goto error;
3689 end_mark = parser->mark;
3691 if (!CACHE(parser, 2)) goto error;
3694 /* Is it the end? */
3696 if (!(IS_BLANK(parser) || IS_BREAK(parser)))
3699 /* Consume blank characters. */
3701 if (!CACHE(parser, 1)) goto error;
3703 while (IS_BLANK(parser) || IS_BREAK(parser))
3705 if (IS_BLANK(parser))
3707 /* Check for tab character that abuse intendation. */
3709 if (leading_blanks && parser->mark.column < indent && IS_TAB(parser)) {
3710 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3711 start_mark, "found a tab character that violate intendation");
3715 /* Consume a space or a tab character. */
3717 if (!leading_blanks) {
3718 if (!READ(parser, whitespaces)) goto error;
3726 if (!CACHE(parser, 2)) goto error;
3728 /* Check if it is a first line break. */
3730 if (!leading_blanks)
3732 CLEAR(parser, whitespaces);
3733 if (!READ_LINE(parser, leading_break)) goto error;
3738 if (!READ_LINE(parser, trailing_breaks)) goto error;
3741 if (!CACHE(parser, 1)) goto error;
3744 /* Check intendation level. */
3746 if (!parser->flow_level && parser->mark.column < indent)
3750 /* Create a token. */
3752 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3753 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3755 /* Note that we change the 'simple_key_allowed' flag. */
3757 if (leading_blanks) {
3758 parser->simple_key_allowed = 1;
3761 STRING_DEL(parser, leading_break);
3762 STRING_DEL(parser, trailing_breaks);
3763 STRING_DEL(parser, whitespaces);
3768 STRING_DEL(parser, string);
3769 STRING_DEL(parser, leading_break);
3770 STRING_DEL(parser, trailing_breaks);
3771 STRING_DEL(parser, whitespaces);