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