]> andersk Git - libyaml.git/blame - src/scanner.c
Fix unitialized value crash found by OSS Fuzz
[libyaml.git] / src / scanner.c
CommitLineData
03be97ab
KS
1
2/*
3 * Introduction
4 * ************
5 *
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.
9 *
10 * The process of transforming a YAML stream into a sequence of events is
11 * divided on two steps: Scanning and Parsing.
12 *
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.
16 *
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).
20 *
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.
24 *
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.
27 *
28 * Now, tokens:
29 *
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 # '}'
43 * BLOCK-ENTRY # '-'
44 * FLOW-ENTRY # ','
45 * KEY # '?' or nothing (simple keys).
46 * VALUE # ':'
47 * ALIAS(anchor) # '*anchor'
48 * ANCHOR(anchor) # '&anchor'
49 * TAG(handle,suffix) # '!handle!suffix'
50 * SCALAR(value,style) # A scalar.
51 *
52 * The following two tokens are "virtual" tokens denoting the beginning and the
53 * end of the stream:
54 *
55 * STREAM-START(encoding)
56 * STREAM-END
57 *
58 * We pass the information about the input stream encoding with the
59 * STREAM-START token.
60 *
61 * The next two tokens are responsible for tags:
62 *
63 * VERSION-DIRECTIVE(major,minor)
64 * TAG-DIRECTIVE(handle,prefix)
65 *
66 * Example:
67 *
68 * %YAML 1.1
69 * %TAG ! !foo
70 * %TAG !yaml! tag:yaml.org,2002:
71 * ---
72 *
bdf4d192 73 * The corresponding sequence of tokens:
03be97ab
KS
74 *
75 * STREAM-START(utf-8)
76 * VERSION-DIRECTIVE(1,1)
77 * TAG-DIRECTIVE("!","!foo")
78 * TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
79 * DOCUMENT-START
80 * STREAM-END
81 *
82 * Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
83 * line.
84 *
85 * The document start and end indicators are represented by:
86 *
87 * DOCUMENT-START
88 * DOCUMENT-END
89 *
90 * Note that if a YAML stream contains an implicit document (without '---'
91 * and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
92 * produced.
93 *
94 * In the following examples, we present whole documents together with the
95 * produced tokens.
96 *
97 * 1. An implicit document:
98 *
99 * 'a scalar'
100 *
101 * Tokens:
102 *
103 * STREAM-START(utf-8)
104 * SCALAR("a scalar",single-quoted)
105 * STREAM-END
106 *
107 * 2. An explicit document:
108 *
109 * ---
110 * 'a scalar'
111 * ...
112 *
113 * Tokens:
114 *
115 * STREAM-START(utf-8)
116 * DOCUMENT-START
117 * SCALAR("a scalar",single-quoted)
118 * DOCUMENT-END
119 * STREAM-END
120 *
121 * 3. Several documents in a stream:
122 *
123 * 'a scalar'
124 * ---
125 * 'another scalar'
126 * ---
127 * 'yet another scalar'
128 *
129 * Tokens:
130 *
131 * STREAM-START(utf-8)
132 * SCALAR("a scalar",single-quoted)
133 * DOCUMENT-START
134 * SCALAR("another scalar",single-quoted)
135 * DOCUMENT-START
136 * SCALAR("yet another scalar",single-quoted)
137 * STREAM-END
138 *
139 * We have already introduced the SCALAR token above. The following tokens are
140 * used to describe aliases, anchors, tag, and scalars:
141 *
142 * ALIAS(anchor)
143 * ANCHOR(anchor)
144 * TAG(handle,suffix)
145 * SCALAR(value,style)
146 *
147 * The following series of examples illustrate the usage of these tokens:
148 *
149 * 1. A recursive sequence:
150 *
151 * &A [ *A ]
152 *
153 * Tokens:
154 *
155 * STREAM-START(utf-8)
156 * ANCHOR("A")
157 * FLOW-SEQUENCE-START
158 * ALIAS("A")
159 * FLOW-SEQUENCE-END
160 * STREAM-END
161 *
162 * 2. A tagged scalar:
163 *
164 * !!float "3.14" # A good approximation.
165 *
166 * Tokens:
167 *
168 * STREAM-START(utf-8)
169 * TAG("!!","float")
170 * SCALAR("3.14",double-quoted)
171 * STREAM-END
172 *
173 * 3. Various scalar styles:
174 *
175 * --- # Implicit empty plain scalars do not produce tokens.
176 * --- a plain scalar
177 * --- 'a single-quoted scalar'
178 * --- "a double-quoted scalar"
179 * --- |-
180 * a literal scalar
181 * --- >-
182 * a folded
183 * scalar
184 *
185 * Tokens:
186 *
187 * STREAM-START(utf-8)
188 * DOCUMENT-START
189 * DOCUMENT-START
190 * SCALAR("a plain scalar",plain)
191 * DOCUMENT-START
192 * SCALAR("a single-quoted scalar",single-quoted)
193 * DOCUMENT-START
194 * SCALAR("a double-quoted scalar",double-quoted)
195 * DOCUMENT-START
196 * SCALAR("a literal scalar",literal)
197 * DOCUMENT-START
198 * SCALAR("a folded scalar",folded)
199 * STREAM-END
200 *
201 * Now it's time to review collection-related tokens. We will start with
202 * flow collections:
203 *
204 * FLOW-SEQUENCE-START
205 * FLOW-SEQUENCE-END
206 * FLOW-MAPPING-START
207 * FLOW-MAPPING-END
208 * FLOW-ENTRY
209 * KEY
210 * VALUE
211 *
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.
217 *
218 * The following examples show flow collections:
219 *
220 * 1. A flow sequence:
221 *
222 * [item 1, item 2, item 3]
223 *
224 * Tokens:
225 *
226 * STREAM-START(utf-8)
227 * FLOW-SEQUENCE-START
228 * SCALAR("item 1",plain)
229 * FLOW-ENTRY
230 * SCALAR("item 2",plain)
231 * FLOW-ENTRY
232 * SCALAR("item 3",plain)
233 * FLOW-SEQUENCE-END
234 * STREAM-END
235 *
236 * 2. A flow mapping:
237 *
238 * {
239 * a simple key: a value, # Note that the KEY token is produced.
240 * ? a complex key: another value,
241 * }
242 *
243 * Tokens:
244 *
245 * STREAM-START(utf-8)
246 * FLOW-MAPPING-START
247 * KEY
248 * SCALAR("a simple key",plain)
249 * VALUE
250 * SCALAR("a value",plain)
251 * FLOW-ENTRY
252 * KEY
253 * SCALAR("a complex key",plain)
254 * VALUE
255 * SCALAR("another value",plain)
256 * FLOW-ENTRY
257 * FLOW-MAPPING-END
258 * STREAM-END
259 *
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.
262 *
263 * For scanning block collections, the following tokens are used (note that we
264 * repeat KEY and VALUE here):
265 *
266 * BLOCK-SEQUENCE-START
267 * BLOCK-MAPPING-START
268 * BLOCK-END
269 * BLOCK-ENTRY
270 * KEY
271 * VALUE
272 *
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.
278 *
279 * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
280 * '-', '?', and ':' correspondingly.
281 *
282 * The following examples show how the tokens BLOCK-SEQUENCE-START,
283 * BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
284 *
285 * 1. Block sequences:
286 *
287 * - item 1
288 * - item 2
289 * -
290 * - item 3.1
291 * - item 3.2
292 * -
293 * key 1: value 1
294 * key 2: value 2
295 *
296 * Tokens:
297 *
298 * STREAM-START(utf-8)
299 * BLOCK-SEQUENCE-START
300 * BLOCK-ENTRY
301 * SCALAR("item 1",plain)
302 * BLOCK-ENTRY
303 * SCALAR("item 2",plain)
304 * BLOCK-ENTRY
305 * BLOCK-SEQUENCE-START
306 * BLOCK-ENTRY
307 * SCALAR("item 3.1",plain)
308 * BLOCK-ENTRY
309 * SCALAR("item 3.2",plain)
310 * BLOCK-END
311 * BLOCK-ENTRY
312 * BLOCK-MAPPING-START
313 * KEY
314 * SCALAR("key 1",plain)
315 * VALUE
316 * SCALAR("value 1",plain)
317 * KEY
318 * SCALAR("key 2",plain)
319 * VALUE
320 * SCALAR("value 2",plain)
321 * BLOCK-END
322 * BLOCK-END
323 * STREAM-END
324 *
325 * 2. Block mappings:
326 *
327 * a simple key: a value # The KEY token is produced here.
328 * ? a complex key
329 * : another value
330 * a mapping:
331 * key 1: value 1
332 * key 2: value 2
333 * a sequence:
334 * - item 1
335 * - item 2
336 *
337 * Tokens:
338 *
339 * STREAM-START(utf-8)
340 * BLOCK-MAPPING-START
341 * KEY
342 * SCALAR("a simple key",plain)
343 * VALUE
344 * SCALAR("a value",plain)
345 * KEY
346 * SCALAR("a complex key",plain)
347 * VALUE
348 * SCALAR("another value",plain)
349 * KEY
350 * SCALAR("a mapping",plain)
351 * BLOCK-MAPPING-START
352 * KEY
353 * SCALAR("key 1",plain)
354 * VALUE
355 * SCALAR("value 1",plain)
356 * KEY
357 * SCALAR("key 2",plain)
358 * VALUE
359 * SCALAR("value 2",plain)
360 * BLOCK-END
361 * KEY
362 * SCALAR("a sequence",plain)
363 * VALUE
364 * BLOCK-SEQUENCE-START
365 * BLOCK-ENTRY
366 * SCALAR("item 1",plain)
367 * BLOCK-ENTRY
368 * SCALAR("item 2",plain)
369 * BLOCK-END
370 * BLOCK-END
371 * STREAM-END
372 *
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:
377 *
378 * 1. Collections in a sequence:
379 *
380 * - - item 1
381 * - item 2
382 * - key 1: value 1
383 * key 2: value 2
384 * - ? complex key
385 * : complex value
386 *
387 * Tokens:
388 *
389 * STREAM-START(utf-8)
390 * BLOCK-SEQUENCE-START
391 * BLOCK-ENTRY
392 * BLOCK-SEQUENCE-START
393 * BLOCK-ENTRY
394 * SCALAR("item 1",plain)
395 * BLOCK-ENTRY
396 * SCALAR("item 2",plain)
397 * BLOCK-END
398 * BLOCK-ENTRY
399 * BLOCK-MAPPING-START
400 * KEY
401 * SCALAR("key 1",plain)
402 * VALUE
403 * SCALAR("value 1",plain)
404 * KEY
405 * SCALAR("key 2",plain)
406 * VALUE
407 * SCALAR("value 2",plain)
408 * BLOCK-END
409 * BLOCK-ENTRY
410 * BLOCK-MAPPING-START
411 * KEY
412 * SCALAR("complex key")
413 * VALUE
414 * SCALAR("complex value")
415 * BLOCK-END
416 * BLOCK-END
417 * STREAM-END
418 *
419 * 2. Collections in a mapping:
420 *
421 * ? a sequence
422 * : - item 1
423 * - item 2
424 * ? a mapping
425 * : key 1: value 1
426 * key 2: value 2
427 *
428 * Tokens:
429 *
430 * STREAM-START(utf-8)
431 * BLOCK-MAPPING-START
432 * KEY
433 * SCALAR("a sequence",plain)
434 * VALUE
435 * BLOCK-SEQUENCE-START
436 * BLOCK-ENTRY
437 * SCALAR("item 1",plain)
438 * BLOCK-ENTRY
439 * SCALAR("item 2",plain)
440 * BLOCK-END
441 * KEY
442 * SCALAR("a mapping",plain)
443 * VALUE
444 * BLOCK-MAPPING-START
445 * KEY
446 * SCALAR("key 1",plain)
447 * VALUE
448 * SCALAR("value 1",plain)
449 * KEY
450 * SCALAR("key 2",plain)
451 * VALUE
452 * SCALAR("value 2",plain)
453 * BLOCK-END
454 * BLOCK-END
455 * STREAM-END
456 *
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:
459 *
460 * key:
461 * - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
462 * - item 2
463 *
464 * Tokens:
465 *
466 * STREAM-START(utf-8)
467 * BLOCK-MAPPING-START
468 * KEY
469 * SCALAR("key",plain)
470 * VALUE
471 * BLOCK-ENTRY
472 * SCALAR("item 1",plain)
473 * BLOCK-ENTRY
474 * SCALAR("item 2",plain)
475 * BLOCK-END
476 */
477
625fcfe9 478#include "yaml_private.h"
03be97ab 479
f2b59d4d
KS
480/*
481 * Ensure that the buffer contains the required number of characters.
482 * Return 1 on success, 0 on failure (reader error or memory error).
483 */
484
625fcfe9
KS
485#define CACHE(parser,length) \
486 (parser->unread >= (length) \
487 ? 1 \
f2b59d4d
KS
488 : yaml_parser_update_buffer(parser, (length)))
489
eb9cceb5
KS
490/*
491 * Advance the buffer pointer.
492 */
493
625fcfe9
KS
494#define SKIP(parser) \
495 (parser->mark.index ++, \
496 parser->mark.column ++, \
497 parser->unread --, \
e35af832 498 parser->buffer.pointer += WIDTH(parser->buffer))
e71095e3 499
625fcfe9 500#define SKIP_LINE(parser) \
e35af832 501 (IS_CRLF(parser->buffer) ? \
625fcfe9
KS
502 (parser->mark.index += 2, \
503 parser->mark.column = 0, \
504 parser->mark.line ++, \
505 parser->unread -= 2, \
506 parser->buffer.pointer += 2) : \
e35af832 507 IS_BREAK(parser->buffer) ? \
625fcfe9
KS
508 (parser->mark.index ++, \
509 parser->mark.column = 0, \
510 parser->mark.line ++, \
511 parser->unread --, \
e35af832 512 parser->buffer.pointer += WIDTH(parser->buffer)) : 0)
e71095e3
KS
513
514/*
515 * Copy a character to a string buffer and advance pointers.
516 */
517
625fcfe9
KS
518#define READ(parser,string) \
519 (STRING_EXTEND(parser,string) ? \
e35af832 520 (COPY(string,parser->buffer), \
625fcfe9
KS
521 parser->mark.index ++, \
522 parser->mark.column ++, \
523 parser->unread --, \
524 1) : 0)
92d41fe1
KS
525
526/*
527 * Copy a line break character to a string buffer and advance pointers.
528 */
529
625fcfe9
KS
530#define READ_LINE(parser,string) \
531 (STRING_EXTEND(parser,string) ? \
e35af832
KS
532 (((CHECK_AT(parser->buffer,'\r',0) \
533 && CHECK_AT(parser->buffer,'\n',1)) ? /* CR LF -> LF */ \
92d41fe1 534 (*((string).pointer++) = (yaml_char_t) '\n', \
625fcfe9
KS
535 parser->buffer.pointer += 2, \
536 parser->mark.index += 2, \
537 parser->mark.column = 0, \
538 parser->mark.line ++, \
92d41fe1 539 parser->unread -= 2) : \
e35af832
KS
540 (CHECK_AT(parser->buffer,'\r',0) \
541 || CHECK_AT(parser->buffer,'\n',0)) ? /* CR|LF -> LF */ \
92d41fe1 542 (*((string).pointer++) = (yaml_char_t) '\n', \
625fcfe9
KS
543 parser->buffer.pointer ++, \
544 parser->mark.index ++, \
545 parser->mark.column = 0, \
546 parser->mark.line ++, \
92d41fe1 547 parser->unread --) : \
e35af832
KS
548 (CHECK_AT(parser->buffer,'\xC2',0) \
549 && CHECK_AT(parser->buffer,'\x85',1)) ? /* NEL -> LF */ \
92d41fe1 550 (*((string).pointer++) = (yaml_char_t) '\n', \
625fcfe9
KS
551 parser->buffer.pointer += 2, \
552 parser->mark.index ++, \
553 parser->mark.column = 0, \
554 parser->mark.line ++, \
92d41fe1 555 parser->unread --) : \
e35af832
KS
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 */ \
625fcfe9
KS
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), \
567 1) : 0)
92d41fe1 568
625fcfe9
KS
569/*
570 * Public API declarations.
571 */
e71095e3 572
625fcfe9
KS
573YAML_DECLARE(int)
574yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
92d41fe1 575
625fcfe9
KS
576/*
577 * Error handling.
578 */
92d41fe1 579
e71095e3 580static int
625fcfe9
KS
581yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
582 yaml_mark_t context_mark, const char *problem);
e71095e3 583
03be97ab
KS
584/*
585 * High-level token API.
586 */
587
625fcfe9 588YAML_DECLARE(int)
03be97ab
KS
589yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
590
591static int
592yaml_parser_fetch_next_token(yaml_parser_t *parser);
593
594/*
595 * Potential simple keys.
596 */
597
598static int
599yaml_parser_stale_simple_keys(yaml_parser_t *parser);
600
601static int
602yaml_parser_save_simple_key(yaml_parser_t *parser);
603
604static int
605yaml_parser_remove_simple_key(yaml_parser_t *parser);
606
eb9cceb5
KS
607static int
608yaml_parser_increase_flow_level(yaml_parser_t *parser);
609
610static int
611yaml_parser_decrease_flow_level(yaml_parser_t *parser);
612
03be97ab
KS
613/*
614 * Indentation treatment.
615 */
616
617static int
c201bf64
KS
618yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column,
619 ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark);
03be97ab
KS
620
621static int
c201bf64 622yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column);
03be97ab
KS
623
624/*
625 * Token fetchers.
626 */
627
628static int
629yaml_parser_fetch_stream_start(yaml_parser_t *parser);
630
631static int
632yaml_parser_fetch_stream_end(yaml_parser_t *parser);
633
634static int
635yaml_parser_fetch_directive(yaml_parser_t *parser);
636
03be97ab
KS
637static int
638yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
639 yaml_token_type_t type);
640
03be97ab
KS
641static int
642yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
643 yaml_token_type_t type);
644
03be97ab
KS
645static int
646yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
647 yaml_token_type_t type);
648
649static int
650yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
651
652static int
653yaml_parser_fetch_block_entry(yaml_parser_t *parser);
654
655static int
656yaml_parser_fetch_key(yaml_parser_t *parser);
657
658static int
659yaml_parser_fetch_value(yaml_parser_t *parser);
660
661static int
eb9cceb5 662yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
03be97ab
KS
663
664static int
665yaml_parser_fetch_tag(yaml_parser_t *parser);
666
03be97ab
KS
667static int
668yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
669
03be97ab
KS
670static int
671yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
672
673static int
674yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
675
676/*
677 * Token scanners.
678 */
679
680static int
681yaml_parser_scan_to_next_token(yaml_parser_t *parser);
682
625fcfe9
KS
683static int
684yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
03be97ab
KS
685
686static int
687yaml_parser_scan_directive_name(yaml_parser_t *parser,
688 yaml_mark_t start_mark, yaml_char_t **name);
689
690static int
e71095e3 691yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
03be97ab
KS
692 yaml_mark_t start_mark, int *major, int *minor);
693
694static int
e71095e3 695yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
03be97ab
KS
696 yaml_mark_t start_mark, int *number);
697
698static int
699yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
e71095e3 700 yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
03be97ab 701
625fcfe9
KS
702static int
703yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
03be97ab
KS
704 yaml_token_type_t type);
705
625fcfe9
KS
706static int
707yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
03be97ab
KS
708
709static int
710yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
711 yaml_mark_t start_mark, yaml_char_t **handle);
712
713static int
714yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
e71095e3
KS
715 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
716
717static int
718yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
719 yaml_mark_t start_mark, yaml_string_t *string);
03be97ab 720
625fcfe9
KS
721static int
722yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
723 int literal);
03be97ab 724
92d41fe1
KS
725static int
726yaml_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);
729
625fcfe9
KS
730static int
731yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
732 int single);
03be97ab 733
625fcfe9
KS
734static int
735yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
03be97ab 736
f2b59d4d 737/*
625fcfe9 738 * Get the next token.
f2b59d4d
KS
739 */
740
625fcfe9
KS
741YAML_DECLARE(int)
742yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
f2b59d4d 743{
f2b59d4d 744 assert(parser); /* Non-NULL parser object is expected. */
625fcfe9 745 assert(token); /* Non-NULL token object is expected. */
f2b59d4d 746
5a00d8fe
KS
747 /* Erase the token object. */
748
749 memset(token, 0, sizeof(yaml_token_t));
750
625fcfe9 751 /* No tokens after STREAM-END or error. */
f2b59d4d 752
625fcfe9 753 if (parser->stream_end_produced || parser->error) {
625fcfe9 754 return 1;
7e32c194
KS
755 }
756
f2b59d4d
KS
757 /* Ensure that the tokens queue contains enough tokens. */
758
625fcfe9
KS
759 if (!parser->token_available) {
760 if (!yaml_parser_fetch_more_tokens(parser))
761 return 0;
92d41fe1
KS
762 }
763
625fcfe9 764 /* Fetch the next token from the queue. */
986dbde7 765
625fcfe9
KS
766 *token = DEQUEUE(parser, parser->tokens);
767 parser->token_available = 0;
768 parser->tokens_parsed ++;
e71095e3 769
625fcfe9
KS
770 if (token->type == YAML_STREAM_END_TOKEN) {
771 parser->stream_end_produced = 1;
e71095e3
KS
772 }
773
e71095e3
KS
774 return 1;
775}
776
f2b59d4d
KS
777/*
778 * Set the scanner error and return 0.
779 */
780
781static int
782yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
783 yaml_mark_t context_mark, const char *problem)
784{
785 parser->error = YAML_SCANNER_ERROR;
786 parser->context = context;
787 parser->context_mark = context_mark;
788 parser->problem = problem;
625fcfe9 789 parser->problem_mark = parser->mark;
7e32c194
KS
790
791 return 0;
f2b59d4d
KS
792}
793
f2b59d4d
KS
794/*
795 * Ensure that the tokens queue contains at least one token which can be
796 * returned to the Parser.
797 */
798
625fcfe9 799YAML_DECLARE(int)
f2b59d4d
KS
800yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
801{
802 int need_more_tokens;
f2b59d4d
KS
803
804 /* While we need more tokens to fetch, do it. */
805
806 while (1)
807 {
808 /*
809 * Check if we really need to fetch more tokens.
810 */
811
812 need_more_tokens = 0;
813
625fcfe9 814 if (parser->tokens.head == parser->tokens.tail)
f2b59d4d
KS
815 {
816 /* Queue is empty. */
817
818 need_more_tokens = 1;
819 }
820 else
821 {
625fcfe9
KS
822 yaml_simple_key_t *simple_key;
823
f2b59d4d
KS
824 /* Check if any potential simple key may occupy the head position. */
825
7e32c194
KS
826 if (!yaml_parser_stale_simple_keys(parser))
827 return 0;
828
625fcfe9
KS
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) {
f2b59d4d
KS
833 need_more_tokens = 1;
834 break;
835 }
836 }
837 }
838
839 /* We are finished. */
840
841 if (!need_more_tokens)
842 break;
843
844 /* Fetch the next token. */
845
846 if (!yaml_parser_fetch_next_token(parser))
847 return 0;
848 }
849
625fcfe9
KS
850 parser->token_available = 1;
851
f2b59d4d
KS
852 return 1;
853}
854
855/*
856 * The dispatcher for token fetchers.
857 */
858
859static int
860yaml_parser_fetch_next_token(yaml_parser_t *parser)
861{
862 /* Ensure that the buffer is initialized. */
863
625fcfe9 864 if (!CACHE(parser, 1))
f2b59d4d
KS
865 return 0;
866
867 /* Check if we just started scanning. Fetch STREAM-START then. */
868
869 if (!parser->stream_start_produced)
870 return yaml_parser_fetch_stream_start(parser);
871
872 /* Eat whitespaces and comments until we reach the next token. */
873
874 if (!yaml_parser_scan_to_next_token(parser))
875 return 0;
876
7e32c194
KS
877 /* Remove obsolete potential simple keys. */
878
879 if (!yaml_parser_stale_simple_keys(parser))
880 return 0;
881
f2b59d4d
KS
882 /* Check the indentation level against the current column. */
883
625fcfe9 884 if (!yaml_parser_unroll_indent(parser, parser->mark.column))
f2b59d4d
KS
885 return 0;
886
887 /*
888 * Ensure that the buffer contains at least 4 characters. 4 is the length
889 * of the longest indicators ('--- ' and '... ').
890 */
891
625fcfe9 892 if (!CACHE(parser, 4))
f2b59d4d
KS
893 return 0;
894
895 /* Is it the end of the stream? */
896
e35af832 897 if (IS_Z(parser->buffer))
f2b59d4d
KS
898 return yaml_parser_fetch_stream_end(parser);
899
900 /* Is it a directive? */
901
e35af832 902 if (parser->mark.column == 0 && CHECK(parser->buffer, '%'))
f2b59d4d
KS
903 return yaml_parser_fetch_directive(parser);
904
905 /* Is it the document start indicator? */
906
625fcfe9 907 if (parser->mark.column == 0
e35af832
KS
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))
eb9cceb5
KS
912 return yaml_parser_fetch_document_indicator(parser,
913 YAML_DOCUMENT_START_TOKEN);
f2b59d4d
KS
914
915 /* Is it the document end indicator? */
916
625fcfe9 917 if (parser->mark.column == 0
e35af832
KS
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))
eb9cceb5
KS
922 return yaml_parser_fetch_document_indicator(parser,
923 YAML_DOCUMENT_END_TOKEN);
f2b59d4d
KS
924
925 /* Is it the flow sequence start indicator? */
926
e35af832 927 if (CHECK(parser->buffer, '['))
eb9cceb5
KS
928 return yaml_parser_fetch_flow_collection_start(parser,
929 YAML_FLOW_SEQUENCE_START_TOKEN);
f2b59d4d
KS
930
931 /* Is it the flow mapping start indicator? */
932
e35af832 933 if (CHECK(parser->buffer, '{'))
eb9cceb5
KS
934 return yaml_parser_fetch_flow_collection_start(parser,
935 YAML_FLOW_MAPPING_START_TOKEN);
f2b59d4d
KS
936
937 /* Is it the flow sequence end indicator? */
938
e35af832 939 if (CHECK(parser->buffer, ']'))
eb9cceb5
KS
940 return yaml_parser_fetch_flow_collection_end(parser,
941 YAML_FLOW_SEQUENCE_END_TOKEN);
f2b59d4d
KS
942
943 /* Is it the flow mapping end indicator? */
944
e35af832 945 if (CHECK(parser->buffer, '}'))
eb9cceb5
KS
946 return yaml_parser_fetch_flow_collection_end(parser,
947 YAML_FLOW_MAPPING_END_TOKEN);
f2b59d4d
KS
948
949 /* Is it the flow entry indicator? */
950
e35af832 951 if (CHECK(parser->buffer, ','))
f2b59d4d
KS
952 return yaml_parser_fetch_flow_entry(parser);
953
954 /* Is it the block entry indicator? */
955
e35af832 956 if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1))
f2b59d4d
KS
957 return yaml_parser_fetch_block_entry(parser);
958
959 /* Is it the key indicator? */
960
e35af832
KS
961 if (CHECK(parser->buffer, '?')
962 && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
f2b59d4d
KS
963 return yaml_parser_fetch_key(parser);
964
965 /* Is it the value indicator? */
966
e35af832
KS
967 if (CHECK(parser->buffer, ':')
968 && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
f2b59d4d
KS
969 return yaml_parser_fetch_value(parser);
970
971 /* Is it an alias? */
972
e35af832 973 if (CHECK(parser->buffer, '*'))
eb9cceb5 974 return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
f2b59d4d
KS
975
976 /* Is it an anchor? */
977
e35af832 978 if (CHECK(parser->buffer, '&'))
eb9cceb5 979 return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
f2b59d4d
KS
980
981 /* Is it a tag? */
982
e35af832 983 if (CHECK(parser->buffer, '!'))
f2b59d4d
KS
984 return yaml_parser_fetch_tag(parser);
985
986 /* Is it a literal scalar? */
987
e35af832 988 if (CHECK(parser->buffer, '|') && !parser->flow_level)
f2b59d4d
KS
989 return yaml_parser_fetch_block_scalar(parser, 1);
990
991 /* Is it a folded scalar? */
992
e35af832 993 if (CHECK(parser->buffer, '>') && !parser->flow_level)
f2b59d4d
KS
994 return yaml_parser_fetch_block_scalar(parser, 0);
995
996 /* Is it a single-quoted scalar? */
997
e35af832 998 if (CHECK(parser->buffer, '\''))
f2b59d4d
KS
999 return yaml_parser_fetch_flow_scalar(parser, 1);
1000
1001 /* Is it a double-quoted scalar? */
1002
e35af832 1003 if (CHECK(parser->buffer, '"'))
f2b59d4d
KS
1004 return yaml_parser_fetch_flow_scalar(parser, 0);
1005
1006 /*
1007 * Is it a plain scalar?
1008 *
1009 * A plain scalar may start with any non-blank characters except
1010 *
1011 * '-', '?', ':', ',', '[', ']', '{', '}',
1012 * '#', '&', '*', '!', '|', '>', '\'', '\"',
1013 * '%', '@', '`'.
1014 *
7e32c194
KS
1015 * In the block context (and, for the '-' indicator, in the flow context
1016 * too), it may also start with the characters
f2b59d4d
KS
1017 *
1018 * '-', '?', ':'
1019 *
1020 * if it is followed by a non-space character.
1021 *
1022 * The last rule is more restrictive than the specification requires.
1023 */
1024
e35af832
KS
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)) ||
f2b59d4d 1036 (!parser->flow_level &&
e35af832
KS
1037 (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':'))
1038 && !IS_BLANKZ_AT(parser->buffer, 1)))
f2b59d4d
KS
1039 return yaml_parser_fetch_plain_scalar(parser);
1040
1041 /*
1042 * If we don't determine the token type so far, it is an error.
1043 */
1044
625fcfe9
KS
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");
f2b59d4d
KS
1048}
1049
eb9cceb5
KS
1050/*
1051 * Check the list of potential simple keys and remove the positions that
1052 * cannot contain simple keys anymore.
1053 */
1054
1055static int
1056yaml_parser_stale_simple_keys(yaml_parser_t *parser)
1057{
625fcfe9 1058 yaml_simple_key_t *simple_key;
eb9cceb5
KS
1059
1060 /* Check for a potential simple key for each flow level. */
1061
625fcfe9
KS
1062 for (simple_key = parser->simple_keys.start;
1063 simple_key != parser->simple_keys.top; simple_key ++)
eb9cceb5 1064 {
eb9cceb5
KS
1065 /*
1066 * The specification requires that a simple key
1067 *
1068 * - is limited to a single line,
1069 * - is shorter than 1024 characters.
1070 */
1071
625fcfe9
KS
1072 if (simple_key->possible
1073 && (simple_key->mark.line < parser->mark.line
1074 || simple_key->mark.index+1024 < parser->mark.index)) {
eb9cceb5
KS
1075
1076 /* Check if the potential simple key to be removed is required. */
1077
1078 if (simple_key->required) {
1079 return yaml_parser_set_scanner_error(parser,
1080 "while scanning a simple key", simple_key->mark,
6be8109b 1081 "could not find expected ':'");
eb9cceb5
KS
1082 }
1083
625fcfe9 1084 simple_key->possible = 0;
eb9cceb5
KS
1085 }
1086 }
1087
1088 return 1;
1089}
1090
1091/*
1092 * Check if a simple key may start at the current position and add it if
1093 * needed.
1094 */
1095
1096static int
1097yaml_parser_save_simple_key(yaml_parser_t *parser)
1098{
1099 /*
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
1102 * level.
1103 */
1104
625fcfe9 1105 int required = (!parser->flow_level
c201bf64 1106 && parser->indent == (ptrdiff_t)parser->mark.column);
eb9cceb5 1107
eb9cceb5
KS
1108 /*
1109 * If the current position may start a simple key, save it.
1110 */
1111
1112 if (parser->simple_key_allowed)
1113 {
252c575a
KS
1114 yaml_simple_key_t simple_key;
1115 simple_key.possible = 1;
1116 simple_key.required = required;
986dbde7 1117 simple_key.token_number =
3b160b60 1118 parser->tokens_parsed + (parser->tokens.tail - parser->tokens.head);
0174ed6e 1119 simple_key.mark = parser->mark;
eb9cceb5
KS
1120
1121 if (!yaml_parser_remove_simple_key(parser)) return 0;
1122
625fcfe9 1123 *(parser->simple_keys.top-1) = simple_key;
eb9cceb5
KS
1124 }
1125
1126 return 1;
1127}
1128
1129/*
1130 * Remove a potential simple key at the current flow level.
1131 */
1132
1133static int
1134yaml_parser_remove_simple_key(yaml_parser_t *parser)
1135{
625fcfe9 1136 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
eb9cceb5 1137
625fcfe9 1138 if (simple_key->possible)
eb9cceb5
KS
1139 {
1140 /* If the key is required, it is an error. */
1141
1142 if (simple_key->required) {
1143 return yaml_parser_set_scanner_error(parser,
1144 "while scanning a simple key", simple_key->mark,
6be8109b 1145 "could not find expected ':'");
eb9cceb5 1146 }
625fcfe9 1147 }
eb9cceb5 1148
625fcfe9 1149 /* Remove the key from the stack. */
eb9cceb5 1150
625fcfe9 1151 simple_key->possible = 0;
eb9cceb5
KS
1152
1153 return 1;
1154}
1155
1156/*
1157 * Increase the flow level and resize the simple key list if needed.
1158 */
1159
1160static int
1161yaml_parser_increase_flow_level(yaml_parser_t *parser)
1162{
625fcfe9 1163 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
eb9cceb5 1164
625fcfe9 1165 /* Reset the simple key on the next level. */
eb9cceb5 1166
625fcfe9
KS
1167 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1168 return 0;
1169
1170 /* Increase the flow level. */
eb9cceb5 1171
1ef11717
KS
1172 if (parser->flow_level == INT_MAX) {
1173 parser->error = YAML_MEMORY_ERROR;
c201bf64 1174 return 0;
1ef11717 1175 }
c201bf64 1176
625fcfe9 1177 parser->flow_level++;
eb9cceb5
KS
1178
1179 return 1;
1180}
1181
1182/*
1183 * Decrease the flow level.
1184 */
1185
1186static int
1187yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1188{
c9b74def
KS
1189 yaml_simple_key_t dummy_key; /* Used to eliminate a compiler warning. */
1190
625fcfe9
KS
1191 if (parser->flow_level) {
1192 parser->flow_level --;
c9b74def 1193 dummy_key = POP(parser, parser->simple_keys);
eb9cceb5
KS
1194 }
1195
eb9cceb5
KS
1196 return 1;
1197}
1198
1199/*
1200 * Push the current indentation level to the stack and set the new level
1201 * the current column is greater than the indentation level. In this case,
1202 * append or insert the specified token into the token queue.
986dbde7 1203 *
eb9cceb5
KS
1204 */
1205
1206static int
c201bf64
KS
1207yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column,
1208 ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark)
eb9cceb5 1209{
625fcfe9 1210 yaml_token_t token;
eb9cceb5
KS
1211
1212 /* In the flow context, do nothing. */
1213
1214 if (parser->flow_level)
1215 return 1;
1216
1217 if (parser->indent < column)
1218 {
eb9cceb5
KS
1219 /*
1220 * Push the current indentation level to the stack and set the new
1221 * indentation level.
1222 */
1223
625fcfe9
KS
1224 if (!PUSH(parser, parser->indents, parser->indent))
1225 return 0;
1226
1ef11717
KS
1227 if (column > INT_MAX) {
1228 parser->error = YAML_MEMORY_ERROR;
c201bf64 1229 return 0;
1ef11717 1230 }
c201bf64 1231
eb9cceb5
KS
1232 parser->indent = column;
1233
625fcfe9 1234 /* Create a token and insert it into the queue. */
eb9cceb5 1235
625fcfe9 1236 TOKEN_INIT(token, type, mark, mark);
eb9cceb5 1237
625fcfe9
KS
1238 if (number == -1) {
1239 if (!ENQUEUE(parser, parser->tokens, token))
1240 return 0;
1241 }
1242 else {
1243 if (!QUEUE_INSERT(parser,
1244 parser->tokens, number - parser->tokens_parsed, token))
1245 return 0;
eb9cceb5
KS
1246 }
1247 }
1248
1249 return 1;
1250}
1251
1252/*
1253 * Pop indentation levels from the indents stack until the current level
bdf4d192 1254 * becomes less or equal to the column. For each indentation level, append
eb9cceb5
KS
1255 * the BLOCK-END token.
1256 */
1257
1258
1259static int
c201bf64 1260yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column)
eb9cceb5 1261{
625fcfe9 1262 yaml_token_t token;
eb9cceb5
KS
1263
1264 /* In the flow context, do nothing. */
1265
1266 if (parser->flow_level)
1267 return 1;
1268
bdf4d192 1269 /* Loop through the indentation levels in the stack. */
eb9cceb5
KS
1270
1271 while (parser->indent > column)
1272 {
625fcfe9 1273 /* Create a token and append it to the queue. */
eb9cceb5 1274
625fcfe9 1275 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
eb9cceb5 1276
625fcfe9 1277 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1278 return 0;
eb9cceb5
KS
1279
1280 /* Pop the indentation level. */
1281
625fcfe9 1282 parser->indent = POP(parser, parser->indents);
eb9cceb5
KS
1283 }
1284
1285 return 1;
1286}
1287
1288/*
1289 * Initialize the scanner and produce the STREAM-START token.
1290 */
1291
1292static int
1293yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1294{
625fcfe9
KS
1295 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1296 yaml_token_t token;
eb9cceb5
KS
1297
1298 /* Set the initial indentation. */
1299
1300 parser->indent = -1;
1301
625fcfe9
KS
1302 /* Initialize the simple key stack. */
1303
1304 if (!PUSH(parser, parser->simple_keys, simple_key))
1305 return 0;
1306
eb9cceb5
KS
1307 /* A simple key is allowed at the beginning of the stream. */
1308
1309 parser->simple_key_allowed = 1;
1310
1311 /* We have started. */
1312
1313 parser->stream_start_produced = 1;
1314
625fcfe9 1315 /* Create the STREAM-START token and append it to the queue. */
eb9cceb5 1316
625fcfe9
KS
1317 STREAM_START_TOKEN_INIT(token, parser->encoding,
1318 parser->mark, parser->mark);
eb9cceb5 1319
625fcfe9 1320 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1321 return 0;
eb9cceb5
KS
1322
1323 return 1;
1324}
1325
1326/*
1327 * Produce the STREAM-END token and shut down the scanner.
1328 */
1329
1330static int
1331yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1332{
625fcfe9 1333 yaml_token_t token;
eb9cceb5 1334
c83b67a6
KS
1335 /* Force new line. */
1336
1337 if (parser->mark.column != 0) {
1338 parser->mark.column = 0;
1339 parser->mark.line ++;
1340 }
1341
eb9cceb5
KS
1342 /* Reset the indentation level. */
1343
1344 if (!yaml_parser_unroll_indent(parser, -1))
1345 return 0;
1346
7e32c194 1347 /* Reset simple keys. */
eb9cceb5 1348
7e32c194
KS
1349 if (!yaml_parser_remove_simple_key(parser))
1350 return 0;
1351
1352 parser->simple_key_allowed = 0;
eb9cceb5 1353
625fcfe9 1354 /* Create the STREAM-END token and append it to the queue. */
eb9cceb5 1355
625fcfe9 1356 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
eb9cceb5 1357
625fcfe9 1358 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1359 return 0;
eb9cceb5
KS
1360
1361 return 1;
1362}
1363
1364/*
625fcfe9 1365 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
eb9cceb5
KS
1366 */
1367
1368static int
1369yaml_parser_fetch_directive(yaml_parser_t *parser)
1370{
625fcfe9 1371 yaml_token_t token;
eb9cceb5
KS
1372
1373 /* Reset the indentation level. */
1374
1375 if (!yaml_parser_unroll_indent(parser, -1))
1376 return 0;
1377
1378 /* Reset simple keys. */
1379
1380 if (!yaml_parser_remove_simple_key(parser))
1381 return 0;
1382
1383 parser->simple_key_allowed = 0;
1384
1385 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1386
625fcfe9
KS
1387 if (!yaml_parser_scan_directive(parser, &token))
1388 return 0;
eb9cceb5
KS
1389
1390 /* Append the token to the queue. */
1391
625fcfe9
KS
1392 if (!ENQUEUE(parser, parser->tokens, token)) {
1393 yaml_token_delete(&token);
eb9cceb5
KS
1394 return 0;
1395 }
1396
1397 return 1;
1398}
1399
1400/*
1401 * Produce the DOCUMENT-START or DOCUMENT-END token.
1402 */
1403
1404static int
1405yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1406 yaml_token_type_t type)
1407{
1408 yaml_mark_t start_mark, end_mark;
625fcfe9 1409 yaml_token_t token;
eb9cceb5
KS
1410
1411 /* Reset the indentation level. */
1412
1413 if (!yaml_parser_unroll_indent(parser, -1))
1414 return 0;
1415
1416 /* Reset simple keys. */
1417
1418 if (!yaml_parser_remove_simple_key(parser))
1419 return 0;
1420
1421 parser->simple_key_allowed = 0;
1422
1423 /* Consume the token. */
1424
625fcfe9 1425 start_mark = parser->mark;
eb9cceb5 1426
625fcfe9
KS
1427 SKIP(parser);
1428 SKIP(parser);
1429 SKIP(parser);
eb9cceb5 1430
625fcfe9 1431 end_mark = parser->mark;
eb9cceb5
KS
1432
1433 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1434
625fcfe9 1435 TOKEN_INIT(token, type, start_mark, end_mark);
eb9cceb5
KS
1436
1437 /* Append the token to the queue. */
1438
625fcfe9 1439 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1440 return 0;
eb9cceb5
KS
1441
1442 return 1;
1443}
1444
1445/*
1446 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1447 */
1448
1449static int
1450yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1451 yaml_token_type_t type)
1452{
1453 yaml_mark_t start_mark, end_mark;
625fcfe9 1454 yaml_token_t token;
eb9cceb5
KS
1455
1456 /* The indicators '[' and '{' may start a simple key. */
1457
1458 if (!yaml_parser_save_simple_key(parser))
1459 return 0;
1460
1461 /* Increase the flow level. */
1462
1463 if (!yaml_parser_increase_flow_level(parser))
1464 return 0;
1465
1466 /* A simple key may follow the indicators '[' and '{'. */
1467
1468 parser->simple_key_allowed = 1;
1469
1470 /* Consume the token. */
1471
625fcfe9
KS
1472 start_mark = parser->mark;
1473 SKIP(parser);
1474 end_mark = parser->mark;
eb9cceb5
KS
1475
1476 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1477
625fcfe9 1478 TOKEN_INIT(token, type, start_mark, end_mark);
eb9cceb5
KS
1479
1480 /* Append the token to the queue. */
1481
625fcfe9 1482 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1483 return 0;
eb9cceb5
KS
1484
1485 return 1;
1486}
1487
1488/*
1489 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1490 */
1491
1492static int
1493yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1494 yaml_token_type_t type)
1495{
1496 yaml_mark_t start_mark, end_mark;
625fcfe9 1497 yaml_token_t token;
eb9cceb5
KS
1498
1499 /* Reset any potential simple key on the current flow level. */
1500
1501 if (!yaml_parser_remove_simple_key(parser))
1502 return 0;
1503
1504 /* Decrease the flow level. */
1505
1506 if (!yaml_parser_decrease_flow_level(parser))
1507 return 0;
1508
1509 /* No simple keys after the indicators ']' and '}'. */
1510
1511 parser->simple_key_allowed = 0;
1512
1513 /* Consume the token. */
1514
625fcfe9
KS
1515 start_mark = parser->mark;
1516 SKIP(parser);
1517 end_mark = parser->mark;
eb9cceb5
KS
1518
1519 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1520
625fcfe9 1521 TOKEN_INIT(token, type, start_mark, end_mark);
eb9cceb5
KS
1522
1523 /* Append the token to the queue. */
1524
625fcfe9 1525 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1526 return 0;
eb9cceb5
KS
1527
1528 return 1;
1529}
1530
1531/*
1532 * Produce the FLOW-ENTRY token.
1533 */
1534
1535static int
1536yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1537{
1538 yaml_mark_t start_mark, end_mark;
625fcfe9 1539 yaml_token_t token;
eb9cceb5
KS
1540
1541 /* Reset any potential simple keys on the current flow level. */
1542
1543 if (!yaml_parser_remove_simple_key(parser))
1544 return 0;
1545
1546 /* Simple keys are allowed after ','. */
1547
1548 parser->simple_key_allowed = 1;
1549
1550 /* Consume the token. */
1551
625fcfe9
KS
1552 start_mark = parser->mark;
1553 SKIP(parser);
1554 end_mark = parser->mark;
eb9cceb5 1555
625fcfe9 1556 /* Create the FLOW-ENTRY token and append it to the queue. */
eb9cceb5 1557
625fcfe9 1558 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
eb9cceb5 1559
625fcfe9 1560 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1561 return 0;
eb9cceb5
KS
1562
1563 return 1;
1564}
1565
1566/*
1567 * Produce the BLOCK-ENTRY token.
1568 */
1569
1570static int
1571yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1572{
1573 yaml_mark_t start_mark, end_mark;
625fcfe9 1574 yaml_token_t token;
eb9cceb5
KS
1575
1576 /* Check if the scanner is in the block context. */
1577
1578 if (!parser->flow_level)
1579 {
1580 /* Check if we are allowed to start a new entry. */
1581
1582 if (!parser->simple_key_allowed) {
625fcfe9 1583 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
eb9cceb5
KS
1584 "block sequence entries are not allowed in this context");
1585 }
1586
1587 /* Add the BLOCK-SEQUENCE-START token if needed. */
1588
625fcfe9
KS
1589 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1590 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
eb9cceb5
KS
1591 return 0;
1592 }
1593 else
1594 {
1595 /*
1596 * It is an error for the '-' indicator to occur in the flow context,
1597 * but we let the Parser detect and report about it because the Parser
1598 * is able to point to the context.
1599 */
1600 }
1601
1602 /* Reset any potential simple keys on the current flow level. */
1603
1604 if (!yaml_parser_remove_simple_key(parser))
1605 return 0;
1606
1607 /* Simple keys are allowed after '-'. */
1608
1609 parser->simple_key_allowed = 1;
1610
1611 /* Consume the token. */
1612
625fcfe9
KS
1613 start_mark = parser->mark;
1614 SKIP(parser);
1615 end_mark = parser->mark;
eb9cceb5 1616
625fcfe9 1617 /* Create the BLOCK-ENTRY token and append it to the queue. */
eb9cceb5 1618
625fcfe9 1619 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
eb9cceb5 1620
625fcfe9 1621 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1622 return 0;
eb9cceb5
KS
1623
1624 return 1;
1625}
1626
1627/*
1628 * Produce the KEY token.
1629 */
1630
1631static int
1632yaml_parser_fetch_key(yaml_parser_t *parser)
1633{
1634 yaml_mark_t start_mark, end_mark;
625fcfe9 1635 yaml_token_t token;
eb9cceb5
KS
1636
1637 /* In the block context, additional checks are required. */
1638
1639 if (!parser->flow_level)
1640 {
1641 /* Check if we are allowed to start a new key (not nessesary simple). */
1642
1643 if (!parser->simple_key_allowed) {
625fcfe9 1644 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
eb9cceb5
KS
1645 "mapping keys are not allowed in this context");
1646 }
1647
1648 /* Add the BLOCK-MAPPING-START token if needed. */
1649
625fcfe9
KS
1650 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1651 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
eb9cceb5
KS
1652 return 0;
1653 }
1654
1655 /* Reset any potential simple keys on the current flow level. */
1656
1657 if (!yaml_parser_remove_simple_key(parser))
1658 return 0;
1659
1660 /* Simple keys are allowed after '?' in the block context. */
1661
1662 parser->simple_key_allowed = (!parser->flow_level);
1663
1664 /* Consume the token. */
1665
625fcfe9
KS
1666 start_mark = parser->mark;
1667 SKIP(parser);
1668 end_mark = parser->mark;
eb9cceb5 1669
625fcfe9 1670 /* Create the KEY token and append it to the queue. */
eb9cceb5 1671
625fcfe9 1672 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
eb9cceb5 1673
625fcfe9 1674 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1675 return 0;
eb9cceb5
KS
1676
1677 return 1;
1678}
1679
1680/*
1681 * Produce the VALUE token.
1682 */
1683
1684static int
1685yaml_parser_fetch_value(yaml_parser_t *parser)
1686{
1687 yaml_mark_t start_mark, end_mark;
625fcfe9
KS
1688 yaml_token_t token;
1689 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
eb9cceb5
KS
1690
1691 /* Have we found a simple key? */
1692
625fcfe9 1693 if (simple_key->possible)
eb9cceb5 1694 {
eb9cceb5 1695
625fcfe9 1696 /* Create the KEY token and insert it into the queue. */
eb9cceb5 1697
625fcfe9 1698 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
eb9cceb5 1699
625fcfe9
KS
1700 if (!QUEUE_INSERT(parser, parser->tokens,
1701 simple_key->token_number - parser->tokens_parsed, token))
eb9cceb5 1702 return 0;
eb9cceb5
KS
1703
1704 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1705
625fcfe9 1706 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
eb9cceb5
KS
1707 simple_key->token_number,
1708 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1709 return 0;
1710
625fcfe9 1711 /* Remove the simple key. */
eb9cceb5 1712
625fcfe9 1713 simple_key->possible = 0;
eb9cceb5
KS
1714
1715 /* A simple key cannot follow another simple key. */
1716
1717 parser->simple_key_allowed = 0;
1718 }
1719 else
1720 {
1721 /* The ':' indicator follows a complex key. */
1722
1723 /* In the block context, extra checks are required. */
1724
1725 if (!parser->flow_level)
1726 {
1727 /* Check if we are allowed to start a complex value. */
1728
1729 if (!parser->simple_key_allowed) {
625fcfe9 1730 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
eb9cceb5
KS
1731 "mapping values are not allowed in this context");
1732 }
1733
1734 /* Add the BLOCK-MAPPING-START token if needed. */
1735
625fcfe9
KS
1736 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1737 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
eb9cceb5
KS
1738 return 0;
1739 }
1740
eb9cceb5
KS
1741 /* Simple keys after ':' are allowed in the block context. */
1742
1743 parser->simple_key_allowed = (!parser->flow_level);
1744 }
1745
1746 /* Consume the token. */
1747
625fcfe9
KS
1748 start_mark = parser->mark;
1749 SKIP(parser);
1750 end_mark = parser->mark;
eb9cceb5 1751
625fcfe9 1752 /* Create the VALUE token and append it to the queue. */
eb9cceb5 1753
625fcfe9 1754 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
eb9cceb5 1755
625fcfe9 1756 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1757 return 0;
eb9cceb5
KS
1758
1759 return 1;
1760}
1761
1762/*
1763 * Produce the ALIAS or ANCHOR token.
1764 */
1765
1766static int
1767yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1768{
625fcfe9 1769 yaml_token_t token;
eb9cceb5
KS
1770
1771 /* An anchor or an alias could be a simple key. */
1772
1773 if (!yaml_parser_save_simple_key(parser))
1774 return 0;
1775
1776 /* A simple key cannot follow an anchor or an alias. */
1777
1778 parser->simple_key_allowed = 0;
1779
625fcfe9 1780 /* Create the ALIAS or ANCHOR token and append it to the queue. */
eb9cceb5 1781
625fcfe9
KS
1782 if (!yaml_parser_scan_anchor(parser, &token, type))
1783 return 0;
eb9cceb5 1784
625fcfe9
KS
1785 if (!ENQUEUE(parser, parser->tokens, token)) {
1786 yaml_token_delete(&token);
eb9cceb5
KS
1787 return 0;
1788 }
eb9cceb5
KS
1789 return 1;
1790}
1791
1792/*
1793 * Produce the TAG token.
1794 */
1795
1796static int
1797yaml_parser_fetch_tag(yaml_parser_t *parser)
1798{
625fcfe9 1799 yaml_token_t token;
eb9cceb5
KS
1800
1801 /* A tag could be a simple key. */
1802
1803 if (!yaml_parser_save_simple_key(parser))
1804 return 0;
1805
1806 /* A simple key cannot follow a tag. */
1807
1808 parser->simple_key_allowed = 0;
1809
625fcfe9 1810 /* Create the TAG token and append it to the queue. */
eb9cceb5 1811
625fcfe9
KS
1812 if (!yaml_parser_scan_tag(parser, &token))
1813 return 0;
eb9cceb5 1814
625fcfe9
KS
1815 if (!ENQUEUE(parser, parser->tokens, token)) {
1816 yaml_token_delete(&token);
eb9cceb5
KS
1817 return 0;
1818 }
1819
1820 return 1;
1821}
1822
1823/*
1824 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1825 */
1826
1827static int
1828yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1829{
625fcfe9 1830 yaml_token_t token;
eb9cceb5
KS
1831
1832 /* Remove any potential simple keys. */
1833
1834 if (!yaml_parser_remove_simple_key(parser))
1835 return 0;
1836
1837 /* A simple key may follow a block scalar. */
1838
1839 parser->simple_key_allowed = 1;
1840
625fcfe9 1841 /* Create the SCALAR token and append it to the queue. */
eb9cceb5 1842
625fcfe9
KS
1843 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1844 return 0;
eb9cceb5 1845
625fcfe9
KS
1846 if (!ENQUEUE(parser, parser->tokens, token)) {
1847 yaml_token_delete(&token);
eb9cceb5
KS
1848 return 0;
1849 }
1850
1851 return 1;
1852}
1853
1854/*
1855 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1856 */
1857
1858static int
1859yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1860{
625fcfe9 1861 yaml_token_t token;
eb9cceb5
KS
1862
1863 /* A plain scalar could be a simple key. */
1864
1865 if (!yaml_parser_save_simple_key(parser))
1866 return 0;
1867
1868 /* A simple key cannot follow a flow scalar. */
1869
1870 parser->simple_key_allowed = 0;
1871
625fcfe9 1872 /* Create the SCALAR token and append it to the queue. */
eb9cceb5 1873
625fcfe9
KS
1874 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1875 return 0;
eb9cceb5 1876
625fcfe9
KS
1877 if (!ENQUEUE(parser, parser->tokens, token)) {
1878 yaml_token_delete(&token);
eb9cceb5
KS
1879 return 0;
1880 }
1881
1882 return 1;
1883}
1884
1885/*
1886 * Produce the SCALAR(...,plain) token.
1887 */
1888
1889static int
1890yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1891{
625fcfe9 1892 yaml_token_t token;
eb9cceb5
KS
1893
1894 /* A plain scalar could be a simple key. */
1895
1896 if (!yaml_parser_save_simple_key(parser))
1897 return 0;
1898
1899 /* A simple key cannot follow a flow scalar. */
1900
1901 parser->simple_key_allowed = 0;
1902
625fcfe9 1903 /* Create the SCALAR token and append it to the queue. */
eb9cceb5 1904
625fcfe9
KS
1905 if (!yaml_parser_scan_plain_scalar(parser, &token))
1906 return 0;
eb9cceb5 1907
625fcfe9
KS
1908 if (!ENQUEUE(parser, parser->tokens, token)) {
1909 yaml_token_delete(&token);
eb9cceb5
KS
1910 return 0;
1911 }
1912
1913 return 1;
1914}
1915
e71095e3
KS
1916/*
1917 * Eat whitespaces and comments until the next token is found.
1918 */
1919
1920static int
1921yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1922{
1923 /* Until the next token is not found. */
1924
1925 while (1)
1926 {
1927 /* Allow the BOM mark to start a line. */
1928
625fcfe9 1929 if (!CACHE(parser, 1)) return 0;
e71095e3 1930
e35af832 1931 if (parser->mark.column == 0 && IS_BOM(parser->buffer))
625fcfe9 1932 SKIP(parser);
e71095e3
KS
1933
1934 /*
1935 * Eat whitespaces.
1936 *
1937 * Tabs are allowed:
1938 *
1939 * - in the flow context;
1940 * - in the block context, but not at the beginning of the line or
986dbde7 1941 * after '-', '?', or ':' (complex value).
e71095e3
KS
1942 */
1943
625fcfe9 1944 if (!CACHE(parser, 1)) return 0;
e71095e3 1945
e35af832 1946 while (CHECK(parser->buffer,' ') ||
e71095e3 1947 ((parser->flow_level || !parser->simple_key_allowed) &&
e35af832 1948 CHECK(parser->buffer, '\t'))) {
625fcfe9
KS
1949 SKIP(parser);
1950 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
1951 }
1952
1953 /* Eat a comment until a line break. */
1954
e35af832
KS
1955 if (CHECK(parser->buffer, '#')) {
1956 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
1957 SKIP(parser);
1958 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
1959 }
1960 }
1961
1962 /* If it is a line break, eat it. */
1963
e35af832 1964 if (IS_BREAK(parser->buffer))
e71095e3 1965 {
625fcfe9
KS
1966 if (!CACHE(parser, 2)) return 0;
1967 SKIP_LINE(parser);
e71095e3
KS
1968
1969 /* In the block context, a new line may start a simple key. */
1970
1971 if (!parser->flow_level) {
1972 parser->simple_key_allowed = 1;
1973 }
1974 }
1975 else
1976 {
1977 /* We have found a token. */
1978
1979 break;
1980 }
1981 }
1982
1983 return 1;
1984}
1985
1986/*
1987 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1988 *
1989 * Scope:
1990 * %YAML 1.1 # a comment \n
1991 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1992 * %TAG !yaml! tag:yaml.org,2002: \n
1993 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1994 */
1995
625fcfe9
KS
1996int
1997yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
e71095e3
KS
1998{
1999 yaml_mark_t start_mark, end_mark;
2000 yaml_char_t *name = NULL;
2001 int major, minor;
2002 yaml_char_t *handle = NULL, *prefix = NULL;
e71095e3
KS
2003
2004 /* Eat '%'. */
2005
625fcfe9 2006 start_mark = parser->mark;
e71095e3 2007
625fcfe9 2008 SKIP(parser);
e71095e3
KS
2009
2010 /* Scan the directive name. */
2011
2012 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2013 goto error;
2014
2015 /* Is it a YAML directive? */
2016
2017 if (strcmp((char *)name, "YAML") == 0)
2018 {
2019 /* Scan the VERSION directive value. */
2020
2021 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2022 &major, &minor))
2023 goto error;
2024
625fcfe9 2025 end_mark = parser->mark;
e71095e3
KS
2026
2027 /* Create a VERSION-DIRECTIVE token. */
2028
625fcfe9 2029 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
e71095e3 2030 start_mark, end_mark);
e71095e3
KS
2031 }
2032
2033 /* Is it a TAG directive? */
2034
2035 else if (strcmp((char *)name, "TAG") == 0)
2036 {
2037 /* Scan the TAG directive value. */
2038
2039 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2040 &handle, &prefix))
2041 goto error;
2042
625fcfe9 2043 end_mark = parser->mark;
e71095e3
KS
2044
2045 /* Create a TAG-DIRECTIVE token. */
2046
625fcfe9 2047 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
e71095e3 2048 start_mark, end_mark);
e71095e3
KS
2049 }
2050
2051 /* Unknown directive. */
2052
2053 else
2054 {
92d41fe1 2055 yaml_parser_set_scanner_error(parser, "while scanning a directive",
bdf4d192 2056 start_mark, "found unknown directive name");
e71095e3
KS
2057 goto error;
2058 }
2059
2060 /* Eat the rest of the line including any comments. */
2061
625fcfe9
KS
2062 if (!CACHE(parser, 1)) goto error;
2063
e35af832 2064 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2065 SKIP(parser);
2066 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2067 }
2068
e35af832
KS
2069 if (CHECK(parser->buffer, '#')) {
2070 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
2071 SKIP(parser);
2072 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2073 }
2074 }
2075
2076 /* Check if we are at the end of the line. */
2077
e35af832 2078 if (!IS_BREAKZ(parser->buffer)) {
92d41fe1 2079 yaml_parser_set_scanner_error(parser, "while scanning a directive",
6be8109b 2080 start_mark, "did not find expected comment or line break");
e71095e3
KS
2081 goto error;
2082 }
2083
2084 /* Eat a line break. */
2085
e35af832 2086 if (IS_BREAK(parser->buffer)) {
625fcfe9
KS
2087 if (!CACHE(parser, 2)) goto error;
2088 SKIP_LINE(parser);
e71095e3
KS
2089 }
2090
2091 yaml_free(name);
2092
625fcfe9 2093 return 1;
e71095e3
KS
2094
2095error:
e71095e3
KS
2096 yaml_free(prefix);
2097 yaml_free(handle);
2098 yaml_free(name);
625fcfe9 2099 return 0;
e71095e3
KS
2100}
2101
2102/*
2103 * Scan the directive name.
2104 *
2105 * Scope:
2106 * %YAML 1.1 # a comment \n
2107 * ^^^^
2108 * %TAG !yaml! tag:yaml.org,2002: \n
2109 * ^^^
2110 */
2111
2112static int
2113yaml_parser_scan_directive_name(yaml_parser_t *parser,
2114 yaml_mark_t start_mark, yaml_char_t **name)
2115{
625fcfe9 2116 yaml_string_t string = NULL_STRING;
e71095e3 2117
625fcfe9 2118 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2119
2120 /* Consume the directive name. */
2121
625fcfe9 2122 if (!CACHE(parser, 1)) goto error;
e71095e3 2123
e35af832 2124 while (IS_ALPHA(parser->buffer))
e71095e3 2125 {
625fcfe9
KS
2126 if (!READ(parser, string)) goto error;
2127 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2128 }
2129
2130 /* Check if the name is empty. */
2131
625fcfe9 2132 if (string.start == string.pointer) {
e71095e3 2133 yaml_parser_set_scanner_error(parser, "while scanning a directive",
6be8109b 2134 start_mark, "could not find expected directive name");
e71095e3
KS
2135 goto error;
2136 }
2137
2138 /* Check for an blank character after the name. */
2139
e35af832 2140 if (!IS_BLANKZ(parser->buffer)) {
e71095e3
KS
2141 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2142 start_mark, "found unexpected non-alphabetical character");
2143 goto error;
2144 }
2145
625fcfe9 2146 *name = string.start;
e71095e3
KS
2147
2148 return 1;
2149
2150error:
625fcfe9 2151 STRING_DEL(parser, string);
e71095e3
KS
2152 return 0;
2153}
2154
2155/*
2156 * Scan the value of VERSION-DIRECTIVE.
2157 *
2158 * Scope:
2159 * %YAML 1.1 # a comment \n
2160 * ^^^^^^
2161 */
2162
2163static int
2164yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2165 yaml_mark_t start_mark, int *major, int *minor)
2166{
2167 /* Eat whitespaces. */
2168
625fcfe9 2169 if (!CACHE(parser, 1)) return 0;
e71095e3 2170
e35af832 2171 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2172 SKIP(parser);
2173 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
2174 }
2175
2176 /* Consume the major version number. */
2177
2178 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2179 return 0;
2180
2181 /* Eat '.'. */
2182
e35af832 2183 if (!CHECK(parser->buffer, '.')) {
e71095e3
KS
2184 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2185 start_mark, "did not find expected digit or '.' character");
2186 }
2187
625fcfe9 2188 SKIP(parser);
e71095e3
KS
2189
2190 /* Consume the minor version number. */
2191
2192 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2193 return 0;
ab01bac8
KS
2194
2195 return 1;
e71095e3
KS
2196}
2197
2198#define MAX_NUMBER_LENGTH 9
2199
2200/*
2201 * Scan the version number of VERSION-DIRECTIVE.
2202 *
2203 * Scope:
2204 * %YAML 1.1 # a comment \n
2205 * ^
2206 * %YAML 1.1 # a comment \n
2207 * ^
2208 */
2209
2210static int
2211yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2212 yaml_mark_t start_mark, int *number)
2213{
2214 int value = 0;
2215 size_t length = 0;
2216
2217 /* Repeat while the next character is digit. */
2218
625fcfe9 2219 if (!CACHE(parser, 1)) return 0;
e71095e3 2220
e35af832 2221 while (IS_DIGIT(parser->buffer))
e71095e3
KS
2222 {
2223 /* Check if the number is too long. */
2224
2225 if (++length > MAX_NUMBER_LENGTH) {
2226 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2227 start_mark, "found extremely long version number");
2228 }
2229
e35af832 2230 value = value*10 + AS_DIGIT(parser->buffer);
e71095e3 2231
625fcfe9 2232 SKIP(parser);
e71095e3 2233
625fcfe9 2234 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
2235 }
2236
2237 /* Check if the number was present. */
2238
2239 if (!length) {
2240 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2241 start_mark, "did not find expected version number");
2242 }
2243
2244 *number = value;
2245
2246 return 1;
2247}
2248
2249/*
2250 * Scan the value of a TAG-DIRECTIVE token.
2251 *
2252 * Scope:
2253 * %TAG !yaml! tag:yaml.org,2002: \n
2254 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2255 */
2256
2257static int
2258yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2259 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2260{
2261 yaml_char_t *handle_value = NULL;
2262 yaml_char_t *prefix_value = NULL;
2263
2264 /* Eat whitespaces. */
2265
625fcfe9 2266 if (!CACHE(parser, 1)) goto error;
e71095e3 2267
e35af832 2268 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2269 SKIP(parser);
2270 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2271 }
2272
2273 /* Scan a handle. */
2274
2275 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2276 goto error;
2277
2278 /* Expect a whitespace. */
2279
625fcfe9 2280 if (!CACHE(parser, 1)) goto error;
e71095e3 2281
e35af832 2282 if (!IS_BLANK(parser->buffer)) {
e71095e3
KS
2283 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2284 start_mark, "did not find expected whitespace");
2285 goto error;
2286 }
2287
2288 /* Eat whitespaces. */
2289
e35af832 2290 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2291 SKIP(parser);
2292 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2293 }
2294
2295 /* Scan a prefix. */
2296
2297 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2298 goto error;
2299
2300 /* Expect a whitespace or line break. */
2301
625fcfe9 2302 if (!CACHE(parser, 1)) goto error;
e71095e3 2303
e35af832 2304 if (!IS_BLANKZ(parser->buffer)) {
e71095e3
KS
2305 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2306 start_mark, "did not find expected whitespace or line break");
2307 goto error;
2308 }
2309
2310 *handle = handle_value;
2311 *prefix = prefix_value;
2312
2313 return 1;
2314
2315error:
2316 yaml_free(handle_value);
2317 yaml_free(prefix_value);
2318 return 0;
2319}
2320
625fcfe9
KS
2321static int
2322yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
e71095e3
KS
2323 yaml_token_type_t type)
2324{
2325 int length = 0;
2326 yaml_mark_t start_mark, end_mark;
625fcfe9 2327 yaml_string_t string = NULL_STRING;
e71095e3 2328
625fcfe9 2329 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2330
2331 /* Eat the indicator character. */
2332
625fcfe9 2333 start_mark = parser->mark;
e71095e3 2334
625fcfe9 2335 SKIP(parser);
e71095e3
KS
2336
2337 /* Consume the value. */
2338
625fcfe9 2339 if (!CACHE(parser, 1)) goto error;
e71095e3 2340
e35af832 2341 while (IS_ALPHA(parser->buffer)) {
625fcfe9
KS
2342 if (!READ(parser, string)) goto error;
2343 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2344 length ++;
2345 }
2346
625fcfe9 2347 end_mark = parser->mark;
e71095e3
KS
2348
2349 /*
2350 * Check if length of the anchor is greater than 0 and it is followed by
2351 * a whitespace character or one of the indicators:
2352 *
2353 * '?', ':', ',', ']', '}', '%', '@', '`'.
2354 */
2355
e35af832
KS
2356 if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2357 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2358 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2359 || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2360 || CHECK(parser->buffer, '`'))) {
e71095e3
KS
2361 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2362 "while scanning an anchor" : "while scanning an alias", start_mark,
2363 "did not find expected alphabetic or numeric character");
2364 goto error;
2365 }
2366
2367 /* Create a token. */
2368
625fcfe9
KS
2369 if (type == YAML_ANCHOR_TOKEN) {
2370 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2371 }
2372 else {
2373 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
92d41fe1 2374 }
e71095e3 2375
625fcfe9 2376 return 1;
e71095e3
KS
2377
2378error:
625fcfe9 2379 STRING_DEL(parser, string);
e71095e3
KS
2380 return 0;
2381}
2382
2383/*
2384 * Scan a TAG token.
2385 */
2386
625fcfe9
KS
2387static int
2388yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
e71095e3
KS
2389{
2390 yaml_char_t *handle = NULL;
2391 yaml_char_t *suffix = NULL;
e71095e3
KS
2392 yaml_mark_t start_mark, end_mark;
2393
625fcfe9 2394 start_mark = parser->mark;
e71095e3
KS
2395
2396 /* Check if the tag is in the canonical form. */
2397
625fcfe9 2398 if (!CACHE(parser, 2)) goto error;
e71095e3 2399
e35af832 2400 if (CHECK_AT(parser->buffer, '<', 1))
e71095e3
KS
2401 {
2402 /* Set the handle to '' */
2403
2404 handle = yaml_malloc(1);
2405 if (!handle) goto error;
2406 handle[0] = '\0';
2407
2408 /* Eat '!<' */
2409
625fcfe9
KS
2410 SKIP(parser);
2411 SKIP(parser);
e71095e3
KS
2412
2413 /* Consume the tag value. */
2414
2415 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2416 goto error;
2417
2418 /* Check for '>' and eat it. */
2419
e35af832 2420 if (!CHECK(parser->buffer, '>')) {
e71095e3
KS
2421 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2422 start_mark, "did not find the expected '>'");
2423 goto error;
2424 }
2425
625fcfe9 2426 SKIP(parser);
e71095e3
KS
2427 }
2428 else
2429 {
2430 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2431
2432 /* First, try to scan a handle. */
2433
2434 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2435 goto error;
2436
2437 /* Check if it is, indeed, handle. */
2438
2439 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2440 {
2441 /* Scan the suffix now. */
2442
2443 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2444 goto error;
2445 }
2446 else
2447 {
2448 /* It wasn't a handle after all. Scan the rest of the tag. */
2449
2450 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2451 goto error;
2452
2453 /* Set the handle to '!'. */
2454
2455 yaml_free(handle);
2456 handle = yaml_malloc(2);
2457 if (!handle) goto error;
2458 handle[0] = '!';
2459 handle[1] = '\0';
7e32c194
KS
2460
2461 /*
625fcfe9
KS
2462 * A special case: the '!' tag. Set the handle to '' and the
2463 * suffix to '!'.
7e32c194
KS
2464 */
2465
2466 if (suffix[0] == '\0') {
2467 yaml_char_t *tmp = handle;
2468 handle = suffix;
2469 suffix = tmp;
2470 }
e71095e3
KS
2471 }
2472 }
2473
2474 /* Check the character which ends the tag. */
2475
625fcfe9 2476 if (!CACHE(parser, 1)) goto error;
e71095e3 2477
e35af832 2478 if (!IS_BLANKZ(parser->buffer)) {
e71095e3 2479 yaml_parser_set_scanner_error(parser, "while scanning a tag",
6be8109b 2480 start_mark, "did not find expected whitespace or line break");
e71095e3
KS
2481 goto error;
2482 }
2483
625fcfe9 2484 end_mark = parser->mark;
e71095e3
KS
2485
2486 /* Create a token. */
2487
625fcfe9 2488 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
e71095e3 2489
625fcfe9 2490 return 1;
e71095e3
KS
2491
2492error:
2493 yaml_free(handle);
2494 yaml_free(suffix);
625fcfe9 2495 return 0;
e71095e3
KS
2496}
2497
2498/*
2499 * Scan a tag handle.
2500 */
2501
2502static int
2503yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2504 yaml_mark_t start_mark, yaml_char_t **handle)
2505{
625fcfe9 2506 yaml_string_t string = NULL_STRING;
e71095e3 2507
625fcfe9 2508 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2509
2510 /* Check the initial '!' character. */
2511
625fcfe9 2512 if (!CACHE(parser, 1)) goto error;
e71095e3 2513
e35af832 2514 if (!CHECK(parser->buffer, '!')) {
e71095e3
KS
2515 yaml_parser_set_scanner_error(parser, directive ?
2516 "while scanning a tag directive" : "while scanning a tag",
2517 start_mark, "did not find expected '!'");
2518 goto error;
2519 }
2520
2521 /* Copy the '!' character. */
2522
625fcfe9 2523 if (!READ(parser, string)) goto error;
e71095e3
KS
2524
2525 /* Copy all subsequent alphabetical and numerical characters. */
2526
625fcfe9 2527 if (!CACHE(parser, 1)) goto error;
e71095e3 2528
e35af832 2529 while (IS_ALPHA(parser->buffer))
e71095e3 2530 {
625fcfe9
KS
2531 if (!READ(parser, string)) goto error;
2532 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2533 }
2534
2535 /* Check if the trailing character is '!' and copy it. */
2536
e35af832 2537 if (CHECK(parser->buffer, '!'))
e71095e3 2538 {
625fcfe9 2539 if (!READ(parser, string)) goto error;
e71095e3
KS
2540 }
2541 else
2542 {
2543 /*
7e32c194
KS
2544 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2545 * directive, it's an error. If it's a tag token, it must be a part of
2546 * URI.
e71095e3
KS
2547 */
2548
625fcfe9 2549 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
7e32c194 2550 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
e71095e3
KS
2551 start_mark, "did not find expected '!'");
2552 goto error;
2553 }
2554 }
2555
625fcfe9 2556 *handle = string.start;
e71095e3
KS
2557
2558 return 1;
2559
2560error:
625fcfe9 2561 STRING_DEL(parser, string);
e71095e3
KS
2562 return 0;
2563}
2564
2565/*
2566 * Scan a tag.
2567 */
2568
2569static int
2570yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2571 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2572{
2573 size_t length = head ? strlen((char *)head) : 0;
625fcfe9 2574 yaml_string_t string = NULL_STRING;
e71095e3 2575
625fcfe9 2576 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2577
2578 /* Resize the string to include the head. */
2579
f56726b9 2580 while ((size_t)(string.end - string.start) <= length) {
625fcfe9
KS
2581 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2582 parser->error = YAML_MEMORY_ERROR;
2583 goto error;
2584 }
e71095e3
KS
2585 }
2586
7e32c194
KS
2587 /*
2588 * Copy the head if needed.
2589 *
2590 * Note that we don't copy the leading '!' character.
2591 */
e71095e3 2592
7e32c194 2593 if (length > 1) {
625fcfe9 2594 memcpy(string.start, head+1, length-1);
7e32c194 2595 string.pointer += length-1;
e71095e3
KS
2596 }
2597
2598 /* Scan the tag. */
2599
625fcfe9 2600 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2601
2602 /*
2603 * The set of characters that may appear in URI is as follows:
2604 *
2605 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2606 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2607 * '%'.
2608 */
2609
e35af832
KS
2610 while (IS_ALPHA(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, '(') || CHECK(parser->buffer, ')')
2619 || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2620 || CHECK(parser->buffer, '%'))
e71095e3 2621 {
e71095e3
KS
2622 /* Check if it is a URI-escape sequence. */
2623
e35af832 2624 if (CHECK(parser->buffer, '%')) {
d1003a9d
KS
2625 if (!STRING_EXTEND(parser, string))
2626 goto error;
2627
e71095e3
KS
2628 if (!yaml_parser_scan_uri_escapes(parser,
2629 directive, start_mark, &string)) goto error;
2630 }
2631 else {
625fcfe9 2632 if (!READ(parser, string)) goto error;
e71095e3
KS
2633 }
2634
2635 length ++;
625fcfe9 2636 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2637 }
2638
2639 /* Check if the tag is non-empty. */
2640
2641 if (!length) {
625fcfe9
KS
2642 if (!STRING_EXTEND(parser, string))
2643 goto error;
2644
e71095e3
KS
2645 yaml_parser_set_scanner_error(parser, directive ?
2646 "while parsing a %TAG directive" : "while parsing a tag",
2647 start_mark, "did not find expected tag URI");
2648 goto error;
2649 }
2650
625fcfe9 2651 *uri = string.start;
e71095e3
KS
2652
2653 return 1;
2654
2655error:
625fcfe9 2656 STRING_DEL(parser, string);
e71095e3
KS
2657 return 0;
2658}
2659
2660/*
2661 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2662 */
2663
2664static int
2665yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2666 yaml_mark_t start_mark, yaml_string_t *string)
2667{
2668 int width = 0;
2669
2670 /* Decode the required number of characters. */
2671
2672 do {
2673
2674 unsigned char octet = 0;
2675
2676 /* Check for a URI-escaped octet. */
2677
625fcfe9 2678 if (!CACHE(parser, 3)) return 0;
e71095e3 2679
e35af832
KS
2680 if (!(CHECK(parser->buffer, '%')
2681 && IS_HEX_AT(parser->buffer, 1)
2682 && IS_HEX_AT(parser->buffer, 2))) {
e71095e3
KS
2683 return yaml_parser_set_scanner_error(parser, directive ?
2684 "while parsing a %TAG directive" : "while parsing a tag",
2685 start_mark, "did not find URI escaped octet");
2686 }
2687
2688 /* Get the octet. */
2689
e35af832 2690 octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
e71095e3
KS
2691
2692 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2693
2694 if (!width)
2695 {
2696 width = (octet & 0x80) == 0x00 ? 1 :
2697 (octet & 0xE0) == 0xC0 ? 2 :
2698 (octet & 0xF0) == 0xE0 ? 3 :
2699 (octet & 0xF8) == 0xF0 ? 4 : 0;
2700 if (!width) {
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 leading UTF-8 octet");
2704 }
2705 }
2706 else
2707 {
2708 /* Check if the trailing octet is correct. */
2709
2710 if ((octet & 0xC0) != 0x80) {
2711 return yaml_parser_set_scanner_error(parser, directive ?
2712 "while parsing a %TAG directive" : "while parsing a tag",
2713 start_mark, "found an incorrect trailing UTF-8 octet");
2714 }
2715 }
2716
2717 /* Copy the octet and move the pointers. */
2718
2719 *(string->pointer++) = octet;
625fcfe9
KS
2720 SKIP(parser);
2721 SKIP(parser);
2722 SKIP(parser);
e71095e3
KS
2723
2724 } while (--width);
2725
2726 return 1;
2727}
2728
92d41fe1
KS
2729/*
2730 * Scan a block scalar.
2731 */
2732
625fcfe9
KS
2733static int
2734yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2735 int literal)
92d41fe1
KS
2736{
2737 yaml_mark_t start_mark;
2738 yaml_mark_t end_mark;
625fcfe9
KS
2739 yaml_string_t string = NULL_STRING;
2740 yaml_string_t leading_break = NULL_STRING;
2741 yaml_string_t trailing_breaks = NULL_STRING;
92d41fe1
KS
2742 int chomping = 0;
2743 int increment = 0;
2744 int indent = 0;
2745 int leading_blank = 0;
2746 int trailing_blank = 0;
2747
625fcfe9
KS
2748 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2749 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2750 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
92d41fe1
KS
2751
2752 /* Eat the indicator '|' or '>'. */
2753
625fcfe9 2754 start_mark = parser->mark;
92d41fe1 2755
625fcfe9 2756 SKIP(parser);
92d41fe1
KS
2757
2758 /* Scan the additional block scalar indicators. */
2759
625fcfe9 2760 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2761
2762 /* Check for a chomping indicator. */
2763
e35af832 2764 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
92d41fe1
KS
2765 {
2766 /* Set the chomping method and eat the indicator. */
2767
e35af832 2768 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
92d41fe1 2769
625fcfe9 2770 SKIP(parser);
92d41fe1
KS
2771
2772 /* Check for an indentation indicator. */
2773
625fcfe9 2774 if (!CACHE(parser, 1)) goto error;
92d41fe1 2775
e35af832 2776 if (IS_DIGIT(parser->buffer))
92d41fe1 2777 {
bdf4d192 2778 /* Check that the indentation is greater than 0. */
92d41fe1 2779
e35af832 2780 if (CHECK(parser->buffer, '0')) {
92d41fe1 2781 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
bdf4d192 2782 start_mark, "found an indentation indicator equal to 0");
92d41fe1
KS
2783 goto error;
2784 }
2785
bdf4d192 2786 /* Get the indentation level and eat the indicator. */
92d41fe1 2787
e35af832 2788 increment = AS_DIGIT(parser->buffer);
92d41fe1 2789
625fcfe9 2790 SKIP(parser);
92d41fe1
KS
2791 }
2792 }
2793
2794 /* Do the same as above, but in the opposite order. */
2795
e35af832 2796 else if (IS_DIGIT(parser->buffer))
92d41fe1 2797 {
e35af832 2798 if (CHECK(parser->buffer, '0')) {
92d41fe1 2799 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
bdf4d192 2800 start_mark, "found an indentation indicator equal to 0");
92d41fe1
KS
2801 goto error;
2802 }
2803
e35af832 2804 increment = AS_DIGIT(parser->buffer);
92d41fe1 2805
625fcfe9 2806 SKIP(parser);
92d41fe1 2807
625fcfe9 2808 if (!CACHE(parser, 1)) goto error;
92d41fe1 2809
e35af832
KS
2810 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2811 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
625fcfe9
KS
2812
2813 SKIP(parser);
92d41fe1
KS
2814 }
2815 }
2816
2817 /* Eat whitespaces and comments to the end of the line. */
2818
625fcfe9 2819 if (!CACHE(parser, 1)) goto error;
92d41fe1 2820
e35af832 2821 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2822 SKIP(parser);
2823 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2824 }
2825
e35af832
KS
2826 if (CHECK(parser->buffer, '#')) {
2827 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
2828 SKIP(parser);
2829 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2830 }
2831 }
2832
2833 /* Check if we are at the end of the line. */
2834
e35af832 2835 if (!IS_BREAKZ(parser->buffer)) {
92d41fe1 2836 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
6be8109b 2837 start_mark, "did not find expected comment or line break");
92d41fe1
KS
2838 goto error;
2839 }
2840
2841 /* Eat a line break. */
2842
e35af832 2843 if (IS_BREAK(parser->buffer)) {
625fcfe9
KS
2844 if (!CACHE(parser, 2)) goto error;
2845 SKIP_LINE(parser);
92d41fe1
KS
2846 }
2847
625fcfe9 2848 end_mark = parser->mark;
92d41fe1 2849
bdf4d192 2850 /* Set the indentation level if it was specified. */
92d41fe1
KS
2851
2852 if (increment) {
2853 indent = parser->indent >= 0 ? parser->indent+increment : increment;
2854 }
2855
2856 /* Scan the leading line breaks and determine the indentation level if needed. */
2857
21fbedd4 2858 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
92d41fe1
KS
2859 start_mark, &end_mark)) goto error;
2860
2861 /* Scan the block scalar content. */
2862
625fcfe9 2863 if (!CACHE(parser, 1)) goto error;
92d41fe1 2864
0174ed6e 2865 while ((int)parser->mark.column == indent && !IS_Z(parser->buffer))
92d41fe1
KS
2866 {
2867 /*
2868 * We are at the beginning of a non-empty line.
2869 */
2870
2871 /* Is it a trailing whitespace? */
2872
e35af832 2873 trailing_blank = IS_BLANK(parser->buffer);
92d41fe1
KS
2874
2875 /* Check if we need to fold the leading line break. */
2876
625fcfe9 2877 if (!literal && (*leading_break.start == '\n')
92d41fe1
KS
2878 && !leading_blank && !trailing_blank)
2879 {
2880 /* Do we need to join the lines by space? */
2881
625fcfe9
KS
2882 if (*trailing_breaks.start == '\0') {
2883 if (!STRING_EXTEND(parser, string)) goto error;
92d41fe1
KS
2884 *(string.pointer ++) = ' ';
2885 }
2886
625fcfe9 2887 CLEAR(parser, leading_break);
92d41fe1
KS
2888 }
2889 else {
21fbedd4 2890 if (!JOIN(parser, string, leading_break)) goto error;
625fcfe9 2891 CLEAR(parser, leading_break);
92d41fe1
KS
2892 }
2893
2894 /* Append the remaining line breaks. */
2895
21fbedd4 2896 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9 2897 CLEAR(parser, trailing_breaks);
92d41fe1
KS
2898
2899 /* Is it a leading whitespace? */
2900
e35af832 2901 leading_blank = IS_BLANK(parser->buffer);
92d41fe1
KS
2902
2903 /* Consume the current line. */
2904
e35af832 2905 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
2906 if (!READ(parser, string)) goto error;
2907 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2908 }
2909
2910 /* Consume the line break. */
2911
625fcfe9 2912 if (!CACHE(parser, 2)) goto error;
92d41fe1 2913
625fcfe9 2914 if (!READ_LINE(parser, leading_break)) goto error;
92d41fe1 2915
bdf4d192 2916 /* Eat the following indentation spaces and line breaks. */
92d41fe1
KS
2917
2918 if (!yaml_parser_scan_block_scalar_breaks(parser,
21fbedd4 2919 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
92d41fe1
KS
2920 }
2921
2922 /* Chomp the tail. */
2923
2924 if (chomping != -1) {
21fbedd4 2925 if (!JOIN(parser, string, leading_break)) goto error;
92d41fe1
KS
2926 }
2927 if (chomping == 1) {
21fbedd4 2928 if (!JOIN(parser, string, trailing_breaks)) goto error;
92d41fe1
KS
2929 }
2930
2931 /* Create a token. */
2932
625fcfe9 2933 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
92d41fe1
KS
2934 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2935 start_mark, end_mark);
92d41fe1 2936
625fcfe9
KS
2937 STRING_DEL(parser, leading_break);
2938 STRING_DEL(parser, trailing_breaks);
92d41fe1 2939
625fcfe9 2940 return 1;
92d41fe1
KS
2941
2942error:
625fcfe9
KS
2943 STRING_DEL(parser, string);
2944 STRING_DEL(parser, leading_break);
2945 STRING_DEL(parser, trailing_breaks);
92d41fe1 2946
625fcfe9 2947 return 0;
92d41fe1
KS
2948}
2949
2950/*
bdf4d192
SH
2951 * Scan indentation spaces and line breaks for a block scalar. Determine the
2952 * indentation level if needed.
92d41fe1
KS
2953 */
2954
2955static int
2956yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2957 int *indent, yaml_string_t *breaks,
2958 yaml_mark_t start_mark, yaml_mark_t *end_mark)
2959{
2960 int max_indent = 0;
2961
625fcfe9 2962 *end_mark = parser->mark;
92d41fe1 2963
bdf4d192 2964 /* Eat the indentation spaces and line breaks. */
92d41fe1
KS
2965
2966 while (1)
2967 {
bdf4d192 2968 /* Eat the indentation spaces. */
92d41fe1 2969
625fcfe9 2970 if (!CACHE(parser, 1)) return 0;
92d41fe1 2971
0174ed6e 2972 while ((!*indent || (int)parser->mark.column < *indent)
e35af832 2973 && IS_SPACE(parser->buffer)) {
625fcfe9
KS
2974 SKIP(parser);
2975 if (!CACHE(parser, 1)) return 0;
92d41fe1
KS
2976 }
2977
0174ed6e
KS
2978 if ((int)parser->mark.column > max_indent)
2979 max_indent = (int)parser->mark.column;
92d41fe1 2980
bdf4d192 2981 /* Check for a tab character messing the indentation. */
92d41fe1 2982
0174ed6e 2983 if ((!*indent || (int)parser->mark.column < *indent)
e35af832 2984 && IS_TAB(parser->buffer)) {
92d41fe1 2985 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
bdf4d192 2986 start_mark, "found a tab character where an indentation space is expected");
92d41fe1
KS
2987 }
2988
2989 /* Have we found a non-empty line? */
2990
e35af832 2991 if (!IS_BREAK(parser->buffer)) break;
92d41fe1
KS
2992
2993 /* Consume the line break. */
2994
625fcfe9
KS
2995 if (!CACHE(parser, 2)) return 0;
2996 if (!READ_LINE(parser, *breaks)) return 0;
2997 *end_mark = parser->mark;
92d41fe1
KS
2998 }
2999
3000 /* Determine the indentation level if needed. */
3001
3002 if (!*indent) {
3003 *indent = max_indent;
3004 if (*indent < parser->indent + 1)
3005 *indent = parser->indent + 1;
3006 if (*indent < 1)
3007 *indent = 1;
3008 }
3009
986dbde7 3010 return 1;
92d41fe1
KS
3011}
3012
21fbedd4
KS
3013/*
3014 * Scan a quoted scalar.
3015 */
3016
625fcfe9
KS
3017static int
3018yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3019 int single)
21fbedd4
KS
3020{
3021 yaml_mark_t start_mark;
3022 yaml_mark_t end_mark;
625fcfe9
KS
3023 yaml_string_t string = NULL_STRING;
3024 yaml_string_t leading_break = NULL_STRING;
3025 yaml_string_t trailing_breaks = NULL_STRING;
3026 yaml_string_t whitespaces = NULL_STRING;
21fbedd4
KS
3027 int leading_blanks;
3028
625fcfe9
KS
3029 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3030 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3031 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3032 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
21fbedd4
KS
3033
3034 /* Eat the left quote. */
3035
625fcfe9 3036 start_mark = parser->mark;
21fbedd4 3037
625fcfe9 3038 SKIP(parser);
21fbedd4
KS
3039
3040 /* Consume the content of the quoted scalar. */
3041
3042 while (1)
3043 {
3044 /* Check that there are no document indicators at the beginning of the line. */
3045
625fcfe9 3046 if (!CACHE(parser, 4)) goto error;
21fbedd4 3047
625fcfe9 3048 if (parser->mark.column == 0 &&
e35af832
KS
3049 ((CHECK_AT(parser->buffer, '-', 0) &&
3050 CHECK_AT(parser->buffer, '-', 1) &&
3051 CHECK_AT(parser->buffer, '-', 2)) ||
3052 (CHECK_AT(parser->buffer, '.', 0) &&
3053 CHECK_AT(parser->buffer, '.', 1) &&
3054 CHECK_AT(parser->buffer, '.', 2))) &&
3055 IS_BLANKZ_AT(parser->buffer, 3))
21fbedd4
KS
3056 {
3057 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3058 start_mark, "found unexpected document indicator");
3059 goto error;
3060 }
3061
3062 /* Check for EOF. */
3063
e35af832 3064 if (IS_Z(parser->buffer)) {
21fbedd4
KS
3065 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3066 start_mark, "found unexpected end of stream");
3067 goto error;
3068 }
3069
3070 /* Consume non-blank characters. */
3071
625fcfe9 3072 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3073
3074 leading_blanks = 0;
3075
e35af832 3076 while (!IS_BLANKZ(parser->buffer))
21fbedd4
KS
3077 {
3078 /* Check for an escaped single quote. */
3079
e35af832
KS
3080 if (single && CHECK_AT(parser->buffer, '\'', 0)
3081 && CHECK_AT(parser->buffer, '\'', 1))
21fbedd4 3082 {
625fcfe9 3083 if (!STRING_EXTEND(parser, string)) goto error;
21fbedd4 3084 *(string.pointer++) = '\'';
625fcfe9
KS
3085 SKIP(parser);
3086 SKIP(parser);
21fbedd4
KS
3087 }
3088
3089 /* Check for the right quote. */
3090
e35af832 3091 else if (CHECK(parser->buffer, single ? '\'' : '"'))
21fbedd4
KS
3092 {
3093 break;
3094 }
3095
3096 /* Check for an escaped line break. */
3097
e35af832
KS
3098 else if (!single && CHECK(parser->buffer, '\\')
3099 && IS_BREAK_AT(parser->buffer, 1))
21fbedd4 3100 {
625fcfe9
KS
3101 if (!CACHE(parser, 3)) goto error;
3102 SKIP(parser);
3103 SKIP_LINE(parser);
21fbedd4
KS
3104 leading_blanks = 1;
3105 break;
3106 }
3107
3108 /* Check for an escape sequence. */
3109
e35af832 3110 else if (!single && CHECK(parser->buffer, '\\'))
21fbedd4 3111 {
0174ed6e 3112 size_t code_length = 0;
21fbedd4 3113
625fcfe9
KS
3114 if (!STRING_EXTEND(parser, string)) goto error;
3115
21fbedd4
KS
3116 /* Check the escape character. */
3117
625fcfe9 3118 switch (parser->buffer.pointer[1])
21fbedd4
KS
3119 {
3120 case '0':
3121 *(string.pointer++) = '\0';
3122 break;
3123
3124 case 'a':
3125 *(string.pointer++) = '\x07';
3126 break;
3127
3128 case 'b':
3129 *(string.pointer++) = '\x08';
3130 break;
3131
3132 case 't':
3133 case '\t':
3134 *(string.pointer++) = '\x09';
3135 break;
3136
3137 case 'n':
3138 *(string.pointer++) = '\x0A';
3139 break;
3140
3141 case 'v':
3142 *(string.pointer++) = '\x0B';
3143 break;
3144
3145 case 'f':
3146 *(string.pointer++) = '\x0C';
3147 break;
3148
3149 case 'r':
3150 *(string.pointer++) = '\x0D';
3151 break;
3152
3153 case 'e':
3154 *(string.pointer++) = '\x1B';
3155 break;
3156
3157 case ' ':
3158 *(string.pointer++) = '\x20';
3159 break;
3160
3161 case '"':
3162 *(string.pointer++) = '"';
3163 break;
3164
6db69c72
IN
3165 case '/':
3166 *(string.pointer++) = '/';
3167 break;
3168
21fbedd4
KS
3169 case '\'':
3170 *(string.pointer++) = '\'';
3171 break;
3172
7e32c194
KS
3173 case '\\':
3174 *(string.pointer++) = '\\';
3175 break;
3176
21fbedd4
KS
3177 case 'N': /* NEL (#x85) */
3178 *(string.pointer++) = '\xC2';
3179 *(string.pointer++) = '\x85';
3180 break;
3181
3182 case '_': /* #xA0 */
3183 *(string.pointer++) = '\xC2';
3184 *(string.pointer++) = '\xA0';
3185 break;
3186
3187 case 'L': /* LS (#x2028) */
3188 *(string.pointer++) = '\xE2';
3189 *(string.pointer++) = '\x80';
3190 *(string.pointer++) = '\xA8';
3191 break;
3192
3193 case 'P': /* PS (#x2029) */
3194 *(string.pointer++) = '\xE2';
3195 *(string.pointer++) = '\x80';
7e32c194 3196 *(string.pointer++) = '\xA9';
21fbedd4
KS
3197 break;
3198
3199 case 'x':
3200 code_length = 2;
3201 break;
3202
3203 case 'u':
3204 code_length = 4;
3205 break;
3206
3207 case 'U':
3208 code_length = 8;
3209 break;
3210
3211 default:
3212 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3213 start_mark, "found unknown escape character");
3214 goto error;
3215 }
3216
625fcfe9
KS
3217 SKIP(parser);
3218 SKIP(parser);
21fbedd4
KS
3219
3220 /* Consume an arbitrary escape code. */
3221
3222 if (code_length)
3223 {
3224 unsigned int value = 0;
0174ed6e 3225 size_t k;
21fbedd4
KS
3226
3227 /* Scan the character value. */
3228
625fcfe9 3229 if (!CACHE(parser, code_length)) goto error;
21fbedd4
KS
3230
3231 for (k = 0; k < code_length; k ++) {
e35af832 3232 if (!IS_HEX_AT(parser->buffer, k)) {
21fbedd4
KS
3233 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3234 start_mark, "did not find expected hexdecimal number");
3235 goto error;
3236 }
e35af832 3237 value = (value << 4) + AS_HEX_AT(parser->buffer, k);
21fbedd4
KS
3238 }
3239
3240 /* Check the value and write the character. */
3241
3242 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3243 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3244 start_mark, "found invalid Unicode character escape code");
3245 goto error;
3246 }
3247
3248 if (value <= 0x7F) {
3249 *(string.pointer++) = value;
3250 }
3251 else if (value <= 0x7FF) {
3252 *(string.pointer++) = 0xC0 + (value >> 6);
3253 *(string.pointer++) = 0x80 + (value & 0x3F);
3254 }
3255 else if (value <= 0xFFFF) {
3256 *(string.pointer++) = 0xE0 + (value >> 12);
3257 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3258 *(string.pointer++) = 0x80 + (value & 0x3F);
3259 }
3260 else {
3261 *(string.pointer++) = 0xF0 + (value >> 18);
3262 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3263 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3264 *(string.pointer++) = 0x80 + (value & 0x3F);
3265 }
3266
3267 /* Advance the pointer. */
3268
3269 for (k = 0; k < code_length; k ++) {
625fcfe9 3270 SKIP(parser);
21fbedd4
KS
3271 }
3272 }
3273 }
3274
3275 else
3276 {
3277 /* It is a non-escaped non-blank character. */
3278
625fcfe9 3279 if (!READ(parser, string)) goto error;
21fbedd4
KS
3280 }
3281
625fcfe9 3282 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3283 }
3284
3285 /* Check if we are at the end of the scalar. */
3286
6bbc217f
IC
3287 /* Fix for crash unitialized value crash
3288 * Credit for the bug and input is to OSS Fuzz
3289 * Credit for the fix to Alex Gaynor
3290 */
3291 if (!CACHE(parser, 1)) goto error;
e35af832 3292 if (CHECK(parser->buffer, single ? '\'' : '"'))
21fbedd4
KS
3293 break;
3294
3295 /* Consume blank characters. */
3296
625fcfe9 3297 if (!CACHE(parser, 1)) goto error;
21fbedd4 3298
e35af832 3299 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
21fbedd4 3300 {
e35af832 3301 if (IS_BLANK(parser->buffer))
21fbedd4
KS
3302 {
3303 /* Consume a space or a tab character. */
3304
3305 if (!leading_blanks) {
625fcfe9 3306 if (!READ(parser, whitespaces)) goto error;
21fbedd4 3307 }
7e32c194 3308 else {
625fcfe9 3309 SKIP(parser);
7e32c194 3310 }
21fbedd4
KS
3311 }
3312 else
3313 {
625fcfe9 3314 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3315
3316 /* Check if it is a first line break. */
3317
3318 if (!leading_blanks)
3319 {
625fcfe9
KS
3320 CLEAR(parser, whitespaces);
3321 if (!READ_LINE(parser, leading_break)) goto error;
21fbedd4
KS
3322 leading_blanks = 1;
3323 }
3324 else
3325 {
625fcfe9 3326 if (!READ_LINE(parser, trailing_breaks)) goto error;
21fbedd4
KS
3327 }
3328 }
625fcfe9 3329 if (!CACHE(parser, 1)) goto error;
21fbedd4
KS
3330 }
3331
3332 /* Join the whitespaces or fold line breaks. */
3333
21fbedd4
KS
3334 if (leading_blanks)
3335 {
3336 /* Do we need to fold line breaks? */
3337
625fcfe9
KS
3338 if (leading_break.start[0] == '\n') {
3339 if (trailing_breaks.start[0] == '\0') {
3340 if (!STRING_EXTEND(parser, string)) goto error;
21fbedd4
KS
3341 *(string.pointer++) = ' ';
3342 }
3343 else {
3344 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9 3345 CLEAR(parser, trailing_breaks);
21fbedd4 3346 }
625fcfe9 3347 CLEAR(parser, leading_break);
21fbedd4
KS
3348 }
3349 else {
3350 if (!JOIN(parser, string, leading_break)) goto error;
3351 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9
KS
3352 CLEAR(parser, leading_break);
3353 CLEAR(parser, trailing_breaks);
21fbedd4
KS
3354 }
3355 }
3356 else
3357 {
3358 if (!JOIN(parser, string, whitespaces)) goto error;
625fcfe9 3359 CLEAR(parser, whitespaces);
21fbedd4
KS
3360 }
3361 }
3362
3363 /* Eat the right quote. */
3364
625fcfe9 3365 SKIP(parser);
21fbedd4 3366
625fcfe9 3367 end_mark = parser->mark;
21fbedd4
KS
3368
3369 /* Create a token. */
3370
625fcfe9 3371 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
21fbedd4
KS
3372 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3373 start_mark, end_mark);
21fbedd4 3374
625fcfe9
KS
3375 STRING_DEL(parser, leading_break);
3376 STRING_DEL(parser, trailing_breaks);
3377 STRING_DEL(parser, whitespaces);
21fbedd4 3378
625fcfe9 3379 return 1;
21fbedd4
KS
3380
3381error:
625fcfe9
KS
3382 STRING_DEL(parser, string);
3383 STRING_DEL(parser, leading_break);
3384 STRING_DEL(parser, trailing_breaks);
3385 STRING_DEL(parser, whitespaces);
21fbedd4 3386
625fcfe9 3387 return 0;
21fbedd4
KS
3388}
3389
3390/*
3391 * Scan a plain scalar.
3392 */
3393
625fcfe9
KS
3394static int
3395yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
21fbedd4
KS
3396{
3397 yaml_mark_t start_mark;
3398 yaml_mark_t end_mark;
625fcfe9
KS
3399 yaml_string_t string = NULL_STRING;
3400 yaml_string_t leading_break = NULL_STRING;
3401 yaml_string_t trailing_breaks = NULL_STRING;
3402 yaml_string_t whitespaces = NULL_STRING;
21fbedd4
KS
3403 int leading_blanks = 0;
3404 int indent = parser->indent+1;
3405
625fcfe9
KS
3406 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3407 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3408 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3409 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
21fbedd4 3410
54815ffd 3411 start_mark = end_mark = parser->mark;
21fbedd4
KS
3412
3413 /* Consume the content of the plain scalar. */
3414
3415 while (1)
3416 {
3417 /* Check for a document indicator. */
3418
625fcfe9 3419 if (!CACHE(parser, 4)) goto error;
21fbedd4 3420
625fcfe9 3421 if (parser->mark.column == 0 &&
e35af832
KS
3422 ((CHECK_AT(parser->buffer, '-', 0) &&
3423 CHECK_AT(parser->buffer, '-', 1) &&
3424 CHECK_AT(parser->buffer, '-', 2)) ||
3425 (CHECK_AT(parser->buffer, '.', 0) &&
3426 CHECK_AT(parser->buffer, '.', 1) &&
3427 CHECK_AT(parser->buffer, '.', 2))) &&
3428 IS_BLANKZ_AT(parser->buffer, 3)) break;
21fbedd4
KS
3429
3430 /* Check for a comment. */
3431
e35af832 3432 if (CHECK(parser->buffer, '#'))
21fbedd4
KS
3433 break;
3434
3435 /* Consume non-blank characters. */
3436
e35af832 3437 while (!IS_BLANKZ(parser->buffer))
21fbedd4 3438 {
7e32c194 3439 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
21fbedd4 3440
e35af832
KS
3441 if (parser->flow_level
3442 && CHECK(parser->buffer, ':')
3443 && !IS_BLANKZ_AT(parser->buffer, 1)) {
21fbedd4
KS
3444 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3445 start_mark, "found unexpected ':'");
3446 goto error;
3447 }
3448
3449 /* Check for indicators that may end a plain scalar. */
3450
e35af832
KS
3451 if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3452 || (parser->flow_level &&
3453 (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':')
3454 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
3455 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3456 || CHECK(parser->buffer, '}'))))
21fbedd4
KS
3457 break;
3458
3459 /* Check if we need to join whitespaces and breaks. */
3460
625fcfe9 3461 if (leading_blanks || whitespaces.start != whitespaces.pointer)
21fbedd4 3462 {
21fbedd4
KS
3463 if (leading_blanks)
3464 {
3465 /* Do we need to fold line breaks? */
3466
625fcfe9
KS
3467 if (leading_break.start[0] == '\n') {
3468 if (trailing_breaks.start[0] == '\0') {
3469 if (!STRING_EXTEND(parser, string)) goto error;
21fbedd4
KS
3470 *(string.pointer++) = ' ';
3471 }
3472 else {
3473 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9 3474 CLEAR(parser, trailing_breaks);
21fbedd4 3475 }
625fcfe9 3476 CLEAR(parser, leading_break);
21fbedd4
KS
3477 }
3478 else {
3479 if (!JOIN(parser, string, leading_break)) goto error;
3480 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9
KS
3481 CLEAR(parser, leading_break);
3482 CLEAR(parser, trailing_breaks);
21fbedd4
KS
3483 }
3484
3485 leading_blanks = 0;
3486 }
3487 else
3488 {
3489 if (!JOIN(parser, string, whitespaces)) goto error;
625fcfe9 3490 CLEAR(parser, whitespaces);
21fbedd4
KS
3491 }
3492 }
3493
3494 /* Copy the character. */
3495
625fcfe9 3496 if (!READ(parser, string)) goto error;
21fbedd4 3497
625fcfe9 3498 end_mark = parser->mark;
21fbedd4 3499
625fcfe9 3500 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3501 }
3502
3503 /* Is it the end? */
3504
e35af832 3505 if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
21fbedd4
KS
3506 break;
3507
3508 /* Consume blank characters. */
3509
625fcfe9 3510 if (!CACHE(parser, 1)) goto error;
21fbedd4 3511
e35af832 3512 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
21fbedd4 3513 {
e35af832 3514 if (IS_BLANK(parser->buffer))
21fbedd4 3515 {
bdf4d192 3516 /* Check for tab character that abuse indentation. */
21fbedd4 3517
0174ed6e 3518 if (leading_blanks && (int)parser->mark.column < indent
e35af832 3519 && IS_TAB(parser->buffer)) {
21fbedd4 3520 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
bdf4d192 3521 start_mark, "found a tab character that violate indentation");
7e32c194 3522 goto error;
21fbedd4
KS
3523 }
3524
3525 /* Consume a space or a tab character. */
3526
3527 if (!leading_blanks) {
625fcfe9 3528 if (!READ(parser, whitespaces)) goto error;
21fbedd4 3529 }
7e32c194 3530 else {
625fcfe9 3531 SKIP(parser);
7e32c194 3532 }
21fbedd4
KS
3533 }
3534 else
3535 {
625fcfe9 3536 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3537
3538 /* Check if it is a first line break. */
3539
3540 if (!leading_blanks)
3541 {
625fcfe9
KS
3542 CLEAR(parser, whitespaces);
3543 if (!READ_LINE(parser, leading_break)) goto error;
21fbedd4
KS
3544 leading_blanks = 1;
3545 }
3546 else
3547 {
625fcfe9 3548 if (!READ_LINE(parser, trailing_breaks)) goto error;
21fbedd4
KS
3549 }
3550 }
625fcfe9 3551 if (!CACHE(parser, 1)) goto error;
21fbedd4
KS
3552 }
3553
bdf4d192 3554 /* Check indentation level. */
21fbedd4 3555
0174ed6e 3556 if (!parser->flow_level && (int)parser->mark.column < indent)
21fbedd4
KS
3557 break;
3558 }
3559
3560 /* Create a token. */
3561
625fcfe9 3562 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
21fbedd4 3563 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
21fbedd4
KS
3564
3565 /* Note that we change the 'simple_key_allowed' flag. */
3566
3567 if (leading_blanks) {
3568 parser->simple_key_allowed = 1;
3569 }
3570
625fcfe9
KS
3571 STRING_DEL(parser, leading_break);
3572 STRING_DEL(parser, trailing_breaks);
3573 STRING_DEL(parser, whitespaces);
21fbedd4 3574
625fcfe9 3575 return 1;
21fbedd4
KS
3576
3577error:
625fcfe9
KS
3578 STRING_DEL(parser, string);
3579 STRING_DEL(parser, leading_break);
3580 STRING_DEL(parser, trailing_breaks);
3581 STRING_DEL(parser, whitespaces);
21fbedd4 3582
625fcfe9 3583 return 0;
21fbedd4 3584}
This page took 0.626743 seconds and 5 git commands to generate.