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 == (int)parser->mark.column);
1109 * A simple key is required only when it is the first token in the current
1110 * line. Therefore it is always allowed. But we add a check anyway.
1113 assert(parser->simple_key_allowed || !required); /* Impossible. */
1116 * If the current position may start a simple key, save it.
1119 if (parser->simple_key_allowed)
1121 yaml_simple_key_t simple_key = { 1, required,
1122 parser->tokens_parsed + parser->tokens.tail - parser->tokens.head,
1124 simple_key.mark = parser->mark;
1126 if (!yaml_parser_remove_simple_key(parser)) return 0;
1128 *(parser->simple_keys.top-1) = simple_key;
1135 * Remove a potential simple key at the current flow level.
1139 yaml_parser_remove_simple_key(yaml_parser_t *parser)
1141 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1143 if (simple_key->possible)
1145 /* If the key is required, it is an error. */
1147 if (simple_key->required) {
1148 return yaml_parser_set_scanner_error(parser,
1149 "while scanning a simple key", simple_key->mark,
1150 "could not found expected ':'");
1154 /* Remove the key from the stack. */
1156 simple_key->possible = 0;
1162 * Increase the flow level and resize the simple key list if needed.
1166 yaml_parser_increase_flow_level(yaml_parser_t *parser)
1168 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1170 /* Reset the simple key on the next level. */
1172 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1175 /* Increase the flow level. */
1177 parser->flow_level++;
1183 * Decrease the flow level.
1187 yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1189 if (parser->flow_level) {
1190 parser->flow_level --;
1191 POP(parser, parser->simple_keys);
1198 * Push the current indentation level to the stack and set the new level
1199 * the current column is greater than the indentation level. In this case,
1200 * append or insert the specified token into the token queue.
1205 yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1206 int number, yaml_token_type_t type, yaml_mark_t mark)
1210 /* In the flow context, do nothing. */
1212 if (parser->flow_level)
1215 if (parser->indent < column)
1218 * Push the current indentation level to the stack and set the new
1219 * indentation level.
1222 if (!PUSH(parser, parser->indents, parser->indent))
1225 parser->indent = column;
1227 /* Create a token and insert it into the queue. */
1229 TOKEN_INIT(token, type, mark, mark);
1232 if (!ENQUEUE(parser, parser->tokens, token))
1236 if (!QUEUE_INSERT(parser,
1237 parser->tokens, number - parser->tokens_parsed, token))
1246 * Pop indentation levels from the indents stack until the current level
1247 * becomes less or equal to the column. For each intendation level, append
1248 * the BLOCK-END token.
1253 yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1257 /* In the flow context, do nothing. */
1259 if (parser->flow_level)
1262 /* Loop through the intendation levels in the stack. */
1264 while (parser->indent > column)
1266 /* Create a token and append it to the queue. */
1268 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1270 if (!ENQUEUE(parser, parser->tokens, token))
1273 /* Pop the indentation level. */
1275 parser->indent = POP(parser, parser->indents);
1282 * Initialize the scanner and produce the STREAM-START token.
1286 yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1288 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1291 /* Set the initial indentation. */
1293 parser->indent = -1;
1295 /* Initialize the simple key stack. */
1297 if (!PUSH(parser, parser->simple_keys, simple_key))
1300 /* A simple key is allowed at the beginning of the stream. */
1302 parser->simple_key_allowed = 1;
1304 /* We have started. */
1306 parser->stream_start_produced = 1;
1308 /* Create the STREAM-START token and append it to the queue. */
1310 STREAM_START_TOKEN_INIT(token, parser->encoding,
1311 parser->mark, parser->mark);
1313 if (!ENQUEUE(parser, parser->tokens, token))
1320 * Produce the STREAM-END token and shut down the scanner.
1324 yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1328 /* Force new line. */
1330 if (parser->mark.column != 0) {
1331 parser->mark.column = 0;
1332 parser->mark.line ++;
1335 /* Reset the indentation level. */
1337 if (!yaml_parser_unroll_indent(parser, -1))
1340 /* Reset simple keys. */
1342 if (!yaml_parser_remove_simple_key(parser))
1345 parser->simple_key_allowed = 0;
1347 /* Create the STREAM-END token and append it to the queue. */
1349 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1351 if (!ENQUEUE(parser, parser->tokens, token))
1358 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1362 yaml_parser_fetch_directive(yaml_parser_t *parser)
1366 /* Reset the indentation level. */
1368 if (!yaml_parser_unroll_indent(parser, -1))
1371 /* Reset simple keys. */
1373 if (!yaml_parser_remove_simple_key(parser))
1376 parser->simple_key_allowed = 0;
1378 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1380 if (!yaml_parser_scan_directive(parser, &token))
1383 /* Append the token to the queue. */
1385 if (!ENQUEUE(parser, parser->tokens, token)) {
1386 yaml_token_delete(&token);
1394 * Produce the DOCUMENT-START or DOCUMENT-END token.
1398 yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1399 yaml_token_type_t type)
1401 yaml_mark_t start_mark, end_mark;
1404 /* Reset the indentation level. */
1406 if (!yaml_parser_unroll_indent(parser, -1))
1409 /* Reset simple keys. */
1411 if (!yaml_parser_remove_simple_key(parser))
1414 parser->simple_key_allowed = 0;
1416 /* Consume the token. */
1418 start_mark = parser->mark;
1424 end_mark = parser->mark;
1426 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1428 TOKEN_INIT(token, type, start_mark, end_mark);
1430 /* Append the token to the queue. */
1432 if (!ENQUEUE(parser, parser->tokens, token))
1439 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1443 yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1444 yaml_token_type_t type)
1446 yaml_mark_t start_mark, end_mark;
1449 /* The indicators '[' and '{' may start a simple key. */
1451 if (!yaml_parser_save_simple_key(parser))
1454 /* Increase the flow level. */
1456 if (!yaml_parser_increase_flow_level(parser))
1459 /* A simple key may follow the indicators '[' and '{'. */
1461 parser->simple_key_allowed = 1;
1463 /* Consume the token. */
1465 start_mark = parser->mark;
1467 end_mark = parser->mark;
1469 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1471 TOKEN_INIT(token, type, start_mark, end_mark);
1473 /* Append the token to the queue. */
1475 if (!ENQUEUE(parser, parser->tokens, token))
1482 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1486 yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1487 yaml_token_type_t type)
1489 yaml_mark_t start_mark, end_mark;
1492 /* Reset any potential simple key on the current flow level. */
1494 if (!yaml_parser_remove_simple_key(parser))
1497 /* Decrease the flow level. */
1499 if (!yaml_parser_decrease_flow_level(parser))
1502 /* No simple keys after the indicators ']' and '}'. */
1504 parser->simple_key_allowed = 0;
1506 /* Consume the token. */
1508 start_mark = parser->mark;
1510 end_mark = parser->mark;
1512 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1514 TOKEN_INIT(token, type, start_mark, end_mark);
1516 /* Append the token to the queue. */
1518 if (!ENQUEUE(parser, parser->tokens, token))
1525 * Produce the FLOW-ENTRY token.
1529 yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1531 yaml_mark_t start_mark, end_mark;
1534 /* Reset any potential simple keys on the current flow level. */
1536 if (!yaml_parser_remove_simple_key(parser))
1539 /* Simple keys are allowed after ','. */
1541 parser->simple_key_allowed = 1;
1543 /* Consume the token. */
1545 start_mark = parser->mark;
1547 end_mark = parser->mark;
1549 /* Create the FLOW-ENTRY token and append it to the queue. */
1551 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1553 if (!ENQUEUE(parser, parser->tokens, token))
1560 * Produce the BLOCK-ENTRY token.
1564 yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1566 yaml_mark_t start_mark, end_mark;
1569 /* Check if the scanner is in the block context. */
1571 if (!parser->flow_level)
1573 /* Check if we are allowed to start a new entry. */
1575 if (!parser->simple_key_allowed) {
1576 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1577 "block sequence entries are not allowed in this context");
1580 /* Add the BLOCK-SEQUENCE-START token if needed. */
1582 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1583 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1589 * It is an error for the '-' indicator to occur in the flow context,
1590 * but we let the Parser detect and report about it because the Parser
1591 * is able to point to the context.
1595 /* Reset any potential simple keys on the current flow level. */
1597 if (!yaml_parser_remove_simple_key(parser))
1600 /* Simple keys are allowed after '-'. */
1602 parser->simple_key_allowed = 1;
1604 /* Consume the token. */
1606 start_mark = parser->mark;
1608 end_mark = parser->mark;
1610 /* Create the BLOCK-ENTRY token and append it to the queue. */
1612 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1614 if (!ENQUEUE(parser, parser->tokens, token))
1621 * Produce the KEY token.
1625 yaml_parser_fetch_key(yaml_parser_t *parser)
1627 yaml_mark_t start_mark, end_mark;
1630 /* In the block context, additional checks are required. */
1632 if (!parser->flow_level)
1634 /* Check if we are allowed to start a new key (not nessesary simple). */
1636 if (!parser->simple_key_allowed) {
1637 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1638 "mapping keys are not allowed in this context");
1641 /* Add the BLOCK-MAPPING-START token if needed. */
1643 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1644 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1648 /* Reset any potential simple keys on the current flow level. */
1650 if (!yaml_parser_remove_simple_key(parser))
1653 /* Simple keys are allowed after '?' in the block context. */
1655 parser->simple_key_allowed = (!parser->flow_level);
1657 /* Consume the token. */
1659 start_mark = parser->mark;
1661 end_mark = parser->mark;
1663 /* Create the KEY token and append it to the queue. */
1665 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1667 if (!ENQUEUE(parser, parser->tokens, token))
1674 * Produce the VALUE token.
1678 yaml_parser_fetch_value(yaml_parser_t *parser)
1680 yaml_mark_t start_mark, end_mark;
1682 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1684 /* Have we found a simple key? */
1686 if (simple_key->possible)
1689 /* Create the KEY token and insert it into the queue. */
1691 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1693 if (!QUEUE_INSERT(parser, parser->tokens,
1694 simple_key->token_number - parser->tokens_parsed, token))
1697 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1699 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1700 simple_key->token_number,
1701 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1704 /* Remove the simple key. */
1706 simple_key->possible = 0;
1708 /* A simple key cannot follow another simple key. */
1710 parser->simple_key_allowed = 0;
1714 /* The ':' indicator follows a complex key. */
1716 /* In the block context, extra checks are required. */
1718 if (!parser->flow_level)
1720 /* Check if we are allowed to start a complex value. */
1722 if (!parser->simple_key_allowed) {
1723 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1724 "mapping values are not allowed in this context");
1727 /* Add the BLOCK-MAPPING-START token if needed. */
1729 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1730 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1734 /* Simple keys after ':' are allowed in the block context. */
1736 parser->simple_key_allowed = (!parser->flow_level);
1739 /* Consume the token. */
1741 start_mark = parser->mark;
1743 end_mark = parser->mark;
1745 /* Create the VALUE token and append it to the queue. */
1747 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1749 if (!ENQUEUE(parser, parser->tokens, token))
1756 * Produce the ALIAS or ANCHOR token.
1760 yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1764 /* An anchor or an alias could be a simple key. */
1766 if (!yaml_parser_save_simple_key(parser))
1769 /* A simple key cannot follow an anchor or an alias. */
1771 parser->simple_key_allowed = 0;
1773 /* Create the ALIAS or ANCHOR token and append it to the queue. */
1775 if (!yaml_parser_scan_anchor(parser, &token, type))
1778 if (!ENQUEUE(parser, parser->tokens, token)) {
1779 yaml_token_delete(&token);
1786 * Produce the TAG token.
1790 yaml_parser_fetch_tag(yaml_parser_t *parser)
1794 /* A tag could be a simple key. */
1796 if (!yaml_parser_save_simple_key(parser))
1799 /* A simple key cannot follow a tag. */
1801 parser->simple_key_allowed = 0;
1803 /* Create the TAG token and append it to the queue. */
1805 if (!yaml_parser_scan_tag(parser, &token))
1808 if (!ENQUEUE(parser, parser->tokens, token)) {
1809 yaml_token_delete(&token);
1817 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1821 yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1825 /* Remove any potential simple keys. */
1827 if (!yaml_parser_remove_simple_key(parser))
1830 /* A simple key may follow a block scalar. */
1832 parser->simple_key_allowed = 1;
1834 /* Create the SCALAR token and append it to the queue. */
1836 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1839 if (!ENQUEUE(parser, parser->tokens, token)) {
1840 yaml_token_delete(&token);
1848 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1852 yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1856 /* A plain scalar could be a simple key. */
1858 if (!yaml_parser_save_simple_key(parser))
1861 /* A simple key cannot follow a flow scalar. */
1863 parser->simple_key_allowed = 0;
1865 /* Create the SCALAR token and append it to the queue. */
1867 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1870 if (!ENQUEUE(parser, parser->tokens, token)) {
1871 yaml_token_delete(&token);
1879 * Produce the SCALAR(...,plain) token.
1883 yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1887 /* A plain scalar could be a simple key. */
1889 if (!yaml_parser_save_simple_key(parser))
1892 /* A simple key cannot follow a flow scalar. */
1894 parser->simple_key_allowed = 0;
1896 /* Create the SCALAR token and append it to the queue. */
1898 if (!yaml_parser_scan_plain_scalar(parser, &token))
1901 if (!ENQUEUE(parser, parser->tokens, token)) {
1902 yaml_token_delete(&token);
1910 * Eat whitespaces and comments until the next token is found.
1914 yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1916 /* Until the next token is not found. */
1920 /* Allow the BOM mark to start a line. */
1922 if (!CACHE(parser, 1)) return 0;
1924 if (parser->mark.column == 0 && IS_BOM(parser->buffer))
1932 * - in the flow context;
1933 * - in the block context, but not at the beginning of the line or
1934 * after '-', '?', or ':' (complex value).
1937 if (!CACHE(parser, 1)) return 0;
1939 while (CHECK(parser->buffer,' ') ||
1940 ((parser->flow_level || !parser->simple_key_allowed) &&
1941 CHECK(parser->buffer, '\t'))) {
1943 if (!CACHE(parser, 1)) return 0;
1946 /* Eat a comment until a line break. */
1948 if (CHECK(parser->buffer, '#')) {
1949 while (!IS_BREAKZ(parser->buffer)) {
1951 if (!CACHE(parser, 1)) return 0;
1955 /* If it is a line break, eat it. */
1957 if (IS_BREAK(parser->buffer))
1959 if (!CACHE(parser, 2)) return 0;
1962 /* In the block context, a new line may start a simple key. */
1964 if (!parser->flow_level) {
1965 parser->simple_key_allowed = 1;
1970 /* We have found a token. */
1980 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1983 * %YAML 1.1 # a comment \n
1984 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1985 * %TAG !yaml! tag:yaml.org,2002: \n
1986 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1990 yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
1992 yaml_mark_t start_mark, end_mark;
1993 yaml_char_t *name = NULL;
1995 yaml_char_t *handle = NULL, *prefix = NULL;
1999 start_mark = parser->mark;
2003 /* Scan the directive name. */
2005 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2008 /* Is it a YAML directive? */
2010 if (strcmp((char *)name, "YAML") == 0)
2012 /* Scan the VERSION directive value. */
2014 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2018 end_mark = parser->mark;
2020 /* Create a VERSION-DIRECTIVE token. */
2022 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2023 start_mark, end_mark);
2026 /* Is it a TAG directive? */
2028 else if (strcmp((char *)name, "TAG") == 0)
2030 /* Scan the TAG directive value. */
2032 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2036 end_mark = parser->mark;
2038 /* Create a TAG-DIRECTIVE token. */
2040 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2041 start_mark, end_mark);
2044 /* Unknown directive. */
2048 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2049 start_mark, "found uknown directive name");
2053 /* Eat the rest of the line including any comments. */
2055 if (!CACHE(parser, 1)) goto error;
2057 while (IS_BLANK(parser->buffer)) {
2059 if (!CACHE(parser, 1)) goto error;
2062 if (CHECK(parser->buffer, '#')) {
2063 while (!IS_BREAKZ(parser->buffer)) {
2065 if (!CACHE(parser, 1)) goto error;
2069 /* Check if we are at the end of the line. */
2071 if (!IS_BREAKZ(parser->buffer)) {
2072 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2073 start_mark, "did not found expected comment or line break");
2077 /* Eat a line break. */
2079 if (IS_BREAK(parser->buffer)) {
2080 if (!CACHE(parser, 2)) goto error;
2096 * Scan the directive name.
2099 * %YAML 1.1 # a comment \n
2101 * %TAG !yaml! tag:yaml.org,2002: \n
2106 yaml_parser_scan_directive_name(yaml_parser_t *parser,
2107 yaml_mark_t start_mark, yaml_char_t **name)
2109 yaml_string_t string = NULL_STRING;
2111 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2113 /* Consume the directive name. */
2115 if (!CACHE(parser, 1)) goto error;
2117 while (IS_ALPHA(parser->buffer))
2119 if (!READ(parser, string)) goto error;
2120 if (!CACHE(parser, 1)) goto error;
2123 /* Check if the name is empty. */
2125 if (string.start == string.pointer) {
2126 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2127 start_mark, "cannot found expected directive name");
2131 /* Check for an blank character after the name. */
2133 if (!IS_BLANKZ(parser->buffer)) {
2134 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2135 start_mark, "found unexpected non-alphabetical character");
2139 *name = string.start;
2144 STRING_DEL(parser, string);
2149 * Scan the value of VERSION-DIRECTIVE.
2152 * %YAML 1.1 # a comment \n
2157 yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2158 yaml_mark_t start_mark, int *major, int *minor)
2160 /* Eat whitespaces. */
2162 if (!CACHE(parser, 1)) return 0;
2164 while (IS_BLANK(parser->buffer)) {
2166 if (!CACHE(parser, 1)) return 0;
2169 /* Consume the major version number. */
2171 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2176 if (!CHECK(parser->buffer, '.')) {
2177 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2178 start_mark, "did not find expected digit or '.' character");
2183 /* Consume the minor version number. */
2185 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2191 #define MAX_NUMBER_LENGTH 9
2194 * Scan the version number of VERSION-DIRECTIVE.
2197 * %YAML 1.1 # a comment \n
2199 * %YAML 1.1 # a comment \n
2204 yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2205 yaml_mark_t start_mark, int *number)
2210 /* Repeat while the next character is digit. */
2212 if (!CACHE(parser, 1)) return 0;
2214 while (IS_DIGIT(parser->buffer))
2216 /* Check if the number is too long. */
2218 if (++length > MAX_NUMBER_LENGTH) {
2219 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2220 start_mark, "found extremely long version number");
2223 value = value*10 + AS_DIGIT(parser->buffer);
2227 if (!CACHE(parser, 1)) return 0;
2230 /* Check if the number was present. */
2233 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2234 start_mark, "did not find expected version number");
2243 * Scan the value of a TAG-DIRECTIVE token.
2246 * %TAG !yaml! tag:yaml.org,2002: \n
2247 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2251 yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2252 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2254 yaml_char_t *handle_value = NULL;
2255 yaml_char_t *prefix_value = NULL;
2257 /* Eat whitespaces. */
2259 if (!CACHE(parser, 1)) goto error;
2261 while (IS_BLANK(parser->buffer)) {
2263 if (!CACHE(parser, 1)) goto error;
2266 /* Scan a handle. */
2268 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2271 /* Expect a whitespace. */
2273 if (!CACHE(parser, 1)) goto error;
2275 if (!IS_BLANK(parser->buffer)) {
2276 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2277 start_mark, "did not find expected whitespace");
2281 /* Eat whitespaces. */
2283 while (IS_BLANK(parser->buffer)) {
2285 if (!CACHE(parser, 1)) goto error;
2288 /* Scan a prefix. */
2290 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2293 /* Expect a whitespace or line break. */
2295 if (!CACHE(parser, 1)) goto error;
2297 if (!IS_BLANKZ(parser->buffer)) {
2298 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2299 start_mark, "did not find expected whitespace or line break");
2303 *handle = handle_value;
2304 *prefix = prefix_value;
2309 yaml_free(handle_value);
2310 yaml_free(prefix_value);
2315 yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2316 yaml_token_type_t type)
2319 yaml_mark_t start_mark, end_mark;
2320 yaml_string_t string = NULL_STRING;
2322 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2324 /* Eat the indicator character. */
2326 start_mark = parser->mark;
2330 /* Consume the value. */
2332 if (!CACHE(parser, 1)) goto error;
2334 while (IS_ALPHA(parser->buffer)) {
2335 if (!READ(parser, string)) goto error;
2336 if (!CACHE(parser, 1)) goto error;
2340 end_mark = parser->mark;
2343 * Check if length of the anchor is greater than 0 and it is followed by
2344 * a whitespace character or one of the indicators:
2346 * '?', ':', ',', ']', '}', '%', '@', '`'.
2349 if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2350 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2351 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2352 || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2353 || CHECK(parser->buffer, '`'))) {
2354 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2355 "while scanning an anchor" : "while scanning an alias", start_mark,
2356 "did not find expected alphabetic or numeric character");
2360 /* Create a token. */
2362 if (type == YAML_ANCHOR_TOKEN) {
2363 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2366 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2372 STRING_DEL(parser, string);
2381 yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2383 yaml_char_t *handle = NULL;
2384 yaml_char_t *suffix = NULL;
2385 yaml_mark_t start_mark, end_mark;
2387 start_mark = parser->mark;
2389 /* Check if the tag is in the canonical form. */
2391 if (!CACHE(parser, 2)) goto error;
2393 if (CHECK_AT(parser->buffer, '<', 1))
2395 /* Set the handle to '' */
2397 handle = yaml_malloc(1);
2398 if (!handle) goto error;
2406 /* Consume the tag value. */
2408 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2411 /* Check for '>' and eat it. */
2413 if (!CHECK(parser->buffer, '>')) {
2414 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2415 start_mark, "did not find the expected '>'");
2423 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2425 /* First, try to scan a handle. */
2427 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2430 /* Check if it is, indeed, handle. */
2432 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2434 /* Scan the suffix now. */
2436 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2441 /* It wasn't a handle after all. Scan the rest of the tag. */
2443 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2446 /* Set the handle to '!'. */
2449 handle = yaml_malloc(2);
2450 if (!handle) goto error;
2455 * A special case: the '!' tag. Set the handle to '' and the
2459 if (suffix[0] == '\0') {
2460 yaml_char_t *tmp = handle;
2467 /* Check the character which ends the tag. */
2469 if (!CACHE(parser, 1)) goto error;
2471 if (!IS_BLANKZ(parser->buffer)) {
2472 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2473 start_mark, "did not found expected whitespace or line break");
2477 end_mark = parser->mark;
2479 /* Create a token. */
2481 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2492 * Scan a tag handle.
2496 yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2497 yaml_mark_t start_mark, yaml_char_t **handle)
2499 yaml_string_t string = NULL_STRING;
2501 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2503 /* Check the initial '!' character. */
2505 if (!CACHE(parser, 1)) goto error;
2507 if (!CHECK(parser->buffer, '!')) {
2508 yaml_parser_set_scanner_error(parser, directive ?
2509 "while scanning a tag directive" : "while scanning a tag",
2510 start_mark, "did not find expected '!'");
2514 /* Copy the '!' character. */
2516 if (!READ(parser, string)) goto error;
2518 /* Copy all subsequent alphabetical and numerical characters. */
2520 if (!CACHE(parser, 1)) goto error;
2522 while (IS_ALPHA(parser->buffer))
2524 if (!READ(parser, string)) goto error;
2525 if (!CACHE(parser, 1)) goto error;
2528 /* Check if the trailing character is '!' and copy it. */
2530 if (CHECK(parser->buffer, '!'))
2532 if (!READ(parser, string)) goto error;
2537 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2538 * directive, it's an error. If it's a tag token, it must be a part of
2542 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2543 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2544 start_mark, "did not find expected '!'");
2549 *handle = string.start;
2554 STRING_DEL(parser, string);
2563 yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2564 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2566 size_t length = head ? strlen((char *)head) : 0;
2567 yaml_string_t string = NULL_STRING;
2569 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2571 /* Resize the string to include the head. */
2573 while (string.end - string.start <= (int)length) {
2574 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2575 parser->error = YAML_MEMORY_ERROR;
2581 * Copy the head if needed.
2583 * Note that we don't copy the leading '!' character.
2587 memcpy(string.start, head+1, length-1);
2588 string.pointer += length-1;
2593 if (!CACHE(parser, 1)) goto error;
2596 * The set of characters that may appear in URI is as follows:
2598 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2599 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2603 while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';')
2604 || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?')
2605 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@')
2606 || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=')
2607 || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$')
2608 || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.')
2609 || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
2610 || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
2611 || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
2612 || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2613 || CHECK(parser->buffer, '%'))
2615 /* Check if it is a URI-escape sequence. */
2617 if (CHECK(parser->buffer, '%')) {
2618 if (!yaml_parser_scan_uri_escapes(parser,
2619 directive, start_mark, &string)) goto error;
2622 if (!READ(parser, string)) goto error;
2626 if (!CACHE(parser, 1)) goto error;
2629 /* Check if the tag is non-empty. */
2632 if (!STRING_EXTEND(parser, string))
2635 yaml_parser_set_scanner_error(parser, directive ?
2636 "while parsing a %TAG directive" : "while parsing a tag",
2637 start_mark, "did not find expected tag URI");
2641 *uri = string.start;
2646 STRING_DEL(parser, string);
2651 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2655 yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2656 yaml_mark_t start_mark, yaml_string_t *string)
2660 /* Decode the required number of characters. */
2664 unsigned char octet = 0;
2666 /* Check for a URI-escaped octet. */
2668 if (!CACHE(parser, 3)) return 0;
2670 if (!(CHECK(parser->buffer, '%')
2671 && IS_HEX_AT(parser->buffer, 1)
2672 && IS_HEX_AT(parser->buffer, 2))) {
2673 return yaml_parser_set_scanner_error(parser, directive ?
2674 "while parsing a %TAG directive" : "while parsing a tag",
2675 start_mark, "did not find URI escaped octet");
2678 /* Get the octet. */
2680 octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
2682 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2686 width = (octet & 0x80) == 0x00 ? 1 :
2687 (octet & 0xE0) == 0xC0 ? 2 :
2688 (octet & 0xF0) == 0xE0 ? 3 :
2689 (octet & 0xF8) == 0xF0 ? 4 : 0;
2691 return yaml_parser_set_scanner_error(parser, directive ?
2692 "while parsing a %TAG directive" : "while parsing a tag",
2693 start_mark, "found an incorrect leading UTF-8 octet");
2698 /* Check if the trailing octet is correct. */
2700 if ((octet & 0xC0) != 0x80) {
2701 return yaml_parser_set_scanner_error(parser, directive ?
2702 "while parsing a %TAG directive" : "while parsing a tag",
2703 start_mark, "found an incorrect trailing UTF-8 octet");
2707 /* Copy the octet and move the pointers. */
2709 *(string->pointer++) = octet;
2720 * Scan a block scalar.
2724 yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2727 yaml_mark_t start_mark;
2728 yaml_mark_t end_mark;
2729 yaml_string_t string = NULL_STRING;
2730 yaml_string_t leading_break = NULL_STRING;
2731 yaml_string_t trailing_breaks = NULL_STRING;
2735 int leading_blank = 0;
2736 int trailing_blank = 0;
2738 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2739 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2740 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2742 /* Eat the indicator '|' or '>'. */
2744 start_mark = parser->mark;
2748 /* Scan the additional block scalar indicators. */
2750 if (!CACHE(parser, 1)) goto error;
2752 /* Check for a chomping indicator. */
2754 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
2756 /* Set the chomping method and eat the indicator. */
2758 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2762 /* Check for an indentation indicator. */
2764 if (!CACHE(parser, 1)) goto error;
2766 if (IS_DIGIT(parser->buffer))
2768 /* Check that the intendation is greater than 0. */
2770 if (CHECK(parser->buffer, '0')) {
2771 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2772 start_mark, "found an intendation indicator equal to 0");
2776 /* Get the intendation level and eat the indicator. */
2778 increment = AS_DIGIT(parser->buffer);
2784 /* Do the same as above, but in the opposite order. */
2786 else if (IS_DIGIT(parser->buffer))
2788 if (CHECK(parser->buffer, '0')) {
2789 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2790 start_mark, "found an intendation indicator equal to 0");
2794 increment = AS_DIGIT(parser->buffer);
2798 if (!CACHE(parser, 1)) goto error;
2800 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2801 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2807 /* Eat whitespaces and comments to the end of the line. */
2809 if (!CACHE(parser, 1)) goto error;
2811 while (IS_BLANK(parser->buffer)) {
2813 if (!CACHE(parser, 1)) goto error;
2816 if (CHECK(parser->buffer, '#')) {
2817 while (!IS_BREAKZ(parser->buffer)) {
2819 if (!CACHE(parser, 1)) goto error;
2823 /* Check if we are at the end of the line. */
2825 if (!IS_BREAKZ(parser->buffer)) {
2826 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2827 start_mark, "did not found expected comment or line break");
2831 /* Eat a line break. */
2833 if (IS_BREAK(parser->buffer)) {
2834 if (!CACHE(parser, 2)) goto error;
2838 end_mark = parser->mark;
2840 /* Set the intendation level if it was specified. */
2843 indent = parser->indent >= 0 ? parser->indent+increment : increment;
2846 /* Scan the leading line breaks and determine the indentation level if needed. */
2848 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
2849 start_mark, &end_mark)) goto error;
2851 /* Scan the block scalar content. */
2853 if (!CACHE(parser, 1)) goto error;
2855 while ((int)parser->mark.column == indent && !IS_Z(parser->buffer))
2858 * We are at the beginning of a non-empty line.
2861 /* Is it a trailing whitespace? */
2863 trailing_blank = IS_BLANK(parser->buffer);
2865 /* Check if we need to fold the leading line break. */
2867 if (!literal && (*leading_break.start == '\n')
2868 && !leading_blank && !trailing_blank)
2870 /* Do we need to join the lines by space? */
2872 if (*trailing_breaks.start == '\0') {
2873 if (!STRING_EXTEND(parser, string)) goto error;
2874 *(string.pointer ++) = ' ';
2877 CLEAR(parser, leading_break);
2880 if (!JOIN(parser, string, leading_break)) goto error;
2881 CLEAR(parser, leading_break);
2884 /* Append the remaining line breaks. */
2886 if (!JOIN(parser, string, trailing_breaks)) goto error;
2887 CLEAR(parser, trailing_breaks);
2889 /* Is it a leading whitespace? */
2891 leading_blank = IS_BLANK(parser->buffer);
2893 /* Consume the current line. */
2895 while (!IS_BREAKZ(parser->buffer)) {
2896 if (!READ(parser, string)) goto error;
2897 if (!CACHE(parser, 1)) goto error;
2900 /* Consume the line break. */
2902 if (!CACHE(parser, 2)) goto error;
2904 if (!READ_LINE(parser, leading_break)) goto error;
2906 /* Eat the following intendation spaces and line breaks. */
2908 if (!yaml_parser_scan_block_scalar_breaks(parser,
2909 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
2912 /* Chomp the tail. */
2914 if (chomping != -1) {
2915 if (!JOIN(parser, string, leading_break)) goto error;
2917 if (chomping == 1) {
2918 if (!JOIN(parser, string, trailing_breaks)) goto error;
2921 /* Create a token. */
2923 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
2924 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2925 start_mark, end_mark);
2927 STRING_DEL(parser, leading_break);
2928 STRING_DEL(parser, trailing_breaks);
2933 STRING_DEL(parser, string);
2934 STRING_DEL(parser, leading_break);
2935 STRING_DEL(parser, trailing_breaks);
2941 * Scan intendation spaces and line breaks for a block scalar. Determine the
2942 * intendation level if needed.
2946 yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2947 int *indent, yaml_string_t *breaks,
2948 yaml_mark_t start_mark, yaml_mark_t *end_mark)
2952 *end_mark = parser->mark;
2954 /* Eat the intendation spaces and line breaks. */
2958 /* Eat the intendation spaces. */
2960 if (!CACHE(parser, 1)) return 0;
2962 while ((!*indent || (int)parser->mark.column < *indent)
2963 && IS_SPACE(parser->buffer)) {
2965 if (!CACHE(parser, 1)) return 0;
2968 if ((int)parser->mark.column > max_indent)
2969 max_indent = (int)parser->mark.column;
2971 /* Check for a tab character messing the intendation. */
2973 if ((!*indent || (int)parser->mark.column < *indent)
2974 && IS_TAB(parser->buffer)) {
2975 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2976 start_mark, "found a tab character where an intendation space is expected");
2979 /* Have we found a non-empty line? */
2981 if (!IS_BREAK(parser->buffer)) break;
2983 /* Consume the line break. */
2985 if (!CACHE(parser, 2)) return 0;
2986 if (!READ_LINE(parser, *breaks)) return 0;
2987 *end_mark = parser->mark;
2990 /* Determine the indentation level if needed. */
2993 *indent = max_indent;
2994 if (*indent < parser->indent + 1)
2995 *indent = parser->indent + 1;
3004 * Scan a quoted scalar.
3008 yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3011 yaml_mark_t start_mark;
3012 yaml_mark_t end_mark;
3013 yaml_string_t string = NULL_STRING;
3014 yaml_string_t leading_break = NULL_STRING;
3015 yaml_string_t trailing_breaks = NULL_STRING;
3016 yaml_string_t whitespaces = NULL_STRING;
3019 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3020 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3021 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3022 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3024 /* Eat the left quote. */
3026 start_mark = parser->mark;
3030 /* Consume the content of the quoted scalar. */
3034 /* Check that there are no document indicators at the beginning of the line. */
3036 if (!CACHE(parser, 4)) goto error;
3038 if (parser->mark.column == 0 &&
3039 ((CHECK_AT(parser->buffer, '-', 0) &&
3040 CHECK_AT(parser->buffer, '-', 1) &&
3041 CHECK_AT(parser->buffer, '-', 2)) ||
3042 (CHECK_AT(parser->buffer, '.', 0) &&
3043 CHECK_AT(parser->buffer, '.', 1) &&
3044 CHECK_AT(parser->buffer, '.', 2))) &&
3045 IS_BLANKZ_AT(parser->buffer, 3))
3047 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3048 start_mark, "found unexpected document indicator");
3052 /* Check for EOF. */
3054 if (IS_Z(parser->buffer)) {
3055 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3056 start_mark, "found unexpected end of stream");
3060 /* Consume non-blank characters. */
3062 if (!CACHE(parser, 2)) goto error;
3066 while (!IS_BLANKZ(parser->buffer))
3068 /* Check for an escaped single quote. */
3070 if (single && CHECK_AT(parser->buffer, '\'', 0)
3071 && CHECK_AT(parser->buffer, '\'', 1))
3073 if (!STRING_EXTEND(parser, string)) goto error;
3074 *(string.pointer++) = '\'';
3079 /* Check for the right quote. */
3081 else if (CHECK(parser->buffer, single ? '\'' : '"'))
3086 /* Check for an escaped line break. */
3088 else if (!single && CHECK(parser->buffer, '\\')
3089 && IS_BREAK_AT(parser->buffer, 1))
3091 if (!CACHE(parser, 3)) goto error;
3098 /* Check for an escape sequence. */
3100 else if (!single && CHECK(parser->buffer, '\\'))
3102 size_t code_length = 0;
3104 if (!STRING_EXTEND(parser, string)) goto error;
3106 /* Check the escape character. */
3108 switch (parser->buffer.pointer[1])
3111 *(string.pointer++) = '\0';
3115 *(string.pointer++) = '\x07';
3119 *(string.pointer++) = '\x08';
3124 *(string.pointer++) = '\x09';
3128 *(string.pointer++) = '\x0A';
3132 *(string.pointer++) = '\x0B';
3136 *(string.pointer++) = '\x0C';
3140 *(string.pointer++) = '\x0D';
3144 *(string.pointer++) = '\x1B';
3148 *(string.pointer++) = '\x20';
3152 *(string.pointer++) = '"';
3156 *(string.pointer++) = '\'';
3160 *(string.pointer++) = '\\';
3163 case 'N': /* NEL (#x85) */
3164 *(string.pointer++) = '\xC2';
3165 *(string.pointer++) = '\x85';
3168 case '_': /* #xA0 */
3169 *(string.pointer++) = '\xC2';
3170 *(string.pointer++) = '\xA0';
3173 case 'L': /* LS (#x2028) */
3174 *(string.pointer++) = '\xE2';
3175 *(string.pointer++) = '\x80';
3176 *(string.pointer++) = '\xA8';
3179 case 'P': /* PS (#x2029) */
3180 *(string.pointer++) = '\xE2';
3181 *(string.pointer++) = '\x80';
3182 *(string.pointer++) = '\xA9';
3198 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3199 start_mark, "found unknown escape character");
3206 /* Consume an arbitrary escape code. */
3210 unsigned int value = 0;
3213 /* Scan the character value. */
3215 if (!CACHE(parser, code_length)) goto error;
3217 for (k = 0; k < code_length; k ++) {
3218 if (!IS_HEX_AT(parser->buffer, k)) {
3219 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3220 start_mark, "did not find expected hexdecimal number");
3223 value = (value << 4) + AS_HEX_AT(parser->buffer, k);
3226 /* Check the value and write the character. */
3228 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3229 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3230 start_mark, "found invalid Unicode character escape code");
3234 if (value <= 0x7F) {
3235 *(string.pointer++) = value;
3237 else if (value <= 0x7FF) {
3238 *(string.pointer++) = 0xC0 + (value >> 6);
3239 *(string.pointer++) = 0x80 + (value & 0x3F);
3241 else if (value <= 0xFFFF) {
3242 *(string.pointer++) = 0xE0 + (value >> 12);
3243 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3244 *(string.pointer++) = 0x80 + (value & 0x3F);
3247 *(string.pointer++) = 0xF0 + (value >> 18);
3248 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3249 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3250 *(string.pointer++) = 0x80 + (value & 0x3F);
3253 /* Advance the pointer. */
3255 for (k = 0; k < code_length; k ++) {
3263 /* It is a non-escaped non-blank character. */
3265 if (!READ(parser, string)) goto error;
3268 if (!CACHE(parser, 2)) goto error;
3271 /* Check if we are at the end of the scalar. */
3273 if (CHECK(parser->buffer, single ? '\'' : '"'))
3276 /* Consume blank characters. */
3278 if (!CACHE(parser, 1)) goto error;
3280 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3282 if (IS_BLANK(parser->buffer))
3284 /* Consume a space or a tab character. */
3286 if (!leading_blanks) {
3287 if (!READ(parser, whitespaces)) goto error;
3295 if (!CACHE(parser, 2)) goto error;
3297 /* Check if it is a first line break. */
3299 if (!leading_blanks)
3301 CLEAR(parser, whitespaces);
3302 if (!READ_LINE(parser, leading_break)) goto error;
3307 if (!READ_LINE(parser, trailing_breaks)) goto error;
3310 if (!CACHE(parser, 1)) goto error;
3313 /* Join the whitespaces or fold line breaks. */
3317 /* Do we need to fold line breaks? */
3319 if (leading_break.start[0] == '\n') {
3320 if (trailing_breaks.start[0] == '\0') {
3321 if (!STRING_EXTEND(parser, string)) goto error;
3322 *(string.pointer++) = ' ';
3325 if (!JOIN(parser, string, trailing_breaks)) goto error;
3326 CLEAR(parser, trailing_breaks);
3328 CLEAR(parser, leading_break);
3331 if (!JOIN(parser, string, leading_break)) goto error;
3332 if (!JOIN(parser, string, trailing_breaks)) goto error;
3333 CLEAR(parser, leading_break);
3334 CLEAR(parser, trailing_breaks);
3339 if (!JOIN(parser, string, whitespaces)) goto error;
3340 CLEAR(parser, whitespaces);
3344 /* Eat the right quote. */
3348 end_mark = parser->mark;
3350 /* Create a token. */
3352 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3353 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3354 start_mark, end_mark);
3356 STRING_DEL(parser, leading_break);
3357 STRING_DEL(parser, trailing_breaks);
3358 STRING_DEL(parser, whitespaces);
3363 STRING_DEL(parser, string);
3364 STRING_DEL(parser, leading_break);
3365 STRING_DEL(parser, trailing_breaks);
3366 STRING_DEL(parser, whitespaces);
3372 * Scan a plain scalar.
3376 yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3378 yaml_mark_t start_mark;
3379 yaml_mark_t end_mark;
3380 yaml_string_t string = NULL_STRING;
3381 yaml_string_t leading_break = NULL_STRING;
3382 yaml_string_t trailing_breaks = NULL_STRING;
3383 yaml_string_t whitespaces = NULL_STRING;
3384 int leading_blanks = 0;
3385 int indent = parser->indent+1;
3387 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3388 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3389 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3390 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3392 start_mark = end_mark = parser->mark;
3394 /* Consume the content of the plain scalar. */
3398 /* Check for a document indicator. */
3400 if (!CACHE(parser, 4)) goto error;
3402 if (parser->mark.column == 0 &&
3403 ((CHECK_AT(parser->buffer, '-', 0) &&
3404 CHECK_AT(parser->buffer, '-', 1) &&
3405 CHECK_AT(parser->buffer, '-', 2)) ||
3406 (CHECK_AT(parser->buffer, '.', 0) &&
3407 CHECK_AT(parser->buffer, '.', 1) &&
3408 CHECK_AT(parser->buffer, '.', 2))) &&
3409 IS_BLANKZ_AT(parser->buffer, 3)) break;
3411 /* Check for a comment. */
3413 if (CHECK(parser->buffer, '#'))
3416 /* Consume non-blank characters. */
3418 while (!IS_BLANKZ(parser->buffer))
3420 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
3422 if (parser->flow_level
3423 && CHECK(parser->buffer, ':')
3424 && !IS_BLANKZ_AT(parser->buffer, 1)) {
3425 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3426 start_mark, "found unexpected ':'");
3430 /* Check for indicators that may end a plain scalar. */
3432 if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3433 || (parser->flow_level &&
3434 (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':')
3435 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
3436 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3437 || CHECK(parser->buffer, '}'))))
3440 /* Check if we need to join whitespaces and breaks. */
3442 if (leading_blanks || whitespaces.start != whitespaces.pointer)
3446 /* Do we need to fold line breaks? */
3448 if (leading_break.start[0] == '\n') {
3449 if (trailing_breaks.start[0] == '\0') {
3450 if (!STRING_EXTEND(parser, string)) goto error;
3451 *(string.pointer++) = ' ';
3454 if (!JOIN(parser, string, trailing_breaks)) goto error;
3455 CLEAR(parser, trailing_breaks);
3457 CLEAR(parser, leading_break);
3460 if (!JOIN(parser, string, leading_break)) goto error;
3461 if (!JOIN(parser, string, trailing_breaks)) goto error;
3462 CLEAR(parser, leading_break);
3463 CLEAR(parser, trailing_breaks);
3470 if (!JOIN(parser, string, whitespaces)) goto error;
3471 CLEAR(parser, whitespaces);
3475 /* Copy the character. */
3477 if (!READ(parser, string)) goto error;
3479 end_mark = parser->mark;
3481 if (!CACHE(parser, 2)) goto error;
3484 /* Is it the end? */
3486 if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
3489 /* Consume blank characters. */
3491 if (!CACHE(parser, 1)) goto error;
3493 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3495 if (IS_BLANK(parser->buffer))
3497 /* Check for tab character that abuse intendation. */
3499 if (leading_blanks && (int)parser->mark.column < indent
3500 && IS_TAB(parser->buffer)) {
3501 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3502 start_mark, "found a tab character that violate intendation");
3506 /* Consume a space or a tab character. */
3508 if (!leading_blanks) {
3509 if (!READ(parser, whitespaces)) goto error;
3517 if (!CACHE(parser, 2)) goto error;
3519 /* Check if it is a first line break. */
3521 if (!leading_blanks)
3523 CLEAR(parser, whitespaces);
3524 if (!READ_LINE(parser, leading_break)) goto error;
3529 if (!READ_LINE(parser, trailing_breaks)) goto error;
3532 if (!CACHE(parser, 1)) goto error;
3535 /* Check intendation level. */
3537 if (!parser->flow_level && (int)parser->mark.column < indent)
3541 /* Create a token. */
3543 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3544 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3546 /* Note that we change the 'simple_key_allowed' flag. */
3548 if (leading_blanks) {
3549 parser->simple_key_allowed = 1;
3552 STRING_DEL(parser, leading_break);
3553 STRING_DEL(parser, trailing_breaks);
3554 STRING_DEL(parser, whitespaces);
3559 STRING_DEL(parser, string);
3560 STRING_DEL(parser, leading_break);
3561 STRING_DEL(parser, trailing_breaks);
3562 STRING_DEL(parser, whitespaces);