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