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