]> andersk Git - libyaml.git/blame - src/scanner.c
Add functions for constructing, parsing and emitting YAML documents.
[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 *
73 * The correspoding sequence of tokens:
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
eb9cceb5
KS
618yaml_parser_roll_indent(yaml_parser_t *parser, int column,
619 int number, yaml_token_type_t type, yaml_mark_t mark);
03be97ab
KS
620
621static int
f2b59d4d 622yaml_parser_unroll_indent(yaml_parser_t *parser, int 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
KS
764 /* Fetch the next token from the queue. */
765
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,
1081 "could not found expected ':'");
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
KS
1105 int required = (!parser->flow_level
1106 && parser->indent == parser->mark.column);
eb9cceb5
KS
1107
1108 /*
1109 * A simple key is required only when it is the first token in the current
1110 * line. Therefore it is always allowed. But we add a check anyway.
1111 */
1112
1113 assert(parser->simple_key_allowed || !required); /* Impossible. */
1114
1115 /*
1116 * If the current position may start a simple key, save it.
1117 */
1118
1119 if (parser->simple_key_allowed)
1120 {
625fcfe9
KS
1121 yaml_simple_key_t simple_key = { 1, required,
1122 parser->tokens_parsed + parser->tokens.tail - parser->tokens.head,
1123 parser->mark };
eb9cceb5
KS
1124
1125 if (!yaml_parser_remove_simple_key(parser)) return 0;
1126
625fcfe9 1127 *(parser->simple_keys.top-1) = simple_key;
eb9cceb5
KS
1128 }
1129
1130 return 1;
1131}
1132
1133/*
1134 * Remove a potential simple key at the current flow level.
1135 */
1136
1137static int
1138yaml_parser_remove_simple_key(yaml_parser_t *parser)
1139{
625fcfe9 1140 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
eb9cceb5 1141
625fcfe9 1142 if (simple_key->possible)
eb9cceb5
KS
1143 {
1144 /* If the key is required, it is an error. */
1145
1146 if (simple_key->required) {
1147 return yaml_parser_set_scanner_error(parser,
1148 "while scanning a simple key", simple_key->mark,
1149 "could not found expected ':'");
1150 }
625fcfe9 1151 }
eb9cceb5 1152
625fcfe9 1153 /* Remove the key from the stack. */
eb9cceb5 1154
625fcfe9 1155 simple_key->possible = 0;
eb9cceb5
KS
1156
1157 return 1;
1158}
1159
1160/*
1161 * Increase the flow level and resize the simple key list if needed.
1162 */
1163
1164static int
1165yaml_parser_increase_flow_level(yaml_parser_t *parser)
1166{
625fcfe9 1167 yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
eb9cceb5 1168
625fcfe9 1169 /* Reset the simple key on the next level. */
eb9cceb5 1170
625fcfe9
KS
1171 if (!PUSH(parser, parser->simple_keys, empty_simple_key))
1172 return 0;
1173
1174 /* Increase the flow level. */
eb9cceb5 1175
625fcfe9 1176 parser->flow_level++;
eb9cceb5
KS
1177
1178 return 1;
1179}
1180
1181/*
1182 * Decrease the flow level.
1183 */
1184
1185static int
1186yaml_parser_decrease_flow_level(yaml_parser_t *parser)
1187{
625fcfe9
KS
1188 if (parser->flow_level) {
1189 parser->flow_level --;
1190 POP(parser, parser->simple_keys);
eb9cceb5
KS
1191 }
1192
eb9cceb5
KS
1193 return 1;
1194}
1195
1196/*
1197 * Push the current indentation level to the stack and set the new level
1198 * the current column is greater than the indentation level. In this case,
1199 * append or insert the specified token into the token queue.
1200 *
1201 */
1202
1203static int
1204yaml_parser_roll_indent(yaml_parser_t *parser, int column,
1205 int number, yaml_token_type_t type, yaml_mark_t mark)
1206{
625fcfe9 1207 yaml_token_t token;
eb9cceb5
KS
1208
1209 /* In the flow context, do nothing. */
1210
1211 if (parser->flow_level)
1212 return 1;
1213
1214 if (parser->indent < column)
1215 {
eb9cceb5
KS
1216 /*
1217 * Push the current indentation level to the stack and set the new
1218 * indentation level.
1219 */
1220
625fcfe9
KS
1221 if (!PUSH(parser, parser->indents, parser->indent))
1222 return 0;
1223
eb9cceb5
KS
1224 parser->indent = column;
1225
625fcfe9 1226 /* Create a token and insert it into the queue. */
eb9cceb5 1227
625fcfe9 1228 TOKEN_INIT(token, type, mark, mark);
eb9cceb5 1229
625fcfe9
KS
1230 if (number == -1) {
1231 if (!ENQUEUE(parser, parser->tokens, token))
1232 return 0;
1233 }
1234 else {
1235 if (!QUEUE_INSERT(parser,
1236 parser->tokens, number - parser->tokens_parsed, token))
1237 return 0;
eb9cceb5
KS
1238 }
1239 }
1240
1241 return 1;
1242}
1243
1244/*
1245 * Pop indentation levels from the indents stack until the current level
1246 * becomes less or equal to the column. For each intendation level, append
1247 * the BLOCK-END token.
1248 */
1249
1250
1251static int
1252yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
1253{
625fcfe9 1254 yaml_token_t token;
eb9cceb5
KS
1255
1256 /* In the flow context, do nothing. */
1257
1258 if (parser->flow_level)
1259 return 1;
1260
1261 /* Loop through the intendation levels in the stack. */
1262
1263 while (parser->indent > column)
1264 {
625fcfe9 1265 /* Create a token and append it to the queue. */
eb9cceb5 1266
625fcfe9 1267 TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
eb9cceb5 1268
625fcfe9 1269 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1270 return 0;
eb9cceb5
KS
1271
1272 /* Pop the indentation level. */
1273
625fcfe9 1274 parser->indent = POP(parser, parser->indents);
eb9cceb5
KS
1275 }
1276
1277 return 1;
1278}
1279
1280/*
1281 * Initialize the scanner and produce the STREAM-START token.
1282 */
1283
1284static int
1285yaml_parser_fetch_stream_start(yaml_parser_t *parser)
1286{
625fcfe9
KS
1287 yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
1288 yaml_token_t token;
eb9cceb5
KS
1289
1290 /* Set the initial indentation. */
1291
1292 parser->indent = -1;
1293
625fcfe9
KS
1294 /* Initialize the simple key stack. */
1295
1296 if (!PUSH(parser, parser->simple_keys, simple_key))
1297 return 0;
1298
eb9cceb5
KS
1299 /* A simple key is allowed at the beginning of the stream. */
1300
1301 parser->simple_key_allowed = 1;
1302
1303 /* We have started. */
1304
1305 parser->stream_start_produced = 1;
1306
625fcfe9 1307 /* Create the STREAM-START token and append it to the queue. */
eb9cceb5 1308
625fcfe9
KS
1309 STREAM_START_TOKEN_INIT(token, parser->encoding,
1310 parser->mark, parser->mark);
eb9cceb5 1311
625fcfe9 1312 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1313 return 0;
eb9cceb5
KS
1314
1315 return 1;
1316}
1317
1318/*
1319 * Produce the STREAM-END token and shut down the scanner.
1320 */
1321
1322static int
1323yaml_parser_fetch_stream_end(yaml_parser_t *parser)
1324{
625fcfe9 1325 yaml_token_t token;
eb9cceb5 1326
c83b67a6
KS
1327 /* Force new line. */
1328
1329 if (parser->mark.column != 0) {
1330 parser->mark.column = 0;
1331 parser->mark.line ++;
1332 }
1333
eb9cceb5
KS
1334 /* Reset the indentation level. */
1335
1336 if (!yaml_parser_unroll_indent(parser, -1))
1337 return 0;
1338
7e32c194 1339 /* Reset simple keys. */
eb9cceb5 1340
7e32c194
KS
1341 if (!yaml_parser_remove_simple_key(parser))
1342 return 0;
1343
1344 parser->simple_key_allowed = 0;
eb9cceb5 1345
625fcfe9 1346 /* Create the STREAM-END token and append it to the queue. */
eb9cceb5 1347
625fcfe9 1348 STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
eb9cceb5 1349
625fcfe9 1350 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1351 return 0;
eb9cceb5
KS
1352
1353 return 1;
1354}
1355
1356/*
625fcfe9 1357 * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
eb9cceb5
KS
1358 */
1359
1360static int
1361yaml_parser_fetch_directive(yaml_parser_t *parser)
1362{
625fcfe9 1363 yaml_token_t token;
eb9cceb5
KS
1364
1365 /* Reset the indentation level. */
1366
1367 if (!yaml_parser_unroll_indent(parser, -1))
1368 return 0;
1369
1370 /* Reset simple keys. */
1371
1372 if (!yaml_parser_remove_simple_key(parser))
1373 return 0;
1374
1375 parser->simple_key_allowed = 0;
1376
1377 /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
1378
625fcfe9
KS
1379 if (!yaml_parser_scan_directive(parser, &token))
1380 return 0;
eb9cceb5
KS
1381
1382 /* Append the token to the queue. */
1383
625fcfe9
KS
1384 if (!ENQUEUE(parser, parser->tokens, token)) {
1385 yaml_token_delete(&token);
eb9cceb5
KS
1386 return 0;
1387 }
1388
1389 return 1;
1390}
1391
1392/*
1393 * Produce the DOCUMENT-START or DOCUMENT-END token.
1394 */
1395
1396static int
1397yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
1398 yaml_token_type_t type)
1399{
1400 yaml_mark_t start_mark, end_mark;
625fcfe9 1401 yaml_token_t token;
eb9cceb5
KS
1402
1403 /* Reset the indentation level. */
1404
1405 if (!yaml_parser_unroll_indent(parser, -1))
1406 return 0;
1407
1408 /* Reset simple keys. */
1409
1410 if (!yaml_parser_remove_simple_key(parser))
1411 return 0;
1412
1413 parser->simple_key_allowed = 0;
1414
1415 /* Consume the token. */
1416
625fcfe9 1417 start_mark = parser->mark;
eb9cceb5 1418
625fcfe9
KS
1419 SKIP(parser);
1420 SKIP(parser);
1421 SKIP(parser);
eb9cceb5 1422
625fcfe9 1423 end_mark = parser->mark;
eb9cceb5
KS
1424
1425 /* Create the DOCUMENT-START or DOCUMENT-END token. */
1426
625fcfe9 1427 TOKEN_INIT(token, type, start_mark, end_mark);
eb9cceb5
KS
1428
1429 /* Append the token to the queue. */
1430
625fcfe9 1431 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1432 return 0;
eb9cceb5
KS
1433
1434 return 1;
1435}
1436
1437/*
1438 * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1439 */
1440
1441static int
1442yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
1443 yaml_token_type_t type)
1444{
1445 yaml_mark_t start_mark, end_mark;
625fcfe9 1446 yaml_token_t token;
eb9cceb5
KS
1447
1448 /* The indicators '[' and '{' may start a simple key. */
1449
1450 if (!yaml_parser_save_simple_key(parser))
1451 return 0;
1452
1453 /* Increase the flow level. */
1454
1455 if (!yaml_parser_increase_flow_level(parser))
1456 return 0;
1457
1458 /* A simple key may follow the indicators '[' and '{'. */
1459
1460 parser->simple_key_allowed = 1;
1461
1462 /* Consume the token. */
1463
625fcfe9
KS
1464 start_mark = parser->mark;
1465 SKIP(parser);
1466 end_mark = parser->mark;
eb9cceb5
KS
1467
1468 /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
1469
625fcfe9 1470 TOKEN_INIT(token, type, start_mark, end_mark);
eb9cceb5
KS
1471
1472 /* Append the token to the queue. */
1473
625fcfe9 1474 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1475 return 0;
eb9cceb5
KS
1476
1477 return 1;
1478}
1479
1480/*
1481 * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1482 */
1483
1484static int
1485yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
1486 yaml_token_type_t type)
1487{
1488 yaml_mark_t start_mark, end_mark;
625fcfe9 1489 yaml_token_t token;
eb9cceb5
KS
1490
1491 /* Reset any potential simple key on the current flow level. */
1492
1493 if (!yaml_parser_remove_simple_key(parser))
1494 return 0;
1495
1496 /* Decrease the flow level. */
1497
1498 if (!yaml_parser_decrease_flow_level(parser))
1499 return 0;
1500
1501 /* No simple keys after the indicators ']' and '}'. */
1502
1503 parser->simple_key_allowed = 0;
1504
1505 /* Consume the token. */
1506
625fcfe9
KS
1507 start_mark = parser->mark;
1508 SKIP(parser);
1509 end_mark = parser->mark;
eb9cceb5
KS
1510
1511 /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
1512
625fcfe9 1513 TOKEN_INIT(token, type, start_mark, end_mark);
eb9cceb5
KS
1514
1515 /* Append the token to the queue. */
1516
625fcfe9 1517 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1518 return 0;
eb9cceb5
KS
1519
1520 return 1;
1521}
1522
1523/*
1524 * Produce the FLOW-ENTRY token.
1525 */
1526
1527static int
1528yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
1529{
1530 yaml_mark_t start_mark, end_mark;
625fcfe9 1531 yaml_token_t token;
eb9cceb5
KS
1532
1533 /* Reset any potential simple keys on the current flow level. */
1534
1535 if (!yaml_parser_remove_simple_key(parser))
1536 return 0;
1537
1538 /* Simple keys are allowed after ','. */
1539
1540 parser->simple_key_allowed = 1;
1541
1542 /* Consume the token. */
1543
625fcfe9
KS
1544 start_mark = parser->mark;
1545 SKIP(parser);
1546 end_mark = parser->mark;
eb9cceb5 1547
625fcfe9 1548 /* Create the FLOW-ENTRY token and append it to the queue. */
eb9cceb5 1549
625fcfe9 1550 TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
eb9cceb5 1551
625fcfe9 1552 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1553 return 0;
eb9cceb5
KS
1554
1555 return 1;
1556}
1557
1558/*
1559 * Produce the BLOCK-ENTRY token.
1560 */
1561
1562static int
1563yaml_parser_fetch_block_entry(yaml_parser_t *parser)
1564{
1565 yaml_mark_t start_mark, end_mark;
625fcfe9 1566 yaml_token_t token;
eb9cceb5
KS
1567
1568 /* Check if the scanner is in the block context. */
1569
1570 if (!parser->flow_level)
1571 {
1572 /* Check if we are allowed to start a new entry. */
1573
1574 if (!parser->simple_key_allowed) {
625fcfe9 1575 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
eb9cceb5
KS
1576 "block sequence entries are not allowed in this context");
1577 }
1578
1579 /* Add the BLOCK-SEQUENCE-START token if needed. */
1580
625fcfe9
KS
1581 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1582 YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
eb9cceb5
KS
1583 return 0;
1584 }
1585 else
1586 {
1587 /*
1588 * It is an error for the '-' indicator to occur in the flow context,
1589 * but we let the Parser detect and report about it because the Parser
1590 * is able to point to the context.
1591 */
1592 }
1593
1594 /* Reset any potential simple keys on the current flow level. */
1595
1596 if (!yaml_parser_remove_simple_key(parser))
1597 return 0;
1598
1599 /* Simple keys are allowed after '-'. */
1600
1601 parser->simple_key_allowed = 1;
1602
1603 /* Consume the token. */
1604
625fcfe9
KS
1605 start_mark = parser->mark;
1606 SKIP(parser);
1607 end_mark = parser->mark;
eb9cceb5 1608
625fcfe9 1609 /* Create the BLOCK-ENTRY token and append it to the queue. */
eb9cceb5 1610
625fcfe9 1611 TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
eb9cceb5 1612
625fcfe9 1613 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1614 return 0;
eb9cceb5
KS
1615
1616 return 1;
1617}
1618
1619/*
1620 * Produce the KEY token.
1621 */
1622
1623static int
1624yaml_parser_fetch_key(yaml_parser_t *parser)
1625{
1626 yaml_mark_t start_mark, end_mark;
625fcfe9 1627 yaml_token_t token;
eb9cceb5
KS
1628
1629 /* In the block context, additional checks are required. */
1630
1631 if (!parser->flow_level)
1632 {
1633 /* Check if we are allowed to start a new key (not nessesary simple). */
1634
1635 if (!parser->simple_key_allowed) {
625fcfe9 1636 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
eb9cceb5
KS
1637 "mapping keys are not allowed in this context");
1638 }
1639
1640 /* Add the BLOCK-MAPPING-START token if needed. */
1641
625fcfe9
KS
1642 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1643 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
eb9cceb5
KS
1644 return 0;
1645 }
1646
1647 /* Reset any potential simple keys on the current flow level. */
1648
1649 if (!yaml_parser_remove_simple_key(parser))
1650 return 0;
1651
1652 /* Simple keys are allowed after '?' in the block context. */
1653
1654 parser->simple_key_allowed = (!parser->flow_level);
1655
1656 /* Consume the token. */
1657
625fcfe9
KS
1658 start_mark = parser->mark;
1659 SKIP(parser);
1660 end_mark = parser->mark;
eb9cceb5 1661
625fcfe9 1662 /* Create the KEY token and append it to the queue. */
eb9cceb5 1663
625fcfe9 1664 TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
eb9cceb5 1665
625fcfe9 1666 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1667 return 0;
eb9cceb5
KS
1668
1669 return 1;
1670}
1671
1672/*
1673 * Produce the VALUE token.
1674 */
1675
1676static int
1677yaml_parser_fetch_value(yaml_parser_t *parser)
1678{
1679 yaml_mark_t start_mark, end_mark;
625fcfe9
KS
1680 yaml_token_t token;
1681 yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
eb9cceb5
KS
1682
1683 /* Have we found a simple key? */
1684
625fcfe9 1685 if (simple_key->possible)
eb9cceb5 1686 {
eb9cceb5 1687
625fcfe9 1688 /* Create the KEY token and insert it into the queue. */
eb9cceb5 1689
625fcfe9 1690 TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
eb9cceb5 1691
625fcfe9
KS
1692 if (!QUEUE_INSERT(parser, parser->tokens,
1693 simple_key->token_number - parser->tokens_parsed, token))
eb9cceb5 1694 return 0;
eb9cceb5
KS
1695
1696 /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
1697
625fcfe9 1698 if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
eb9cceb5
KS
1699 simple_key->token_number,
1700 YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
1701 return 0;
1702
625fcfe9 1703 /* Remove the simple key. */
eb9cceb5 1704
625fcfe9 1705 simple_key->possible = 0;
eb9cceb5
KS
1706
1707 /* A simple key cannot follow another simple key. */
1708
1709 parser->simple_key_allowed = 0;
1710 }
1711 else
1712 {
1713 /* The ':' indicator follows a complex key. */
1714
1715 /* In the block context, extra checks are required. */
1716
1717 if (!parser->flow_level)
1718 {
1719 /* Check if we are allowed to start a complex value. */
1720
1721 if (!parser->simple_key_allowed) {
625fcfe9 1722 return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
eb9cceb5
KS
1723 "mapping values are not allowed in this context");
1724 }
1725
1726 /* Add the BLOCK-MAPPING-START token if needed. */
1727
625fcfe9
KS
1728 if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
1729 YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
eb9cceb5
KS
1730 return 0;
1731 }
1732
eb9cceb5
KS
1733 /* Simple keys after ':' are allowed in the block context. */
1734
1735 parser->simple_key_allowed = (!parser->flow_level);
1736 }
1737
1738 /* Consume the token. */
1739
625fcfe9
KS
1740 start_mark = parser->mark;
1741 SKIP(parser);
1742 end_mark = parser->mark;
eb9cceb5 1743
625fcfe9 1744 /* Create the VALUE token and append it to the queue. */
eb9cceb5 1745
625fcfe9 1746 TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
eb9cceb5 1747
625fcfe9 1748 if (!ENQUEUE(parser, parser->tokens, token))
eb9cceb5 1749 return 0;
eb9cceb5
KS
1750
1751 return 1;
1752}
1753
1754/*
1755 * Produce the ALIAS or ANCHOR token.
1756 */
1757
1758static int
1759yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
1760{
625fcfe9 1761 yaml_token_t token;
eb9cceb5
KS
1762
1763 /* An anchor or an alias could be a simple key. */
1764
1765 if (!yaml_parser_save_simple_key(parser))
1766 return 0;
1767
1768 /* A simple key cannot follow an anchor or an alias. */
1769
1770 parser->simple_key_allowed = 0;
1771
625fcfe9 1772 /* Create the ALIAS or ANCHOR token and append it to the queue. */
eb9cceb5 1773
625fcfe9
KS
1774 if (!yaml_parser_scan_anchor(parser, &token, type))
1775 return 0;
eb9cceb5 1776
625fcfe9
KS
1777 if (!ENQUEUE(parser, parser->tokens, token)) {
1778 yaml_token_delete(&token);
eb9cceb5
KS
1779 return 0;
1780 }
eb9cceb5
KS
1781 return 1;
1782}
1783
1784/*
1785 * Produce the TAG token.
1786 */
1787
1788static int
1789yaml_parser_fetch_tag(yaml_parser_t *parser)
1790{
625fcfe9 1791 yaml_token_t token;
eb9cceb5
KS
1792
1793 /* A tag could be a simple key. */
1794
1795 if (!yaml_parser_save_simple_key(parser))
1796 return 0;
1797
1798 /* A simple key cannot follow a tag. */
1799
1800 parser->simple_key_allowed = 0;
1801
625fcfe9 1802 /* Create the TAG token and append it to the queue. */
eb9cceb5 1803
625fcfe9
KS
1804 if (!yaml_parser_scan_tag(parser, &token))
1805 return 0;
eb9cceb5 1806
625fcfe9
KS
1807 if (!ENQUEUE(parser, parser->tokens, token)) {
1808 yaml_token_delete(&token);
eb9cceb5
KS
1809 return 0;
1810 }
1811
1812 return 1;
1813}
1814
1815/*
1816 * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1817 */
1818
1819static int
1820yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
1821{
625fcfe9 1822 yaml_token_t token;
eb9cceb5
KS
1823
1824 /* Remove any potential simple keys. */
1825
1826 if (!yaml_parser_remove_simple_key(parser))
1827 return 0;
1828
1829 /* A simple key may follow a block scalar. */
1830
1831 parser->simple_key_allowed = 1;
1832
625fcfe9 1833 /* Create the SCALAR token and append it to the queue. */
eb9cceb5 1834
625fcfe9
KS
1835 if (!yaml_parser_scan_block_scalar(parser, &token, literal))
1836 return 0;
eb9cceb5 1837
625fcfe9
KS
1838 if (!ENQUEUE(parser, parser->tokens, token)) {
1839 yaml_token_delete(&token);
eb9cceb5
KS
1840 return 0;
1841 }
1842
1843 return 1;
1844}
1845
1846/*
1847 * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1848 */
1849
1850static int
1851yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
1852{
625fcfe9 1853 yaml_token_t token;
eb9cceb5
KS
1854
1855 /* A plain scalar could be a simple key. */
1856
1857 if (!yaml_parser_save_simple_key(parser))
1858 return 0;
1859
1860 /* A simple key cannot follow a flow scalar. */
1861
1862 parser->simple_key_allowed = 0;
1863
625fcfe9 1864 /* Create the SCALAR token and append it to the queue. */
eb9cceb5 1865
625fcfe9
KS
1866 if (!yaml_parser_scan_flow_scalar(parser, &token, single))
1867 return 0;
eb9cceb5 1868
625fcfe9
KS
1869 if (!ENQUEUE(parser, parser->tokens, token)) {
1870 yaml_token_delete(&token);
eb9cceb5
KS
1871 return 0;
1872 }
1873
1874 return 1;
1875}
1876
1877/*
1878 * Produce the SCALAR(...,plain) token.
1879 */
1880
1881static int
1882yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
1883{
625fcfe9 1884 yaml_token_t token;
eb9cceb5
KS
1885
1886 /* A plain scalar could be a simple key. */
1887
1888 if (!yaml_parser_save_simple_key(parser))
1889 return 0;
1890
1891 /* A simple key cannot follow a flow scalar. */
1892
1893 parser->simple_key_allowed = 0;
1894
625fcfe9 1895 /* Create the SCALAR token and append it to the queue. */
eb9cceb5 1896
625fcfe9
KS
1897 if (!yaml_parser_scan_plain_scalar(parser, &token))
1898 return 0;
eb9cceb5 1899
625fcfe9
KS
1900 if (!ENQUEUE(parser, parser->tokens, token)) {
1901 yaml_token_delete(&token);
eb9cceb5
KS
1902 return 0;
1903 }
1904
1905 return 1;
1906}
1907
e71095e3
KS
1908/*
1909 * Eat whitespaces and comments until the next token is found.
1910 */
1911
1912static int
1913yaml_parser_scan_to_next_token(yaml_parser_t *parser)
1914{
1915 /* Until the next token is not found. */
1916
1917 while (1)
1918 {
1919 /* Allow the BOM mark to start a line. */
1920
625fcfe9 1921 if (!CACHE(parser, 1)) return 0;
e71095e3 1922
e35af832 1923 if (parser->mark.column == 0 && IS_BOM(parser->buffer))
625fcfe9 1924 SKIP(parser);
e71095e3
KS
1925
1926 /*
1927 * Eat whitespaces.
1928 *
1929 * Tabs are allowed:
1930 *
1931 * - in the flow context;
1932 * - in the block context, but not at the beginning of the line or
1933 * after '-', '?', or ':' (complex value).
1934 */
1935
625fcfe9 1936 if (!CACHE(parser, 1)) return 0;
e71095e3 1937
e35af832 1938 while (CHECK(parser->buffer,' ') ||
e71095e3 1939 ((parser->flow_level || !parser->simple_key_allowed) &&
e35af832 1940 CHECK(parser->buffer, '\t'))) {
625fcfe9
KS
1941 SKIP(parser);
1942 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
1943 }
1944
1945 /* Eat a comment until a line break. */
1946
e35af832
KS
1947 if (CHECK(parser->buffer, '#')) {
1948 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
1949 SKIP(parser);
1950 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
1951 }
1952 }
1953
1954 /* If it is a line break, eat it. */
1955
e35af832 1956 if (IS_BREAK(parser->buffer))
e71095e3 1957 {
625fcfe9
KS
1958 if (!CACHE(parser, 2)) return 0;
1959 SKIP_LINE(parser);
e71095e3
KS
1960
1961 /* In the block context, a new line may start a simple key. */
1962
1963 if (!parser->flow_level) {
1964 parser->simple_key_allowed = 1;
1965 }
1966 }
1967 else
1968 {
1969 /* We have found a token. */
1970
1971 break;
1972 }
1973 }
1974
1975 return 1;
1976}
1977
1978/*
1979 * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1980 *
1981 * Scope:
1982 * %YAML 1.1 # a comment \n
1983 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1984 * %TAG !yaml! tag:yaml.org,2002: \n
1985 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1986 */
1987
625fcfe9
KS
1988int
1989yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
e71095e3
KS
1990{
1991 yaml_mark_t start_mark, end_mark;
1992 yaml_char_t *name = NULL;
1993 int major, minor;
1994 yaml_char_t *handle = NULL, *prefix = NULL;
e71095e3
KS
1995
1996 /* Eat '%'. */
1997
625fcfe9 1998 start_mark = parser->mark;
e71095e3 1999
625fcfe9 2000 SKIP(parser);
e71095e3
KS
2001
2002 /* Scan the directive name. */
2003
2004 if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
2005 goto error;
2006
2007 /* Is it a YAML directive? */
2008
2009 if (strcmp((char *)name, "YAML") == 0)
2010 {
2011 /* Scan the VERSION directive value. */
2012
2013 if (!yaml_parser_scan_version_directive_value(parser, start_mark,
2014 &major, &minor))
2015 goto error;
2016
625fcfe9 2017 end_mark = parser->mark;
e71095e3
KS
2018
2019 /* Create a VERSION-DIRECTIVE token. */
2020
625fcfe9 2021 VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
e71095e3 2022 start_mark, end_mark);
e71095e3
KS
2023 }
2024
2025 /* Is it a TAG directive? */
2026
2027 else if (strcmp((char *)name, "TAG") == 0)
2028 {
2029 /* Scan the TAG directive value. */
2030
2031 if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
2032 &handle, &prefix))
2033 goto error;
2034
625fcfe9 2035 end_mark = parser->mark;
e71095e3
KS
2036
2037 /* Create a TAG-DIRECTIVE token. */
2038
625fcfe9 2039 TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
e71095e3 2040 start_mark, end_mark);
e71095e3
KS
2041 }
2042
2043 /* Unknown directive. */
2044
2045 else
2046 {
92d41fe1 2047 yaml_parser_set_scanner_error(parser, "while scanning a directive",
e71095e3
KS
2048 start_mark, "found uknown directive name");
2049 goto error;
2050 }
2051
2052 /* Eat the rest of the line including any comments. */
2053
625fcfe9
KS
2054 if (!CACHE(parser, 1)) goto error;
2055
e35af832 2056 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2057 SKIP(parser);
2058 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2059 }
2060
e35af832
KS
2061 if (CHECK(parser->buffer, '#')) {
2062 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
2063 SKIP(parser);
2064 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2065 }
2066 }
2067
2068 /* Check if we are at the end of the line. */
2069
e35af832 2070 if (!IS_BREAKZ(parser->buffer)) {
92d41fe1 2071 yaml_parser_set_scanner_error(parser, "while scanning a directive",
e71095e3
KS
2072 start_mark, "did not found expected comment or line break");
2073 goto error;
2074 }
2075
2076 /* Eat a line break. */
2077
e35af832 2078 if (IS_BREAK(parser->buffer)) {
625fcfe9
KS
2079 if (!CACHE(parser, 2)) goto error;
2080 SKIP_LINE(parser);
e71095e3
KS
2081 }
2082
2083 yaml_free(name);
2084
625fcfe9 2085 return 1;
e71095e3
KS
2086
2087error:
e71095e3
KS
2088 yaml_free(prefix);
2089 yaml_free(handle);
2090 yaml_free(name);
625fcfe9 2091 return 0;
e71095e3
KS
2092}
2093
2094/*
2095 * Scan the directive name.
2096 *
2097 * Scope:
2098 * %YAML 1.1 # a comment \n
2099 * ^^^^
2100 * %TAG !yaml! tag:yaml.org,2002: \n
2101 * ^^^
2102 */
2103
2104static int
2105yaml_parser_scan_directive_name(yaml_parser_t *parser,
2106 yaml_mark_t start_mark, yaml_char_t **name)
2107{
625fcfe9 2108 yaml_string_t string = NULL_STRING;
e71095e3 2109
625fcfe9 2110 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2111
2112 /* Consume the directive name. */
2113
625fcfe9 2114 if (!CACHE(parser, 1)) goto error;
e71095e3 2115
e35af832 2116 while (IS_ALPHA(parser->buffer))
e71095e3 2117 {
625fcfe9
KS
2118 if (!READ(parser, string)) goto error;
2119 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2120 }
2121
2122 /* Check if the name is empty. */
2123
625fcfe9 2124 if (string.start == string.pointer) {
e71095e3
KS
2125 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2126 start_mark, "cannot found expected directive name");
2127 goto error;
2128 }
2129
2130 /* Check for an blank character after the name. */
2131
e35af832 2132 if (!IS_BLANKZ(parser->buffer)) {
e71095e3
KS
2133 yaml_parser_set_scanner_error(parser, "while scanning a directive",
2134 start_mark, "found unexpected non-alphabetical character");
2135 goto error;
2136 }
2137
625fcfe9 2138 *name = string.start;
e71095e3
KS
2139
2140 return 1;
2141
2142error:
625fcfe9 2143 STRING_DEL(parser, string);
e71095e3
KS
2144 return 0;
2145}
2146
2147/*
2148 * Scan the value of VERSION-DIRECTIVE.
2149 *
2150 * Scope:
2151 * %YAML 1.1 # a comment \n
2152 * ^^^^^^
2153 */
2154
2155static int
2156yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
2157 yaml_mark_t start_mark, int *major, int *minor)
2158{
2159 /* Eat whitespaces. */
2160
625fcfe9 2161 if (!CACHE(parser, 1)) return 0;
e71095e3 2162
e35af832 2163 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2164 SKIP(parser);
2165 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
2166 }
2167
2168 /* Consume the major version number. */
2169
2170 if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
2171 return 0;
2172
2173 /* Eat '.'. */
2174
e35af832 2175 if (!CHECK(parser->buffer, '.')) {
e71095e3
KS
2176 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2177 start_mark, "did not find expected digit or '.' character");
2178 }
2179
625fcfe9 2180 SKIP(parser);
e71095e3
KS
2181
2182 /* Consume the minor version number. */
2183
2184 if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
2185 return 0;
ab01bac8
KS
2186
2187 return 1;
e71095e3
KS
2188}
2189
2190#define MAX_NUMBER_LENGTH 9
2191
2192/*
2193 * Scan the version number of VERSION-DIRECTIVE.
2194 *
2195 * Scope:
2196 * %YAML 1.1 # a comment \n
2197 * ^
2198 * %YAML 1.1 # a comment \n
2199 * ^
2200 */
2201
2202static int
2203yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
2204 yaml_mark_t start_mark, int *number)
2205{
2206 int value = 0;
2207 size_t length = 0;
2208
2209 /* Repeat while the next character is digit. */
2210
625fcfe9 2211 if (!CACHE(parser, 1)) return 0;
e71095e3 2212
e35af832 2213 while (IS_DIGIT(parser->buffer))
e71095e3
KS
2214 {
2215 /* Check if the number is too long. */
2216
2217 if (++length > MAX_NUMBER_LENGTH) {
2218 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2219 start_mark, "found extremely long version number");
2220 }
2221
e35af832 2222 value = value*10 + AS_DIGIT(parser->buffer);
e71095e3 2223
625fcfe9 2224 SKIP(parser);
e71095e3 2225
625fcfe9 2226 if (!CACHE(parser, 1)) return 0;
e71095e3
KS
2227 }
2228
2229 /* Check if the number was present. */
2230
2231 if (!length) {
2232 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
2233 start_mark, "did not find expected version number");
2234 }
2235
2236 *number = value;
2237
2238 return 1;
2239}
2240
2241/*
2242 * Scan the value of a TAG-DIRECTIVE token.
2243 *
2244 * Scope:
2245 * %TAG !yaml! tag:yaml.org,2002: \n
2246 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2247 */
2248
2249static int
2250yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
2251 yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
2252{
2253 yaml_char_t *handle_value = NULL;
2254 yaml_char_t *prefix_value = NULL;
2255
2256 /* Eat whitespaces. */
2257
625fcfe9 2258 if (!CACHE(parser, 1)) goto error;
e71095e3 2259
e35af832 2260 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2261 SKIP(parser);
2262 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2263 }
2264
2265 /* Scan a handle. */
2266
2267 if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
2268 goto error;
2269
2270 /* Expect a whitespace. */
2271
625fcfe9 2272 if (!CACHE(parser, 1)) goto error;
e71095e3 2273
e35af832 2274 if (!IS_BLANK(parser->buffer)) {
e71095e3
KS
2275 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2276 start_mark, "did not find expected whitespace");
2277 goto error;
2278 }
2279
2280 /* Eat whitespaces. */
2281
e35af832 2282 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2283 SKIP(parser);
2284 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2285 }
2286
2287 /* Scan a prefix. */
2288
2289 if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
2290 goto error;
2291
2292 /* Expect a whitespace or line break. */
2293
625fcfe9 2294 if (!CACHE(parser, 1)) goto error;
e71095e3 2295
e35af832 2296 if (!IS_BLANKZ(parser->buffer)) {
e71095e3
KS
2297 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
2298 start_mark, "did not find expected whitespace or line break");
2299 goto error;
2300 }
2301
2302 *handle = handle_value;
2303 *prefix = prefix_value;
2304
2305 return 1;
2306
2307error:
2308 yaml_free(handle_value);
2309 yaml_free(prefix_value);
2310 return 0;
2311}
2312
625fcfe9
KS
2313static int
2314yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
e71095e3
KS
2315 yaml_token_type_t type)
2316{
2317 int length = 0;
2318 yaml_mark_t start_mark, end_mark;
625fcfe9 2319 yaml_string_t string = NULL_STRING;
e71095e3 2320
625fcfe9 2321 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2322
2323 /* Eat the indicator character. */
2324
625fcfe9 2325 start_mark = parser->mark;
e71095e3 2326
625fcfe9 2327 SKIP(parser);
e71095e3
KS
2328
2329 /* Consume the value. */
2330
625fcfe9 2331 if (!CACHE(parser, 1)) goto error;
e71095e3 2332
e35af832 2333 while (IS_ALPHA(parser->buffer)) {
625fcfe9
KS
2334 if (!READ(parser, string)) goto error;
2335 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2336 length ++;
2337 }
2338
625fcfe9 2339 end_mark = parser->mark;
e71095e3
KS
2340
2341 /*
2342 * Check if length of the anchor is greater than 0 and it is followed by
2343 * a whitespace character or one of the indicators:
2344 *
2345 * '?', ':', ',', ']', '}', '%', '@', '`'.
2346 */
2347
e35af832
KS
2348 if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
2349 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
2350 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
2351 || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
2352 || CHECK(parser->buffer, '`'))) {
e71095e3
KS
2353 yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
2354 "while scanning an anchor" : "while scanning an alias", start_mark,
2355 "did not find expected alphabetic or numeric character");
2356 goto error;
2357 }
2358
2359 /* Create a token. */
2360
625fcfe9
KS
2361 if (type == YAML_ANCHOR_TOKEN) {
2362 ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
2363 }
2364 else {
2365 ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
92d41fe1 2366 }
e71095e3 2367
625fcfe9 2368 return 1;
e71095e3
KS
2369
2370error:
625fcfe9 2371 STRING_DEL(parser, string);
e71095e3
KS
2372 return 0;
2373}
2374
2375/*
2376 * Scan a TAG token.
2377 */
2378
625fcfe9
KS
2379static int
2380yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
e71095e3
KS
2381{
2382 yaml_char_t *handle = NULL;
2383 yaml_char_t *suffix = NULL;
e71095e3
KS
2384 yaml_mark_t start_mark, end_mark;
2385
625fcfe9 2386 start_mark = parser->mark;
e71095e3
KS
2387
2388 /* Check if the tag is in the canonical form. */
2389
625fcfe9 2390 if (!CACHE(parser, 2)) goto error;
e71095e3 2391
e35af832 2392 if (CHECK_AT(parser->buffer, '<', 1))
e71095e3
KS
2393 {
2394 /* Set the handle to '' */
2395
2396 handle = yaml_malloc(1);
2397 if (!handle) goto error;
2398 handle[0] = '\0';
2399
2400 /* Eat '!<' */
2401
625fcfe9
KS
2402 SKIP(parser);
2403 SKIP(parser);
e71095e3
KS
2404
2405 /* Consume the tag value. */
2406
2407 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2408 goto error;
2409
2410 /* Check for '>' and eat it. */
2411
e35af832 2412 if (!CHECK(parser->buffer, '>')) {
e71095e3
KS
2413 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2414 start_mark, "did not find the expected '>'");
2415 goto error;
2416 }
2417
625fcfe9 2418 SKIP(parser);
e71095e3
KS
2419 }
2420 else
2421 {
2422 /* The tag has either the '!suffix' or the '!handle!suffix' form. */
2423
2424 /* First, try to scan a handle. */
2425
2426 if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
2427 goto error;
2428
2429 /* Check if it is, indeed, handle. */
2430
2431 if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
2432 {
2433 /* Scan the suffix now. */
2434
2435 if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
2436 goto error;
2437 }
2438 else
2439 {
2440 /* It wasn't a handle after all. Scan the rest of the tag. */
2441
2442 if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
2443 goto error;
2444
2445 /* Set the handle to '!'. */
2446
2447 yaml_free(handle);
2448 handle = yaml_malloc(2);
2449 if (!handle) goto error;
2450 handle[0] = '!';
2451 handle[1] = '\0';
7e32c194
KS
2452
2453 /*
625fcfe9
KS
2454 * A special case: the '!' tag. Set the handle to '' and the
2455 * suffix to '!'.
7e32c194
KS
2456 */
2457
2458 if (suffix[0] == '\0') {
2459 yaml_char_t *tmp = handle;
2460 handle = suffix;
2461 suffix = tmp;
2462 }
e71095e3
KS
2463 }
2464 }
2465
2466 /* Check the character which ends the tag. */
2467
625fcfe9 2468 if (!CACHE(parser, 1)) goto error;
e71095e3 2469
e35af832 2470 if (!IS_BLANKZ(parser->buffer)) {
e71095e3
KS
2471 yaml_parser_set_scanner_error(parser, "while scanning a tag",
2472 start_mark, "did not found expected whitespace or line break");
2473 goto error;
2474 }
2475
625fcfe9 2476 end_mark = parser->mark;
e71095e3
KS
2477
2478 /* Create a token. */
2479
625fcfe9 2480 TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
e71095e3 2481
625fcfe9 2482 return 1;
e71095e3
KS
2483
2484error:
2485 yaml_free(handle);
2486 yaml_free(suffix);
625fcfe9 2487 return 0;
e71095e3
KS
2488}
2489
2490/*
2491 * Scan a tag handle.
2492 */
2493
2494static int
2495yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
2496 yaml_mark_t start_mark, yaml_char_t **handle)
2497{
625fcfe9 2498 yaml_string_t string = NULL_STRING;
e71095e3 2499
625fcfe9 2500 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2501
2502 /* Check the initial '!' character. */
2503
625fcfe9 2504 if (!CACHE(parser, 1)) goto error;
e71095e3 2505
e35af832 2506 if (!CHECK(parser->buffer, '!')) {
e71095e3
KS
2507 yaml_parser_set_scanner_error(parser, directive ?
2508 "while scanning a tag directive" : "while scanning a tag",
2509 start_mark, "did not find expected '!'");
2510 goto error;
2511 }
2512
2513 /* Copy the '!' character. */
2514
625fcfe9 2515 if (!READ(parser, string)) goto error;
e71095e3
KS
2516
2517 /* Copy all subsequent alphabetical and numerical characters. */
2518
625fcfe9 2519 if (!CACHE(parser, 1)) goto error;
e71095e3 2520
e35af832 2521 while (IS_ALPHA(parser->buffer))
e71095e3 2522 {
625fcfe9
KS
2523 if (!READ(parser, string)) goto error;
2524 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2525 }
2526
2527 /* Check if the trailing character is '!' and copy it. */
2528
e35af832 2529 if (CHECK(parser->buffer, '!'))
e71095e3 2530 {
625fcfe9 2531 if (!READ(parser, string)) goto error;
e71095e3
KS
2532 }
2533 else
2534 {
2535 /*
7e32c194
KS
2536 * It's either the '!' tag or not really a tag handle. If it's a %TAG
2537 * directive, it's an error. If it's a tag token, it must be a part of
2538 * URI.
e71095e3
KS
2539 */
2540
625fcfe9 2541 if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
7e32c194 2542 yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
e71095e3
KS
2543 start_mark, "did not find expected '!'");
2544 goto error;
2545 }
2546 }
2547
625fcfe9 2548 *handle = string.start;
e71095e3
KS
2549
2550 return 1;
2551
2552error:
625fcfe9 2553 STRING_DEL(parser, string);
e71095e3
KS
2554 return 0;
2555}
2556
2557/*
2558 * Scan a tag.
2559 */
2560
2561static int
2562yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
2563 yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
2564{
2565 size_t length = head ? strlen((char *)head) : 0;
625fcfe9 2566 yaml_string_t string = NULL_STRING;
e71095e3 2567
625fcfe9 2568 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
e71095e3
KS
2569
2570 /* Resize the string to include the head. */
2571
625fcfe9
KS
2572 while (string.end - string.start <= length) {
2573 if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
2574 parser->error = YAML_MEMORY_ERROR;
2575 goto error;
2576 }
e71095e3
KS
2577 }
2578
7e32c194
KS
2579 /*
2580 * Copy the head if needed.
2581 *
2582 * Note that we don't copy the leading '!' character.
2583 */
e71095e3 2584
7e32c194 2585 if (length > 1) {
625fcfe9 2586 memcpy(string.start, head+1, length-1);
7e32c194 2587 string.pointer += length-1;
e71095e3
KS
2588 }
2589
2590 /* Scan the tag. */
2591
625fcfe9 2592 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2593
2594 /*
2595 * The set of characters that may appear in URI is as follows:
2596 *
2597 * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
2598 * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
2599 * '%'.
2600 */
2601
e35af832
KS
2602 while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';')
2603 || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?')
2604 || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@')
2605 || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=')
2606 || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$')
2607 || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.')
2608 || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
2609 || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
2610 || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
2611 || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
2612 || CHECK(parser->buffer, '%'))
e71095e3 2613 {
e71095e3
KS
2614 /* Check if it is a URI-escape sequence. */
2615
e35af832 2616 if (CHECK(parser->buffer, '%')) {
e71095e3
KS
2617 if (!yaml_parser_scan_uri_escapes(parser,
2618 directive, start_mark, &string)) goto error;
2619 }
2620 else {
625fcfe9 2621 if (!READ(parser, string)) goto error;
e71095e3
KS
2622 }
2623
2624 length ++;
625fcfe9 2625 if (!CACHE(parser, 1)) goto error;
e71095e3
KS
2626 }
2627
2628 /* Check if the tag is non-empty. */
2629
2630 if (!length) {
625fcfe9
KS
2631 if (!STRING_EXTEND(parser, string))
2632 goto error;
2633
e71095e3
KS
2634 yaml_parser_set_scanner_error(parser, directive ?
2635 "while parsing a %TAG directive" : "while parsing a tag",
2636 start_mark, "did not find expected tag URI");
2637 goto error;
2638 }
2639
625fcfe9 2640 *uri = string.start;
e71095e3
KS
2641
2642 return 1;
2643
2644error:
625fcfe9 2645 STRING_DEL(parser, string);
e71095e3
KS
2646 return 0;
2647}
2648
2649/*
2650 * Decode an URI-escape sequence corresponding to a single UTF-8 character.
2651 */
2652
2653static int
2654yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
2655 yaml_mark_t start_mark, yaml_string_t *string)
2656{
2657 int width = 0;
2658
2659 /* Decode the required number of characters. */
2660
2661 do {
2662
2663 unsigned char octet = 0;
2664
2665 /* Check for a URI-escaped octet. */
2666
625fcfe9 2667 if (!CACHE(parser, 3)) return 0;
e71095e3 2668
e35af832
KS
2669 if (!(CHECK(parser->buffer, '%')
2670 && IS_HEX_AT(parser->buffer, 1)
2671 && IS_HEX_AT(parser->buffer, 2))) {
e71095e3
KS
2672 return yaml_parser_set_scanner_error(parser, directive ?
2673 "while parsing a %TAG directive" : "while parsing a tag",
2674 start_mark, "did not find URI escaped octet");
2675 }
2676
2677 /* Get the octet. */
2678
e35af832 2679 octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
e71095e3
KS
2680
2681 /* If it is the leading octet, determine the length of the UTF-8 sequence. */
2682
2683 if (!width)
2684 {
2685 width = (octet & 0x80) == 0x00 ? 1 :
2686 (octet & 0xE0) == 0xC0 ? 2 :
2687 (octet & 0xF0) == 0xE0 ? 3 :
2688 (octet & 0xF8) == 0xF0 ? 4 : 0;
2689 if (!width) {
2690 return yaml_parser_set_scanner_error(parser, directive ?
2691 "while parsing a %TAG directive" : "while parsing a tag",
2692 start_mark, "found an incorrect leading UTF-8 octet");
2693 }
2694 }
2695 else
2696 {
2697 /* Check if the trailing octet is correct. */
2698
2699 if ((octet & 0xC0) != 0x80) {
2700 return yaml_parser_set_scanner_error(parser, directive ?
2701 "while parsing a %TAG directive" : "while parsing a tag",
2702 start_mark, "found an incorrect trailing UTF-8 octet");
2703 }
2704 }
2705
2706 /* Copy the octet and move the pointers. */
2707
2708 *(string->pointer++) = octet;
625fcfe9
KS
2709 SKIP(parser);
2710 SKIP(parser);
2711 SKIP(parser);
e71095e3
KS
2712
2713 } while (--width);
2714
2715 return 1;
2716}
2717
92d41fe1
KS
2718/*
2719 * Scan a block scalar.
2720 */
2721
625fcfe9
KS
2722static int
2723yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
2724 int literal)
92d41fe1
KS
2725{
2726 yaml_mark_t start_mark;
2727 yaml_mark_t end_mark;
625fcfe9
KS
2728 yaml_string_t string = NULL_STRING;
2729 yaml_string_t leading_break = NULL_STRING;
2730 yaml_string_t trailing_breaks = NULL_STRING;
92d41fe1
KS
2731 int chomping = 0;
2732 int increment = 0;
2733 int indent = 0;
2734 int leading_blank = 0;
2735 int trailing_blank = 0;
2736
625fcfe9
KS
2737 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
2738 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
2739 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
92d41fe1
KS
2740
2741 /* Eat the indicator '|' or '>'. */
2742
625fcfe9 2743 start_mark = parser->mark;
92d41fe1 2744
625fcfe9 2745 SKIP(parser);
92d41fe1
KS
2746
2747 /* Scan the additional block scalar indicators. */
2748
625fcfe9 2749 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2750
2751 /* Check for a chomping indicator. */
2752
e35af832 2753 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
92d41fe1
KS
2754 {
2755 /* Set the chomping method and eat the indicator. */
2756
e35af832 2757 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
92d41fe1 2758
625fcfe9 2759 SKIP(parser);
92d41fe1
KS
2760
2761 /* Check for an indentation indicator. */
2762
625fcfe9 2763 if (!CACHE(parser, 1)) goto error;
92d41fe1 2764
e35af832 2765 if (IS_DIGIT(parser->buffer))
92d41fe1
KS
2766 {
2767 /* Check that the intendation is greater than 0. */
2768
e35af832 2769 if (CHECK(parser->buffer, '0')) {
92d41fe1
KS
2770 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2771 start_mark, "found an intendation indicator equal to 0");
2772 goto error;
2773 }
2774
2775 /* Get the intendation level and eat the indicator. */
2776
e35af832 2777 increment = AS_DIGIT(parser->buffer);
92d41fe1 2778
625fcfe9 2779 SKIP(parser);
92d41fe1
KS
2780 }
2781 }
2782
2783 /* Do the same as above, but in the opposite order. */
2784
e35af832 2785 else if (IS_DIGIT(parser->buffer))
92d41fe1 2786 {
e35af832 2787 if (CHECK(parser->buffer, '0')) {
92d41fe1
KS
2788 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2789 start_mark, "found an intendation indicator equal to 0");
2790 goto error;
2791 }
2792
e35af832 2793 increment = AS_DIGIT(parser->buffer);
92d41fe1 2794
625fcfe9 2795 SKIP(parser);
92d41fe1 2796
625fcfe9 2797 if (!CACHE(parser, 1)) goto error;
92d41fe1 2798
e35af832
KS
2799 if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
2800 chomping = CHECK(parser->buffer, '+') ? +1 : -1;
625fcfe9
KS
2801
2802 SKIP(parser);
92d41fe1
KS
2803 }
2804 }
2805
2806 /* Eat whitespaces and comments to the end of the line. */
2807
625fcfe9 2808 if (!CACHE(parser, 1)) goto error;
92d41fe1 2809
e35af832 2810 while (IS_BLANK(parser->buffer)) {
625fcfe9
KS
2811 SKIP(parser);
2812 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2813 }
2814
e35af832
KS
2815 if (CHECK(parser->buffer, '#')) {
2816 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
2817 SKIP(parser);
2818 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2819 }
2820 }
2821
2822 /* Check if we are at the end of the line. */
2823
e35af832 2824 if (!IS_BREAKZ(parser->buffer)) {
92d41fe1
KS
2825 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2826 start_mark, "did not found expected comment or line break");
2827 goto error;
2828 }
2829
2830 /* Eat a line break. */
2831
e35af832 2832 if (IS_BREAK(parser->buffer)) {
625fcfe9
KS
2833 if (!CACHE(parser, 2)) goto error;
2834 SKIP_LINE(parser);
92d41fe1
KS
2835 }
2836
625fcfe9 2837 end_mark = parser->mark;
92d41fe1
KS
2838
2839 /* Set the intendation level if it was specified. */
2840
2841 if (increment) {
2842 indent = parser->indent >= 0 ? parser->indent+increment : increment;
2843 }
2844
2845 /* Scan the leading line breaks and determine the indentation level if needed. */
2846
21fbedd4 2847 if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
92d41fe1
KS
2848 start_mark, &end_mark)) goto error;
2849
2850 /* Scan the block scalar content. */
2851
625fcfe9 2852 if (!CACHE(parser, 1)) goto error;
92d41fe1 2853
e35af832 2854 while (parser->mark.column == indent && !IS_Z(parser->buffer))
92d41fe1
KS
2855 {
2856 /*
2857 * We are at the beginning of a non-empty line.
2858 */
2859
2860 /* Is it a trailing whitespace? */
2861
e35af832 2862 trailing_blank = IS_BLANK(parser->buffer);
92d41fe1
KS
2863
2864 /* Check if we need to fold the leading line break. */
2865
625fcfe9 2866 if (!literal && (*leading_break.start == '\n')
92d41fe1
KS
2867 && !leading_blank && !trailing_blank)
2868 {
2869 /* Do we need to join the lines by space? */
2870
625fcfe9
KS
2871 if (*trailing_breaks.start == '\0') {
2872 if (!STRING_EXTEND(parser, string)) goto error;
92d41fe1
KS
2873 *(string.pointer ++) = ' ';
2874 }
2875
625fcfe9 2876 CLEAR(parser, leading_break);
92d41fe1
KS
2877 }
2878 else {
21fbedd4 2879 if (!JOIN(parser, string, leading_break)) goto error;
625fcfe9 2880 CLEAR(parser, leading_break);
92d41fe1
KS
2881 }
2882
2883 /* Append the remaining line breaks. */
2884
21fbedd4 2885 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9 2886 CLEAR(parser, trailing_breaks);
92d41fe1
KS
2887
2888 /* Is it a leading whitespace? */
2889
e35af832 2890 leading_blank = IS_BLANK(parser->buffer);
92d41fe1
KS
2891
2892 /* Consume the current line. */
2893
e35af832 2894 while (!IS_BREAKZ(parser->buffer)) {
625fcfe9
KS
2895 if (!READ(parser, string)) goto error;
2896 if (!CACHE(parser, 1)) goto error;
92d41fe1
KS
2897 }
2898
2899 /* Consume the line break. */
2900
625fcfe9 2901 if (!CACHE(parser, 2)) goto error;
92d41fe1 2902
625fcfe9 2903 if (!READ_LINE(parser, leading_break)) goto error;
92d41fe1
KS
2904
2905 /* Eat the following intendation spaces and line breaks. */
2906
2907 if (!yaml_parser_scan_block_scalar_breaks(parser,
21fbedd4 2908 &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
92d41fe1
KS
2909 }
2910
2911 /* Chomp the tail. */
2912
2913 if (chomping != -1) {
21fbedd4 2914 if (!JOIN(parser, string, leading_break)) goto error;
92d41fe1
KS
2915 }
2916 if (chomping == 1) {
21fbedd4 2917 if (!JOIN(parser, string, trailing_breaks)) goto error;
92d41fe1
KS
2918 }
2919
2920 /* Create a token. */
2921
625fcfe9 2922 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
92d41fe1
KS
2923 literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
2924 start_mark, end_mark);
92d41fe1 2925
625fcfe9
KS
2926 STRING_DEL(parser, leading_break);
2927 STRING_DEL(parser, trailing_breaks);
92d41fe1 2928
625fcfe9 2929 return 1;
92d41fe1
KS
2930
2931error:
625fcfe9
KS
2932 STRING_DEL(parser, string);
2933 STRING_DEL(parser, leading_break);
2934 STRING_DEL(parser, trailing_breaks);
92d41fe1 2935
625fcfe9 2936 return 0;
92d41fe1
KS
2937}
2938
2939/*
2940 * Scan intendation spaces and line breaks for a block scalar. Determine the
2941 * intendation level if needed.
2942 */
2943
2944static int
2945yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
2946 int *indent, yaml_string_t *breaks,
2947 yaml_mark_t start_mark, yaml_mark_t *end_mark)
2948{
2949 int max_indent = 0;
2950
625fcfe9 2951 *end_mark = parser->mark;
92d41fe1
KS
2952
2953 /* Eat the intendation spaces and line breaks. */
2954
2955 while (1)
2956 {
2957 /* Eat the intendation spaces. */
2958
625fcfe9 2959 if (!CACHE(parser, 1)) return 0;
92d41fe1 2960
e35af832
KS
2961 while ((!*indent || parser->mark.column < *indent)
2962 && IS_SPACE(parser->buffer)) {
625fcfe9
KS
2963 SKIP(parser);
2964 if (!CACHE(parser, 1)) return 0;
92d41fe1
KS
2965 }
2966
625fcfe9
KS
2967 if (parser->mark.column > max_indent)
2968 max_indent = parser->mark.column;
92d41fe1
KS
2969
2970 /* Check for a tab character messing the intendation. */
2971
e35af832
KS
2972 if ((!*indent || parser->mark.column < *indent)
2973 && IS_TAB(parser->buffer)) {
92d41fe1
KS
2974 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2975 start_mark, "found a tab character where an intendation space is expected");
2976 }
2977
2978 /* Have we found a non-empty line? */
2979
e35af832 2980 if (!IS_BREAK(parser->buffer)) break;
92d41fe1
KS
2981
2982 /* Consume the line break. */
2983
625fcfe9
KS
2984 if (!CACHE(parser, 2)) return 0;
2985 if (!READ_LINE(parser, *breaks)) return 0;
2986 *end_mark = parser->mark;
92d41fe1
KS
2987 }
2988
2989 /* Determine the indentation level if needed. */
2990
2991 if (!*indent) {
2992 *indent = max_indent;
2993 if (*indent < parser->indent + 1)
2994 *indent = parser->indent + 1;
2995 if (*indent < 1)
2996 *indent = 1;
2997 }
2998
2999 return 1;
3000}
3001
21fbedd4
KS
3002/*
3003 * Scan a quoted scalar.
3004 */
3005
625fcfe9
KS
3006static int
3007yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3008 int single)
21fbedd4
KS
3009{
3010 yaml_mark_t start_mark;
3011 yaml_mark_t end_mark;
625fcfe9
KS
3012 yaml_string_t string = NULL_STRING;
3013 yaml_string_t leading_break = NULL_STRING;
3014 yaml_string_t trailing_breaks = NULL_STRING;
3015 yaml_string_t whitespaces = NULL_STRING;
21fbedd4
KS
3016 int leading_blanks;
3017
625fcfe9
KS
3018 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3019 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3020 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3021 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
21fbedd4
KS
3022
3023 /* Eat the left quote. */
3024
625fcfe9 3025 start_mark = parser->mark;
21fbedd4 3026
625fcfe9 3027 SKIP(parser);
21fbedd4
KS
3028
3029 /* Consume the content of the quoted scalar. */
3030
3031 while (1)
3032 {
3033 /* Check that there are no document indicators at the beginning of the line. */
3034
625fcfe9 3035 if (!CACHE(parser, 4)) goto error;
21fbedd4 3036
625fcfe9 3037 if (parser->mark.column == 0 &&
e35af832
KS
3038 ((CHECK_AT(parser->buffer, '-', 0) &&
3039 CHECK_AT(parser->buffer, '-', 1) &&
3040 CHECK_AT(parser->buffer, '-', 2)) ||
3041 (CHECK_AT(parser->buffer, '.', 0) &&
3042 CHECK_AT(parser->buffer, '.', 1) &&
3043 CHECK_AT(parser->buffer, '.', 2))) &&
3044 IS_BLANKZ_AT(parser->buffer, 3))
21fbedd4
KS
3045 {
3046 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3047 start_mark, "found unexpected document indicator");
3048 goto error;
3049 }
3050
3051 /* Check for EOF. */
3052
e35af832 3053 if (IS_Z(parser->buffer)) {
21fbedd4
KS
3054 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
3055 start_mark, "found unexpected end of stream");
3056 goto error;
3057 }
3058
3059 /* Consume non-blank characters. */
3060
625fcfe9 3061 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3062
3063 leading_blanks = 0;
3064
e35af832 3065 while (!IS_BLANKZ(parser->buffer))
21fbedd4
KS
3066 {
3067 /* Check for an escaped single quote. */
3068
e35af832
KS
3069 if (single && CHECK_AT(parser->buffer, '\'', 0)
3070 && CHECK_AT(parser->buffer, '\'', 1))
21fbedd4 3071 {
625fcfe9 3072 if (!STRING_EXTEND(parser, string)) goto error;
21fbedd4 3073 *(string.pointer++) = '\'';
625fcfe9
KS
3074 SKIP(parser);
3075 SKIP(parser);
21fbedd4
KS
3076 }
3077
3078 /* Check for the right quote. */
3079
e35af832 3080 else if (CHECK(parser->buffer, single ? '\'' : '"'))
21fbedd4
KS
3081 {
3082 break;
3083 }
3084
3085 /* Check for an escaped line break. */
3086
e35af832
KS
3087 else if (!single && CHECK(parser->buffer, '\\')
3088 && IS_BREAK_AT(parser->buffer, 1))
21fbedd4 3089 {
625fcfe9
KS
3090 if (!CACHE(parser, 3)) goto error;
3091 SKIP(parser);
3092 SKIP_LINE(parser);
21fbedd4
KS
3093 leading_blanks = 1;
3094 break;
3095 }
3096
3097 /* Check for an escape sequence. */
3098
e35af832 3099 else if (!single && CHECK(parser->buffer, '\\'))
21fbedd4
KS
3100 {
3101 int code_length = 0;
3102
625fcfe9
KS
3103 if (!STRING_EXTEND(parser, string)) goto error;
3104
21fbedd4
KS
3105 /* Check the escape character. */
3106
625fcfe9 3107 switch (parser->buffer.pointer[1])
21fbedd4
KS
3108 {
3109 case '0':
3110 *(string.pointer++) = '\0';
3111 break;
3112
3113 case 'a':
3114 *(string.pointer++) = '\x07';
3115 break;
3116
3117 case 'b':
3118 *(string.pointer++) = '\x08';
3119 break;
3120
3121 case 't':
3122 case '\t':
3123 *(string.pointer++) = '\x09';
3124 break;
3125
3126 case 'n':
3127 *(string.pointer++) = '\x0A';
3128 break;
3129
3130 case 'v':
3131 *(string.pointer++) = '\x0B';
3132 break;
3133
3134 case 'f':
3135 *(string.pointer++) = '\x0C';
3136 break;
3137
3138 case 'r':
3139 *(string.pointer++) = '\x0D';
3140 break;
3141
3142 case 'e':
3143 *(string.pointer++) = '\x1B';
3144 break;
3145
3146 case ' ':
3147 *(string.pointer++) = '\x20';
3148 break;
3149
3150 case '"':
3151 *(string.pointer++) = '"';
3152 break;
3153
3154 case '\'':
3155 *(string.pointer++) = '\'';
3156 break;
3157
7e32c194
KS
3158 case '\\':
3159 *(string.pointer++) = '\\';
3160 break;
3161
21fbedd4
KS
3162 case 'N': /* NEL (#x85) */
3163 *(string.pointer++) = '\xC2';
3164 *(string.pointer++) = '\x85';
3165 break;
3166
3167 case '_': /* #xA0 */
3168 *(string.pointer++) = '\xC2';
3169 *(string.pointer++) = '\xA0';
3170 break;
3171
3172 case 'L': /* LS (#x2028) */
3173 *(string.pointer++) = '\xE2';
3174 *(string.pointer++) = '\x80';
3175 *(string.pointer++) = '\xA8';
3176 break;
3177
3178 case 'P': /* PS (#x2029) */
3179 *(string.pointer++) = '\xE2';
3180 *(string.pointer++) = '\x80';
7e32c194 3181 *(string.pointer++) = '\xA9';
21fbedd4
KS
3182 break;
3183
3184 case 'x':
3185 code_length = 2;
3186 break;
3187
3188 case 'u':
3189 code_length = 4;
3190 break;
3191
3192 case 'U':
3193 code_length = 8;
3194 break;
3195
3196 default:
3197 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3198 start_mark, "found unknown escape character");
3199 goto error;
3200 }
3201
625fcfe9
KS
3202 SKIP(parser);
3203 SKIP(parser);
21fbedd4
KS
3204
3205 /* Consume an arbitrary escape code. */
3206
3207 if (code_length)
3208 {
3209 unsigned int value = 0;
3210 int k;
3211
3212 /* Scan the character value. */
3213
625fcfe9 3214 if (!CACHE(parser, code_length)) goto error;
21fbedd4
KS
3215
3216 for (k = 0; k < code_length; k ++) {
e35af832 3217 if (!IS_HEX_AT(parser->buffer, k)) {
21fbedd4
KS
3218 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3219 start_mark, "did not find expected hexdecimal number");
3220 goto error;
3221 }
e35af832 3222 value = (value << 4) + AS_HEX_AT(parser->buffer, k);
21fbedd4
KS
3223 }
3224
3225 /* Check the value and write the character. */
3226
3227 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
3228 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
3229 start_mark, "found invalid Unicode character escape code");
3230 goto error;
3231 }
3232
3233 if (value <= 0x7F) {
3234 *(string.pointer++) = value;
3235 }
3236 else if (value <= 0x7FF) {
3237 *(string.pointer++) = 0xC0 + (value >> 6);
3238 *(string.pointer++) = 0x80 + (value & 0x3F);
3239 }
3240 else if (value <= 0xFFFF) {
3241 *(string.pointer++) = 0xE0 + (value >> 12);
3242 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3243 *(string.pointer++) = 0x80 + (value & 0x3F);
3244 }
3245 else {
3246 *(string.pointer++) = 0xF0 + (value >> 18);
3247 *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
3248 *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
3249 *(string.pointer++) = 0x80 + (value & 0x3F);
3250 }
3251
3252 /* Advance the pointer. */
3253
3254 for (k = 0; k < code_length; k ++) {
625fcfe9 3255 SKIP(parser);
21fbedd4
KS
3256 }
3257 }
3258 }
3259
3260 else
3261 {
3262 /* It is a non-escaped non-blank character. */
3263
625fcfe9 3264 if (!READ(parser, string)) goto error;
21fbedd4
KS
3265 }
3266
625fcfe9 3267 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3268 }
3269
3270 /* Check if we are at the end of the scalar. */
3271
e35af832 3272 if (CHECK(parser->buffer, single ? '\'' : '"'))
21fbedd4
KS
3273 break;
3274
3275 /* Consume blank characters. */
3276
625fcfe9 3277 if (!CACHE(parser, 1)) goto error;
21fbedd4 3278
e35af832 3279 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
21fbedd4 3280 {
e35af832 3281 if (IS_BLANK(parser->buffer))
21fbedd4
KS
3282 {
3283 /* Consume a space or a tab character. */
3284
3285 if (!leading_blanks) {
625fcfe9 3286 if (!READ(parser, whitespaces)) goto error;
21fbedd4 3287 }
7e32c194 3288 else {
625fcfe9 3289 SKIP(parser);
7e32c194 3290 }
21fbedd4
KS
3291 }
3292 else
3293 {
625fcfe9 3294 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3295
3296 /* Check if it is a first line break. */
3297
3298 if (!leading_blanks)
3299 {
625fcfe9
KS
3300 CLEAR(parser, whitespaces);
3301 if (!READ_LINE(parser, leading_break)) goto error;
21fbedd4
KS
3302 leading_blanks = 1;
3303 }
3304 else
3305 {
625fcfe9 3306 if (!READ_LINE(parser, trailing_breaks)) goto error;
21fbedd4
KS
3307 }
3308 }
625fcfe9 3309 if (!CACHE(parser, 1)) goto error;
21fbedd4
KS
3310 }
3311
3312 /* Join the whitespaces or fold line breaks. */
3313
21fbedd4
KS
3314 if (leading_blanks)
3315 {
3316 /* Do we need to fold line breaks? */
3317
625fcfe9
KS
3318 if (leading_break.start[0] == '\n') {
3319 if (trailing_breaks.start[0] == '\0') {
3320 if (!STRING_EXTEND(parser, string)) goto error;
21fbedd4
KS
3321 *(string.pointer++) = ' ';
3322 }
3323 else {
3324 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9 3325 CLEAR(parser, trailing_breaks);
21fbedd4 3326 }
625fcfe9 3327 CLEAR(parser, leading_break);
21fbedd4
KS
3328 }
3329 else {
3330 if (!JOIN(parser, string, leading_break)) goto error;
3331 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9
KS
3332 CLEAR(parser, leading_break);
3333 CLEAR(parser, trailing_breaks);
21fbedd4
KS
3334 }
3335 }
3336 else
3337 {
3338 if (!JOIN(parser, string, whitespaces)) goto error;
625fcfe9 3339 CLEAR(parser, whitespaces);
21fbedd4
KS
3340 }
3341 }
3342
3343 /* Eat the right quote. */
3344
625fcfe9 3345 SKIP(parser);
21fbedd4 3346
625fcfe9 3347 end_mark = parser->mark;
21fbedd4
KS
3348
3349 /* Create a token. */
3350
625fcfe9 3351 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
21fbedd4
KS
3352 single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
3353 start_mark, end_mark);
21fbedd4 3354
625fcfe9
KS
3355 STRING_DEL(parser, leading_break);
3356 STRING_DEL(parser, trailing_breaks);
3357 STRING_DEL(parser, whitespaces);
21fbedd4 3358
625fcfe9 3359 return 1;
21fbedd4
KS
3360
3361error:
625fcfe9
KS
3362 STRING_DEL(parser, string);
3363 STRING_DEL(parser, leading_break);
3364 STRING_DEL(parser, trailing_breaks);
3365 STRING_DEL(parser, whitespaces);
21fbedd4 3366
625fcfe9 3367 return 0;
21fbedd4
KS
3368}
3369
3370/*
3371 * Scan a plain scalar.
3372 */
3373
625fcfe9
KS
3374static int
3375yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
21fbedd4
KS
3376{
3377 yaml_mark_t start_mark;
3378 yaml_mark_t end_mark;
625fcfe9
KS
3379 yaml_string_t string = NULL_STRING;
3380 yaml_string_t leading_break = NULL_STRING;
3381 yaml_string_t trailing_breaks = NULL_STRING;
3382 yaml_string_t whitespaces = NULL_STRING;
21fbedd4
KS
3383 int leading_blanks = 0;
3384 int indent = parser->indent+1;
3385
625fcfe9
KS
3386 if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
3387 if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
3388 if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
3389 if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
21fbedd4 3390
54815ffd 3391 start_mark = end_mark = parser->mark;
21fbedd4
KS
3392
3393 /* Consume the content of the plain scalar. */
3394
3395 while (1)
3396 {
3397 /* Check for a document indicator. */
3398
625fcfe9 3399 if (!CACHE(parser, 4)) goto error;
21fbedd4 3400
625fcfe9 3401 if (parser->mark.column == 0 &&
e35af832
KS
3402 ((CHECK_AT(parser->buffer, '-', 0) &&
3403 CHECK_AT(parser->buffer, '-', 1) &&
3404 CHECK_AT(parser->buffer, '-', 2)) ||
3405 (CHECK_AT(parser->buffer, '.', 0) &&
3406 CHECK_AT(parser->buffer, '.', 1) &&
3407 CHECK_AT(parser->buffer, '.', 2))) &&
3408 IS_BLANKZ_AT(parser->buffer, 3)) break;
21fbedd4
KS
3409
3410 /* Check for a comment. */
3411
e35af832 3412 if (CHECK(parser->buffer, '#'))
21fbedd4
KS
3413 break;
3414
3415 /* Consume non-blank characters. */
3416
e35af832 3417 while (!IS_BLANKZ(parser->buffer))
21fbedd4 3418 {
7e32c194 3419 /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
21fbedd4 3420
e35af832
KS
3421 if (parser->flow_level
3422 && CHECK(parser->buffer, ':')
3423 && !IS_BLANKZ_AT(parser->buffer, 1)) {
21fbedd4
KS
3424 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3425 start_mark, "found unexpected ':'");
3426 goto error;
3427 }
3428
3429 /* Check for indicators that may end a plain scalar. */
3430
e35af832
KS
3431 if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
3432 || (parser->flow_level &&
3433 (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':')
3434 || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
3435 || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
3436 || CHECK(parser->buffer, '}'))))
21fbedd4
KS
3437 break;
3438
3439 /* Check if we need to join whitespaces and breaks. */
3440
625fcfe9 3441 if (leading_blanks || whitespaces.start != whitespaces.pointer)
21fbedd4 3442 {
21fbedd4
KS
3443 if (leading_blanks)
3444 {
3445 /* Do we need to fold line breaks? */
3446
625fcfe9
KS
3447 if (leading_break.start[0] == '\n') {
3448 if (trailing_breaks.start[0] == '\0') {
3449 if (!STRING_EXTEND(parser, string)) goto error;
21fbedd4
KS
3450 *(string.pointer++) = ' ';
3451 }
3452 else {
3453 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9 3454 CLEAR(parser, trailing_breaks);
21fbedd4 3455 }
625fcfe9 3456 CLEAR(parser, leading_break);
21fbedd4
KS
3457 }
3458 else {
3459 if (!JOIN(parser, string, leading_break)) goto error;
3460 if (!JOIN(parser, string, trailing_breaks)) goto error;
625fcfe9
KS
3461 CLEAR(parser, leading_break);
3462 CLEAR(parser, trailing_breaks);
21fbedd4
KS
3463 }
3464
3465 leading_blanks = 0;
3466 }
3467 else
3468 {
3469 if (!JOIN(parser, string, whitespaces)) goto error;
625fcfe9 3470 CLEAR(parser, whitespaces);
21fbedd4
KS
3471 }
3472 }
3473
3474 /* Copy the character. */
3475
625fcfe9 3476 if (!READ(parser, string)) goto error;
21fbedd4 3477
625fcfe9 3478 end_mark = parser->mark;
21fbedd4 3479
625fcfe9 3480 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3481 }
3482
3483 /* Is it the end? */
3484
e35af832 3485 if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
21fbedd4
KS
3486 break;
3487
3488 /* Consume blank characters. */
3489
625fcfe9 3490 if (!CACHE(parser, 1)) goto error;
21fbedd4 3491
e35af832 3492 while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
21fbedd4 3493 {
e35af832 3494 if (IS_BLANK(parser->buffer))
21fbedd4
KS
3495 {
3496 /* Check for tab character that abuse intendation. */
3497
e35af832
KS
3498 if (leading_blanks && parser->mark.column < indent
3499 && IS_TAB(parser->buffer)) {
21fbedd4
KS
3500 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
3501 start_mark, "found a tab character that violate intendation");
7e32c194 3502 goto error;
21fbedd4
KS
3503 }
3504
3505 /* Consume a space or a tab character. */
3506
3507 if (!leading_blanks) {
625fcfe9 3508 if (!READ(parser, whitespaces)) goto error;
21fbedd4 3509 }
7e32c194 3510 else {
625fcfe9 3511 SKIP(parser);
7e32c194 3512 }
21fbedd4
KS
3513 }
3514 else
3515 {
625fcfe9 3516 if (!CACHE(parser, 2)) goto error;
21fbedd4
KS
3517
3518 /* Check if it is a first line break. */
3519
3520 if (!leading_blanks)
3521 {
625fcfe9
KS
3522 CLEAR(parser, whitespaces);
3523 if (!READ_LINE(parser, leading_break)) goto error;
21fbedd4
KS
3524 leading_blanks = 1;
3525 }
3526 else
3527 {
625fcfe9 3528 if (!READ_LINE(parser, trailing_breaks)) goto error;
21fbedd4
KS
3529 }
3530 }
625fcfe9 3531 if (!CACHE(parser, 1)) goto error;
21fbedd4
KS
3532 }
3533
3534 /* Check intendation level. */
3535
625fcfe9 3536 if (!parser->flow_level && parser->mark.column < indent)
21fbedd4
KS
3537 break;
3538 }
3539
3540 /* Create a token. */
3541
625fcfe9 3542 SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
21fbedd4 3543 YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
21fbedd4
KS
3544
3545 /* Note that we change the 'simple_key_allowed' flag. */
3546
3547 if (leading_blanks) {
3548 parser->simple_key_allowed = 1;
3549 }
3550
625fcfe9
KS
3551 STRING_DEL(parser, leading_break);
3552 STRING_DEL(parser, trailing_breaks);
3553 STRING_DEL(parser, whitespaces);
21fbedd4 3554
625fcfe9 3555 return 1;
21fbedd4
KS
3556
3557error:
625fcfe9
KS
3558 STRING_DEL(parser, string);
3559 STRING_DEL(parser, leading_break);
3560 STRING_DEL(parser, trailing_breaks);
3561 STRING_DEL(parser, whitespaces);
21fbedd4 3562
625fcfe9 3563 return 0;
21fbedd4
KS
3564}
3565
This page took 0.600268 seconds and 5 git commands to generate.