6 * The following notes assume that you are familiar with the YAML specification
7 * (http://yaml.org/spec/cvs/current.html). We mostly follow it, although in
8 * some cases we are less restrictive that it requires.
10 * The process of transforming a YAML stream into a sequence of events is
11 * divided on two steps: Scanning and Parsing.
13 * The Scanner transforms the input stream into a sequence of tokens, while the
14 * parser transform the sequence of tokens produced by the Scanner into a
15 * sequence of parsing events.
17 * The Scanner is rather clever and complicated. The Parser, on the contrary,
18 * is a straightforward implementation of a recursive-descendant parser (or,
19 * LL(1) parser, as it is usually called).
21 * Actually there are two issues of Scanning that might be called "clever", the
22 * rest is quite straightforward. The issues are "block collection start" and
23 * "simple keys". Both issues are explained below in details.
25 * Here the Scanning step is explained and implemented. We start with the list
26 * of all the tokens produced by the Scanner together with short descriptions.
30 * STREAM-START(encoding) # The stream start.
31 * STREAM-END # The stream end.
32 * VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
33 * TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
34 * DOCUMENT-START # '---'
35 * DOCUMENT-END # '...'
36 * BLOCK-SEQUENCE-START # Indentation increase denoting a block
37 * BLOCK-MAPPING-START # sequence or a block mapping.
38 * BLOCK-END # Indentation decrease.
39 * FLOW-SEQUENCE-START # '['
40 * FLOW-SEQUENCE-END # ']'
41 * BLOCK-SEQUENCE-START # '{'
42 * BLOCK-SEQUENCE-END # '}'
45 * KEY # '?' or nothing (simple keys).
47 * ALIAS(anchor) # '*anchor'
48 * ANCHOR(anchor) # '&anchor'
49 * TAG(handle,suffix) # '!handle!suffix'
50 * SCALAR(value,style) # A scalar.
52 * The following two tokens are "virtual" tokens denoting the beginning and the
55 * STREAM-START(encoding)
58 * We pass the information about the input stream encoding with the
61 * The next two tokens are responsible for tags:
63 * VERSION-DIRECTIVE(major,minor)
64 * TAG-DIRECTIVE(handle,prefix)
70 * %TAG !yaml! tag:yaml.org,2002:
73 * The correspoding sequence of tokens:
76 * VERSION-DIRECTIVE(1,1)
77 * TAG-DIRECTIVE("!","!foo")
78 * TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
82 * Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
85 * The document start and end indicators are represented by:
90 * Note that if a YAML stream contains an implicit document (without '---'
91 * and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
94 * In the following examples, we present whole documents together with the
97 * 1. An implicit document:
103 * STREAM-START(utf-8)
104 * SCALAR("a scalar",single-quoted)
107 * 2. An explicit document:
115 * STREAM-START(utf-8)
117 * SCALAR("a scalar",single-quoted)
121 * 3. Several documents in a stream:
127 * 'yet another scalar'
131 * STREAM-START(utf-8)
132 * SCALAR("a scalar",single-quoted)
134 * SCALAR("another scalar",single-quoted)
136 * SCALAR("yet another scalar",single-quoted)
139 * We have already introduced the SCALAR token above. The following tokens are
140 * used to describe aliases, anchors, tag, and scalars:
145 * SCALAR(value,style)
147 * The following series of examples illustrate the usage of these tokens:
149 * 1. A recursive sequence:
155 * STREAM-START(utf-8)
157 * FLOW-SEQUENCE-START
162 * 2. A tagged scalar:
164 * !!float "3.14" # A good approximation.
168 * STREAM-START(utf-8)
170 * SCALAR("3.14",double-quoted)
173 * 3. Various scalar styles:
175 * --- # Implicit empty plain scalars do not produce tokens.
177 * --- 'a single-quoted scalar'
178 * --- "a double-quoted scalar"
187 * STREAM-START(utf-8)
190 * SCALAR("a plain scalar",plain)
192 * SCALAR("a single-quoted scalar",single-quoted)
194 * SCALAR("a double-quoted scalar",double-quoted)
196 * SCALAR("a literal scalar",literal)
198 * SCALAR("a folded scalar",folded)
201 * Now it's time to review collection-related tokens. We will start with
204 * FLOW-SEQUENCE-START
212 * The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
213 * FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
214 * correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
215 * indicators '?' and ':', which are used for denoting mapping keys and values,
216 * are represented by the KEY and VALUE tokens.
218 * The following examples show flow collections:
220 * 1. A flow sequence:
222 * [item 1, item 2, item 3]
226 * STREAM-START(utf-8)
227 * FLOW-SEQUENCE-START
228 * SCALAR("item 1",plain)
230 * SCALAR("item 2",plain)
232 * SCALAR("item 3",plain)
239 * a simple key: a value, # Note that the KEY token is produced.
240 * ? a complex key: another value,
245 * STREAM-START(utf-8)
248 * SCALAR("a simple key",plain)
250 * SCALAR("a value",plain)
253 * SCALAR("a complex key",plain)
255 * SCALAR("another value",plain)
260 * A simple key is a key which is not denoted by the '?' indicator. Note that
261 * the Scanner still produce the KEY token whenever it encounters a simple key.
263 * For scanning block collections, the following tokens are used (note that we
264 * repeat KEY and VALUE here):
266 * BLOCK-SEQUENCE-START
267 * BLOCK-MAPPING-START
273 * The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
274 * increase that precedes a block collection (cf. the INDENT token in Python).
275 * The token BLOCK-END denote indentation decrease that ends a block collection
276 * (cf. the DEDENT token in Python). However YAML has some syntax pecularities
277 * that makes detections of these tokens more complex.
279 * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
280 * '-', '?', and ':' correspondingly.
282 * The following examples show how the tokens BLOCK-SEQUENCE-START,
283 * BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
285 * 1. Block sequences:
298 * STREAM-START(utf-8)
299 * BLOCK-SEQUENCE-START
301 * SCALAR("item 1",plain)
303 * SCALAR("item 2",plain)
305 * BLOCK-SEQUENCE-START
307 * SCALAR("item 3.1",plain)
309 * SCALAR("item 3.2",plain)
312 * BLOCK-MAPPING-START
314 * SCALAR("key 1",plain)
316 * SCALAR("value 1",plain)
318 * SCALAR("key 2",plain)
320 * SCALAR("value 2",plain)
327 * a simple key: a value # The KEY token is produced here.
339 * STREAM-START(utf-8)
340 * BLOCK-MAPPING-START
342 * SCALAR("a simple key",plain)
344 * SCALAR("a value",plain)
346 * SCALAR("a complex key",plain)
348 * SCALAR("another value",plain)
350 * SCALAR("a mapping",plain)
351 * BLOCK-MAPPING-START
353 * SCALAR("key 1",plain)
355 * SCALAR("value 1",plain)
357 * SCALAR("key 2",plain)
359 * SCALAR("value 2",plain)
362 * SCALAR("a sequence",plain)
364 * BLOCK-SEQUENCE-START
366 * SCALAR("item 1",plain)
368 * SCALAR("item 2",plain)
373 * YAML does not always require to start a new block collection from a new
374 * line. If the current line contains only '-', '?', and ':' indicators, a new
375 * block collection may start at the current line. The following examples
376 * illustrate this case:
378 * 1. Collections in a sequence:
389 * STREAM-START(utf-8)
390 * BLOCK-SEQUENCE-START
392 * BLOCK-SEQUENCE-START
394 * SCALAR("item 1",plain)
396 * SCALAR("item 2",plain)
399 * BLOCK-MAPPING-START
401 * SCALAR("key 1",plain)
403 * SCALAR("value 1",plain)
405 * SCALAR("key 2",plain)
407 * SCALAR("value 2",plain)
410 * BLOCK-MAPPING-START
412 * SCALAR("complex key")
414 * SCALAR("complex value")
419 * 2. Collections in a mapping:
430 * STREAM-START(utf-8)
431 * BLOCK-MAPPING-START
433 * SCALAR("a sequence",plain)
435 * BLOCK-SEQUENCE-START
437 * SCALAR("item 1",plain)
439 * SCALAR("item 2",plain)
442 * SCALAR("a mapping",plain)
444 * BLOCK-MAPPING-START
446 * SCALAR("key 1",plain)
448 * SCALAR("value 1",plain)
450 * SCALAR("key 2",plain)
452 * SCALAR("value 2",plain)
457 * YAML also permits non-indented sequences if they are included into a block
458 * mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
461 * - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
466 * STREAM-START(utf-8)
467 * BLOCK-MAPPING-START
469 * SCALAR("key",plain)
472 * SCALAR("item 1",plain)
474 * SCALAR("item 2",plain)
478 #include "yaml_private.h"
481 * Ensure that the buffer contains the required number of characters.
482 * Return 1 on success, 0 on failure (reader error or memory error).
485 #define CACHE(parser,length) \
486 (parser->unread >= (length) \
488 : yaml_parser_update_buffer(parser, (length)))
491 * Advance the buffer pointer.
494 #define SKIP(parser) \
495 (parser->mark.index ++, \
496 parser->mark.column ++, \
498 parser->buffer.pointer += WIDTH(parser->buffer))
500 #define SKIP_LINE(parser) \
501 (IS_CRLF(parser->buffer) ? \
502 (parser->mark.index += 2, \
503 parser->mark.column = 0, \
504 parser->mark.line ++, \
505 parser->unread -= 2, \
506 parser->buffer.pointer += 2) : \
507 IS_BREAK(parser->buffer) ? \
508 (parser->mark.index ++, \
509 parser->mark.column = 0, \
510 parser->mark.line ++, \
512 parser->buffer.pointer += WIDTH(parser->buffer)) : 0)
515 * Copy a character to a string buffer and advance pointers.
518 #define READ(parser,string) \
519 (STRING_EXTEND(parser,string) ? \
520 (COPY(string,parser->buffer), \
521 parser->mark.index ++, \
522 parser->mark.column ++, \
527 * Copy a line break character to a string buffer and advance pointers.
530 #define READ_LINE(parser,string) \
531 (STRING_EXTEND(parser,string) ? \
532 (((CHECK_AT(parser->buffer,'\r',0) \
533 && CHECK_AT(parser->buffer,'\n',1)) ? /* CR LF -> LF */ \
534 (*((string).pointer++) = (yaml_char_t) '\n', \
535 parser->buffer.pointer += 2, \
536 parser->mark.index += 2, \
537 parser->mark.column = 0, \
538 parser->mark.line ++, \
539 parser->unread -= 2) : \
540 (CHECK_AT(parser->buffer,'\r',0) \
541 || CHECK_AT(parser->buffer,'\n',0)) ? /* CR|LF -> LF */ \
542 (*((string).pointer++) = (yaml_char_t) '\n', \
543 parser->buffer.pointer ++, \
544 parser->mark.index ++, \
545 parser->mark.column = 0, \
546 parser->mark.line ++, \
547 parser->unread --) : \
548 (CHECK_AT(parser->buffer,'\xC2',0) \
549 && CHECK_AT(parser->buffer,'\x85',1)) ? /* NEL -> LF */ \
550 (*((string).pointer++) = (yaml_char_t) '\n', \
551 parser->buffer.pointer += 2, \
552 parser->mark.index ++, \
553 parser->mark.column = 0, \
554 parser->mark.line ++, \
555 parser->unread --) : \
556 (CHECK_AT(parser->buffer,'\xE2',0) && \
557 CHECK_AT(parser->buffer,'\x80',1) && \
558 (CHECK_AT(parser->buffer,'\xA8',2) || \
559 CHECK_AT(parser->buffer,'\xA9',2))) ? /* LS|PS -> LS|PS */ \
560 (*((string).pointer++) = *(parser->buffer.pointer++), \
561 *((string).pointer++) = *(parser->buffer.pointer++), \
562 *((string).pointer++) = *(parser->buffer.pointer++), \
563 parser->mark.index ++, \
564 parser->mark.column = 0, \
565 parser->mark.line ++, \
566 parser->unread --) : 0), \
570 * Public API declarations.
574 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
581 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
582 yaml_mark_t context_mark, const char *problem);
585 * High-level token API.
589 yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
592 yaml_parser_fetch_next_token(yaml_parser_t *parser);
595 * Potential simple keys.
599 yaml_parser_stale_simple_keys(yaml_parser_t *parser);
602 yaml_parser_save_simple_key(yaml_parser_t *parser);
605 yaml_parser_remove_simple_key(yaml_parser_t *parser);
608 yaml_parser_increase_flow_level(yaml_parser_t *parser);
611 yaml_parser_decrease_flow_level(yaml_parser_t *parser);
614 * Indentation treatment.
618 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
619 int number, yaml_token_type_t type, yaml_mark_t mark);
622 yaml_parser_unroll_indent(yaml_parser_t *parser, int column);
629 yaml_parser_fetch_stream_start(yaml_parser_t *parser);
632 yaml_parser_fetch_stream_end(yaml_parser_t *parser);
635 yaml_parser_fetch_directive(yaml_parser_t *parser);
638 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
639 yaml_token_type_t type);
642 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
643 yaml_token_type_t type);
646 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
647 yaml_token_type_t type);
650 yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
653 yaml_parser_fetch_block_entry(yaml_parser_t *parser);
656 yaml_parser_fetch_key(yaml_parser_t *parser);
659 yaml_parser_fetch_value(yaml_parser_t *parser);
662 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
665 yaml_parser_fetch_tag(yaml_parser_t *parser);
668 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
671 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
674 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
681 yaml_parser_scan_to_next_token(yaml_parser_t *parser);
684 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
687 yaml_parser_scan_directive_name(yaml_parser_t *parser,
688 yaml_mark_t start_mark, yaml_char_t **name);
691 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
692 yaml_mark_t start_mark, int *major, int *minor);
695 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
696 yaml_mark_t start_mark, int *number);
699 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
700 yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
703 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
704 yaml_token_type_t type);
707 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
710 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
711 yaml_mark_t start_mark, yaml_char_t **handle);
714 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
715 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
718 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
719 yaml_mark_t start_mark, yaml_string_t *string);
722 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
726 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
727 int *indent, yaml_string_t *breaks,
728 yaml_mark_t start_mark, yaml_mark_t *end_mark);
731 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
735 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
738 * Get the next token.
742 yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
744 assert(parser); /* Non-NULL parser object is expected. */
745 assert(token); /* Non-NULL token object is expected. */
747 /* Erase the token object. */
749 memset(token, 0, sizeof(yaml_token_t));
751 /* No tokens after STREAM-END or error. */
753 if (parser->stream_end_produced || parser->error) {
757 /* Ensure that the tokens queue contains enough tokens. */
759 if (!parser->token_available) {
760 if (!yaml_parser_fetch_more_tokens(parser))
764 /* Fetch the next token from the queue. */
766 *token = DEQUEUE(parser, parser->tokens);
767 parser->token_available = 0;
768 parser->tokens_parsed ++;
770 if (token->type == YAML_STREAM_END_TOKEN) {
771 parser->stream_end_produced = 1;
778 * Set the scanner error and return 0.
782 yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
783 yaml_mark_t context_mark, const char *problem)
785 parser->error = YAML_SCANNER_ERROR;
786 parser->context = context;
787 parser->context_mark = context_mark;
788 parser->problem = problem;
789 parser->problem_mark = parser->mark;
795 * Ensure that the tokens queue contains at least one token which can be
796 * returned to the Parser.
800 yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
802 int need_more_tokens;
804 /* While we need more tokens to fetch, do it. */
809 * Check if we really need to fetch more tokens.
812 need_more_tokens = 0;
814 if (parser->tokens.head == parser->tokens.tail)
816 /* Queue is empty. */
818 need_more_tokens = 1;
822 yaml_simple_key_t *simple_key;
824 /* Check if any potential simple key may occupy the head position. */
826 if (!yaml_parser_stale_simple_keys(parser))
829 for (simple_key = parser->simple_keys.start;
830 simple_key != parser->simple_keys.top; simple_key++) {
831 if (simple_key->possible
832 && simple_key->token_number == parser->tokens_parsed) {
833 need_more_tokens = 1;
839 /* We are finished. */
841 if (!need_more_tokens)
844 /* Fetch the next token. */
846 if (!yaml_parser_fetch_next_token(parser))
850 parser->token_available = 1;
856 * The dispatcher for token fetchers.
860 yaml_parser_fetch_next_token(yaml_parser_t *parser)
862 /* Ensure that the buffer is initialized. */
864 if (!CACHE(parser, 1))
867 /* Check if we just started scanning. Fetch STREAM-START then. */
869 if (!parser->stream_start_produced)
870 return yaml_parser_fetch_stream_start(parser);
872 /* Eat whitespaces and comments until we reach the next token. */
874 if (!yaml_parser_scan_to_next_token(parser))
877 /* Remove obsolete potential simple keys. */
879 if (!yaml_parser_stale_simple_keys(parser))
882 /* Check the indentation level against the current column. */
884 if (!yaml_parser_unroll_indent(parser, parser->mark.column))
888 * Ensure that the buffer contains at least 4 characters. 4 is the length
889 * of the longest indicators ('--- ' and '... ').
892 if (!CACHE(parser, 4))
895 /* Is it the end of the stream? */
897 if (IS_Z(parser->buffer))
898 return yaml_parser_fetch_stream_end(parser);
900 /* Is it a directive? */
902 if (parser->mark.column == 0 && CHECK(parser->buffer, '%'))
903 return yaml_parser_fetch_directive(parser);
905 /* Is it the document start indicator? */
907 if (parser->mark.column == 0
908 && CHECK_AT(parser->buffer, '-', 0)
909 && CHECK_AT(parser->buffer, '-', 1)
910 && CHECK_AT(parser->buffer, '-', 2)
911 && IS_BLANKZ_AT(parser->buffer, 3))
912 return yaml_parser_fetch_document_indicator(parser,
913 YAML_DOCUMENT_START_TOKEN);
915 /* Is it the document end indicator? */
917 if (parser->mark.column == 0
918 && CHECK_AT(parser->buffer, '.', 0)
919 && CHECK_AT(parser->buffer, '.', 1)
920 && CHECK_AT(parser->buffer, '.', 2)
921 && IS_BLANKZ_AT(parser->buffer, 3))
922 return yaml_parser_fetch_document_indicator(parser,
923 YAML_DOCUMENT_END_TOKEN);
925 /* Is it the flow sequence start indicator? */
927 if (CHECK(parser->buffer, '['))
928 return yaml_parser_fetch_flow_collection_start(parser,
929 YAML_FLOW_SEQUENCE_START_TOKEN);
931 /* Is it the flow mapping start indicator? */
933 if (CHECK(parser->buffer, '{'))
934 return yaml_parser_fetch_flow_collection_start(parser,
935 YAML_FLOW_MAPPING_START_TOKEN);
937 /* Is it the flow sequence end indicator? */
939 if (CHECK(parser->buffer, ']'))
940 return yaml_parser_fetch_flow_collection_end(parser,
941 YAML_FLOW_SEQUENCE_END_TOKEN);
943 /* Is it the flow mapping end indicator? */
945 if (CHECK(parser->buffer, '}'))
946 return yaml_parser_fetch_flow_collection_end(parser,
947 YAML_FLOW_MAPPING_END_TOKEN);
949 /* Is it the flow entry indicator? */
951 if (CHECK(parser->buffer, ','))
952 return yaml_parser_fetch_flow_entry(parser);
954 /* Is it the block entry indicator? */
956 if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1))
957 return yaml_parser_fetch_block_entry(parser);
959 /* Is it the key indicator? */
961 if (CHECK(parser->buffer, '?')
962 && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
963 return yaml_parser_fetch_key(parser);
965 /* Is it the value indicator? */
967 if (CHECK(parser->buffer, ':')
968 && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
969 return yaml_parser_fetch_value(parser);
971 /* Is it an alias? */
973 if (CHECK(parser->buffer, '*'))
974 return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
976 /* Is it an anchor? */
978 if (CHECK(parser->buffer, '&'))
979 return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
983 if (CHECK(parser->buffer, '!'))
984 return yaml_parser_fetch_tag(parser);
986 /* Is it a literal scalar? */
988 if (CHECK(parser->buffer, '|') && !parser->flow_level)
989 return yaml_parser_fetch_block_scalar(parser, 1);
991 /* Is it a folded scalar? */
993 if (CHECK(parser->buffer, '>') && !parser->flow_level)
994 return yaml_parser_fetch_block_scalar(parser, 0);
996 /* Is it a single-quoted scalar? */
998 if (CHECK(parser->buffer, '\''))
999 return yaml_parser_fetch_flow_scalar(parser, 1);
1001 /* Is it a double-quoted scalar? */
1003 if (CHECK(parser->buffer, '"'))
1004 return yaml_parser_fetch_flow_scalar(parser, 0);
1007 * Is it a plain scalar?
1009 * A plain scalar may start with any non-blank characters except
1011 * '-', '?', ':', ',', '[', ']', '{', '}',
1012 * '#', '&', '*', '!', '|', '>', '\'', '\"',
1015 * In the block context (and, for the '-' indicator, in the flow context
1016 * too), it may also start with the characters
1020 * if it is followed by a non-space character.
1022 * The last rule is more restrictive than the specification requires.
1025 if (!(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '-')
1026 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':')
1027 || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '[')
1028 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
1029 || CHECK(parser->buffer, '}') || CHECK(parser->buffer, '#')
1030 || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '*')
1031 || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '|')
1032 || CHECK(parser->buffer, '>') || CHECK(parser->buffer, '\'')
1033 || CHECK(parser->buffer, '"') || CHECK(parser->buffer, '%')
1034 || CHECK(parser->buffer, '@') || CHECK(parser->buffer, '`')) ||
1035 (CHECK(parser->buffer, '-') && !IS_BLANK_AT(parser->buffer, 1)) ||
1036 (!parser->flow_level &&
1037 (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':'))
1038 && !IS_BLANKZ_AT(parser->buffer, 1)))
1039 return yaml_parser_fetch_plain_scalar(parser);
1042 * If we don't determine the token type so far, it is an error.
1045 return yaml_parser_set_scanner_error(parser,
1046 "while scanning for the next token", parser->mark,
1047 "found character that cannot start any token");
1051 * Check the list of potential simple keys and remove the positions that
1052 * cannot contain simple keys anymore.
1056 yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1058 yaml_simple_key_t *simple_key;
1060 /* Check for a potential simple key for each flow level. */
1062 for (simple_key = parser->simple_keys.start;
1063 simple_key != parser->simple_keys.top; simple_key ++)
1066 * The specification requires that a simple key
1068 * - is limited to a single line,
1069 * - is shorter than 1024 characters.
1072 if (simple_key->possible
1073 && (simple_key->mark.line < parser->mark.line
1074 || simple_key->mark.index+1024 < parser->mark.index)) {
1076 /* Check if the potential simple key to be removed is required. */
1078 if (simple_key->required) {
1079 return yaml_parser_set_scanner_error(parser,
1080 "while scanning a simple key", simple_key->mark,
1081 "could not found expected ':'");
1084 simple_key->possible = 0;
1092 * Check if a simple key may start at the current position and add it if
1097 yaml_parser_save_simple_key(yaml_parser_t *parser)
1100 * A simple key is required at the current position if the scanner is in
1101 * the block context and the current column coincides with the indentation
1105 int required = (!parser->flow_level
1106 && parser->indent == parser->mark.column);
1109 * A simple key is required only when it is the first token in the current
1110 * line. Therefore it is always allowed. But we add a check anyway.
1113 assert(parser->simple_key_allowed || !required); /* Impossible. */
1116 * If the current position may start a simple key, save it.
1119 if (parser->simple_key_allowed)
1121 yaml_simple_key_t simple_key = { 1, required,
1122 parser->tokens_parsed + parser->tokens.tail - parser->tokens.head,
1125 if (!yaml_parser_remove_simple_key(parser)) return 0;
1127 *(parser->simple_keys.top-1) = simple_key;
1134 * Remove a potential simple key at the current flow level.
1138 yaml_parser_remove_simple_key(yaml_parser_t *parser)
1140 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1142 if (simple_key->possible)
1144 /* If the key is required, it is an error. */
1146 if (simple_key->required) {
1147 return yaml_parser_set_scanner_error(parser,
1148 "while scanning a simple key", simple_key->mark,
1149 "could not found expected ':'");
1153 /* Remove the key from the stack. */
1155 simple_key->possible = 0;
1161 * Increase the flow level and resize the simple key list if needed.
1165 yaml_parser_increase_flow_level(yaml_parser_t *parser)
1167 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1169 /* Reset the simple key on the next level. */
1171 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1174 /* Increase the flow level. */
1176 parser->flow_level++;
1182 * Decrease the flow level.
1186 yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1188 if (parser->flow_level) {
1189 parser->flow_level --;
1190 POP(parser, parser->simple_keys);
1197 * Push the current indentation level to the stack and set the new level
1198 * the current column is greater than the indentation level. In this case,
1199 * append or insert the specified token into the token queue.
1204 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1205 int number, yaml_token_type_t type, yaml_mark_t mark)
1209 /* In the flow context, do nothing. */
1211 if (parser->flow_level)
1214 if (parser->indent < column)
1217 * Push the current indentation level to the stack and set the new
1218 * indentation level.
1221 if (!PUSH(parser, parser->indents, parser->indent))
1224 parser->indent = column;
1226 /* Create a token and insert it into the queue. */
1228 TOKEN_INIT(token, type, mark, mark);
1231 if (!ENQUEUE(parser, parser->tokens, token))
1235 if (!QUEUE_INSERT(parser,
1236 parser->tokens, number - parser->tokens_parsed, token))
1245 * Pop indentation levels from the indents stack until the current level
1246 * becomes less or equal to the column. For each intendation level, append
1247 * the BLOCK-END token.
1252 yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1256 /* In the flow context, do nothing. */
1258 if (parser->flow_level)
1261 /* Loop through the intendation levels in the stack. */
1263 while (parser->indent > column)
1265 /* Create a token and append it to the queue. */
1267 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1269 if (!ENQUEUE(parser, parser->tokens, token))
1272 /* Pop the indentation level. */
1274 parser->indent = POP(parser, parser->indents);
1281 * Initialize the scanner and produce the STREAM-START token.
1285 yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1287 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1290 /* Set the initial indentation. */
1292 parser->indent = -1;
1294 /* Initialize the simple key stack. */
1296 if (!PUSH(parser, parser->simple_keys, simple_key))
1299 /* A simple key is allowed at the beginning of the stream. */
1301 parser->simple_key_allowed = 1;
1303 /* We have started. */
1305 parser->stream_start_produced = 1;
1307 /* Create the STREAM-START token and append it to the queue. */
1309 STREAM_START_TOKEN_INIT(token, parser->encoding,
1310 parser->mark, parser->mark);
1312 if (!ENQUEUE(parser, parser->tokens, token))
1319 * Produce the STREAM-END token and shut down the scanner.
1323 yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1327 /* Reset the indentation level. */
1329 if (!yaml_parser_unroll_indent(parser, -1))
1332 /* Reset simple keys. */
1334 if (!yaml_parser_remove_simple_key(parser))
1337 parser->simple_key_allowed = 0;
1339 /* Create the STREAM-END token and append it to the queue. */
1341 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1343 if (!ENQUEUE(parser, parser->tokens, token))
1350 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1354 yaml_parser_fetch_directive(yaml_parser_t *parser)
1358 /* Reset the indentation level. */
1360 if (!yaml_parser_unroll_indent(parser, -1))
1363 /* Reset simple keys. */
1365 if (!yaml_parser_remove_simple_key(parser))
1368 parser->simple_key_allowed = 0;
1370 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1372 if (!yaml_parser_scan_directive(parser, &token))
1375 /* Append the token to the queue. */
1377 if (!ENQUEUE(parser, parser->tokens, token)) {
1378 yaml_token_delete(&token);
1386 * Produce the DOCUMENT-START or DOCUMENT-END token.
1390 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1391 yaml_token_type_t type)
1393 yaml_mark_t start_mark, end_mark;
1396 /* Reset the indentation level. */
1398 if (!yaml_parser_unroll_indent(parser, -1))
1401 /* Reset simple keys. */
1403 if (!yaml_parser_remove_simple_key(parser))
1406 parser->simple_key_allowed = 0;
1408 /* Consume the token. */
1410 start_mark = parser->mark;
1416 end_mark = parser->mark;
1418 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1420 TOKEN_INIT(token, type, start_mark, end_mark);
1422 /* Append the token to the queue. */
1424 if (!ENQUEUE(parser, parser->tokens, token))
1431 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1435 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1436 yaml_token_type_t type)
1438 yaml_mark_t start_mark, end_mark;
1441 /* The indicators '[' and '{' may start a simple key. */
1443 if (!yaml_parser_save_simple_key(parser))
1446 /* Increase the flow level. */
1448 if (!yaml_parser_increase_flow_level(parser))
1451 /* A simple key may follow the indicators '[' and '{'. */
1453 parser->simple_key_allowed = 1;
1455 /* Consume the token. */
1457 start_mark = parser->mark;
1459 end_mark = parser->mark;
1461 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1463 TOKEN_INIT(token, type, start_mark, end_mark);
1465 /* Append the token to the queue. */
1467 if (!ENQUEUE(parser, parser->tokens, token))
1474 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1478 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1479 yaml_token_type_t type)
1481 yaml_mark_t start_mark, end_mark;
1484 /* Reset any potential simple key on the current flow level. */
1486 if (!yaml_parser_remove_simple_key(parser))
1489 /* Decrease the flow level. */
1491 if (!yaml_parser_decrease_flow_level(parser))
1494 /* No simple keys after the indicators ']' and '}'. */
1496 parser->simple_key_allowed = 0;
1498 /* Consume the token. */
1500 start_mark = parser->mark;
1502 end_mark = parser->mark;
1504 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1506 TOKEN_INIT(token, type, start_mark, end_mark);
1508 /* Append the token to the queue. */
1510 if (!ENQUEUE(parser, parser->tokens, token))
1517 * Produce the FLOW-ENTRY token.
1521 yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1523 yaml_mark_t start_mark, end_mark;
1526 /* Reset any potential simple keys on the current flow level. */
1528 if (!yaml_parser_remove_simple_key(parser))
1531 /* Simple keys are allowed after ','. */
1533 parser->simple_key_allowed = 1;
1535 /* Consume the token. */
1537 start_mark = parser->mark;
1539 end_mark = parser->mark;
1541 /* Create the FLOW-ENTRY token and append it to the queue. */
1543 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1545 if (!ENQUEUE(parser, parser->tokens, token))
1552 * Produce the BLOCK-ENTRY token.
1556 yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1558 yaml_mark_t start_mark, end_mark;
1561 /* Check if the scanner is in the block context. */
1563 if (!parser->flow_level)
1565 /* Check if we are allowed to start a new entry. */
1567 if (!parser->simple_key_allowed) {
1568 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1569 "block sequence entries are not allowed in this context");
1572 /* Add the BLOCK-SEQUENCE-START token if needed. */
1574 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1575 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1581 * It is an error for the '-' indicator to occur in the flow context,
1582 * but we let the Parser detect and report about it because the Parser
1583 * is able to point to the context.
1587 /* Reset any potential simple keys on the current flow level. */
1589 if (!yaml_parser_remove_simple_key(parser))
1592 /* Simple keys are allowed after '-'. */
1594 parser->simple_key_allowed = 1;
1596 /* Consume the token. */
1598 start_mark = parser->mark;
1600 end_mark = parser->mark;
1602 /* Create the BLOCK-ENTRY token and append it to the queue. */
1604 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1606 if (!ENQUEUE(parser, parser->tokens, token))
1613 * Produce the KEY token.
1617 yaml_parser_fetch_key(yaml_parser_t *parser)
1619 yaml_mark_t start_mark, end_mark;
1622 /* In the block context, additional checks are required. */
1624 if (!parser->flow_level)
1626 /* Check if we are allowed to start a new key (not nessesary simple). */
1628 if (!parser->simple_key_allowed) {
1629 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1630 "mapping keys are not allowed in this context");
1633 /* Add the BLOCK-MAPPING-START token if needed. */
1635 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1636 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1640 /* Reset any potential simple keys on the current flow level. */
1642 if (!yaml_parser_remove_simple_key(parser))
1645 /* Simple keys are allowed after '?' in the block context. */
1647 parser->simple_key_allowed = (!parser->flow_level);
1649 /* Consume the token. */
1651 start_mark = parser->mark;
1653 end_mark = parser->mark;
1655 /* Create the KEY token and append it to the queue. */
1657 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1659 if (!ENQUEUE(parser, parser->tokens, token))
1666 * Produce the VALUE token.
1670 yaml_parser_fetch_value(yaml_parser_t *parser)
1672 yaml_mark_t start_mark, end_mark;
1674 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1676 /* Have we found a simple key? */
1678 if (simple_key->possible)
1681 /* Create the KEY token and insert it into the queue. */
1683 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1685 if (!QUEUE_INSERT(parser, parser->tokens,
1686 simple_key->token_number - parser->tokens_parsed, token))
1689 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1691 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1692 simple_key->token_number,
1693 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1696 /* Remove the simple key. */
1698 simple_key->possible = 0;
1700 /* A simple key cannot follow another simple key. */
1702 parser->simple_key_allowed = 0;
1706 /* The ':' indicator follows a complex key. */
1708 /* In the block context, extra checks are required. */
1710 if (!parser->flow_level)
1712 /* Check if we are allowed to start a complex value. */
1714 if (!parser->simple_key_allowed) {
1715 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1716 "mapping values are not allowed in this context");
1719 /* Add the BLOCK-MAPPING-START token if needed. */
1721 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1722 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1726 /* Simple keys after ':' are allowed in the block context. */
1728 parser->simple_key_allowed = (!parser->flow_level);
1731 /* Consume the token. */
1733 start_mark = parser->mark;
1735 end_mark = parser->mark;
1737 /* Create the VALUE token and append it to the queue. */
1739 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1741 if (!ENQUEUE(parser, parser->tokens, token))
1748 * Produce the ALIAS or ANCHOR token.
1752 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1756 /* An anchor or an alias could be a simple key. */
1758 if (!yaml_parser_save_simple_key(parser))
1761 /* A simple key cannot follow an anchor or an alias. */
1763 parser->simple_key_allowed = 0;
1765 /* Create the ALIAS or ANCHOR token and append it to the queue. */
1767 if (!yaml_parser_scan_anchor(parser, &token, type))
1770 if (!ENQUEUE(parser, parser->tokens, token)) {
1771 yaml_token_delete(&token);
1778 * Produce the TAG token.
1782 yaml_parser_fetch_tag(yaml_parser_t *parser)
1786 /* A tag could be a simple key. */
1788 if (!yaml_parser_save_simple_key(parser))
1791 /* A simple key cannot follow a tag. */
1793 parser->simple_key_allowed = 0;
1795 /* Create the TAG token and append it to the queue. */
1797 if (!yaml_parser_scan_tag(parser, &token))
1800 if (!ENQUEUE(parser, parser->tokens, token)) {
1801 yaml_token_delete(&token);
1809 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1813 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1817 /* Remove any potential simple keys. */
1819 if (!yaml_parser_remove_simple_key(parser))
1822 /* A simple key may follow a block scalar. */
1824 parser->simple_key_allowed = 1;
1826 /* Create the SCALAR token and append it to the queue. */
1828 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1831 if (!ENQUEUE(parser, parser->tokens, token)) {
1832 yaml_token_delete(&token);
1840 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1844 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1848 /* A plain scalar could be a simple key. */
1850 if (!yaml_parser_save_simple_key(parser))
1853 /* A simple key cannot follow a flow scalar. */
1855 parser->simple_key_allowed = 0;
1857 /* Create the SCALAR token and append it to the queue. */
1859 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1862 if (!ENQUEUE(parser, parser->tokens, token)) {
1863 yaml_token_delete(&token);
1871 * Produce the SCALAR(...,plain) token.
1875 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1879 /* A plain scalar could be a simple key. */
1881 if (!yaml_parser_save_simple_key(parser))
1884 /* A simple key cannot follow a flow scalar. */
1886 parser->simple_key_allowed = 0;
1888 /* Create the SCALAR token and append it to the queue. */
1890 if (!yaml_parser_scan_plain_scalar(parser, &token))
1893 if (!ENQUEUE(parser, parser->tokens, token)) {
1894 yaml_token_delete(&token);
1902 * Eat whitespaces and comments until the next token is found.
1906 yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1908 /* Until the next token is not found. */
1912 /* Allow the BOM mark to start a line. */
1914 if (!CACHE(parser, 1)) return 0;
1916 if (parser->mark.column == 0 && IS_BOM(parser->buffer))
1924 * - in the flow context;
1925 * - in the block context, but not at the beginning of the line or
1926 * after '-', '?', or ':' (complex value).
1929 if (!CACHE(parser, 1)) return 0;
1931 while (CHECK(parser->buffer,' ') ||
1932 ((parser->flow_level || !parser->simple_key_allowed) &&
1933 CHECK(parser->buffer, '\t'))) {
1935 if (!CACHE(parser, 1)) return 0;
1938 /* Eat a comment until a line break. */
1940 if (CHECK(parser->buffer, '#')) {
1941 while (!IS_BREAKZ(parser->buffer)) {
1943 if (!CACHE(parser, 1)) return 0;
1947 /* If it is a line break, eat it. */
1949 if (IS_BREAK(parser->buffer))
1951 if (!CACHE(parser, 2)) return 0;
1954 /* In the block context, a new line may start a simple key. */
1956 if (!parser->flow_level) {
1957 parser->simple_key_allowed = 1;
1962 /* We have found a token. */
1972 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1975 * %YAML 1.1 # a comment \n
1976 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1977 * %TAG !yaml! tag:yaml.org,2002: \n
1978 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1982 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
1984 yaml_mark_t start_mark, end_mark;
1985 yaml_char_t *name = NULL;
1987 yaml_char_t *handle = NULL, *prefix = NULL;
1991 start_mark = parser->mark;
1995 /* Scan the directive name. */
1997 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2000 /* Is it a YAML directive? */
2002 if (strcmp((char *)name, "YAML") == 0)
2004 /* Scan the VERSION directive value. */
2006 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2010 end_mark = parser->mark;
2012 /* Create a VERSION-DIRECTIVE token. */
2014 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2015 start_mark, end_mark);
2018 /* Is it a TAG directive? */
2020 else if (strcmp((char *)name, "TAG") == 0)
2022 /* Scan the TAG directive value. */
2024 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2028 end_mark = parser->mark;
2030 /* Create a TAG-DIRECTIVE token. */
2032 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2033 start_mark, end_mark);
2036 /* Unknown directive. */
2040 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2041 start_mark, "found uknown directive name");
2045 /* Eat the rest of the line including any comments. */
2047 if (!CACHE(parser, 1)) goto error;
2049 while (IS_BLANK(parser->buffer)) {
2051 if (!CACHE(parser, 1)) goto error;
2054 if (CHECK(parser->buffer, '#')) {
2055 while (!IS_BREAKZ(parser->buffer)) {
2057 if (!CACHE(parser, 1)) goto error;
2061 /* Check if we are at the end of the line. */
2063 if (!IS_BREAKZ(parser->buffer)) {
2064 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2065 start_mark, "did not found expected comment or line break");
2069 /* Eat a line break. */
2071 if (IS_BREAK(parser->buffer)) {
2072 if (!CACHE(parser, 2)) goto error;
2088 * Scan the directive name.
2091 * %YAML 1.1 # a comment \n
2093 * %TAG !yaml! tag:yaml.org,2002: \n
2098 yaml_parser_scan_directive_name(yaml_parser_t *parser,
2099 yaml_mark_t start_mark, yaml_char_t **name)
2101 yaml_string_t string = NULL_STRING;
2103 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2105 /* Consume the directive name. */
2107 if (!CACHE(parser, 1)) goto error;
2109 while (IS_ALPHA(parser->buffer))
2111 if (!READ(parser, string)) goto error;
2112 if (!CACHE(parser, 1)) goto error;
2115 /* Check if the name is empty. */
2117 if (string.start == string.pointer) {
2118 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2119 start_mark, "cannot found expected directive name");
2123 /* Check for an blank character after the name. */
2125 if (!IS_BLANKZ(parser->buffer)) {
2126 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2127 start_mark, "found unexpected non-alphabetical character");
2131 *name = string.start;
2136 STRING_DEL(parser, string);
2141 * Scan the value of VERSION-DIRECTIVE.
2144 * %YAML 1.1 # a comment \n
2149 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2150 yaml_mark_t start_mark, int *major, int *minor)
2152 /* Eat whitespaces. */
2154 if (!CACHE(parser, 1)) return 0;
2156 while (IS_BLANK(parser->buffer)) {
2158 if (!CACHE(parser, 1)) return 0;
2161 /* Consume the major version number. */
2163 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2168 if (!CHECK(parser->buffer, '.')) {
2169 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2170 start_mark, "did not find expected digit or '.' character");
2175 /* Consume the minor version number. */
2177 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2183 #define MAX_NUMBER_LENGTH 9
2186 * Scan the version number of VERSION-DIRECTIVE.
2189 * %YAML 1.1 # a comment \n
2191 * %YAML 1.1 # a comment \n
2196 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2197 yaml_mark_t start_mark, int *number)
2202 /* Repeat while the next character is digit. */
2204 if (!CACHE(parser, 1)) return 0;
2206 while (IS_DIGIT(parser->buffer))
2208 /* Check if the number is too long. */
2210 if (++length > MAX_NUMBER_LENGTH) {
2211 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2212 start_mark, "found extremely long version number");
2215 value = value*10 + AS_DIGIT(parser->buffer);
2219 if (!CACHE(parser, 1)) return 0;
2222 /* Check if the number was present. */
2225 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2226 start_mark, "did not find expected version number");
2235 * Scan the value of a TAG-DIRECTIVE token.
2238 * %TAG !yaml! tag:yaml.org,2002: \n
2239 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2243 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2244 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2246 yaml_char_t *handle_value = NULL;
2247 yaml_char_t *prefix_value = NULL;
2249 /* Eat whitespaces. */
2251 if (!CACHE(parser, 1)) goto error;
2253 while (IS_BLANK(parser->buffer)) {
2255 if (!CACHE(parser, 1)) goto error;
2258 /* Scan a handle. */
2260 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2263 /* Expect a whitespace. */
2265 if (!CACHE(parser, 1)) goto error;
2267 if (!IS_BLANK(parser->buffer)) {
2268 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2269 start_mark, "did not find expected whitespace");
2273 /* Eat whitespaces. */
2275 while (IS_BLANK(parser->buffer)) {
2277 if (!CACHE(parser, 1)) goto error;
2280 /* Scan a prefix. */
2282 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2285 /* Expect a whitespace or line break. */
2287 if (!CACHE(parser, 1)) goto error;
2289 if (!IS_BLANKZ(parser->buffer)) {
2290 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2291 start_mark, "did not find expected whitespace or line break");
2295 *handle = handle_value;
2296 *prefix = prefix_value;
2301 yaml_free(handle_value);
2302 yaml_free(prefix_value);
2307 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2308 yaml_token_type_t type)
2311 yaml_mark_t start_mark, end_mark;
2312 yaml_string_t string = NULL_STRING;
2314 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2316 /* Eat the indicator character. */
2318 start_mark = parser->mark;
2322 /* Consume the value. */
2324 if (!CACHE(parser, 1)) goto error;
2326 while (IS_ALPHA(parser->buffer)) {
2327 if (!READ(parser, string)) goto error;
2328 if (!CACHE(parser, 1)) goto error;
2332 end_mark = parser->mark;
2335 * Check if length of the anchor is greater than 0 and it is followed by
2336 * a whitespace character or one of the indicators:
2338 * '?', ':', ',', ']', '}', '%', '@', '`'.
2341 if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2342 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2343 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2344 || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2345 || CHECK(parser->buffer, '`'))) {
2346 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2347 "while scanning an anchor" : "while scanning an alias", start_mark,
2348 "did not find expected alphabetic or numeric character");
2352 /* Create a token. */
2354 if (type == YAML_ANCHOR_TOKEN) {
2355 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2358 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2364 STRING_DEL(parser, string);
2373 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2375 yaml_char_t *handle = NULL;
2376 yaml_char_t *suffix = NULL;
2377 yaml_mark_t start_mark, end_mark;
2379 start_mark = parser->mark;
2381 /* Check if the tag is in the canonical form. */
2383 if (!CACHE(parser, 2)) goto error;
2385 if (CHECK_AT(parser->buffer, '<', 1))
2387 /* Set the handle to '' */
2389 handle = yaml_malloc(1);
2390 if (!handle) goto error;
2398 /* Consume the tag value. */
2400 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2403 /* Check for '>' and eat it. */
2405 if (!CHECK(parser->buffer, '>')) {
2406 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2407 start_mark, "did not find the expected '>'");
2415 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2417 /* First, try to scan a handle. */
2419 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2422 /* Check if it is, indeed, handle. */
2424 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2426 /* Scan the suffix now. */
2428 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2433 /* It wasn't a handle after all. Scan the rest of the tag. */
2435 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2438 /* Set the handle to '!'. */
2441 handle = yaml_malloc(2);
2442 if (!handle) goto error;
2447 * A special case: the '!' tag. Set the handle to '' and the
2451 if (suffix[0] == '\0') {
2452 yaml_char_t *tmp = handle;
2459 /* Check the character which ends the tag. */
2461 if (!CACHE(parser, 1)) goto error;
2463 if (!IS_BLANKZ(parser->buffer)) {
2464 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2465 start_mark, "did not found expected whitespace or line break");
2469 end_mark = parser->mark;
2471 /* Create a token. */
2473 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2484 * Scan a tag handle.
2488 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2489 yaml_mark_t start_mark, yaml_char_t **handle)
2491 yaml_string_t string = NULL_STRING;
2493 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2495 /* Check the initial '!' character. */
2497 if (!CACHE(parser, 1)) goto error;
2499 if (!CHECK(parser->buffer, '!')) {
2500 yaml_parser_set_scanner_error(parser, directive ?
2501 "while scanning a tag directive" : "while scanning a tag",
2502 start_mark, "did not find expected '!'");
2506 /* Copy the '!' character. */
2508 if (!READ(parser, string)) goto error;
2510 /* Copy all subsequent alphabetical and numerical characters. */
2512 if (!CACHE(parser, 1)) goto error;
2514 while (IS_ALPHA(parser->buffer))
2516 if (!READ(parser, string)) goto error;
2517 if (!CACHE(parser, 1)) goto error;
2520 /* Check if the trailing character is '!' and copy it. */
2522 if (CHECK(parser->buffer, '!'))
2524 if (!READ(parser, string)) goto error;
2529 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2530 * directive, it's an error. If it's a tag token, it must be a part of
2534 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2535 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2536 start_mark, "did not find expected '!'");
2541 *handle = string.start;
2546 STRING_DEL(parser, string);
2555 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2556 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2558 size_t length = head ? strlen((char *)head) : 0;
2559 yaml_string_t string = NULL_STRING;
2561 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2563 /* Resize the string to include the head. */
2565 while (string.end - string.start <= length) {
2566 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2567 parser->error = YAML_MEMORY_ERROR;
2573 * Copy the head if needed.
2575 * Note that we don't copy the leading '!' character.
2579 memcpy(string.start, head+1, length-1);
2580 string.pointer += length-1;
2585 if (!CACHE(parser, 1)) goto error;
2588 * The set of characters that may appear in URI is as follows:
2590 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2591 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2595 while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';')
2596 || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?')
2597 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@')
2598 || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=')
2599 || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$')
2600 || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.')
2601 || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
2602 || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
2603 || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
2604 || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2605 || CHECK(parser->buffer, '%'))
2607 /* Check if it is a URI-escape sequence. */
2609 if (CHECK(parser->buffer, '%')) {
2610 if (!yaml_parser_scan_uri_escapes(parser,
2611 directive, start_mark, &string)) goto error;
2614 if (!READ(parser, string)) goto error;
2618 if (!CACHE(parser, 1)) goto error;
2621 /* Check if the tag is non-empty. */
2624 if (!STRING_EXTEND(parser, string))
2627 yaml_parser_set_scanner_error(parser, directive ?
2628 "while parsing a %TAG directive" : "while parsing a tag",
2629 start_mark, "did not find expected tag URI");
2633 *uri = string.start;
2638 STRING_DEL(parser, string);
2643 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2647 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2648 yaml_mark_t start_mark, yaml_string_t *string)
2652 /* Decode the required number of characters. */
2656 unsigned char octet = 0;
2658 /* Check for a URI-escaped octet. */
2660 if (!CACHE(parser, 3)) return 0;
2662 if (!(CHECK(parser->buffer, '%')
2663 && IS_HEX_AT(parser->buffer, 1)
2664 && IS_HEX_AT(parser->buffer, 2))) {
2665 return yaml_parser_set_scanner_error(parser, directive ?
2666 "while parsing a %TAG directive" : "while parsing a tag",
2667 start_mark, "did not find URI escaped octet");
2670 /* Get the octet. */
2672 octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
2674 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2678 width = (octet & 0x80) == 0x00 ? 1 :
2679 (octet & 0xE0) == 0xC0 ? 2 :
2680 (octet & 0xF0) == 0xE0 ? 3 :
2681 (octet & 0xF8) == 0xF0 ? 4 : 0;
2683 return yaml_parser_set_scanner_error(parser, directive ?
2684 "while parsing a %TAG directive" : "while parsing a tag",
2685 start_mark, "found an incorrect leading UTF-8 octet");
2690 /* Check if the trailing octet is correct. */
2692 if ((octet & 0xC0) != 0x80) {
2693 return yaml_parser_set_scanner_error(parser, directive ?
2694 "while parsing a %TAG directive" : "while parsing a tag",
2695 start_mark, "found an incorrect trailing UTF-8 octet");
2699 /* Copy the octet and move the pointers. */
2701 *(string->pointer++) = octet;
2712 * Scan a block scalar.
2716 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2719 yaml_mark_t start_mark;
2720 yaml_mark_t end_mark;
2721 yaml_string_t string = NULL_STRING;
2722 yaml_string_t leading_break = NULL_STRING;
2723 yaml_string_t trailing_breaks = NULL_STRING;
2727 int leading_blank = 0;
2728 int trailing_blank = 0;
2730 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2731 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2732 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2734 /* Eat the indicator '|' or '>'. */
2736 start_mark = parser->mark;
2740 /* Scan the additional block scalar indicators. */
2742 if (!CACHE(parser, 1)) goto error;
2744 /* Check for a chomping indicator. */
2746 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
2748 /* Set the chomping method and eat the indicator. */
2750 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2754 /* Check for an indentation indicator. */
2756 if (!CACHE(parser, 1)) goto error;
2758 if (IS_DIGIT(parser->buffer))
2760 /* Check that the intendation is greater than 0. */
2762 if (CHECK(parser->buffer, '0')) {
2763 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2764 start_mark, "found an intendation indicator equal to 0");
2768 /* Get the intendation level and eat the indicator. */
2770 increment = AS_DIGIT(parser->buffer);
2776 /* Do the same as above, but in the opposite order. */
2778 else if (IS_DIGIT(parser->buffer))
2780 if (CHECK(parser->buffer, '0')) {
2781 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2782 start_mark, "found an intendation indicator equal to 0");
2786 increment = AS_DIGIT(parser->buffer);
2790 if (!CACHE(parser, 1)) goto error;
2792 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2793 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2799 /* Eat whitespaces and comments to the end of the line. */
2801 if (!CACHE(parser, 1)) goto error;
2803 while (IS_BLANK(parser->buffer)) {
2805 if (!CACHE(parser, 1)) goto error;
2808 if (CHECK(parser->buffer, '#')) {
2809 while (!IS_BREAKZ(parser->buffer)) {
2811 if (!CACHE(parser, 1)) goto error;
2815 /* Check if we are at the end of the line. */
2817 if (!IS_BREAKZ(parser->buffer)) {
2818 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2819 start_mark, "did not found expected comment or line break");
2823 /* Eat a line break. */
2825 if (IS_BREAK(parser->buffer)) {
2826 if (!CACHE(parser, 2)) goto error;
2830 end_mark = parser->mark;
2832 /* Set the intendation level if it was specified. */
2835 indent = parser->indent >= 0 ? parser->indent+increment : increment;
2838 /* Scan the leading line breaks and determine the indentation level if needed. */
2840 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
2841 start_mark, &end_mark)) goto error;
2843 /* Scan the block scalar content. */
2845 if (!CACHE(parser, 1)) goto error;
2847 while (parser->mark.column == indent && !IS_Z(parser->buffer))
2850 * We are at the beginning of a non-empty line.
2853 /* Is it a trailing whitespace? */
2855 trailing_blank = IS_BLANK(parser->buffer);
2857 /* Check if we need to fold the leading line break. */
2859 if (!literal && (*leading_break.start == '\n')
2860 && !leading_blank && !trailing_blank)
2862 /* Do we need to join the lines by space? */
2864 if (*trailing_breaks.start == '\0') {
2865 if (!STRING_EXTEND(parser, string)) goto error;
2866 *(string.pointer ++) = ' ';
2869 CLEAR(parser, leading_break);
2872 if (!JOIN(parser, string, leading_break)) goto error;
2873 CLEAR(parser, leading_break);
2876 /* Append the remaining line breaks. */
2878 if (!JOIN(parser, string, trailing_breaks)) goto error;
2879 CLEAR(parser, trailing_breaks);
2881 /* Is it a leading whitespace? */
2883 leading_blank = IS_BLANK(parser->buffer);
2885 /* Consume the current line. */
2887 while (!IS_BREAKZ(parser->buffer)) {
2888 if (!READ(parser, string)) goto error;
2889 if (!CACHE(parser, 1)) goto error;
2892 /* Consume the line break. */
2894 if (!CACHE(parser, 2)) goto error;
2896 if (!READ_LINE(parser, leading_break)) goto error;
2898 /* Eat the following intendation spaces and line breaks. */
2900 if (!yaml_parser_scan_block_scalar_breaks(parser,
2901 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
2904 /* Chomp the tail. */
2906 if (chomping != -1) {
2907 if (!JOIN(parser, string, leading_break)) goto error;
2909 if (chomping == 1) {
2910 if (!JOIN(parser, string, trailing_breaks)) goto error;
2913 /* Create a token. */
2915 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
2916 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2917 start_mark, end_mark);
2919 STRING_DEL(parser, leading_break);
2920 STRING_DEL(parser, trailing_breaks);
2925 STRING_DEL(parser, string);
2926 STRING_DEL(parser, leading_break);
2927 STRING_DEL(parser, trailing_breaks);
2933 * Scan intendation spaces and line breaks for a block scalar. Determine the
2934 * intendation level if needed.
2938 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2939 int *indent, yaml_string_t *breaks,
2940 yaml_mark_t start_mark, yaml_mark_t *end_mark)
2944 *end_mark = parser->mark;
2946 /* Eat the intendation spaces and line breaks. */
2950 /* Eat the intendation spaces. */
2952 if (!CACHE(parser, 1)) return 0;
2954 while ((!*indent || parser->mark.column < *indent)
2955 && IS_SPACE(parser->buffer)) {
2957 if (!CACHE(parser, 1)) return 0;
2960 if (parser->mark.column > max_indent)
2961 max_indent = parser->mark.column;
2963 /* Check for a tab character messing the intendation. */
2965 if ((!*indent || parser->mark.column < *indent)
2966 && IS_TAB(parser->buffer)) {
2967 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2968 start_mark, "found a tab character where an intendation space is expected");
2971 /* Have we found a non-empty line? */
2973 if (!IS_BREAK(parser->buffer)) break;
2975 /* Consume the line break. */
2977 if (!CACHE(parser, 2)) return 0;
2978 if (!READ_LINE(parser, *breaks)) return 0;
2979 *end_mark = parser->mark;
2982 /* Determine the indentation level if needed. */
2985 *indent = max_indent;
2986 if (*indent < parser->indent + 1)
2987 *indent = parser->indent + 1;
2996 * Scan a quoted scalar.
3000 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3003 yaml_mark_t start_mark;
3004 yaml_mark_t end_mark;
3005 yaml_string_t string = NULL_STRING;
3006 yaml_string_t leading_break = NULL_STRING;
3007 yaml_string_t trailing_breaks = NULL_STRING;
3008 yaml_string_t whitespaces = NULL_STRING;
3011 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3012 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3013 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3014 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3016 /* Eat the left quote. */
3018 start_mark = parser->mark;
3022 /* Consume the content of the quoted scalar. */
3026 /* Check that there are no document indicators at the beginning of the line. */
3028 if (!CACHE(parser, 4)) goto error;
3030 if (parser->mark.column == 0 &&
3031 ((CHECK_AT(parser->buffer, '-', 0) &&
3032 CHECK_AT(parser->buffer, '-', 1) &&
3033 CHECK_AT(parser->buffer, '-', 2)) ||
3034 (CHECK_AT(parser->buffer, '.', 0) &&
3035 CHECK_AT(parser->buffer, '.', 1) &&
3036 CHECK_AT(parser->buffer, '.', 2))) &&
3037 IS_BLANKZ_AT(parser->buffer, 3))
3039 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3040 start_mark, "found unexpected document indicator");
3044 /* Check for EOF. */
3046 if (IS_Z(parser->buffer)) {
3047 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3048 start_mark, "found unexpected end of stream");
3052 /* Consume non-blank characters. */
3054 if (!CACHE(parser, 2)) goto error;
3058 while (!IS_BLANKZ(parser->buffer))
3060 /* Check for an escaped single quote. */
3062 if (single && CHECK_AT(parser->buffer, '\'', 0)
3063 && CHECK_AT(parser->buffer, '\'', 1))
3065 if (!STRING_EXTEND(parser, string)) goto error;
3066 *(string.pointer++) = '\'';
3071 /* Check for the right quote. */
3073 else if (CHECK(parser->buffer, single ? '\'' : '"'))
3078 /* Check for an escaped line break. */
3080 else if (!single && CHECK(parser->buffer, '\\')
3081 && IS_BREAK_AT(parser->buffer, 1))
3083 if (!CACHE(parser, 3)) goto error;
3090 /* Check for an escape sequence. */
3092 else if (!single && CHECK(parser->buffer, '\\'))
3094 int code_length = 0;
3096 if (!STRING_EXTEND(parser, string)) goto error;
3098 /* Check the escape character. */
3100 switch (parser->buffer.pointer[1])
3103 *(string.pointer++) = '\0';
3107 *(string.pointer++) = '\x07';
3111 *(string.pointer++) = '\x08';
3116 *(string.pointer++) = '\x09';
3120 *(string.pointer++) = '\x0A';
3124 *(string.pointer++) = '\x0B';
3128 *(string.pointer++) = '\x0C';
3132 *(string.pointer++) = '\x0D';
3136 *(string.pointer++) = '\x1B';
3140 *(string.pointer++) = '\x20';
3144 *(string.pointer++) = '"';
3148 *(string.pointer++) = '\'';
3152 *(string.pointer++) = '\\';
3155 case 'N': /* NEL (#x85) */
3156 *(string.pointer++) = '\xC2';
3157 *(string.pointer++) = '\x85';
3160 case '_': /* #xA0 */
3161 *(string.pointer++) = '\xC2';
3162 *(string.pointer++) = '\xA0';
3165 case 'L': /* LS (#x2028) */
3166 *(string.pointer++) = '\xE2';
3167 *(string.pointer++) = '\x80';
3168 *(string.pointer++) = '\xA8';
3171 case 'P': /* PS (#x2029) */
3172 *(string.pointer++) = '\xE2';
3173 *(string.pointer++) = '\x80';
3174 *(string.pointer++) = '\xA9';
3190 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3191 start_mark, "found unknown escape character");
3198 /* Consume an arbitrary escape code. */
3202 unsigned int value = 0;
3205 /* Scan the character value. */
3207 if (!CACHE(parser, code_length)) goto error;
3209 for (k = 0; k < code_length; k ++) {
3210 if (!IS_HEX_AT(parser->buffer, k)) {
3211 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3212 start_mark, "did not find expected hexdecimal number");
3215 value = (value << 4) + AS_HEX_AT(parser->buffer, k);
3218 /* Check the value and write the character. */
3220 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3221 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3222 start_mark, "found invalid Unicode character escape code");
3226 if (value <= 0x7F) {
3227 *(string.pointer++) = value;
3229 else if (value <= 0x7FF) {
3230 *(string.pointer++) = 0xC0 + (value >> 6);
3231 *(string.pointer++) = 0x80 + (value & 0x3F);
3233 else if (value <= 0xFFFF) {
3234 *(string.pointer++) = 0xE0 + (value >> 12);
3235 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3236 *(string.pointer++) = 0x80 + (value & 0x3F);
3239 *(string.pointer++) = 0xF0 + (value >> 18);
3240 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3241 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3242 *(string.pointer++) = 0x80 + (value & 0x3F);
3245 /* Advance the pointer. */
3247 for (k = 0; k < code_length; k ++) {
3255 /* It is a non-escaped non-blank character. */
3257 if (!READ(parser, string)) goto error;
3260 if (!CACHE(parser, 2)) goto error;
3263 /* Check if we are at the end of the scalar. */
3265 if (CHECK(parser->buffer, single ? '\'' : '"'))
3268 /* Consume blank characters. */
3270 if (!CACHE(parser, 1)) goto error;
3272 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3274 if (IS_BLANK(parser->buffer))
3276 /* Consume a space or a tab character. */
3278 if (!leading_blanks) {
3279 if (!READ(parser, whitespaces)) goto error;
3287 if (!CACHE(parser, 2)) goto error;
3289 /* Check if it is a first line break. */
3291 if (!leading_blanks)
3293 CLEAR(parser, whitespaces);
3294 if (!READ_LINE(parser, leading_break)) goto error;
3299 if (!READ_LINE(parser, trailing_breaks)) goto error;
3302 if (!CACHE(parser, 1)) goto error;
3305 /* Join the whitespaces or fold line breaks. */
3309 /* Do we need to fold line breaks? */
3311 if (leading_break.start[0] == '\n') {
3312 if (trailing_breaks.start[0] == '\0') {
3313 if (!STRING_EXTEND(parser, string)) goto error;
3314 *(string.pointer++) = ' ';
3317 if (!JOIN(parser, string, trailing_breaks)) goto error;
3318 CLEAR(parser, trailing_breaks);
3320 CLEAR(parser, leading_break);
3323 if (!JOIN(parser, string, leading_break)) goto error;
3324 if (!JOIN(parser, string, trailing_breaks)) goto error;
3325 CLEAR(parser, leading_break);
3326 CLEAR(parser, trailing_breaks);
3331 if (!JOIN(parser, string, whitespaces)) goto error;
3332 CLEAR(parser, whitespaces);
3336 /* Eat the right quote. */
3340 end_mark = parser->mark;
3342 /* Create a token. */
3344 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3345 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3346 start_mark, end_mark);
3348 STRING_DEL(parser, leading_break);
3349 STRING_DEL(parser, trailing_breaks);
3350 STRING_DEL(parser, whitespaces);
3355 STRING_DEL(parser, string);
3356 STRING_DEL(parser, leading_break);
3357 STRING_DEL(parser, trailing_breaks);
3358 STRING_DEL(parser, whitespaces);
3364 * Scan a plain scalar.
3368 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3370 yaml_mark_t start_mark;
3371 yaml_mark_t end_mark;
3372 yaml_string_t string = NULL_STRING;
3373 yaml_string_t leading_break = NULL_STRING;
3374 yaml_string_t trailing_breaks = NULL_STRING;
3375 yaml_string_t whitespaces = NULL_STRING;
3376 int leading_blanks = 0;
3377 int indent = parser->indent+1;
3379 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3380 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3381 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3382 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3384 start_mark = end_mark = parser->mark;
3386 /* Consume the content of the plain scalar. */
3390 /* Check for a document indicator. */
3392 if (!CACHE(parser, 4)) goto error;
3394 if (parser->mark.column == 0 &&
3395 ((CHECK_AT(parser->buffer, '-', 0) &&
3396 CHECK_AT(parser->buffer, '-', 1) &&
3397 CHECK_AT(parser->buffer, '-', 2)) ||
3398 (CHECK_AT(parser->buffer, '.', 0) &&
3399 CHECK_AT(parser->buffer, '.', 1) &&
3400 CHECK_AT(parser->buffer, '.', 2))) &&
3401 IS_BLANKZ_AT(parser->buffer, 3)) break;
3403 /* Check for a comment. */
3405 if (CHECK(parser->buffer, '#'))
3408 /* Consume non-blank characters. */
3410 while (!IS_BLANKZ(parser->buffer))
3412 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
3414 if (parser->flow_level
3415 && CHECK(parser->buffer, ':')
3416 && !IS_BLANKZ_AT(parser->buffer, 1)) {
3417 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3418 start_mark, "found unexpected ':'");
3422 /* Check for indicators that may end a plain scalar. */
3424 if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3425 || (parser->flow_level &&
3426 (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':')
3427 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
3428 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3429 || CHECK(parser->buffer, '}'))))
3432 /* Check if we need to join whitespaces and breaks. */
3434 if (leading_blanks || whitespaces.start != whitespaces.pointer)
3438 /* Do we need to fold line breaks? */
3440 if (leading_break.start[0] == '\n') {
3441 if (trailing_breaks.start[0] == '\0') {
3442 if (!STRING_EXTEND(parser, string)) goto error;
3443 *(string.pointer++) = ' ';
3446 if (!JOIN(parser, string, trailing_breaks)) goto error;
3447 CLEAR(parser, trailing_breaks);
3449 CLEAR(parser, leading_break);
3452 if (!JOIN(parser, string, leading_break)) goto error;
3453 if (!JOIN(parser, string, trailing_breaks)) goto error;
3454 CLEAR(parser, leading_break);
3455 CLEAR(parser, trailing_breaks);
3462 if (!JOIN(parser, string, whitespaces)) goto error;
3463 CLEAR(parser, whitespaces);
3467 /* Copy the character. */
3469 if (!READ(parser, string)) goto error;
3471 end_mark = parser->mark;
3473 if (!CACHE(parser, 2)) goto error;
3476 /* Is it the end? */
3478 if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
3481 /* Consume blank characters. */
3483 if (!CACHE(parser, 1)) goto error;
3485 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3487 if (IS_BLANK(parser->buffer))
3489 /* Check for tab character that abuse intendation. */
3491 if (leading_blanks && parser->mark.column < indent
3492 && IS_TAB(parser->buffer)) {
3493 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3494 start_mark, "found a tab character that violate intendation");
3498 /* Consume a space or a tab character. */
3500 if (!leading_blanks) {
3501 if (!READ(parser, whitespaces)) goto error;
3509 if (!CACHE(parser, 2)) goto error;
3511 /* Check if it is a first line break. */
3513 if (!leading_blanks)
3515 CLEAR(parser, whitespaces);
3516 if (!READ_LINE(parser, leading_break)) goto error;
3521 if (!READ_LINE(parser, trailing_breaks)) goto error;
3524 if (!CACHE(parser, 1)) goto error;
3527 /* Check intendation level. */
3529 if (!parser->flow_level && parser->mark.column < indent)
3533 /* Create a token. */
3535 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3536 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3538 /* Note that we change the 'simple_key_allowed' flag. */
3540 if (leading_blanks) {
3541 parser->simple_key_allowed = 1;
3544 STRING_DEL(parser, leading_break);
3545 STRING_DEL(parser, trailing_breaks);
3546 STRING_DEL(parser, whitespaces);
3551 STRING_DEL(parser, string);
3552 STRING_DEL(parser, leading_break);
3553 STRING_DEL(parser, trailing_breaks);
3554 STRING_DEL(parser, whitespaces);