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