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