@@ -0,0 +1,3589 @@
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+ * FLOW-MAPPING-START # '{'
42+ * FLOW-MAPPING-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
59+ * STREAM-START token.
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 corresponding sequence of tokens:
75+ * STREAM-START(utf-8)
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:
144+ * TAG(handle,suffix)
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.
176+ * --- a plain scalar
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
206+ * FLOW-MAPPING-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)
236+ * 2. A flow mapping:
239+ * a simple key: a value, # Note that the KEY token is produced.
240+ * ? a complex key: another value,
245+ * STREAM-START(utf-8)
246+ * FLOW-MAPPING-START
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)
325+ * 2. Block mappings:
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 ++, \
497+ parser->unread --, \
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 ++, \
511+ parser->unread --, \
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 ++, \
523+ parser->unread --, \
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, ptrdiff_t column,
619+ ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark);
622+yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t 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 find 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 == (ptrdiff_t)parser->mark.column);
1109+ * If the current position may start a simple key, save it.
1112+ if (parser->simple_key_allowed)
1114+ yaml_simple_key_t simple_key;
1115+ simple_key.possible = 1;
1116+ simple_key.required = required;
1117+ simple_key.token_number =
1118+ parser->tokens_parsed + (parser->tokens.tail - parser->tokens.head);
1119+ simple_key.mark = parser->mark;
1121+ if (!yaml_parser_remove_simple_key(parser)) return 0;
1123+ *(parser->simple_keys.top-1) = simple_key;
1130+ * Remove a potential simple key at the current flow level.
1134+yaml_parser_remove_simple_key(yaml_parser_t *parser)
1136+ yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1138+ if (simple_key->possible)
1140+ /* If the key is required, it is an error. */
1142+ if (simple_key->required) {
1143+ return yaml_parser_set_scanner_error(parser,
1144+ "while scanning a simple key", simple_key->mark,
1145+ "could not find expected ':'");
1149+ /* Remove the key from the stack. */
1151+ simple_key->possible = 0;
1157+ * Increase the flow level and resize the simple key list if needed.
1161+yaml_parser_increase_flow_level(yaml_parser_t *parser)
1163+ yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
1165+ /* Reset the simple key on the next level. */
1167+ if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1170+ /* Increase the flow level. */
1172+ if (parser->flow_level == INT_MAX) {
1173+ parser->error = YAML_MEMORY_ERROR;
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+ (void)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, ptrdiff_t column,
1206+ ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark)
1208+ yaml_token_t token;
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+ if (column > INT_MAX) {
1226+ parser->error = YAML_MEMORY_ERROR;
1230+ parser->indent = column;
1232+ /* Create a token and insert it into the queue. */
1234+ TOKEN_INIT(token, type, mark, mark);
1236+ if (number == -1) {
1237+ if (!ENQUEUE(parser, parser->tokens, token))
1241+ if (!QUEUE_INSERT(parser,
1242+ parser->tokens, number - parser->tokens_parsed, token))
1251+ * Pop indentation levels from the indents stack until the current level
1252+ * becomes less or equal to the column. For each indentation level, append
1253+ * the BLOCK-END token.
1258+yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column)
1260+ yaml_token_t token;
1262+ /* In the flow context, do nothing. */
1264+ if (parser->flow_level)
1267+ /* Loop through the indentation levels in the stack. */
1269+ while (parser->indent > column)
1271+ /* Create a token and append it to the queue. */
1273+ TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
1275+ if (!ENQUEUE(parser, parser->tokens, token))
1278+ /* Pop the indentation level. */
1280+ parser->indent = POP(parser, parser->indents);
1287+ * Initialize the scanner and produce the STREAM-START token.
1291+yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1293+ yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1294+ yaml_token_t token;
1296+ /* Set the initial indentation. */
1298+ parser->indent = -1;
1300+ /* Initialize the simple key stack. */
1302+ if (!PUSH(parser, parser->simple_keys, simple_key))
1305+ /* A simple key is allowed at the beginning of the stream. */
1307+ parser->simple_key_allowed = 1;
1309+ /* We have started. */
1311+ parser->stream_start_produced = 1;
1313+ /* Create the STREAM-START token and append it to the queue. */
1315+ STREAM_START_TOKEN_INIT(token, parser->encoding,
1316+ parser->mark, parser->mark);
1318+ if (!ENQUEUE(parser, parser->tokens, token))
1325+ * Produce the STREAM-END token and shut down the scanner.
1329+yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1331+ yaml_token_t token;
1333+ /* Force new line. */
1335+ if (parser->mark.column != 0) {
1336+ parser->mark.column = 0;
1337+ parser->mark.line ++;
1340+ /* Reset the indentation level. */
1342+ if (!yaml_parser_unroll_indent(parser, -1))
1345+ /* Reset simple keys. */
1347+ if (!yaml_parser_remove_simple_key(parser))
1350+ parser->simple_key_allowed = 0;
1352+ /* Create the STREAM-END token and append it to the queue. */
1354+ STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
1356+ if (!ENQUEUE(parser, parser->tokens, token))
1363+ * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1367+yaml_parser_fetch_directive(yaml_parser_t *parser)
1369+ yaml_token_t token;
1371+ /* Reset the indentation level. */
1373+ if (!yaml_parser_unroll_indent(parser, -1))
1376+ /* Reset simple keys. */
1378+ if (!yaml_parser_remove_simple_key(parser))
1381+ parser->simple_key_allowed = 0;
1383+ /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1385+ if (!yaml_parser_scan_directive(parser, &token))
1388+ /* Append the token to the queue. */
1390+ if (!ENQUEUE(parser, parser->tokens, token)) {
1391+ yaml_token_delete(&token);
1399+ * Produce the DOCUMENT-START or DOCUMENT-END token.
1403+yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1404+ yaml_token_type_t type)
1406+ yaml_mark_t start_mark, end_mark;
1407+ yaml_token_t token;
1409+ /* Reset the indentation level. */
1411+ if (!yaml_parser_unroll_indent(parser, -1))
1414+ /* Reset simple keys. */
1416+ if (!yaml_parser_remove_simple_key(parser))
1419+ parser->simple_key_allowed = 0;
1421+ /* Consume the token. */
1423+ start_mark = parser->mark;
1429+ end_mark = parser->mark;
1431+ /* Create the DOCUMENT-START or DOCUMENT-END token. */
1433+ TOKEN_INIT(token, type, start_mark, end_mark);
1435+ /* Append the token to the queue. */
1437+ if (!ENQUEUE(parser, parser->tokens, token))
1444+ * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1448+yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1449+ yaml_token_type_t type)
1451+ yaml_mark_t start_mark, end_mark;
1452+ yaml_token_t token;
1454+ /* The indicators '[' and '{' may start a simple key. */
1456+ if (!yaml_parser_save_simple_key(parser))
1459+ /* Increase the flow level. */
1461+ if (!yaml_parser_increase_flow_level(parser))
1464+ /* A simple key may follow the indicators '[' and '{'. */
1466+ parser->simple_key_allowed = 1;
1468+ /* Consume the token. */
1470+ start_mark = parser->mark;
1472+ end_mark = parser->mark;
1474+ /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1476+ TOKEN_INIT(token, type, start_mark, end_mark);
1478+ /* Append the token to the queue. */
1480+ if (!ENQUEUE(parser, parser->tokens, token))
1487+ * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1491+yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1492+ yaml_token_type_t type)
1494+ yaml_mark_t start_mark, end_mark;
1495+ yaml_token_t token;
1497+ /* Reset any potential simple key on the current flow level. */
1499+ if (!yaml_parser_remove_simple_key(parser))
1502+ /* Decrease the flow level. */
1504+ if (!yaml_parser_decrease_flow_level(parser))
1507+ /* No simple keys after the indicators ']' and '}'. */
1509+ parser->simple_key_allowed = 0;
1511+ /* Consume the token. */
1513+ start_mark = parser->mark;
1515+ end_mark = parser->mark;
1517+ /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1519+ TOKEN_INIT(token, type, start_mark, end_mark);
1521+ /* Append the token to the queue. */
1523+ if (!ENQUEUE(parser, parser->tokens, token))
1530+ * Produce the FLOW-ENTRY token.
1534+yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1536+ yaml_mark_t start_mark, end_mark;
1537+ yaml_token_t token;
1539+ /* Reset any potential simple keys on the current flow level. */
1541+ if (!yaml_parser_remove_simple_key(parser))
1544+ /* Simple keys are allowed after ','. */
1546+ parser->simple_key_allowed = 1;
1548+ /* Consume the token. */
1550+ start_mark = parser->mark;
1552+ end_mark = parser->mark;
1554+ /* Create the FLOW-ENTRY token and append it to the queue. */
1556+ TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
1558+ if (!ENQUEUE(parser, parser->tokens, token))
1565+ * Produce the BLOCK-ENTRY token.
1569+yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1571+ yaml_mark_t start_mark, end_mark;
1572+ yaml_token_t token;
1574+ /* Check if the scanner is in the block context. */
1576+ if (!parser->flow_level)
1578+ /* Check if we are allowed to start a new entry. */
1580+ if (!parser->simple_key_allowed) {
1581+ return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1582+ "block sequence entries are not allowed in this context");
1585+ /* Add the BLOCK-SEQUENCE-START token if needed. */
1587+ if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1588+ YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
1594+ * It is an error for the '-' indicator to occur in the flow context,
1595+ * but we let the Parser detect and report about it because the Parser
1596+ * is able to point to the context.
1600+ /* Reset any potential simple keys on the current flow level. */
1602+ if (!yaml_parser_remove_simple_key(parser))
1605+ /* Simple keys are allowed after '-'. */
1607+ parser->simple_key_allowed = 1;
1609+ /* Consume the token. */
1611+ start_mark = parser->mark;
1613+ end_mark = parser->mark;
1615+ /* Create the BLOCK-ENTRY token and append it to the queue. */
1617+ TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
1619+ if (!ENQUEUE(parser, parser->tokens, token))
1626+ * Produce the KEY token.
1630+yaml_parser_fetch_key(yaml_parser_t *parser)
1632+ yaml_mark_t start_mark, end_mark;
1633+ yaml_token_t token;
1635+ /* In the block context, additional checks are required. */
1637+ if (!parser->flow_level)
1639+ /* Check if we are allowed to start a new key (not necessary simple). */
1641+ if (!parser->simple_key_allowed) {
1642+ return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1643+ "mapping keys are not allowed in this context");
1646+ /* Add the BLOCK-MAPPING-START token if needed. */
1648+ if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1649+ YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1653+ /* Reset any potential simple keys on the current flow level. */
1655+ if (!yaml_parser_remove_simple_key(parser))
1658+ /* Simple keys are allowed after '?' in the block context. */
1660+ parser->simple_key_allowed = (!parser->flow_level);
1662+ /* Consume the token. */
1664+ start_mark = parser->mark;
1666+ end_mark = parser->mark;
1668+ /* Create the KEY token and append it to the queue. */
1670+ TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
1672+ if (!ENQUEUE(parser, parser->tokens, token))
1679+ * Produce the VALUE token.
1683+yaml_parser_fetch_value(yaml_parser_t *parser)
1685+ yaml_mark_t start_mark, end_mark;
1686+ yaml_token_t token;
1687+ yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
1689+ /* Have we found a simple key? */
1691+ if (simple_key->possible)
1694+ /* Create the KEY token and insert it into the queue. */
1696+ TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
1698+ if (!QUEUE_INSERT(parser, parser->tokens,
1699+ simple_key->token_number - parser->tokens_parsed, token))
1702+ /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1704+ if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
1705+ simple_key->token_number,
1706+ YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1709+ /* Remove the simple key. */
1711+ simple_key->possible = 0;
1713+ /* A simple key cannot follow another simple key. */
1715+ parser->simple_key_allowed = 0;
1719+ /* The ':' indicator follows a complex key. */
1721+ /* In the block context, extra checks are required. */
1723+ if (!parser->flow_level)
1725+ /* Check if we are allowed to start a complex value. */
1727+ if (!parser->simple_key_allowed) {
1728+ return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
1729+ "mapping values are not allowed in this context");
1732+ /* Add the BLOCK-MAPPING-START token if needed. */
1734+ if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1735+ YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
1739+ /* Simple keys after ':' are allowed in the block context. */
1741+ parser->simple_key_allowed = (!parser->flow_level);
1744+ /* Consume the token. */
1746+ start_mark = parser->mark;
1748+ end_mark = parser->mark;
1750+ /* Create the VALUE token and append it to the queue. */
1752+ TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
1754+ if (!ENQUEUE(parser, parser->tokens, token))
1761+ * Produce the ALIAS or ANCHOR token.
1765+yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1767+ yaml_token_t token;
1769+ /* An anchor or an alias could be a simple key. */
1771+ if (!yaml_parser_save_simple_key(parser))
1774+ /* A simple key cannot follow an anchor or an alias. */
1776+ parser->simple_key_allowed = 0;
1778+ /* Create the ALIAS or ANCHOR token and append it to the queue. */
1780+ if (!yaml_parser_scan_anchor(parser, &token, type))
1783+ if (!ENQUEUE(parser, parser->tokens, token)) {
1784+ yaml_token_delete(&token);
1791+ * Produce the TAG token.
1795+yaml_parser_fetch_tag(yaml_parser_t *parser)
1797+ yaml_token_t token;
1799+ /* A tag could be a simple key. */
1801+ if (!yaml_parser_save_simple_key(parser))
1804+ /* A simple key cannot follow a tag. */
1806+ parser->simple_key_allowed = 0;
1808+ /* Create the TAG token and append it to the queue. */
1810+ if (!yaml_parser_scan_tag(parser, &token))
1813+ if (!ENQUEUE(parser, parser->tokens, token)) {
1814+ yaml_token_delete(&token);
1822+ * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1826+yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1828+ yaml_token_t token;
1830+ /* Remove any potential simple keys. */
1832+ if (!yaml_parser_remove_simple_key(parser))
1835+ /* A simple key may follow a block scalar. */
1837+ parser->simple_key_allowed = 1;
1839+ /* Create the SCALAR token and append it to the queue. */
1841+ if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1844+ if (!ENQUEUE(parser, parser->tokens, token)) {
1845+ yaml_token_delete(&token);
1853+ * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1857+yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1859+ yaml_token_t token;
1861+ /* A plain scalar could be a simple key. */
1863+ if (!yaml_parser_save_simple_key(parser))
1866+ /* A simple key cannot follow a flow scalar. */
1868+ parser->simple_key_allowed = 0;
1870+ /* Create the SCALAR token and append it to the queue. */
1872+ if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1875+ if (!ENQUEUE(parser, parser->tokens, token)) {
1876+ yaml_token_delete(&token);
1884+ * Produce the SCALAR(...,plain) token.
1888+yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1890+ yaml_token_t token;
1892+ /* A plain scalar could be a simple key. */
1894+ if (!yaml_parser_save_simple_key(parser))
1897+ /* A simple key cannot follow a flow scalar. */
1899+ parser->simple_key_allowed = 0;
1901+ /* Create the SCALAR token and append it to the queue. */
1903+ if (!yaml_parser_scan_plain_scalar(parser, &token))
1906+ if (!ENQUEUE(parser, parser->tokens, token)) {
1907+ yaml_token_delete(&token);
1915+ * Eat whitespaces and comments until the next token is found.
1919+yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1921+ /* Until the next token is not found. */
1925+ /* Allow the BOM mark to start a line. */
1927+ if (!CACHE(parser, 1)) return 0;
1929+ if (parser->mark.column == 0 && IS_BOM(parser->buffer))
1935+ * Tabs are allowed:
1937+ * - in the flow context;
1938+ * - in the block context, but not at the beginning of the line or
1939+ * after '-', '?', or ':' (complex value).
1942+ if (!CACHE(parser, 1)) return 0;
1944+ while (CHECK(parser->buffer,' ') ||
1945+ ((parser->flow_level || !parser->simple_key_allowed) &&
1946+ CHECK(parser->buffer, '\t'))) {
1948+ if (!CACHE(parser, 1)) return 0;
1951+ /* Eat a comment until a line break. */
1953+ if (CHECK(parser->buffer, '#')) {
1954+ while (!IS_BREAKZ(parser->buffer)) {
1956+ if (!CACHE(parser, 1)) return 0;
1960+ /* If it is a line break, eat it. */
1962+ if (IS_BREAK(parser->buffer))
1964+ if (!CACHE(parser, 2)) return 0;
1967+ /* In the block context, a new line may start a simple key. */
1969+ if (!parser->flow_level) {
1970+ parser->simple_key_allowed = 1;
1975+ /* We have found a token. */
1985+ * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1988+ * %YAML 1.1 # a comment \n
1989+ * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1990+ * %TAG !yaml! tag:yaml.org,2002: \n
1991+ * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1995+yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
1997+ yaml_mark_t start_mark, end_mark;
1998+ yaml_char_t *name = NULL;
2000+ yaml_char_t *handle = NULL, *prefix = NULL;
2004+ start_mark = parser->mark;
2008+ /* Scan the directive name. */
2010+ if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2013+ /* Is it a YAML directive? */
2015+ if (strcmp((char *)name, "YAML") == 0)
2017+ /* Scan the VERSION directive value. */
2019+ if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2023+ end_mark = parser->mark;
2025+ /* Create a VERSION-DIRECTIVE token. */
2027+ VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
2028+ start_mark, end_mark);
2031+ /* Is it a TAG directive? */
2033+ else if (strcmp((char *)name, "TAG") == 0)
2035+ /* Scan the TAG directive value. */
2037+ if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2041+ end_mark = parser->mark;
2043+ /* Create a TAG-DIRECTIVE token. */
2045+ TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
2046+ start_mark, end_mark);
2049+ /* Unknown directive. */
2053+ yaml_parser_set_scanner_error(parser, "while scanning a directive",
2054+ start_mark, "found unknown directive name");
2058+ /* Eat the rest of the line including any comments. */
2060+ if (!CACHE(parser, 1)) goto error;
2062+ while (IS_BLANK(parser->buffer)) {
2064+ if (!CACHE(parser, 1)) goto error;
2067+ if (CHECK(parser->buffer, '#')) {
2068+ while (!IS_BREAKZ(parser->buffer)) {
2070+ if (!CACHE(parser, 1)) goto error;
2074+ /* Check if we are at the end of the line. */
2076+ if (!IS_BREAKZ(parser->buffer)) {
2077+ yaml_parser_set_scanner_error(parser, "while scanning a directive",
2078+ start_mark, "did not find expected comment or line break");
2082+ /* Eat a line break. */
2084+ if (IS_BREAK(parser->buffer)) {
2085+ if (!CACHE(parser, 2)) goto error;
2101+ * Scan the directive name.
2104+ * %YAML 1.1 # a comment \n
2106+ * %TAG !yaml! tag:yaml.org,2002: \n
2111+yaml_parser_scan_directive_name(yaml_parser_t *parser,
2112+ yaml_mark_t start_mark, yaml_char_t **name)
2114+ yaml_string_t string = NULL_STRING;
2116+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2118+ /* Consume the directive name. */
2120+ if (!CACHE(parser, 1)) goto error;
2122+ while (IS_ALPHA(parser->buffer))
2124+ if (!READ(parser, string)) goto error;
2125+ if (!CACHE(parser, 1)) goto error;
2128+ /* Check if the name is empty. */
2130+ if (string.start == string.pointer) {
2131+ yaml_parser_set_scanner_error(parser, "while scanning a directive",
2132+ start_mark, "could not find expected directive name");
2136+ /* Check for an blank character after the name. */
2138+ if (!IS_BLANKZ(parser->buffer)) {
2139+ yaml_parser_set_scanner_error(parser, "while scanning a directive",
2140+ start_mark, "found unexpected non-alphabetical character");
2144+ *name = string.start;
2149+ STRING_DEL(parser, string);
2154+ * Scan the value of VERSION-DIRECTIVE.
2157+ * %YAML 1.1 # a comment \n
2162+yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2163+ yaml_mark_t start_mark, int *major, int *minor)
2165+ /* Eat whitespaces. */
2167+ if (!CACHE(parser, 1)) return 0;
2169+ while (IS_BLANK(parser->buffer)) {
2171+ if (!CACHE(parser, 1)) return 0;
2174+ /* Consume the major version number. */
2176+ if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2181+ if (!CHECK(parser->buffer, '.')) {
2182+ return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2183+ start_mark, "did not find expected digit or '.' character");
2188+ /* Consume the minor version number. */
2190+ if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2196+#define MAX_NUMBER_LENGTH 9
2199+ * Scan the version number of VERSION-DIRECTIVE.
2202+ * %YAML 1.1 # a comment \n
2204+ * %YAML 1.1 # a comment \n
2209+yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2210+ yaml_mark_t start_mark, int *number)
2215+ /* Repeat while the next character is digit. */
2217+ if (!CACHE(parser, 1)) return 0;
2219+ while (IS_DIGIT(parser->buffer))
2221+ /* Check if the number is too long. */
2223+ if (++length > MAX_NUMBER_LENGTH) {
2224+ return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2225+ start_mark, "found extremely long version number");
2228+ value = value*10 + AS_DIGIT(parser->buffer);
2232+ if (!CACHE(parser, 1)) return 0;
2235+ /* Check if the number was present. */
2238+ return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2239+ start_mark, "did not find expected version number");
2248+ * Scan the value of a TAG-DIRECTIVE token.
2251+ * %TAG !yaml! tag:yaml.org,2002: \n
2252+ * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2256+yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2257+ yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2259+ yaml_char_t *handle_value = NULL;
2260+ yaml_char_t *prefix_value = NULL;
2262+ /* Eat whitespaces. */
2264+ if (!CACHE(parser, 1)) goto error;
2266+ while (IS_BLANK(parser->buffer)) {
2268+ if (!CACHE(parser, 1)) goto error;
2271+ /* Scan a handle. */
2273+ if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2276+ /* Expect a whitespace. */
2278+ if (!CACHE(parser, 1)) goto error;
2280+ if (!IS_BLANK(parser->buffer)) {
2281+ yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2282+ start_mark, "did not find expected whitespace");
2286+ /* Eat whitespaces. */
2288+ while (IS_BLANK(parser->buffer)) {
2290+ if (!CACHE(parser, 1)) goto error;
2293+ /* Scan a prefix. */
2295+ if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2298+ /* Expect a whitespace or line break. */
2300+ if (!CACHE(parser, 1)) goto error;
2302+ if (!IS_BLANKZ(parser->buffer)) {
2303+ yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2304+ start_mark, "did not find expected whitespace or line break");
2308+ *handle = handle_value;
2309+ *prefix = prefix_value;
2314+ yaml_free(handle_value);
2315+ yaml_free(prefix_value);
2320+yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
2321+ yaml_token_type_t type)
2324+ yaml_mark_t start_mark, end_mark;
2325+ yaml_string_t string = NULL_STRING;
2327+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2329+ /* Eat the indicator character. */
2331+ start_mark = parser->mark;
2335+ /* Consume the value. */
2337+ if (!CACHE(parser, 1)) goto error;
2339+ while (IS_ALPHA(parser->buffer)) {
2340+ if (!READ(parser, string)) goto error;
2341+ if (!CACHE(parser, 1)) goto error;
2345+ end_mark = parser->mark;
2348+ * Check if length of the anchor is greater than 0 and it is followed by
2349+ * a whitespace character or one of the indicators:
2351+ * '?', ':', ',', ']', '}', '%', '@', '`'.
2354+ if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2355+ || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2356+ || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2357+ || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2358+ || CHECK(parser->buffer, '`'))) {
2359+ yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2360+ "while scanning an anchor" : "while scanning an alias", start_mark,
2361+ "did not find expected alphabetic or numeric character");
2365+ /* Create a token. */
2367+ if (type == YAML_ANCHOR_TOKEN) {
2368+ ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2371+ ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2377+ STRING_DEL(parser, string);
2382+ * Scan a TAG token.
2386+yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
2388+ yaml_char_t *handle = NULL;
2389+ yaml_char_t *suffix = NULL;
2390+ yaml_mark_t start_mark, end_mark;
2392+ start_mark = parser->mark;
2394+ /* Check if the tag is in the canonical form. */
2396+ if (!CACHE(parser, 2)) goto error;
2398+ if (CHECK_AT(parser->buffer, '<', 1))
2400+ /* Set the handle to '' */
2402+ handle = YAML_MALLOC(1);
2403+ if (!handle) goto error;
2411+ /* Consume the tag value. */
2413+ if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2416+ /* Check for '>' and eat it. */
2418+ if (!CHECK(parser->buffer, '>')) {
2419+ yaml_parser_set_scanner_error(parser, "while scanning a tag",
2420+ start_mark, "did not find the expected '>'");
2428+ /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2430+ /* First, try to scan a handle. */
2432+ if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2435+ /* Check if it is, indeed, handle. */
2437+ if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2439+ /* Scan the suffix now. */
2441+ if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2446+ /* It wasn't a handle after all. Scan the rest of the tag. */
2448+ if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2451+ /* Set the handle to '!'. */
2454+ handle = YAML_MALLOC(2);
2455+ if (!handle) goto error;
2460+ * A special case: the '!' tag. Set the handle to '' and the
2464+ if (suffix[0] == '\0') {
2465+ yaml_char_t *tmp = handle;
2472+ /* Check the character which ends the tag. */
2474+ if (!CACHE(parser, 1)) goto error;
2476+ if (!IS_BLANKZ(parser->buffer)) {
2477+ yaml_parser_set_scanner_error(parser, "while scanning a tag",
2478+ start_mark, "did not find expected whitespace or line break");
2482+ end_mark = parser->mark;
2484+ /* Create a token. */
2486+ TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
2497+ * Scan a tag handle.
2501+yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2502+ yaml_mark_t start_mark, yaml_char_t **handle)
2504+ yaml_string_t string = NULL_STRING;
2506+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2508+ /* Check the initial '!' character. */
2510+ if (!CACHE(parser, 1)) goto error;
2512+ if (!CHECK(parser->buffer, '!')) {
2513+ yaml_parser_set_scanner_error(parser, directive ?
2514+ "while scanning a tag directive" : "while scanning a tag",
2515+ start_mark, "did not find expected '!'");
2519+ /* Copy the '!' character. */
2521+ if (!READ(parser, string)) goto error;
2523+ /* Copy all subsequent alphabetical and numerical characters. */
2525+ if (!CACHE(parser, 1)) goto error;
2527+ while (IS_ALPHA(parser->buffer))
2529+ if (!READ(parser, string)) goto error;
2530+ if (!CACHE(parser, 1)) goto error;
2533+ /* Check if the trailing character is '!' and copy it. */
2535+ if (CHECK(parser->buffer, '!'))
2537+ if (!READ(parser, string)) goto error;
2542+ * It's either the '!' tag or not really a tag handle. If it's a %TAG
2543+ * directive, it's an error. If it's a tag token, it must be a part of
2547+ if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
2548+ yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
2549+ start_mark, "did not find expected '!'");
2554+ *handle = string.start;
2559+ STRING_DEL(parser, string);
2568+yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2569+ yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2571+ size_t length = head ? strlen((char *)head) : 0;
2572+ yaml_string_t string = NULL_STRING;
2574+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2576+ /* Resize the string to include the head. */
2578+ while ((size_t)(string.end - string.start) <= length) {
2579+ if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2580+ parser->error = YAML_MEMORY_ERROR;
2586+ * Copy the head if needed.
2588+ * Note that we don't copy the leading '!' character.
2592+ memcpy(string.start, head+1, length-1);
2593+ string.pointer += length-1;
2596+ /* Scan the tag. */
2598+ if (!CACHE(parser, 1)) goto error;
2601+ * The set of characters that may appear in URI is as follows:
2603+ * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2604+ * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2608+ while (IS_ALPHA(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, ',') || CHECK(parser->buffer, '.')
2614+ || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
2615+ || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
2616+ || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
2617+ || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2618+ || CHECK(parser->buffer, '%'))
2620+ /* Check if it is a URI-escape sequence. */
2622+ if (CHECK(parser->buffer, '%')) {
2623+ if (!STRING_EXTEND(parser, string))
2626+ if (!yaml_parser_scan_uri_escapes(parser,
2627+ directive, start_mark, &string)) goto error;
2630+ if (!READ(parser, string)) goto error;
2634+ if (!CACHE(parser, 1)) goto error;
2637+ /* Check if the tag is non-empty. */
2640+ if (!STRING_EXTEND(parser, string))
2643+ yaml_parser_set_scanner_error(parser, directive ?
2644+ "while parsing a %TAG directive" : "while parsing a tag",
2645+ start_mark, "did not find expected tag URI");
2649+ *uri = string.start;
2654+ STRING_DEL(parser, string);
2659+ * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2663+yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2664+ yaml_mark_t start_mark, yaml_string_t *string)
2668+ /* Decode the required number of characters. */
2672+ unsigned char octet = 0;
2674+ /* Check for a URI-escaped octet. */
2676+ if (!CACHE(parser, 3)) return 0;
2678+ if (!(CHECK(parser->buffer, '%')
2679+ && IS_HEX_AT(parser->buffer, 1)
2680+ && IS_HEX_AT(parser->buffer, 2))) {
2681+ return yaml_parser_set_scanner_error(parser, directive ?
2682+ "while parsing a %TAG directive" : "while parsing a tag",
2683+ start_mark, "did not find URI escaped octet");
2686+ /* Get the octet. */
2688+ octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
2690+ /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2694+ width = (octet & 0x80) == 0x00 ? 1 :
2695+ (octet & 0xE0) == 0xC0 ? 2 :
2696+ (octet & 0xF0) == 0xE0 ? 3 :
2697+ (octet & 0xF8) == 0xF0 ? 4 : 0;
2699+ return yaml_parser_set_scanner_error(parser, directive ?
2700+ "while parsing a %TAG directive" : "while parsing a tag",
2701+ start_mark, "found an incorrect leading UTF-8 octet");
2706+ /* Check if the trailing octet is correct. */
2708+ if ((octet & 0xC0) != 0x80) {
2709+ return yaml_parser_set_scanner_error(parser, directive ?
2710+ "while parsing a %TAG directive" : "while parsing a tag",
2711+ start_mark, "found an incorrect trailing UTF-8 octet");
2715+ /* Copy the octet and move the pointers. */
2717+ *(string->pointer++) = octet;
2728+ * Scan a block scalar.
2732+yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2735+ yaml_mark_t start_mark;
2736+ yaml_mark_t end_mark;
2737+ yaml_string_t string = NULL_STRING;
2738+ yaml_string_t leading_break = NULL_STRING;
2739+ yaml_string_t trailing_breaks = NULL_STRING;
2743+ int leading_blank = 0;
2744+ int trailing_blank = 0;
2746+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2747+ if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2748+ if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
2750+ /* Eat the indicator '|' or '>'. */
2752+ start_mark = parser->mark;
2756+ /* Scan the additional block scalar indicators. */
2758+ if (!CACHE(parser, 1)) goto error;
2760+ /* Check for a chomping indicator. */
2762+ if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
2764+ /* Set the chomping method and eat the indicator. */
2766+ chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2770+ /* Check for an indentation indicator. */
2772+ if (!CACHE(parser, 1)) goto error;
2774+ if (IS_DIGIT(parser->buffer))
2776+ /* Check that the indentation is greater than 0. */
2778+ if (CHECK(parser->buffer, '0')) {
2779+ yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2780+ start_mark, "found an indentation indicator equal to 0");
2784+ /* Get the indentation level and eat the indicator. */
2786+ increment = AS_DIGIT(parser->buffer);
2792+ /* Do the same as above, but in the opposite order. */
2794+ else if (IS_DIGIT(parser->buffer))
2796+ if (CHECK(parser->buffer, '0')) {
2797+ yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2798+ start_mark, "found an indentation indicator equal to 0");
2802+ increment = AS_DIGIT(parser->buffer);
2806+ if (!CACHE(parser, 1)) goto error;
2808+ if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2809+ chomping = CHECK(parser->buffer, '+') ? +1 : -1;
2815+ /* Eat whitespaces and comments to the end of the line. */
2817+ if (!CACHE(parser, 1)) goto error;
2819+ while (IS_BLANK(parser->buffer)) {
2821+ if (!CACHE(parser, 1)) goto error;
2824+ if (CHECK(parser->buffer, '#')) {
2825+ while (!IS_BREAKZ(parser->buffer)) {
2827+ if (!CACHE(parser, 1)) goto error;
2831+ /* Check if we are at the end of the line. */
2833+ if (!IS_BREAKZ(parser->buffer)) {
2834+ yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2835+ start_mark, "did not find expected comment or line break");
2839+ /* Eat a line break. */
2841+ if (IS_BREAK(parser->buffer)) {
2842+ if (!CACHE(parser, 2)) goto error;
2846+ end_mark = parser->mark;
2848+ /* Set the indentation level if it was specified. */
2851+ indent = parser->indent >= 0 ? parser->indent+increment : increment;
2854+ /* Scan the leading line breaks and determine the indentation level if needed. */
2856+ if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
2857+ start_mark, &end_mark)) goto error;
2859+ /* Scan the block scalar content. */
2861+ if (!CACHE(parser, 1)) goto error;
2863+ while ((int)parser->mark.column == indent && !(IS_Z(parser->buffer)))
2866+ * We are at the beginning of a non-empty line.
2869+ /* Is it a trailing whitespace? */
2871+ trailing_blank = IS_BLANK(parser->buffer);
2873+ /* Check if we need to fold the leading line break. */
2875+ if (!literal && (*leading_break.start == '\n')
2876+ && !leading_blank && !trailing_blank)
2878+ /* Do we need to join the lines by space? */
2880+ if (*trailing_breaks.start == '\0') {
2881+ if (!STRING_EXTEND(parser, string)) goto error;
2882+ *(string.pointer ++) = ' ';
2885+ CLEAR(parser, leading_break);
2888+ if (!JOIN(parser, string, leading_break)) goto error;
2889+ CLEAR(parser, leading_break);
2892+ /* Append the remaining line breaks. */
2894+ if (!JOIN(parser, string, trailing_breaks)) goto error;
2895+ CLEAR(parser, trailing_breaks);
2897+ /* Is it a leading whitespace? */
2899+ leading_blank = IS_BLANK(parser->buffer);
2901+ /* Consume the current line. */
2903+ while (!IS_BREAKZ(parser->buffer)) {
2904+ if (!READ(parser, string)) goto error;
2905+ if (!CACHE(parser, 1)) goto error;
2908+ /* Consume the line break. */
2910+ if (!CACHE(parser, 2)) goto error;
2912+ if (!READ_LINE(parser, leading_break)) goto error;
2914+ /* Eat the following indentation spaces and line breaks. */
2916+ if (!yaml_parser_scan_block_scalar_breaks(parser,
2917+ &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
2920+ /* Chomp the tail. */
2922+ if (chomping != -1) {
2923+ if (!JOIN(parser, string, leading_break)) goto error;
2925+ if (chomping == 1) {
2926+ if (!JOIN(parser, string, trailing_breaks)) goto error;
2929+ /* Create a token. */
2931+ SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
2932+ literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2933+ start_mark, end_mark);
2935+ STRING_DEL(parser, leading_break);
2936+ STRING_DEL(parser, trailing_breaks);
2941+ STRING_DEL(parser, string);
2942+ STRING_DEL(parser, leading_break);
2943+ STRING_DEL(parser, trailing_breaks);
2949+ * Scan indentation spaces and line breaks for a block scalar. Determine the
2950+ * indentation level if needed.
2954+yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2955+ int *indent, yaml_string_t *breaks,
2956+ yaml_mark_t start_mark, yaml_mark_t *end_mark)
2958+ int max_indent = 0;
2960+ *end_mark = parser->mark;
2962+ /* Eat the indentation spaces and line breaks. */
2966+ /* Eat the indentation spaces. */
2968+ if (!CACHE(parser, 1)) return 0;
2970+ while ((!*indent || (int)parser->mark.column < *indent)
2971+ && IS_SPACE(parser->buffer)) {
2973+ if (!CACHE(parser, 1)) return 0;
2976+ if ((int)parser->mark.column > max_indent)
2977+ max_indent = (int)parser->mark.column;
2979+ /* Check for a tab character messing the indentation. */
2981+ if ((!*indent || (int)parser->mark.column < *indent)
2982+ && IS_TAB(parser->buffer)) {
2983+ return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2984+ start_mark, "found a tab character where an indentation space is expected");
2987+ /* Have we found a non-empty line? */
2989+ if (!IS_BREAK(parser->buffer)) break;
2991+ /* Consume the line break. */
2993+ if (!CACHE(parser, 2)) return 0;
2994+ if (!READ_LINE(parser, *breaks)) return 0;
2995+ *end_mark = parser->mark;
2998+ /* Determine the indentation level if needed. */
3001+ *indent = max_indent;
3002+ if (*indent < parser->indent + 1)
3003+ *indent = parser->indent + 1;
3012+ * Scan a quoted scalar.
3016+yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3019+ yaml_mark_t start_mark;
3020+ yaml_mark_t end_mark;
3021+ yaml_string_t string = NULL_STRING;
3022+ yaml_string_t leading_break = NULL_STRING;
3023+ yaml_string_t trailing_breaks = NULL_STRING;
3024+ yaml_string_t whitespaces = NULL_STRING;
3025+ int leading_blanks;
3027+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3028+ if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3029+ if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3030+ if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3032+ /* Eat the left quote. */
3034+ start_mark = parser->mark;
3038+ /* Consume the content of the quoted scalar. */
3042+ /* Check that there are no document indicators at the beginning of the line. */
3044+ if (!CACHE(parser, 4)) goto error;
3046+ if (parser->mark.column == 0 &&
3047+ ((CHECK_AT(parser->buffer, '-', 0) &&
3048+ CHECK_AT(parser->buffer, '-', 1) &&
3049+ CHECK_AT(parser->buffer, '-', 2)) ||
3050+ (CHECK_AT(parser->buffer, '.', 0) &&
3051+ CHECK_AT(parser->buffer, '.', 1) &&
3052+ CHECK_AT(parser->buffer, '.', 2))) &&
3053+ IS_BLANKZ_AT(parser->buffer, 3))
3055+ yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3056+ start_mark, "found unexpected document indicator");
3060+ /* Check for EOF. */
3062+ if (IS_Z(parser->buffer)) {
3063+ yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3064+ start_mark, "found unexpected end of stream");
3068+ /* Consume non-blank characters. */
3070+ if (!CACHE(parser, 2)) goto error;
3072+ leading_blanks = 0;
3074+ while (!IS_BLANKZ(parser->buffer))
3076+ /* Check for an escaped single quote. */
3078+ if (single && CHECK_AT(parser->buffer, '\'', 0)
3079+ && CHECK_AT(parser->buffer, '\'', 1))
3081+ if (!STRING_EXTEND(parser, string)) goto error;
3082+ *(string.pointer++) = '\'';
3087+ /* Check for the right quote. */
3089+ else if (CHECK(parser->buffer, single ? '\'' : '"'))
3094+ /* Check for an escaped line break. */
3096+ else if (!single && CHECK(parser->buffer, '\\')
3097+ && IS_BREAK_AT(parser->buffer, 1))
3099+ if (!CACHE(parser, 3)) goto error;
3102+ leading_blanks = 1;
3106+ /* Check for an escape sequence. */
3108+ else if (!single && CHECK(parser->buffer, '\\'))
3110+ size_t code_length = 0;
3112+ if (!STRING_EXTEND(parser, string)) goto error;
3114+ /* Check the escape character. */
3116+ switch (parser->buffer.pointer[1])
3119+ *(string.pointer++) = '\0';
3123+ *(string.pointer++) = '\x07';
3127+ *(string.pointer++) = '\x08';
3132+ *(string.pointer++) = '\x09';
3136+ *(string.pointer++) = '\x0A';
3140+ *(string.pointer++) = '\x0B';
3144+ *(string.pointer++) = '\x0C';
3148+ *(string.pointer++) = '\x0D';
3152+ *(string.pointer++) = '\x1B';
3156+ *(string.pointer++) = '\x20';
3160+ *(string.pointer++) = '"';
3164+ *(string.pointer++) = '/';
3168+ *(string.pointer++) = '\\';
3171+ case 'N': /* NEL (#x85) */
3172+ *(string.pointer++) = '\xC2';
3173+ *(string.pointer++) = '\x85';
3176+ case '_': /* #xA0 */
3177+ *(string.pointer++) = '\xC2';
3178+ *(string.pointer++) = '\xA0';
3181+ case 'L': /* LS (#x2028) */
3182+ *(string.pointer++) = '\xE2';
3183+ *(string.pointer++) = '\x80';
3184+ *(string.pointer++) = '\xA8';
3187+ case 'P': /* PS (#x2029) */
3188+ *(string.pointer++) = '\xE2';
3189+ *(string.pointer++) = '\x80';
3190+ *(string.pointer++) = '\xA9';
3206+ yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3207+ start_mark, "found unknown escape character");
3214+ /* Consume an arbitrary escape code. */
3218+ unsigned int value = 0;
3221+ /* Scan the character value. */
3223+ if (!CACHE(parser, code_length)) goto error;
3225+ for (k = 0; k < code_length; k ++) {
3226+ if (!IS_HEX_AT(parser->buffer, k)) {
3227+ yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3228+ start_mark, "did not find expected hexdecimal number");
3231+ value = (value << 4) + AS_HEX_AT(parser->buffer, k);
3234+ /* Check the value and write the character. */
3236+ if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3237+ yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3238+ start_mark, "found invalid Unicode character escape code");
3242+ if (value <= 0x7F) {
3243+ *(string.pointer++) = value;
3245+ else if (value <= 0x7FF) {
3246+ *(string.pointer++) = 0xC0 + (value >> 6);
3247+ *(string.pointer++) = 0x80 + (value & 0x3F);
3249+ else if (value <= 0xFFFF) {
3250+ *(string.pointer++) = 0xE0 + (value >> 12);
3251+ *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3252+ *(string.pointer++) = 0x80 + (value & 0x3F);
3255+ *(string.pointer++) = 0xF0 + (value >> 18);
3256+ *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3257+ *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3258+ *(string.pointer++) = 0x80 + (value & 0x3F);
3261+ /* Advance the pointer. */
3263+ for (k = 0; k < code_length; k ++) {
3271+ /* It is a non-escaped non-blank character. */
3273+ if (!READ(parser, string)) goto error;
3276+ if (!CACHE(parser, 2)) goto error;
3279+ /* Check if we are at the end of the scalar. */
3281+ /* Fix for crash unitialized value crash
3282+ * Credit for the bug and input is to OSS Fuzz
3283+ * Credit for the fix to Alex Gaynor
3285+ if (!CACHE(parser, 1)) goto error;
3286+ if (CHECK(parser->buffer, single ? '\'' : '"'))
3289+ /* Consume blank characters. */
3291+ if (!CACHE(parser, 1)) goto error;
3293+ while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3295+ if (IS_BLANK(parser->buffer))
3297+ /* Consume a space or a tab character. */
3299+ if (!leading_blanks) {
3300+ if (!READ(parser, whitespaces)) goto error;
3308+ if (!CACHE(parser, 2)) goto error;
3310+ /* Check if it is a first line break. */
3312+ if (!leading_blanks)
3314+ CLEAR(parser, whitespaces);
3315+ if (!READ_LINE(parser, leading_break)) goto error;
3316+ leading_blanks = 1;
3320+ if (!READ_LINE(parser, trailing_breaks)) goto error;
3323+ if (!CACHE(parser, 1)) goto error;
3326+ /* Join the whitespaces or fold line breaks. */
3328+ if (leading_blanks)
3330+ /* Do we need to fold line breaks? */
3332+ if (leading_break.start[0] == '\n') {
3333+ if (trailing_breaks.start[0] == '\0') {
3334+ if (!STRING_EXTEND(parser, string)) goto error;
3335+ *(string.pointer++) = ' ';
3338+ if (!JOIN(parser, string, trailing_breaks)) goto error;
3339+ CLEAR(parser, trailing_breaks);
3341+ CLEAR(parser, leading_break);
3344+ if (!JOIN(parser, string, leading_break)) goto error;
3345+ if (!JOIN(parser, string, trailing_breaks)) goto error;
3346+ CLEAR(parser, leading_break);
3347+ CLEAR(parser, trailing_breaks);
3352+ if (!JOIN(parser, string, whitespaces)) goto error;
3353+ CLEAR(parser, whitespaces);
3357+ /* Eat the right quote. */
3361+ end_mark = parser->mark;
3363+ /* Create a token. */
3365+ SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3366+ single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3367+ start_mark, end_mark);
3369+ STRING_DEL(parser, leading_break);
3370+ STRING_DEL(parser, trailing_breaks);
3371+ STRING_DEL(parser, whitespaces);
3376+ STRING_DEL(parser, string);
3377+ STRING_DEL(parser, leading_break);
3378+ STRING_DEL(parser, trailing_breaks);
3379+ STRING_DEL(parser, whitespaces);
3385+ * Scan a plain scalar.
3389+yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
3391+ yaml_mark_t start_mark;
3392+ yaml_mark_t end_mark;
3393+ yaml_string_t string = NULL_STRING;
3394+ yaml_string_t leading_break = NULL_STRING;
3395+ yaml_string_t trailing_breaks = NULL_STRING;
3396+ yaml_string_t whitespaces = NULL_STRING;
3397+ int leading_blanks = 0;
3398+ int indent = parser->indent+1;
3400+ if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3401+ if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3402+ if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3403+ if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
3405+ start_mark = end_mark = parser->mark;
3407+ /* Consume the content of the plain scalar. */
3411+ /* Check for a document indicator. */
3413+ if (!CACHE(parser, 4)) goto error;
3415+ if (parser->mark.column == 0 &&
3416+ ((CHECK_AT(parser->buffer, '-', 0) &&
3417+ CHECK_AT(parser->buffer, '-', 1) &&
3418+ CHECK_AT(parser->buffer, '-', 2)) ||
3419+ (CHECK_AT(parser->buffer, '.', 0) &&
3420+ CHECK_AT(parser->buffer, '.', 1) &&
3421+ CHECK_AT(parser->buffer, '.', 2))) &&
3422+ IS_BLANKZ_AT(parser->buffer, 3)) break;
3424+ /* Check for a comment. */
3426+ if (CHECK(parser->buffer, '#'))
3429+ /* Consume non-blank characters. */
3431+ while (!IS_BLANKZ(parser->buffer))
3433+ /* Check for "x:" + one of ',?[]{}' in the flow context. TODO: Fix the test "spec-08-13".
3434+ * This is not completely according to the spec
3435+ * See http://yaml.org/spec/1.1/#id907281 9.1.3. Plain
3438+ if (parser->flow_level
3439+ && CHECK(parser->buffer, ':')
3441+ CHECK_AT(parser->buffer, ',', 1)
3442+ || CHECK_AT(parser->buffer, '?', 1)
3443+ || CHECK_AT(parser->buffer, '[', 1)
3444+ || CHECK_AT(parser->buffer, ']', 1)
3445+ || CHECK_AT(parser->buffer, '{', 1)
3446+ || CHECK_AT(parser->buffer, '}', 1)
3449+ yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3450+ start_mark, "found unexpected ':'");
3454+ /* Check for indicators that may end a plain scalar. */
3456+ if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3457+ || (parser->flow_level &&
3458+ (CHECK(parser->buffer, ',')
3459+ || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
3460+ || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3461+ || CHECK(parser->buffer, '}'))))
3464+ /* Check if we need to join whitespaces and breaks. */
3466+ if (leading_blanks || whitespaces.start != whitespaces.pointer)
3468+ if (leading_blanks)
3470+ /* Do we need to fold line breaks? */
3472+ if (leading_break.start[0] == '\n') {
3473+ if (trailing_breaks.start[0] == '\0') {
3474+ if (!STRING_EXTEND(parser, string)) goto error;
3475+ *(string.pointer++) = ' ';
3478+ if (!JOIN(parser, string, trailing_breaks)) goto error;
3479+ CLEAR(parser, trailing_breaks);
3481+ CLEAR(parser, leading_break);
3484+ if (!JOIN(parser, string, leading_break)) goto error;
3485+ if (!JOIN(parser, string, trailing_breaks)) goto error;
3486+ CLEAR(parser, leading_break);
3487+ CLEAR(parser, trailing_breaks);
3490+ leading_blanks = 0;
3494+ if (!JOIN(parser, string, whitespaces)) goto error;
3495+ CLEAR(parser, whitespaces);
3499+ /* Copy the character. */
3501+ if (!READ(parser, string)) goto error;
3503+ end_mark = parser->mark;
3505+ if (!CACHE(parser, 2)) goto error;
3508+ /* Is it the end? */
3510+ if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
3513+ /* Consume blank characters. */
3515+ if (!CACHE(parser, 1)) goto error;
3517+ while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
3519+ if (IS_BLANK(parser->buffer))
3521+ /* Check for tab character that abuse indentation. */
3523+ if (leading_blanks && (int)parser->mark.column < indent
3524+ && IS_TAB(parser->buffer)) {
3525+ yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3526+ start_mark, "found a tab character that violate indentation");
3530+ /* Consume a space or a tab character. */
3532+ if (!leading_blanks) {
3533+ if (!READ(parser, whitespaces)) goto error;
3541+ if (!CACHE(parser, 2)) goto error;
3543+ /* Check if it is a first line break. */
3545+ if (!leading_blanks)
3547+ CLEAR(parser, whitespaces);
3548+ if (!READ_LINE(parser, leading_break)) goto error;
3549+ leading_blanks = 1;
3553+ if (!READ_LINE(parser, trailing_breaks)) goto error;
3556+ if (!CACHE(parser, 1)) goto error;
3559+ /* Check indentation level. */
3561+ if (!parser->flow_level && (int)parser->mark.column < indent)
3565+ /* Create a token. */
3567+ SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
3568+ YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
3570+ /* Note that we change the 'simple_key_allowed' flag. */
3572+ if (leading_blanks) {
3573+ parser->simple_key_allowed = 1;
3576+ STRING_DEL(parser, leading_break);
3577+ STRING_DEL(parser, trailing_breaks);
3578+ STRING_DEL(parser, whitespaces);
3583+ STRING_DEL(parser, string);
3584+ STRING_DEL(parser, leading_break);
3585+ STRING_DEL(parser, trailing_breaks);
3586+ STRING_DEL(parser, whitespaces);